User and administrator logout through development and testing through RSPEC

I am using Devise in a rail application.

I have two models: user and administrator.

Currently, I can log in as user and administrator if I visit user login and admin login.

I would like to force logout if other signs. What is the best way to do this?

Also, if anyone has a good way to check that in rspec / capybara, please share!

thanks

+4
source share
1 answer

I would copy over Devise Controllers and add a filter before the following:

class Users::SessionsController < Devise::SessionsController before_filter :logout_admin, :only => "create" def create super end private def logout_admin # change :admin to :user in the admin session controller # the sign_out is a devise controller helper that forces sign out sign_out :admin end end 

Then for testing, you can access the current user in the functional (control) specifications by calling subject.current_user or subject.current_admin, go here: https://github.com/plataformatec/devise , to enable test assistants, try something like this:

 require 'spec_helper' describe Users::SessionsController do login_admin describe "POST create" do it "should logout admin" do post :create, {:user => {:email => " tester@email.com ", :password => "secret"} } subject.current_user.should_not be_nil subject.current_admin.should be_nil end end end 
+5
source

Source: https://habr.com/ru/post/1398966/


All Articles