Note that there are some explanatory texts on larger screens.

plurals
  1. POTesting Rails app with Rspec using declarative authorizations helper method permitted_to?
    text
    copied!<p>I am trying to test a rails engine with rspec where I am using the declarative_authorization gem to manage the roles for my users. UserSessions are managed by Authlogic.</p> <p>In my test I call a helper method which is rendering a view partial. In that partial i am using the <code>permitted_to? do ... end</code> method of declarative authorization.</p> <p>My expection is to get a response with a html string.</p> <p>The problem is that <code>permitted_to?</code> throws an Exception:</p> <p>undefined local variable or method `current_user' for #</p> <p>I took a closer look into declarative auths AuthorizationInController module (which requires the authorization.rb where the current_user method is defined as a class method) and found that the current_user method is getting called from options_for_permit which is called from permitted_to?.</p> <p>When I change <code>:user =&gt; current_user</code> in the options_for_permit method to <code>:user =&gt; Authorization.current_user</code> the exception is gone and my test passes.</p> <p>I am not a very well skilled Rubyist, so i dont know if that is the correct way of fixing that problem. The method permitted_to? is a fundamental method of declarative_authorization, so I dont know why it is working when normally using it and not when using it here in the test.</p> <p>my questions:</p> <ul> <li>Is this a bug in declarative authorization?</li> <li>Could my fix make impact on other areas?</li> </ul> <p>Hopefully waiting for your response...</p> <p>Thanks robin</p> <p>EDIT:</p> <p>I just noticed that my application does not work correctly anymore with my changes in development environment.</p> <p>Authorization.current_user is now an Authorization::AnonymousUser while current_user is a valid User with the admin role...</p> <p>Stubbing current_user does not work, that was what i tried at first.</p> <p>My test code:</p> <pre><code>it "should render the main navi for all core modules" do main_navi = YAML.load_file(File.join(Rails.root, "../..", "config/alchemy", "modules.yml")) helper.stub(:alchemy_modules).and_return(main_navi) Authorization.current_user = Factory(:admin_user) helper.admin_main_navigation.should have_selector('a.main_navi_entry') end </code></pre> <p>view code which should get rendered:</p> <pre><code>&lt;%- permitted_to?( :index, :admin ) do -%&gt; ... &lt;%- end -%&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload