Skip to content

Commit

Permalink
Refactoring log_in_as spec to use a User factory 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 4127b99
Showing 1 changed file with 20 additions and 18 deletions.
38 changes: 20 additions & 18 deletions spec/features/log_in_as_spec.rb
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
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) }

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', :clean 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', :clean do
before do
login_as user
allow(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 4127b99

Please sign in to comment.