Posts [ 11 ]

Topic: login_as not assigning the proper role in my functional test

I'm looking at adding some authentication to my application - since I've never really worked with it before, and want to take advantage of the Rails community, I'm using the restful_authentication plugin, and following the tutorial posted here on the forum: http://www.railsforum.com/viewtopic.php?id=14216

Since I also want to start experimenting with unit tests (I have never really used them in my prior 'life' as a .NET developer), I'm making sure my tests run before I add my own functionality. However, the first functional test fails with the following error:

test_should_get_index(RolesControllerTest)method test_should_get_index in roles_controller_test.rb at line 15method run in default.rb at line 7Expected response to be a <:success>, but was <302>

Now, in my RolesController I specify a before_filter requiring the logged-in user to be a member of the administrators group (this was taken verbatim from the Tutorial).

In app/models/user.rb:

has_many :permissions has_many :roles, :through => :permissions

In app/models/role.rb:

has_many :permissions has_many :users, :through => :permissions

In app/models/permission.rb:

belongs_to :user belongs_to :role

My roles.yml file looks like this:

admin_role: name: administrator

and my permissions.yml file looks like this:

admin: user: quentin # quentin is the first user provided by restful_authentication in users.yml role: admin_role

I have verified that the user 'quentin' has the administrator permission, but for some reason login_as isn't doing something properly, as it's still redirecting (302 is a redirect, right?) instead of getting the proper page. Interestingly enough, if I put login_as :quentin inside of the method itself, ActiveRecord fails with the error that it can't find a User without an ID - I thought that a yml fixture didn't need an explicit ID as of Rails 2.0. It fails if I explicitly say id: 1, as well. I'm really at a loss for why it's failing.

Could anyone point me in the right direction? I'm not really used to Rails and Ruby yet, but I want to learn - it's much more pleasant than using C#, if I do say so myself.

Re: login_as not assigning the proper role in my functional test

I did some more digging, and the problem seems to be that it's not assigning the role (users(:quentin).roles comes up as nil). I'm not sure where the problem lies, then.

Also I noticed that it not finding the ID was coming up only when I froze rails; I unfroze (thawed?) it and now it's at least picking up the user "quentin", although still not the permissions. Any idea why that would be? I thought freezing your rails was a good idea for a site; should I not be doing that when I create a new application, and only when I'm ready to deploy it?

Still hoping someone can shed some light on this - I plan to look around even more when I'm home from work.

Re: login_as not assigning the proper role in my functional test

I don't know if you guys have the problem solved, but I seem to remember I had the same a while ago. Turned out it had to do with the way my fixtures were loaded and the fact you are using direct references to other fixtures for your has_many declarations. So when you declare your 'admin' permission, it supposes there is already a fixture 'quentin' (a user) and a fixture 'admin_role' (a role).

However, the fixtures are loaded in alphabetical order, so permissions before users and roles. That's why the 'admin' permission is not set properly.

Re: login_as not assigning the proper role in my functional test

Why are the id's of your users commented out in the fixture file? Since you are explicitely referencing them to assign roles, you must make sure the ids are correct. Check your test database to see if the fixtures are properly defined in the database with correct ids.

Re: login_as not assigning the proper role in my functional test

I was having a bizarre problem that this thread helped me to track down... maybe it will be useful to someone else. Thx to @dgroten for the lead.

I'm using the restful_authentication and role_requirement plugins. I had a functional test case that was calling "login_as :admin" and performing actions that required the admin role. When I ran this test alone it worked gloriously. But when I ran it together with other functional tests, it failed because the admin user didn't have the admin role.

From this thread I got the idea to look at the fixtures. I discovered that I was calling "fixtures :users, :roles, :roles_users", when I should just be calling "fixtures :users". Changing that, everything works perfectly again. I still don't understand the mechanism that would make it break like that, but what the heck.