Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'spec_helper'
- describe Admin::ImpersonateUserController, type: :controller do
- render_views
- let(:user_signin) { create(:user) }
- describe "GET 'index'" do
- context 'user has :impersonate_admin role' do
- before do
- user_signin.add_role(:impersonate_admin)
- sign_in user_signin
- end
- it 'renders view' do
- get :index
- expect(response).to render_template 'index'
- expect(response.body).to_not include('No results found! Please try a different query.')
- end
- context 'a search is submitted' do
- let!(:search_field) { '' }
- let!(:search_value) { '' }
- let(:user) { nil }
- let(:user1_1) { nil }
- let(:user1_2) { nil }
- let(:user2_1) { nil }
- let(:user2_2) { nil }
- before do
- if user1_1
- user1_1.add_role(:location_admin, location1)
- user1_1.save
- end
- if user1_2
- user1_2.add_role(:location_admin, location1)
- user1_2.save
- end
- if user2_1
- user2_1.add_role(:location_admin, location2)
- user2_1.save
- end
- if user2_2
- user2_2.add_role(:location_admin, location2)
- user2_2.save
- end
- user.save! if user
- get :index, search_field: search_field, search_value: search_value
- end
- context 'invalid search field' do
- let!(:search_field) { 'tree' }
- let!(:search_value) { 'hello@gmail.com' }
- it 'raises' do
- expect(response).to_not be_successful
- expect(response.status).to be 400
- expect(response.body).to include 'missing required params'
- end
- end
- context 'is a valid search' do
- after { expect(response).to be_successful }
- context 'search by email' do
- let(:user) { build(:user, email: 'hello@gmail.com') }
- let(:user_unmatch) { create(:user, email: 'foo@gmail.com') }
- let(:search_field) { 'email' }
- let(:search_value) { 'hello' }
- it 'returns the match' do
- expect(response.body).to include('hello@gmail.com')
- expect(assigns(:users)).to include(user)
- expect(assigns(:users)).to_not include(user_unmatch)
- end
- end
- context 'search by first_name' do
- let(:user) { create(:user, first_name: 'Claire') }
- let(:user_unmatch) { create(:user, first_name: 'Frank') }
- let(:search_field) { 'first_name' }
- let(:search_value) { 'Claire' }
- context 'search matches' do
- it 'returns the match' do
- expect(response.body).to include('Claire')
- expect(assigns(:users)).to include(user)
- expect(assigns(:users)).to_not include(user_unmatch)
- end
- context 'case insensitive' do
- let(:search_value) { 'claire' }
- it 'is not case sensitive' do
- expect(response.body).to include('claire')
- expect(assigns(:users)).to include(user)
- expect(assigns(:users)).to_not include(user_unmatch)
- end
- end
- end
- end
- context 'search by last_name' do
- let!(:user) { create(:user, last_name: 'Schlessinger') }
- let!(:user_unmatch) { create(:user, last_name: 'Foo') }
- let(:search_field) { 'last_name' }
- context 'search matches' do
- let(:search_value) { 'Schlessinger' }
- it 'returns the match' do
- expect(response.body).to include('Schlessinger')
- expect(assigns(:users)).to include(user)
- expect(assigns(:users)).to_not include(user_unmatch)
- end
- context 'case insensitive' do
- let(:search_value) { 'schlessinger' }
- it 'is not case sensitive' do
- expect(response.body).to include('schlessinger')
- expect(assigns(:users)).to include(user)
- expect(assigns(:users)).to_not include(user_unmatch)
- end
- end
- end
- end
- context 'search by location_name' do
- let!(:location1) { create(:location, name: 'The First Best Store', business: create(:business, users: [user1_1, user1_2])) }
- let!(:user1_1) { create(:user, email: 'first_first@gmail.com') }
- let!(:user1_2) { create(:user, email: 'first_second@gmail.com') }
- let!(:location2) { create(:location, name: 'The Second Best Store', business: create(:business, users: [user2_1, user2_2])) }
- let!(:user2_1) { create(:user, email: 'second_first@gmail.com') }
- let!(:user2_2) { create(:user, email: 'second_second@gmail.com') }
- let(:search_field) { 'location_name' }
- before do
- user1_1.add_role(:location_admin, location1)
- user1_2.add_role(:location_admin, location1)
- user2_1.add_role(:location_admin, location2)
- user2_2.add_role(:location_admin, location2)
- end
- context 'one location matches' do
- let(:search_value) { 'First' }
- before do
- expect(Location::AdminService).to receive(:new).with(location: location1).and_call_original
- expect_any_instance_of(Location::AdminService).to receive(:admins).and_call_original
- end
- it 'returns the users for that one location' do
- expect(response.body).to include('First')
- expect(assigns(:users)).to include(user1_1)
- expect(assigns(:users)).to include(user1_2)
- expect(assigns(:users)).to_not include(user2_1)
- expect(assigns(:users)).to_not include(user2_2)
- end
- end
- context 'more than one location matches' do
- let!(:admin_service_1) { Location::AdminService.new(location: location1) }
- let!(:admin_service_2) { Location::AdminService.new(location: location2) }
- let(:search_value) { 'Store' }
- before do
- expect(Location::AdminService).to receive(:new).with(location: location1).and_return admin_service_1
- expect(Location::AdminService).to receive(:new).with(location: location2).and_return admin_service_2
- expect(admin_service_1).to receive(:admins).and_call_original
- expect(admin_service_2).to receive(:admins).and_call_original
- end
- it 'returns the uses for both locations' do
- expect(assigns(:users)).to include(user1_1)
- expect(assigns(:users)).to include(user1_2)
- expect(assigns(:users)).to include(user2_1)
- expect(assigns(:users)).to include(user2_2)
- end
- end
- context 'no locations match' do
- let(:search_value) { 'Sally' }
- before { expect(Location::AdminService).to_not receive(:new) }
- it 'returns no matches' do
- expect(response.body).to include('No results found! Please try a different query.')
- expect(assigns(:users)).to_not include(user1_1)
- expect(assigns(:users)).to_not include(user1_2)
- expect(assigns(:users)).to_not include(user2_1)
- expect(assigns(:users)).to_not include(user2_2)
- end
- end
- context 'there are multiple locations associated with one user' do
- let!(:primary_user) { create(:user, email: 'primaryt@gmail.com') }
- let!(:first_location) { create(:location, name: 'My First Location', business: create(:business, users: [primary_user])) }
- let!(:second_location) { create(:location, name: 'My Second Location', business: create(:business, users: [primary_user])) }
- let!(:third_location) { create(:location, name: 'My Third Location', business: create(:business, users: [primary_user])) }
- let(:search_value) { 'My' }
- before do
- primary_user.add_role(:location_admin, first_location)
- primary_user.add_role(:location_admin, second_location)
- primary_user.add_role(:location_admin, third_location)
- end
- it 'should only include the user\'s result once' do
- expect(assigns(:users)).to include(primary_user)
- expect(assigns(:users).length).to be 1
- end
- end
- end
- context 'search by location_id' do
- let!(:location1) { create(:location, id: SecureRandom.uuid, name: 'The First Best Store', business: create(:business, users: [user1_1, user1_2])) }
- let!(:user1_1) { create(:user, email: 'first_first@gmail.com') }
- let!(:user1_2) { create(:user, email: 'first_second@gmail.com') }
- let(:search_field) { 'location_id' }
- before do
- user1_1.add_role(:location_admin, location1)
- user1_2.add_role(:location_admin, location1)
- end
- context 'with a match' do
- let(:search_value) { location1.id }
- it 'returns the match' do
- expect(response.body).to include('first_first@gmail.com')
- expect(response.body).to include('first_second@gmail.com')
- expect(assigns(:users)).to include(user1_1)
- expect(assigns(:users)).to include(user1_2)
- end
- context 'with whitespace' do
- let(:search_value) { ' ' + location1.id + ' ' }
- it 'returns the match' do
- expect(response.body).to include('first_first@gmail.com')
- expect(response.body).to include('first_second@gmail.com')
- expect(assigns(:users)).to include(user1_1)
- expect(assigns(:users)).to include(user1_2)
- end
- end
- end
- context 'without a match' do
- let(:search_value) { SecureRandom.uuid }
- it 'does not return a match' do
- expect(response.body).to include("Couldn't find Location with")
- expect(assigns(:users)).to be(nil)
- end
- end
- end
- end
- end
- end
- context 'user has session with :impersonate_user set to true' do
- before do
- sign_in user
- get 'index', {}, impersonate_user: true
- end
- it 'renders view' do
- expect(response).to render_template 'index'
- end
- end
- context 'rails environment is development' do
- before do
- allow(Rails).to receive(:env).and_return(ActiveSupport::StringInquirer.new('development'))
- sign_in user
- get 'index'
- end
- it 'renders view' do
- expect(response).to render_template 'index'
- end
- end
- end
- describe "GET 'handle_request'" do
- before do
- sign_in user
- get 'handle_request', email: user.email
- end
- it 'redirects to correct url' do
- expect(response).to redirect_to("/switch_user?scope_identifier=user_#{user.id}")
- end
- end
- describe "GET 'do_not_track'" do
- context 'token is not passed' do
- before { sign_in user }
- it 'raises ActionController::RoutingError' do
- expect { get('do_not_track') }.to raise_error(ActionController::RoutingError)
- end
- end
- context 'tokens dont match' do
- let(:time_now) { Time.mktime(2015,07,01,00,00,00) }
- let(:token) { '1435708800 + CZJNfNn78CuXBu' }
- before do
- allow(Time).to receive(:now).and_return(time_now)
- expect(Digest::MD5).to receive(:hexdigest).with(token).and_return(token)
- sign_in user
- get 'do_not_track', token: token
- end
- it 'sets session data' do
- expect(session[:metadata]['disable_tracking']).to be true
- expect(session[:impersonate_user]).to be true
- end
- it 'redirects to correct url' do
- expect(response).to redirect_to('/')
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement