user_spec.rb 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. require 'rails_helper'
  2. RSpec.describe 'User endpoint', type: :request do
  3. let(:role_with_admin_user_permissions) do
  4. create(:role).tap do |role|
  5. role.permission_grant('admin.user')
  6. end
  7. end
  8. let(:admin_with_admin_user_permissions) { create(:user, roles: [role_with_admin_user_permissions]) }
  9. let(:role_without_admin_user_permissions) do
  10. create(:role).tap do |role|
  11. role.permission_grant('admin.tag')
  12. end
  13. end
  14. let(:admin_without_admin_user_permissions) { create(:user, roles: [role_without_admin_user_permissions]) }
  15. describe 'User creation' do
  16. let(:attributes) { attributes_params_for(:user) }
  17. it 'responds unauthorized for customer' do
  18. requester = create(:customer_user)
  19. authenticated_as(requester)
  20. expect do
  21. post api_v1_users_path, params: attributes
  22. end.to not_change {
  23. User.count
  24. }
  25. expect(response).to have_http_status(:unauthorized)
  26. end
  27. context 'privileged attributes' do
  28. context 'group assignment' do
  29. # group access assignment is in general only valid for agents
  30. # see HasGroups.groups_access_permission?
  31. let(:agent_attributes) do
  32. attributes.merge(
  33. roles: Role.where(name: 'Agent').map(&:name),
  34. )
  35. end
  36. shared_examples 'group assignment' do |map_method_id|
  37. it 'responds success for admin.user' do
  38. authenticated_as(admin_with_admin_user_permissions)
  39. expect do
  40. post api_v1_users_path, params: payload
  41. end.to change {
  42. User.count
  43. }.by(1)
  44. expect(response).to have_http_status(:success)
  45. expect(User.last.send(map_method_id)).to eq(send(map_method_id))
  46. end
  47. it 'responds unauthorized for sub admin without admin.user' do
  48. authenticated_as(admin_without_admin_user_permissions)
  49. expect do
  50. post api_v1_users_path, params: payload
  51. end.to not_change {
  52. User.count
  53. }
  54. expect(response).to have_http_status(:unauthorized)
  55. end
  56. it 'responds successful for agent but removes assignment' do
  57. requester = create(:agent_user)
  58. authenticated_as(requester)
  59. expect do
  60. post api_v1_users_path, params: payload
  61. end.to change {
  62. User.count
  63. }.by(1)
  64. expect(response).to have_http_status(:success)
  65. expect(User.last.send(map_method_id)).to be_blank
  66. end
  67. end
  68. context 'parameter groups' do
  69. let(:group_names_access_map) do
  70. Group.all.map { |g| [g.name, ['full']] }.to_h
  71. end
  72. let(:payload) do
  73. agent_attributes.merge(
  74. groups: group_names_access_map,
  75. )
  76. end
  77. it_behaves_like 'group assignment', :group_names_access_map
  78. end
  79. context 'parameter group_ids' do
  80. let(:group_ids_access_map) do
  81. Group.all.map { |g| [g.id, ['full']] }.to_h
  82. end
  83. let(:payload) do
  84. agent_attributes.merge(
  85. group_ids: group_ids_access_map,
  86. )
  87. end
  88. it_behaves_like 'group assignment', :group_ids_access_map
  89. end
  90. end
  91. context 'role assignment' do
  92. shared_examples 'role assignment' do
  93. let(:privileged) { Role.where(name: 'Admin') }
  94. it 'responds success for admin.user' do
  95. authenticated_as(admin_with_admin_user_permissions)
  96. expect do
  97. post api_v1_users_path, params: payload
  98. end.to change {
  99. User.count
  100. }.by(1)
  101. expect(response).to have_http_status(:success)
  102. expect(User.last.roles).to eq(privileged)
  103. end
  104. it 'responds unauthorized for sub admin without admin.user' do
  105. authenticated_as(admin_without_admin_user_permissions)
  106. expect do
  107. post api_v1_users_path, params: payload
  108. end.to not_change {
  109. User.count
  110. }
  111. expect(response).to have_http_status(:unauthorized)
  112. end
  113. it 'responds successful for agent but removes assignment' do
  114. requester = create(:agent_user)
  115. authenticated_as(requester)
  116. expect do
  117. post api_v1_users_path, params: payload
  118. end.to change {
  119. User.count
  120. }.by(1)
  121. expect(response).to have_http_status(:success)
  122. expect(User.last.roles).to eq(Role.signup_roles)
  123. end
  124. end
  125. context 'parameter roles' do
  126. let(:payload) do
  127. attributes.merge(
  128. roles: privileged.map(&:name),
  129. )
  130. end
  131. it_behaves_like 'role assignment'
  132. end
  133. context 'parameter role_ids' do
  134. let(:payload) do
  135. attributes.merge(
  136. role_ids: privileged.map(&:id),
  137. )
  138. end
  139. it_behaves_like 'role assignment'
  140. end
  141. end
  142. end
  143. end
  144. describe 'User update' do
  145. def authorized_update_request(requester:, requested:)
  146. authenticated_as(requester)
  147. expect do
  148. put api_v1_update_user_path(requested), params: cleaned_params_for(requested).merge(firstname: 'Changed')
  149. end.to change {
  150. requested.reload.firstname
  151. }
  152. expect(response).to have_http_status(:success)
  153. end
  154. def unauthorized_update_request(requester:, requested:)
  155. authenticated_as(requester)
  156. expect do
  157. put api_v1_update_user_path(requested), params: cleaned_params_for(requested).merge(firstname: 'Changed')
  158. end.to not_change {
  159. requested.reload.attributes
  160. }
  161. expect(response).to have_http_status(:unauthorized)
  162. end
  163. context 'request by admin.user' do
  164. let(:requester) { admin_with_admin_user_permissions }
  165. it 'is successful for same admin' do
  166. authorized_update_request(
  167. requester: requester,
  168. requested: requester,
  169. )
  170. end
  171. it 'is successful for other admin' do
  172. authorized_update_request(
  173. requester: requester,
  174. requested: create(:admin_user),
  175. )
  176. end
  177. it 'is successful for agent' do
  178. authorized_update_request(
  179. requester: requester,
  180. requested: create(:agent_user),
  181. )
  182. end
  183. it 'is successful for customer' do
  184. authorized_update_request(
  185. requester: requester,
  186. requested: create(:customer_user),
  187. )
  188. end
  189. end
  190. context 'request by sub admin without admin.user' do
  191. let(:requester) { admin_without_admin_user_permissions }
  192. it 'is unauthorized for same admin' do
  193. unauthorized_update_request(
  194. requester: requester,
  195. requested: requester,
  196. )
  197. end
  198. it 'is unauthorized for other admin' do
  199. unauthorized_update_request(
  200. requester: requester,
  201. requested: create(:admin_user),
  202. )
  203. end
  204. it 'is unauthorized for agent' do
  205. unauthorized_update_request(
  206. requester: requester,
  207. requested: create(:agent_user),
  208. )
  209. end
  210. it 'is unauthorized for customer' do
  211. unauthorized_update_request(
  212. requester: requester,
  213. requested: create(:customer_user),
  214. )
  215. end
  216. end
  217. context 'request by agent' do
  218. let(:requester) { create(:agent_user) }
  219. it 'is unauthorized for admin' do
  220. unauthorized_update_request(
  221. requester: requester,
  222. requested: create(:admin_user),
  223. )
  224. end
  225. it 'is unauthorized same agent' do
  226. unauthorized_update_request(
  227. requester: requester,
  228. requested: requester,
  229. )
  230. end
  231. it 'is unauthorized for other agent' do
  232. unauthorized_update_request(
  233. requester: requester,
  234. requested: create(:agent_user),
  235. )
  236. end
  237. it 'is successful for customer' do
  238. authorized_update_request(
  239. requester: requester,
  240. requested: create(:customer_user),
  241. )
  242. end
  243. end
  244. context 'request by customer' do
  245. let(:requester) { create(:customer_user) }
  246. it 'is unauthorized for admin' do
  247. unauthorized_update_request(
  248. requester: requester,
  249. requested: create(:admin_user),
  250. )
  251. end
  252. it 'is unauthorized for agent' do
  253. unauthorized_update_request(
  254. requester: requester,
  255. requested: create(:agent_user),
  256. )
  257. end
  258. it 'is unauthorized for same customer' do
  259. unauthorized_update_request(
  260. requester: requester,
  261. requested: requester,
  262. )
  263. end
  264. it 'is unauthorized for other customer' do
  265. unauthorized_update_request(
  266. requester: requester,
  267. requested: create(:customer_user),
  268. )
  269. end
  270. it 'is unauthorized for same organization' do
  271. same_organization = create(:organization)
  272. requester.update!(organization: same_organization)
  273. unauthorized_update_request(
  274. requester: requester,
  275. requested: create(:customer_user, organization: same_organization),
  276. )
  277. end
  278. end
  279. context 'privileged attributes' do
  280. let(:requested) { create(:user) }
  281. let(:attribute) { privileged.keys.first }
  282. let(:payload) { cleaned_params_for(requested).merge(privileged) }
  283. def value_of_attribute
  284. # we need to call .to_a otherwise Rails will load the
  285. # ActiveRecord::Associations::CollectionProxy
  286. # on comparsion which is to late
  287. requested.reload.public_send(attribute).to_a
  288. end
  289. shared_examples 'admin types requests' do
  290. it 'responds success for admin.user' do
  291. authenticated_as(admin_with_admin_user_permissions)
  292. expect do
  293. put api_v1_update_user_path(requested), params: payload
  294. end.to change {
  295. value_of_attribute
  296. }
  297. expect(response).to have_http_status(:success)
  298. end
  299. it 'responds unauthorized for sub admin without admin.user' do
  300. authenticated_as(admin_without_admin_user_permissions)
  301. expect do
  302. put api_v1_update_user_path(requested), params: payload
  303. end.to not_change {
  304. value_of_attribute
  305. }
  306. expect(response).to have_http_status(:unauthorized)
  307. end
  308. end
  309. shared_examples 'permitted agent update' do
  310. it 'responds successful for agent but removes assignment' do
  311. requester = create(:agent_user)
  312. authenticated_as(requester)
  313. expect do
  314. put api_v1_update_user_path(requested), params: payload
  315. end.to change {
  316. value_of_attribute
  317. }
  318. expect(response).to have_http_status(:success)
  319. end
  320. end
  321. shared_examples 'forbidden agent update' do
  322. it 'responds successful for agent but removes assignment' do
  323. requester = create(:agent_user)
  324. authenticated_as(requester)
  325. expect do
  326. put api_v1_update_user_path(requested), params: payload
  327. end.to not_change {
  328. value_of_attribute
  329. }
  330. expect(response).to have_http_status(:success)
  331. end
  332. end
  333. context 'group assignment' do
  334. context 'parameter groups' do
  335. let(:privileged) do
  336. {
  337. groups: Group.all.map { |g| [g.name, ['full']] }.to_h
  338. }
  339. end
  340. it_behaves_like 'admin types requests'
  341. it_behaves_like 'forbidden agent update'
  342. end
  343. context 'parameter group_ids' do
  344. let(:privileged) do
  345. {
  346. group_ids: Group.all.map { |g| [g.id, ['full']] }.to_h
  347. }
  348. end
  349. it_behaves_like 'admin types requests'
  350. it_behaves_like 'forbidden agent update'
  351. end
  352. end
  353. context 'role assignment' do
  354. let(:admin_role) { Role.where(name: 'Admin') }
  355. context 'parameter roles' do
  356. let(:privileged) do
  357. {
  358. roles: admin_role.map(&:name),
  359. }
  360. end
  361. it_behaves_like 'admin types requests'
  362. it_behaves_like 'forbidden agent update'
  363. end
  364. context 'parameter role_ids' do
  365. let(:privileged) do
  366. {
  367. role_ids: admin_role.map(&:id),
  368. }
  369. end
  370. it_behaves_like 'admin types requests'
  371. it_behaves_like 'forbidden agent update'
  372. end
  373. end
  374. context 'organization assignment' do
  375. let(:new_organizations) { create_list(:organization, 2) }
  376. context 'parameter organizations' do
  377. let(:privileged) do
  378. {
  379. organizations: new_organizations.map(&:name),
  380. }
  381. end
  382. it_behaves_like 'admin types requests'
  383. it_behaves_like 'permitted agent update'
  384. end
  385. context 'parameter organization_ids' do
  386. let(:privileged) do
  387. {
  388. organization_ids: new_organizations.map(&:id),
  389. }
  390. end
  391. it_behaves_like 'admin types requests'
  392. it_behaves_like 'permitted agent update'
  393. end
  394. end
  395. end
  396. end
  397. describe 'User deletion' do
  398. def authorized_destroy_request(requester:, requested:)
  399. authenticated_as(requester)
  400. delete api_v1_delete_user_path(requested)
  401. expect(response).to have_http_status(:success)
  402. expect(requested).not_to exist_in_database
  403. end
  404. def unauthorized_destroy_request(requester:, requested:)
  405. authenticated_as(requester)
  406. delete api_v1_delete_user_path(requested)
  407. expect(response).to have_http_status(:unauthorized)
  408. expect(requested).to exist_in_database
  409. end
  410. context 'request by admin.user' do
  411. let(:requester) { admin_with_admin_user_permissions }
  412. it 'is successful for same admin' do
  413. authorized_destroy_request(
  414. requester: requester,
  415. requested: requester,
  416. )
  417. end
  418. it 'is successful for other admin' do
  419. authorized_destroy_request(
  420. requester: requester,
  421. requested: create(:admin_user),
  422. )
  423. end
  424. it 'is successful for agent' do
  425. authorized_destroy_request(
  426. requester: requester,
  427. requested: create(:agent_user),
  428. )
  429. end
  430. it 'is successful for customer' do
  431. authorized_destroy_request(
  432. requester: requester,
  433. requested: create(:customer_user),
  434. )
  435. end
  436. end
  437. context 'request by sub admin without admin.user' do
  438. let(:requester) { admin_without_admin_user_permissions }
  439. it 'is unauthorized for same admin' do
  440. unauthorized_destroy_request(
  441. requester: requester,
  442. requested: requester,
  443. )
  444. end
  445. it 'is unauthorized for other admin' do
  446. unauthorized_destroy_request(
  447. requester: requester,
  448. requested: create(:admin_user),
  449. )
  450. end
  451. it 'is unauthorized for agent' do
  452. unauthorized_destroy_request(
  453. requester: requester,
  454. requested: create(:agent_user),
  455. )
  456. end
  457. it 'is unauthorized for customer' do
  458. unauthorized_destroy_request(
  459. requester: requester,
  460. requested: create(:customer_user),
  461. )
  462. end
  463. end
  464. context 'request by agent' do
  465. let(:requester) { create(:agent_user) }
  466. it 'is unauthorized for admin' do
  467. unauthorized_destroy_request(
  468. requester: requester,
  469. requested: create(:admin_user),
  470. )
  471. end
  472. it 'is unauthorized same agent' do
  473. unauthorized_destroy_request(
  474. requester: requester,
  475. requested: requester,
  476. )
  477. end
  478. it 'is unauthorized for other agent' do
  479. unauthorized_destroy_request(
  480. requester: requester,
  481. requested: create(:agent_user),
  482. )
  483. end
  484. it 'is unauthorized for customer' do
  485. unauthorized_destroy_request(
  486. requester: requester,
  487. requested: create(:customer_user),
  488. )
  489. end
  490. end
  491. context 'request by customer' do
  492. let(:requester) { create(:customer_user) }
  493. it 'is unauthorized for admin' do
  494. unauthorized_destroy_request(
  495. requester: requester,
  496. requested: create(:admin_user),
  497. )
  498. end
  499. it 'is unauthorized for agent' do
  500. unauthorized_destroy_request(
  501. requester: requester,
  502. requested: create(:agent_user),
  503. )
  504. end
  505. it 'is unauthorized for same customer' do
  506. unauthorized_destroy_request(
  507. requester: requester,
  508. requested: requester,
  509. )
  510. end
  511. it 'is unauthorized for other customer' do
  512. unauthorized_destroy_request(
  513. requester: requester,
  514. requested: create(:customer_user),
  515. )
  516. end
  517. it 'is unauthorized for same organization' do
  518. same_organization = create(:organization)
  519. requester.update!(organization: same_organization)
  520. unauthorized_destroy_request(
  521. requester: requester,
  522. requested: create(:customer_user, organization: same_organization),
  523. )
  524. end
  525. end
  526. end
  527. end