From 4442988b7b86e2ddb32f6961478140b6482bc778 Mon Sep 17 00:00:00 2001 From: Randall Floyd Date: Fri, 11 Aug 2023 12:22:09 -0400 Subject: [PATCH] Refactoring log_in_as spec to use user factories with clean option. --- spec/features/log_in_as_spec.rb | 41 ++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/spec/features/log_in_as_spec.rb b/spec/features/log_in_as_spec.rb index 65162f5fa..cb8c4df36 100644 --- a/spec/features/log_in_as_spec.rb +++ b/spec/features/log_in_as_spec.rb @@ -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 +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