Skip to content

Commit

Permalink
Refactoring log_in_as spec to use user factories with clean option.
Browse files Browse the repository at this point in the history
  • Loading branch information
randalldfloyd committed Aug 11, 2023
1 parent b811fa8 commit 4442988
Showing 1 changed file with 22 additions and 19 deletions.
41 changes: 22 additions & 19 deletions spec/features/log_in_as_spec.rb
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
require 'rails_helper'

RSpec.feature 'Switch User' do
let(:user_attributes) do
{ email: '[email protected]' }
end
let(:user) do
User.new(user_attributes) { |u| u.save(validate: false) }
end

before do
login_as user
end
RSpec.feature 'Switch User', :clean do
let(:user) { FactoryBot.create(:user) }
let(:admin_user) { FactoryBot.create(:admin)}

scenario 'Non-admin user is not allowed to see switch user form' do
visit '/users/sessions/log_in_as'
expect(page).to have_no_selector('select#switch_user_identifier')
context 'Non-admin user' do
before do
login_as user
end
scenario 'is not allowed to see switch user form' do
visit '/users/sessions/log_in_as'
expect(page).to have_no_selector('select#switch_user_identifier')
logout
end
end

scenario 'Admin user is allowed to see switch user form' do
admin = Role.where(name: 'admin').first_or_create
admin.users << user
visit '/users/sessions/log_in_as'
expect(page).to have_css('select#switch_user_identifier')
context 'Admin user' do
before do
login_as admin_user
allow(admin_user).to receive(:admin?).and_return(true)
end
scenario 'is allowed to see switch user form' do
visit '/users/sessions/log_in_as'
expect(page).to have_css('select#switch_user_identifier')
logout
end
end
end

0 comments on commit 4442988

Please sign in to comment.