'Testing login flow with RSpec in Rails app

I'm using RSpec to test controller behavior in rails. One of the expectations I have is the following scenario:

  1. User attempts to access a protected (requires login) url with params /home?val=123.
  2. That user is redirected to login page.
  3. After login the user should be redirected back to the URL originally requested, with relevant params.

I'm using devise for my auth system (using omniauth), and I've set up the recommended macros to login the user and all that.

I'm a bit stuck on how to test the flow of the above behavior:

describe HomeController do
  context "user is logged out" do
    it "redirects back to original page after signin" do
      # user is not signed in - redirected to login page
      get :index, { :val => '12345' }
      current_user.should_not be_present
      expect(response).to redirect_to(login_page_path)

      # should now login the user and verify
      # that request.fullpath == '/home?val=12345'

      # ?...
    end
  end

end

Does it even belong in a controller spec? Any help or example/s will be appreciated. thanks.



Solution 1:[1]

Luacassus looks like he has a decent answer, but have you looked at request testing with Capybara?

There's a really decent tutorial on Rails Casts here.

That's basically what we use to test our sign-up tests. However, we're not testing omniauth so it might not be exactly what you're looking for.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 simonmorley