Advertisement
Guest User

Untitled

a guest
Apr 18th, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.06 KB | None | 0 0
  1. require 'spec_helper'
  2.  
  3. describe Admin::ImpersonateUserController, type: :controller do
  4. render_views
  5.  
  6. let(:user_signin) { create(:user) }
  7.  
  8. describe "GET 'index'" do
  9. context 'user has :impersonate_admin role' do
  10. before do
  11. user_signin.add_role(:impersonate_admin)
  12.  
  13. sign_in user_signin
  14. end
  15.  
  16. it 'renders view' do
  17. get :index
  18. expect(response).to render_template 'index'
  19. expect(response.body).to_not include('No results found! Please try a different query.')
  20. end
  21.  
  22. context 'a search is submitted' do
  23. let!(:search_field) { '' }
  24. let!(:search_value) { '' }
  25. let(:user) { nil }
  26. let(:user1_1) { nil }
  27. let(:user1_2) { nil }
  28. let(:user2_1) { nil }
  29. let(:user2_2) { nil }
  30.  
  31. before do
  32. if user1_1
  33. user1_1.add_role(:location_admin, location1)
  34. user1_1.save
  35. end
  36.  
  37. if user1_2
  38. user1_2.add_role(:location_admin, location1)
  39. user1_2.save
  40. end
  41.  
  42. if user2_1
  43. user2_1.add_role(:location_admin, location2)
  44. user2_1.save
  45. end
  46.  
  47. if user2_2
  48. user2_2.add_role(:location_admin, location2)
  49. user2_2.save
  50. end
  51.  
  52. user.save! if user
  53. get :index, search_field: search_field, search_value: search_value
  54. end
  55.  
  56. context 'invalid search field' do
  57. let!(:search_field) { 'tree' }
  58. let!(:search_value) { 'hello@gmail.com' }
  59.  
  60. it 'raises' do
  61. expect(response).to_not be_successful
  62. expect(response.status).to be 400
  63. expect(response.body).to include 'missing required params'
  64. end
  65. end
  66.  
  67. context 'is a valid search' do
  68. after { expect(response).to be_successful }
  69.  
  70. context 'search by email' do
  71. let(:user) { build(:user, email: 'hello@gmail.com') }
  72. let(:user_unmatch) { create(:user, email: 'foo@gmail.com') }
  73. let(:search_field) { 'email' }
  74. let(:search_value) { 'hello' }
  75.  
  76. it 'returns the match' do
  77. expect(response.body).to include('hello@gmail.com')
  78. expect(assigns(:users)).to include(user)
  79. expect(assigns(:users)).to_not include(user_unmatch)
  80. end
  81. end
  82.  
  83. context 'search by first_name' do
  84. let(:user) { create(:user, first_name: 'Claire') }
  85. let(:user_unmatch) { create(:user, first_name: 'Frank') }
  86. let(:search_field) { 'first_name' }
  87. let(:search_value) { 'Claire' }
  88.  
  89. context 'search matches' do
  90. it 'returns the match' do
  91. expect(response.body).to include('Claire')
  92. expect(assigns(:users)).to include(user)
  93. expect(assigns(:users)).to_not include(user_unmatch)
  94. end
  95.  
  96. context 'case insensitive' do
  97. let(:search_value) { 'claire' }
  98.  
  99. it 'is not case sensitive' do
  100. expect(response.body).to include('claire')
  101. expect(assigns(:users)).to include(user)
  102. expect(assigns(:users)).to_not include(user_unmatch)
  103. end
  104. end
  105. end
  106. end
  107.  
  108. context 'search by last_name' do
  109. let!(:user) { create(:user, last_name: 'Schlessinger') }
  110. let!(:user_unmatch) { create(:user, last_name: 'Foo') }
  111. let(:search_field) { 'last_name' }
  112.  
  113. context 'search matches' do
  114. let(:search_value) { 'Schlessinger' }
  115.  
  116. it 'returns the match' do
  117. expect(response.body).to include('Schlessinger')
  118. expect(assigns(:users)).to include(user)
  119. expect(assigns(:users)).to_not include(user_unmatch)
  120. end
  121.  
  122. context 'case insensitive' do
  123. let(:search_value) { 'schlessinger' }
  124.  
  125. it 'is not case sensitive' do
  126. expect(response.body).to include('schlessinger')
  127. expect(assigns(:users)).to include(user)
  128. expect(assigns(:users)).to_not include(user_unmatch)
  129. end
  130.  
  131. end
  132. end
  133. end
  134.  
  135. context 'search by location_name' do
  136. let!(:location1) { create(:location, name: 'The First Best Store', business: create(:business, users: [user1_1, user1_2])) }
  137. let!(:user1_1) { create(:user, email: 'first_first@gmail.com') }
  138. let!(:user1_2) { create(:user, email: 'first_second@gmail.com') }
  139. let!(:location2) { create(:location, name: 'The Second Best Store', business: create(:business, users: [user2_1, user2_2])) }
  140. let!(:user2_1) { create(:user, email: 'second_first@gmail.com') }
  141. let!(:user2_2) { create(:user, email: 'second_second@gmail.com') }
  142. let(:search_field) { 'location_name' }
  143.  
  144. before do
  145. user1_1.add_role(:location_admin, location1)
  146. user1_2.add_role(:location_admin, location1)
  147. user2_1.add_role(:location_admin, location2)
  148. user2_2.add_role(:location_admin, location2)
  149. end
  150.  
  151. context 'one location matches' do
  152. let(:search_value) { 'First' }
  153.  
  154. before do
  155. expect(Location::AdminService).to receive(:new).with(location: location1).and_call_original
  156. expect_any_instance_of(Location::AdminService).to receive(:admins).and_call_original
  157. end
  158.  
  159. it 'returns the users for that one location' do
  160. expect(response.body).to include('First')
  161. expect(assigns(:users)).to include(user1_1)
  162. expect(assigns(:users)).to include(user1_2)
  163. expect(assigns(:users)).to_not include(user2_1)
  164. expect(assigns(:users)).to_not include(user2_2)
  165. end
  166. end
  167.  
  168. context 'more than one location matches' do
  169. let!(:admin_service_1) { Location::AdminService.new(location: location1) }
  170. let!(:admin_service_2) { Location::AdminService.new(location: location2) }
  171. let(:search_value) { 'Store' }
  172.  
  173. before do
  174. expect(Location::AdminService).to receive(:new).with(location: location1).and_return admin_service_1
  175. expect(Location::AdminService).to receive(:new).with(location: location2).and_return admin_service_2
  176. expect(admin_service_1).to receive(:admins).and_call_original
  177. expect(admin_service_2).to receive(:admins).and_call_original
  178. end
  179.  
  180. it 'returns the uses for both locations' do
  181. expect(assigns(:users)).to include(user1_1)
  182. expect(assigns(:users)).to include(user1_2)
  183. expect(assigns(:users)).to include(user2_1)
  184. expect(assigns(:users)).to include(user2_2)
  185. end
  186. end
  187.  
  188. context 'no locations match' do
  189. let(:search_value) { 'Sally' }
  190.  
  191. before { expect(Location::AdminService).to_not receive(:new) }
  192.  
  193. it 'returns no matches' do
  194. expect(response.body).to include('No results found! Please try a different query.')
  195. expect(assigns(:users)).to_not include(user1_1)
  196. expect(assigns(:users)).to_not include(user1_2)
  197. expect(assigns(:users)).to_not include(user2_1)
  198. expect(assigns(:users)).to_not include(user2_2)
  199. end
  200. end
  201.  
  202. context 'there are multiple locations associated with one user' do
  203. let!(:primary_user) { create(:user, email: 'primaryt@gmail.com') }
  204. let!(:first_location) { create(:location, name: 'My First Location', business: create(:business, users: [primary_user])) }
  205. let!(:second_location) { create(:location, name: 'My Second Location', business: create(:business, users: [primary_user])) }
  206. let!(:third_location) { create(:location, name: 'My Third Location', business: create(:business, users: [primary_user])) }
  207. let(:search_value) { 'My' }
  208.  
  209. before do
  210. primary_user.add_role(:location_admin, first_location)
  211. primary_user.add_role(:location_admin, second_location)
  212. primary_user.add_role(:location_admin, third_location)
  213. end
  214.  
  215. it 'should only include the user\'s result once' do
  216. expect(assigns(:users)).to include(primary_user)
  217. expect(assigns(:users).length).to be 1
  218. end
  219. end
  220. end
  221.  
  222. context 'search by location_id' do
  223. let!(:location1) { create(:location, id: SecureRandom.uuid, name: 'The First Best Store', business: create(:business, users: [user1_1, user1_2])) }
  224. let!(:user1_1) { create(:user, email: 'first_first@gmail.com') }
  225. let!(:user1_2) { create(:user, email: 'first_second@gmail.com') }
  226. let(:search_field) { 'location_id' }
  227.  
  228. before do
  229. user1_1.add_role(:location_admin, location1)
  230. user1_2.add_role(:location_admin, location1)
  231. end
  232.  
  233. context 'with a match' do
  234. let(:search_value) { location1.id }
  235.  
  236. it 'returns the match' do
  237. expect(response.body).to include('first_first@gmail.com')
  238. expect(response.body).to include('first_second@gmail.com')
  239. expect(assigns(:users)).to include(user1_1)
  240. expect(assigns(:users)).to include(user1_2)
  241. end
  242.  
  243. context 'with whitespace' do
  244. let(:search_value) { ' ' + location1.id + ' ' }
  245.  
  246. it 'returns the match' do
  247. expect(response.body).to include('first_first@gmail.com')
  248. expect(response.body).to include('first_second@gmail.com')
  249. expect(assigns(:users)).to include(user1_1)
  250. expect(assigns(:users)).to include(user1_2)
  251. end
  252. end
  253. end
  254.  
  255. context 'without a match' do
  256. let(:search_value) { SecureRandom.uuid }
  257.  
  258. it 'does not return a match' do
  259. expect(response.body).to include("Couldn't find Location with")
  260. expect(assigns(:users)).to be(nil)
  261. end
  262. end
  263. end
  264. end
  265. end
  266. end
  267.  
  268. context 'user has session with :impersonate_user set to true' do
  269. before do
  270. sign_in user
  271. get 'index', {}, impersonate_user: true
  272. end
  273.  
  274. it 'renders view' do
  275. expect(response).to render_template 'index'
  276. end
  277. end
  278.  
  279. context 'rails environment is development' do
  280. before do
  281. allow(Rails).to receive(:env).and_return(ActiveSupport::StringInquirer.new('development'))
  282.  
  283. sign_in user
  284. get 'index'
  285. end
  286.  
  287. it 'renders view' do
  288. expect(response).to render_template 'index'
  289. end
  290. end
  291. end
  292.  
  293. describe "GET 'handle_request'" do
  294. before do
  295. sign_in user
  296. get 'handle_request', email: user.email
  297. end
  298.  
  299. it 'redirects to correct url' do
  300. expect(response).to redirect_to("/switch_user?scope_identifier=user_#{user.id}")
  301. end
  302. end
  303.  
  304. describe "GET 'do_not_track'" do
  305. context 'token is not passed' do
  306. before { sign_in user }
  307.  
  308. it 'raises ActionController::RoutingError' do
  309. expect { get('do_not_track') }.to raise_error(ActionController::RoutingError)
  310. end
  311. end
  312.  
  313. context 'tokens dont match' do
  314. let(:time_now) { Time.mktime(2015,07,01,00,00,00) }
  315. let(:token) { '1435708800 + CZJNfNn78CuXBu' }
  316.  
  317. before do
  318. allow(Time).to receive(:now).and_return(time_now)
  319. expect(Digest::MD5).to receive(:hexdigest).with(token).and_return(token)
  320.  
  321. sign_in user
  322. get 'do_not_track', token: token
  323. end
  324.  
  325. it 'sets session data' do
  326. expect(session[:metadata]['disable_tracking']).to be true
  327. expect(session[:impersonate_user]).to be true
  328. end
  329.  
  330. it 'redirects to correct url' do
  331. expect(response).to redirect_to('/')
  332. end
  333. end
  334. end
  335. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement