Reputation: 607
I'm trying to write Rspec tests to make sure that if there isn't an admin signed in, then the user can't create an invitation and then gets redirected back to the sign_in page
describe 'GET index', focus: true do
context 'with an admin user signed in' do
with :admin
let!(:invitation) { create :invitation }
before do
sign_in admin
get :index
end
it { should respond_with :ok }
it { should respond_with_content_type :html }
it { should render_template :index }
it 'assigns all invitations as @invitations' do
expect(assigns(:invitations)).to eq([invitation])
end
end
context 'with a non-admin user signed in' do
with :user
before do
sign_in user
get :index
end
it {should redirect_to new_user_session_path }
end
end
Something with the redirect isn't working, though. This is the failure I'm getting:
1) InvitationsController GET index with a non-admin user signed in should redirect to "/users/sign_in"
Failure/Error: it {should redirect_to new_user_session_path }
Expected response to be a redirect to <http://test.host/users/sign_in> but was a redirect to <http://test.host/>.
Expected "http://test.host/users/sign_in" to be === "http://test.host/".
# ./spec/controllers/invitations_controller_spec.rb:26:in `block (4 levels) in <top (required)>'
And I'm not really sure how to address it. I'm learning Rails at the moment, so I apologize if the answer is obvious. Appreciate the help!
Upvotes: 1
Views: 1227
Reputation: 6041
Devise's new user sign in page detects that your user is already logged on, so it redirects your user to the front page.
If you really want the user to get logged out when trying to access the invitations controller without being an admin, then you should force a log out in your failed authorization handler.
Upvotes: 1
Reputation: 53
Go into your rails console with rails c
and type rake routes
.
You should get a list of all the routes in your application as well as their prefixes which are suffixed with _path
when doing a redirect.
For instance:
Prefix Verb URI Pattern Controller#Action
new_user_session GET /users/sign_in(.:format) devise/sessions#new
If this route doesn't exist then you will have to create it in order for the test to pass.
Upvotes: 0