How to test user session within integration test in RSpec - ruby-on-rails

I am new to BDD and I have this controller that requires a user session how can I test that in RSpec? Thank You!

You could directly set the required key in the session variable which is used for verifying that the user session is set. From the RSpec documentation you can access the session variable using session like in Rails.

Do you use some Authentication Plugin/Gem, such as Devise, Restful_Authentication or Authlogic?
Normally they come with certain TestHelper-Methods, that allow you to do kind of an authentication while testing.

Don't. Integration testing is a job for Cucumber, not RSpec.

Related

RSpec integration test error trouble in Rails

Please understand that I cannot speak English well.
I'm having trouble with the rails rspec error not being resolved.
Rails web app login integration test error.
I'll attach the error and test details here.
error details
test details
I designed the controller's create method to "redirect to home after login".
create method for "login and redirect to home"
And it has been confirmed that it returns to home after logging in on the development server. But it doesn't work in integration tests.
I wondered why.
Perhaps the user data prepared for testing is not loaded properly?
so i can't redirect to home after login?
Factory bot gems and user data generation files required to create RSpec test user data
gem for tests
As an aside, this is my first time writing integration tests in RSpec.
So there may be some rookie mistakes. Thank you very much.
FactoryBot.build(:user) initializes a user in memory, but doesn't save it into the database. That means the login fails, because a user with that email is not found in the database. And because the login failed, the user is still on /login.
To fix this, just change
before do
#user = FactoryBot.build(:user)
end
to
before do
#user = FactoryBot.create(:user)
end
in your user_login_spec.rb.

What is the proper way to sign in as a user in an rspec request spec, without devise?

I have a Rails application which has some User authentication which is built without Devise (or any gem for that matter). It uses the typical session[:user_id] to track the current user.
My understanding of the current state of controller tests is that the Rspec team and Rails teams both recommend against using them. This is fine, but I'm not seeing how to actually sign in as a user from within a request spec. I've done it with Devise with no issue, but Devise uses Warden and such.
I've tried to access the session from within the test but the level of abstraction within request specs seems to prevent access to it.
How can I sign in a user from within a request spec?
You can change the session before the request:
#request.session['user_id'] = '1'
Or add anything else that you require on the session to validate your user.
Or you could create a helper method that actually performs the request needed to login, which is what #dhh recommends.

Custom authentication: login helper for request specs

I'm new to RSpec. I've created an authentication from scratch as described in this railscast, I would like login code to run before some (not all) of my request (integration) specs. I've thinking of the few solutions, but I wonder what's the best way to approach this?
Thanks,
You could try Matt Connoly's answer at How to do integration testing with RSpec and Devise/CanCan?.
Obviously you would need to rewrite the methods according to your authentication set-up, but it should get you going.

Session gone in Integration Tests in Rails 2.3.10

In Rails 2.3.8 my integration tests (after considerable work, and the help of this post) were able to make use of a single session across requests. In Rails 2.3.10, that functionality broke, and after logging in, subsequent requests have no session.
One possible symptom is that this bit of code used to pass:
open_session do |s|
s.post 'login', :user=>{:user_name=>username, :password=>pass}
assert_not_nil(s.session[:id])
end
And now it fails (s.session[:id] is nil). This may or may not be the problem, as I can confirm that other variables stored in the session during the login action are present. However on subsequent s.post's, the actions all report that there's nothing in the session.
Any advice? I saw one report that Rails 2.3.9 introduced a session bug with this as the workaround, but it doesn't appear to make a difference.
Thanks,
Tom
Did you try s.session.data[:id]

What is the best way to test authlogic-open-id with cucumber and webrat?

I've been having trouble using cucumber and webrat to test authlogic-openid authentication in a rails app. Following Ryan Bates's excellent screencast I was able to install authlogic with the open-id plugin. OpenID works when I login using the browser but so far I've been unable to test the app using cucumber and webrat.
I've tried using rots as a dummy open id server. Again this works when I try it in the browser but webrat won't doesn't correctly follow the get/post redirects required to authentic with the dummy open id server.
This answer on SO suggests overriding the authentication method to always return a successful login but this approach doesn't seem like integration testing the application.
What is the best way to functionally test authlogic-open-id? Should I even bother? Should I test the actions when the user is already logged in and assume OpenID will work?
have you tried the test helper set_session_for(#user)? I would assume that the plugin is tested and there really is no need to test it again (unless you have modified it).
I asked the same question on the authlogic mailing list. I got a good answer directing me to use WWW:Mechanise with some patch code to that makes webrat follow all redirects. This seems to work for doing rails integration testing using authlogic open id with a local open id server.
Here's the discussion thread.

Resources