Skip to content

Commit

Permalink
Refactoring log_in_as spec to use user factories to eliminate DB lock…
Browse files Browse the repository at this point in the history
… errors.
  • Loading branch information
randalldfloyd committed Aug 11, 2023
1 parent b811fa8 commit 656ba28
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 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: 'test@example.com' }
end
let(:user) do
User.new(user_attributes) { |u| u.save(validate: false) }
end

before do
login_as user
end
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 656ba28

Please sign in to comment.