I’m working on a Rails application that requires you to be logged in to see pretty much anything. For unit tests, this isn’t a problem since they interact with the model directly. For integration tests, this isn’t a big problem since I can just have the appropriate user log in via Rails’ post method. It seems like it makes sense to put all rights-sensitive tests in the integration tests (e.g., test that a user cannot edit someone else’s comments).
However, I run into issues when handling the functional tests. It seems like it makes sense for me to test functionality that does not hinge crucially on user status (other than in its ability act as a gateway) independently of user rights. Now, I can’t figure out a good way to do this, since all requests bounce the test client to the login page.
An inelegant solution I tried would be to put this log in request in the setup method, but the Rails tester tried to access an action, instead of a path. In particular, I had
def setup
post /sessions/create, {:username => 'testdbadmin', :password => 'password}
end
But according to the logs, it just tried to access SomeController#/sessions/create, which is clearly not the intention.
I feel like I’m missing some big, built-in way to do this. Please advise.
I do this in my functional tests by just setting valid logged-in session data in my setup, effectively simulating a request where a user has already authenticated (or hasn’t, as the case may be).
I have a helper method in my test_helper.rb:
And then in my functional test: