users_organization_controller_test.rb 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  1. require 'test_helper'
  2. class UsersOrganizationControllerTest < ActionDispatch::IntegrationTest
  3. include SearchindexHelper
  4. setup do
  5. # set accept header
  6. @headers = { 'ACCEPT' => 'application/json', 'CONTENT_TYPE' => 'application/json' }
  7. # create agent
  8. roles = Role.where(name: %w[Admin Agent])
  9. groups = Group.all
  10. UserInfo.current_user_id = 1
  11. @backup_admin = User.create!(
  12. login: 'backup-admin',
  13. firstname: 'Backup',
  14. lastname: 'Agent',
  15. email: 'backup-admin@example.com',
  16. password: 'adminpw',
  17. active: true,
  18. roles: roles,
  19. groups: groups,
  20. )
  21. @admin = User.create!(
  22. login: 'rest-admin',
  23. firstname: 'Rest',
  24. lastname: 'Agent',
  25. email: 'rest-admin@example.com',
  26. password: 'adminpw',
  27. active: true,
  28. roles: roles,
  29. groups: groups,
  30. )
  31. # create agent
  32. roles = Role.where(name: 'Agent')
  33. @agent = User.create!(
  34. login: 'rest-agent@example.com',
  35. firstname: 'Rest',
  36. lastname: 'Agent',
  37. email: 'rest-agent@example.com',
  38. password: 'agentpw',
  39. active: true,
  40. roles: roles,
  41. groups: groups,
  42. )
  43. # create customer without org
  44. roles = Role.where(name: 'Customer')
  45. @customer_without_org = User.create!(
  46. login: 'rest-customer1@example.com',
  47. firstname: 'Rest',
  48. lastname: 'Customer1',
  49. email: 'rest-customer1@example.com',
  50. password: 'customer1pw',
  51. active: true,
  52. roles: roles,
  53. )
  54. # create orgs
  55. @organization = Organization.create!(
  56. name: 'Rest Org',
  57. note: 'Rest Org A',
  58. )
  59. @organization2 = Organization.create!(
  60. name: 'Rest Org #2',
  61. note: 'Rest Org B',
  62. )
  63. @organization3 = Organization.create!(
  64. name: 'Rest Org #3',
  65. note: 'Rest Org C',
  66. )
  67. # create customer with org
  68. @customer_with_org = User.create!(
  69. login: 'rest-customer2@example.com',
  70. firstname: 'Rest',
  71. lastname: 'Customer2',
  72. email: 'rest-customer2@example.com',
  73. password: 'customer2pw',
  74. active: true,
  75. roles: roles,
  76. organization_id: @organization.id,
  77. )
  78. configure_elasticsearch do
  79. travel 1.minute
  80. rebuild_searchindex
  81. # execute background jobs
  82. Scheduler.worker(true)
  83. sleep 6
  84. end
  85. end
  86. test 'user create tests - no user' do
  87. post '/api/v1/signshow', params: {}, headers: @headers
  88. # create user with disabled feature
  89. Setting.set('user_create_account', false)
  90. token = @response.headers['CSRF-TOKEN']
  91. # token based on form
  92. params = { email: 'some_new_customer@example.com', authenticity_token: token }
  93. post '/api/v1/users', params: params.to_json, headers: @headers
  94. assert_response(422)
  95. result = JSON.parse(@response.body)
  96. assert(result['error'])
  97. assert_equal('Feature not enabled!', result['error'])
  98. # token based on headers
  99. headers = @headers.merge('X-CSRF-Token' => token)
  100. params = { email: 'some_new_customer@example.com' }
  101. post '/api/v1/users', params: params.to_json, headers: headers
  102. assert_response(422)
  103. result = JSON.parse(@response.body)
  104. assert(result['error'])
  105. assert_equal('Feature not enabled!', result['error'])
  106. Setting.set('user_create_account', true)
  107. # no signup param with enabled feature
  108. params = { email: 'some_new_customer@example.com' }
  109. post '/api/v1/users', params: params.to_json, headers: headers
  110. assert_response(422)
  111. result = JSON.parse(@response.body)
  112. assert(result['error'])
  113. assert_equal('Only signup with not authenticate user possible!', result['error'])
  114. # already existing user with enabled feature
  115. params = { email: 'rest-customer1@example.com', signup: true }
  116. post '/api/v1/users', params: params.to_json, headers: headers
  117. assert_response(422)
  118. result = JSON.parse(@response.body)
  119. assert(result['error'])
  120. assert_equal('Email address is already used for other user.', result['error'])
  121. # email missing with enabled feature
  122. params = { firstname: 'some firstname', signup: true }
  123. post '/api/v1/users', params: params.to_json, headers: headers
  124. assert_response(422)
  125. result = JSON.parse(@response.body)
  126. assert(result['error'])
  127. assert_equal('Attribute \'email\' required!', result['error'])
  128. # email missing with enabled feature
  129. params = { firstname: 'some firstname', signup: true }
  130. post '/api/v1/users', params: params.to_json, headers: headers
  131. assert_response(422)
  132. result = JSON.parse(@response.body)
  133. assert(result['error'])
  134. assert_equal('Attribute \'email\' required!', result['error'])
  135. # create user with enabled feature (take customer role)
  136. params = { firstname: 'Me First', lastname: 'Me Last', email: 'new_here@example.com', signup: true }
  137. post '/api/v1/users', params: params.to_json, headers: headers
  138. assert_response(201)
  139. result = JSON.parse(@response.body)
  140. assert(result)
  141. assert_equal('Me First', result['firstname'])
  142. assert_equal('Me Last', result['lastname'])
  143. assert_equal('new_here@example.com', result['login'])
  144. assert_equal('new_here@example.com', result['email'])
  145. user = User.find(result['id'])
  146. assert_not(user.role?('Admin'))
  147. assert_not(user.role?('Agent'))
  148. assert(user.role?('Customer'))
  149. # create user with admin role (not allowed for signup, take customer role)
  150. role = Role.lookup(name: 'Admin')
  151. params = { firstname: 'Admin First', lastname: 'Admin Last', email: 'new_admin@example.com', role_ids: [ role.id ], signup: true }
  152. post '/api/v1/users', params: params.to_json, headers: headers
  153. assert_response(201)
  154. result = JSON.parse(@response.body)
  155. assert(result)
  156. user = User.find(result['id'])
  157. assert_not(user.role?('Admin'))
  158. assert_not(user.role?('Agent'))
  159. assert(user.role?('Customer'))
  160. # create user with agent role (not allowed for signup, take customer role)
  161. role = Role.lookup(name: 'Agent')
  162. params = { firstname: 'Agent First', lastname: 'Agent Last', email: 'new_agent@example.com', role_ids: [ role.id ], signup: true }
  163. post '/api/v1/users', params: params.to_json, headers: headers
  164. assert_response(201)
  165. result = JSON.parse(@response.body)
  166. assert(result)
  167. user = User.find(result['id'])
  168. assert_not(user.role?('Admin'))
  169. assert_not(user.role?('Agent'))
  170. assert(user.role?('Customer'))
  171. # no user (because of no session)
  172. get '/api/v1/users', params: {}, headers: headers
  173. assert_response(401)
  174. result = JSON.parse(@response.body)
  175. assert_equal('authentication failed', result['error'])
  176. # me
  177. get '/api/v1/users/me', params: {}, headers: headers
  178. assert_response(401)
  179. result = JSON.parse(@response.body)
  180. assert_equal('authentication failed', result['error'])
  181. end
  182. test 'auth tests - not existing user' do
  183. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('not_existing@example.com', 'adminpw')
  184. # me
  185. get '/api/v1/users/me', params: {}, headers: @headers.merge('Authorization' => credentials)
  186. assert_response(401)
  187. result = JSON.parse(@response.body)
  188. assert_equal('authentication failed', result['error'])
  189. get '/api/v1/users', params: {}, headers: @headers.merge('Authorization' => credentials)
  190. assert_response(401)
  191. result = JSON.parse(@response.body)
  192. assert_equal('authentication failed', result['error'])
  193. end
  194. test 'auth tests - username auth, wrong pw' do
  195. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('rest-admin', 'not_existing')
  196. get '/api/v1/users', params: {}, headers: @headers.merge('Authorization' => credentials)
  197. assert_response(401)
  198. result = JSON.parse(@response.body)
  199. assert_equal('authentication failed', result['error'])
  200. end
  201. test 'auth tests - email auth, wrong pw' do
  202. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('rest-admin@example.com', 'not_existing')
  203. get '/api/v1/users', params: {}, headers: @headers.merge('Authorization' => credentials)
  204. assert_response(401)
  205. result = JSON.parse(@response.body)
  206. assert_equal('authentication failed', result['error'])
  207. end
  208. test 'auth tests - username auth' do
  209. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('rest-admin', 'adminpw')
  210. get '/api/v1/users', params: {}, headers: @headers.merge('Authorization' => credentials)
  211. assert_response(200)
  212. result = JSON.parse(@response.body)
  213. assert(result)
  214. end
  215. test 'auth tests - email auth' do
  216. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('rest-admin@example.com', 'adminpw')
  217. get '/api/v1/users', params: {}, headers: @headers.merge('Authorization' => credentials)
  218. assert_response(200)
  219. result = JSON.parse(@response.body)
  220. assert(result)
  221. end
  222. test 'user index and create with admin' do
  223. # email auth
  224. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('rest-admin@example.com', 'adminpw')
  225. # me
  226. get '/api/v1/users/me', params: {}, headers: @headers.merge('Authorization' => credentials)
  227. assert_response(200)
  228. result = JSON.parse(@response.body)
  229. assert(result)
  230. assert_equal(result['email'], 'rest-admin@example.com')
  231. # index
  232. get '/api/v1/users', params: {}, headers: @headers.merge('Authorization' => credentials)
  233. assert_response(200)
  234. result = JSON.parse(@response.body)
  235. assert(result)
  236. # index
  237. get '/api/v1/users', params: {}, headers: @headers.merge('Authorization' => credentials)
  238. assert_response(200)
  239. result = JSON.parse(@response.body)
  240. assert(result)
  241. assert_equal(result.class, Array)
  242. assert(result.length >= 3)
  243. # show/:id
  244. get "/api/v1/users/#{@agent.id}", params: {}, headers: @headers.merge('Authorization' => credentials)
  245. assert_response(200)
  246. result = JSON.parse(@response.body)
  247. assert(result)
  248. assert_equal(result.class, Hash)
  249. assert_equal(result['email'], 'rest-agent@example.com')
  250. get "/api/v1/users/#{@customer_without_org.id}", params: {}, headers: @headers.merge('Authorization' => credentials)
  251. assert_response(200)
  252. result = JSON.parse(@response.body)
  253. assert(result)
  254. assert_equal(result.class, Hash)
  255. assert_equal(result['email'], 'rest-customer1@example.com')
  256. # create user with admin role
  257. role = Role.lookup(name: 'Admin')
  258. params = { firstname: 'Admin First', lastname: 'Admin Last', email: 'new_admin_by_admin@example.com', role_ids: [ role.id ] }
  259. post '/api/v1/users', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  260. assert_response(201)
  261. result = JSON.parse(@response.body)
  262. assert(result)
  263. user = User.find(result['id'])
  264. assert(user.role?('Admin'))
  265. assert_not(user.role?('Agent'))
  266. assert_not(user.role?('Customer'))
  267. assert_equal('new_admin_by_admin@example.com', result['login'])
  268. assert_equal('new_admin_by_admin@example.com', result['email'])
  269. # create user with agent role
  270. role = Role.lookup(name: 'Agent')
  271. params = { firstname: 'Agent First', lastname: 'Agent Last', email: 'new_agent_by_admin1@example.com', role_ids: [ role.id ] }
  272. post '/api/v1/users', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  273. assert_response(201)
  274. result = JSON.parse(@response.body)
  275. assert(result)
  276. user = User.find(result['id'])
  277. assert_not(user.role?('Admin'))
  278. assert(user.role?('Agent'))
  279. assert_not(user.role?('Customer'))
  280. assert_equal('new_agent_by_admin1@example.com', result['login'])
  281. assert_equal('new_agent_by_admin1@example.com', result['email'])
  282. role = Role.lookup(name: 'Agent')
  283. params = { firstname: 'Agent First', email: 'new_agent_by_admin2@example.com', role_ids: [ role.id ] }
  284. post '/api/v1/users', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  285. assert_response(201)
  286. result = JSON.parse(@response.body)
  287. assert(result)
  288. user = User.find(result['id'])
  289. assert_not(user.role?('Admin'))
  290. assert(user.role?('Agent'))
  291. assert_not(user.role?('Customer'))
  292. assert_equal('new_agent_by_admin2@example.com', result['login'])
  293. assert_equal('new_agent_by_admin2@example.com', result['email'])
  294. assert_equal('Agent', result['firstname'])
  295. assert_equal('First', result['lastname'])
  296. role = Role.lookup(name: 'Agent')
  297. params = { firstname: 'Agent First', email: 'new_agent_by_admin2@example.com', role_ids: [ role.id ] }
  298. post '/api/v1/users', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  299. assert_response(422)
  300. result = JSON.parse(@response.body)
  301. assert(result)
  302. assert_equal('Email address is already used for other user.', result['error'])
  303. # missing required attributes
  304. params = { note: 'some note' }
  305. post '/api/v1/users', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  306. assert_response(422)
  307. result = JSON.parse(@response.body)
  308. assert(result)
  309. assert_equal('Minimum one identifier (login, firstname, lastname, phone or email) for user is required.', result['error'])
  310. # invalid email
  311. params = { firstname: 'newfirstname123', email: 'some_what', note: 'some note' }
  312. post '/api/v1/users', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  313. assert_response(422)
  314. result = JSON.parse(@response.body)
  315. assert(result)
  316. assert_equal('Invalid email', result['error'])
  317. # with valid attributes
  318. params = { firstname: 'newfirstname123', note: 'some note' }
  319. post '/api/v1/users', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  320. assert_response(201)
  321. result = JSON.parse(@response.body)
  322. assert(result)
  323. user = User.find(result['id'])
  324. assert_not(user.role?('Admin'))
  325. assert_not(user.role?('Agent'))
  326. assert(user.role?('Customer'))
  327. assert(result['login'].start_with?('auto-'))
  328. assert_equal('', result['email'])
  329. assert_equal('newfirstname123', result['firstname'])
  330. assert_equal('', result['lastname'])
  331. end
  332. test 'user index and create with agent' do
  333. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('rest-agent@example.com', 'agentpw')
  334. # me
  335. get '/api/v1/users/me', params: {}, headers: @headers.merge('Authorization' => credentials)
  336. assert_response(200)
  337. result = JSON.parse(@response.body)
  338. assert(result)
  339. assert_equal(result['email'], 'rest-agent@example.com')
  340. # index
  341. get '/api/v1/users', params: {}, headers: @headers.merge('Authorization' => credentials)
  342. assert_response(200)
  343. result = JSON.parse(@response.body)
  344. assert(result)
  345. # index
  346. get '/api/v1/users', params: {}, headers: @headers.merge('Authorization' => credentials)
  347. assert_response(200)
  348. result = JSON.parse(@response.body)
  349. assert(result)
  350. assert_equal(result.class, Array)
  351. assert(result.length >= 3)
  352. get '/api/v1/users?limit=40&page=1&per_page=2', params: {}, headers: @headers.merge('Authorization' => credentials)
  353. assert_response(200)
  354. result = JSON.parse(@response.body)
  355. assert_equal(Array, result.class)
  356. users = User.order(:id).limit(2)
  357. assert_equal(users[0].id, result[0]['id'])
  358. assert_equal(users[1].id, result[1]['id'])
  359. assert_equal(2, result.count)
  360. get '/api/v1/users?limit=40&page=2&per_page=2', params: {}, headers: @headers.merge('Authorization' => credentials)
  361. assert_response(200)
  362. result = JSON.parse(@response.body)
  363. assert_equal(Array, result.class)
  364. users = User.order(:id).limit(4)
  365. assert_equal(users[2].id, result[0]['id'])
  366. assert_equal(users[3].id, result[1]['id'])
  367. assert_equal(2, result.count)
  368. # create user with admin role
  369. firstname = "First test#{rand(999_999_999)}"
  370. role = Role.lookup(name: 'Admin')
  371. params = { firstname: "Admin#{firstname}", lastname: 'Admin Last', email: 'new_admin_by_agent@example.com', role_ids: [ role.id ] }
  372. post '/api/v1/users', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  373. assert_response(201)
  374. result_user1 = JSON.parse(@response.body)
  375. assert(result_user1)
  376. user = User.find(result_user1['id'])
  377. assert_not(user.role?('Admin'))
  378. assert_not(user.role?('Agent'))
  379. assert(user.role?('Customer'))
  380. assert_equal('new_admin_by_agent@example.com', result_user1['login'])
  381. assert_equal('new_admin_by_agent@example.com', result_user1['email'])
  382. # create user with agent role
  383. role = Role.lookup(name: 'Agent')
  384. params = { firstname: "Agent#{firstname}", lastname: 'Agent Last', email: 'new_agent_by_agent@example.com', role_ids: [ role.id ] }
  385. post '/api/v1/users', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  386. assert_response(201)
  387. result_user1 = JSON.parse(@response.body)
  388. assert(result_user1)
  389. user = User.find(result_user1['id'])
  390. assert_not(user.role?('Admin'))
  391. assert_not(user.role?('Agent'))
  392. assert(user.role?('Customer'))
  393. assert_equal('new_agent_by_agent@example.com', result_user1['login'])
  394. assert_equal('new_agent_by_agent@example.com', result_user1['email'])
  395. # create user with customer role
  396. role = Role.lookup(name: 'Customer')
  397. params = { firstname: "Customer#{firstname}", lastname: 'Customer Last', email: 'new_customer_by_agent@example.com', role_ids: [ role.id ] }
  398. post '/api/v1/users', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  399. assert_response(201)
  400. result_user1 = JSON.parse(@response.body)
  401. assert(result_user1)
  402. user = User.find(result_user1['id'])
  403. assert_not(user.role?('Admin'))
  404. assert_not(user.role?('Agent'))
  405. assert(user.role?('Customer'))
  406. assert_equal('new_customer_by_agent@example.com', result_user1['login'])
  407. assert_equal('new_customer_by_agent@example.com', result_user1['email'])
  408. # search as agent
  409. Scheduler.worker(true)
  410. sleep 2 # let es time to come ready
  411. get "/api/v1/users/search?query=#{CGI.escape("Customer#{firstname}")}", params: {}, headers: @headers.merge('Authorization' => credentials)
  412. assert_response(200)
  413. result = JSON.parse(@response.body)
  414. assert_equal(Array, result.class)
  415. assert_equal(result_user1['id'], result[0]['id'])
  416. assert_equal("Customer#{firstname}", result[0]['firstname'])
  417. assert_equal('Customer Last', result[0]['lastname'])
  418. assert(result[0]['role_ids'])
  419. assert_not(result[0]['roles'])
  420. get "/api/v1/users/search?query=#{CGI.escape("Customer#{firstname}")}&expand=true", params: {}, headers: @headers.merge('Authorization' => credentials)
  421. assert_response(200)
  422. result = JSON.parse(@response.body)
  423. assert_equal(Array, result.class)
  424. assert_equal(result_user1['id'], result[0]['id'])
  425. assert_equal("Customer#{firstname}", result[0]['firstname'])
  426. assert_equal('Customer Last', result[0]['lastname'])
  427. assert(result[0]['role_ids'])
  428. assert(result[0]['roles'])
  429. get "/api/v1/users/search?query=#{CGI.escape("Customer#{firstname}")}&label=true", params: {}, headers: @headers.merge('Authorization' => credentials)
  430. assert_response(200)
  431. result = JSON.parse(@response.body)
  432. assert_equal(Array, result.class)
  433. assert_equal(result_user1['id'], result[0]['id'])
  434. assert_equal("Customer#{firstname} Customer Last <new_customer_by_agent@example.com>", result[0]['label'])
  435. assert_equal("Customer#{firstname} Customer Last <new_customer_by_agent@example.com>", result[0]['value'])
  436. assert_not(result[0]['role_ids'])
  437. assert_not(result[0]['roles'])
  438. role = Role.find_by(name: 'Agent')
  439. get "/api/v1/users/search?query=#{CGI.escape("Customer#{firstname}")}&role_ids=#{role.id}&label=true", params: {}, headers: @headers.merge('Authorization' => credentials)
  440. assert_response(200)
  441. result = JSON.parse(@response.body)
  442. assert_equal(Array, result.class)
  443. assert_equal(0, result.count)
  444. role = Role.find_by(name: 'Customer')
  445. get "/api/v1/users/search?query=#{CGI.escape("Customer#{firstname}")}&role_ids=#{role.id}&label=true", params: {}, headers: @headers.merge('Authorization' => credentials)
  446. assert_response(200)
  447. result = JSON.parse(@response.body)
  448. assert_equal(Array, result.class)
  449. assert_equal(result_user1['id'], result[0]['id'])
  450. assert_equal("Customer#{firstname} Customer Last <new_customer_by_agent@example.com>", result[0]['label'])
  451. assert_equal("Customer#{firstname} Customer Last <new_customer_by_agent@example.com>", result[0]['value'])
  452. assert_not(result[0]['role_ids'])
  453. assert_not(result[0]['roles'])
  454. permission = Permission.find_by(name: 'ticket.agent')
  455. get "/api/v1/users/search?query=#{CGI.escape("Customer#{firstname}")}&permissions=#{permission.name}&label=true", params: {}, headers: @headers.merge('Authorization' => credentials)
  456. assert_response(200)
  457. result = JSON.parse(@response.body)
  458. assert_equal(Array, result.class)
  459. assert_equal(0, result.count)
  460. permission = Permission.find_by(name: 'ticket.customer')
  461. get "/api/v1/users/search?query=#{CGI.escape("Customer#{firstname}")}&permissions=#{permission.name}&label=true", params: {}, headers: @headers.merge('Authorization' => credentials)
  462. assert_response(200)
  463. result = JSON.parse(@response.body)
  464. assert_equal(Array, result.class)
  465. assert_equal(result_user1['id'], result[0]['id'])
  466. assert_equal("Customer#{firstname} Customer Last <new_customer_by_agent@example.com>", result[0]['label'])
  467. assert_equal("Customer#{firstname} Customer Last <new_customer_by_agent@example.com>", result[0]['value'])
  468. assert_not(result[0]['role_ids'])
  469. assert_not(result[0]['roles'])
  470. end
  471. test 'user index and create with customer1' do
  472. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('rest-customer1@example.com', 'customer1pw')
  473. # me
  474. get '/api/v1/users/me', params: {}, headers: @headers.merge('Authorization' => credentials)
  475. assert_response(200)
  476. result = JSON.parse(@response.body)
  477. assert(result)
  478. assert_equal(result['email'], 'rest-customer1@example.com')
  479. # index
  480. get '/api/v1/users', params: {}, headers: @headers.merge('Authorization' => credentials)
  481. assert_response(200)
  482. result = JSON.parse(@response.body)
  483. assert_equal(result.class, Array)
  484. assert_equal(result.length, 1)
  485. # show/:id
  486. get "/api/v1/users/#{@customer_without_org.id}", params: {}, headers: @headers.merge('Authorization' => credentials)
  487. assert_response(200)
  488. result = JSON.parse(@response.body)
  489. assert_equal(result.class, Hash)
  490. assert_equal(result['email'], 'rest-customer1@example.com')
  491. get "/api/v1/users/#{@customer_with_org.id}", params: {}, headers: @headers.merge('Authorization' => credentials)
  492. assert_response(401)
  493. result = JSON.parse(@response.body)
  494. assert_equal(result.class, Hash)
  495. assert(result['error'])
  496. # create user with admin role
  497. role = Role.lookup(name: 'Admin')
  498. params = { firstname: 'Admin First', lastname: 'Admin Last', email: 'new_admin_by_customer1@example.com', role_ids: [ role.id ] }
  499. post '/api/v1/users', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  500. assert_response(401)
  501. # create user with agent role
  502. role = Role.lookup(name: 'Agent')
  503. params = { firstname: 'Agent First', lastname: 'Agent Last', email: 'new_agent_by_customer1@example.com', role_ids: [ role.id ] }
  504. post '/api/v1/users', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  505. assert_response(401)
  506. # search
  507. Scheduler.worker(true)
  508. get "/api/v1/users/search?query=#{CGI.escape('First')}", params: {}, headers: @headers.merge('Authorization' => credentials)
  509. assert_response(401)
  510. end
  511. test 'user index with customer2' do
  512. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('rest-customer2@example.com', 'customer2pw')
  513. # me
  514. get '/api/v1/users/me', params: {}, headers: @headers.merge('Authorization' => credentials)
  515. assert_response(200)
  516. result = JSON.parse(@response.body)
  517. assert(result)
  518. assert_equal(result['email'], 'rest-customer2@example.com')
  519. # index
  520. get '/api/v1/users', params: {}, headers: @headers.merge('Authorization' => credentials)
  521. assert_response(200)
  522. result = JSON.parse(@response.body)
  523. assert_equal(result.class, Array)
  524. assert_equal(result.length, 1)
  525. # show/:id
  526. get "/api/v1/users/#{@customer_with_org.id}", params: {}, headers: @headers.merge('Authorization' => credentials)
  527. assert_response(200)
  528. result = JSON.parse(@response.body)
  529. assert_equal(result.class, Hash)
  530. assert_equal(result['email'], 'rest-customer2@example.com')
  531. get "/api/v1/users/#{@customer_without_org.id}", params: {}, headers: @headers.merge('Authorization' => credentials)
  532. assert_response(401)
  533. result = JSON.parse(@response.body)
  534. assert_equal(result.class, Hash)
  535. assert(result['error'])
  536. # search
  537. Scheduler.worker(true)
  538. get "/api/v1/users/search?query=#{CGI.escape('First')}", params: {}, headers: @headers.merge('Authorization' => credentials)
  539. assert_response(401)
  540. end
  541. test 'organization index with agent' do
  542. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('rest-agent@example.com', 'agentpw')
  543. # index
  544. get '/api/v1/organizations', params: {}, headers: @headers.merge('Authorization' => credentials)
  545. assert_response(200)
  546. result = JSON.parse(@response.body)
  547. assert_equal(result.class, Array)
  548. assert_equal(result[0]['member_ids'].class, Array)
  549. assert(result.length >= 3)
  550. get '/api/v1/organizations?limit=40&page=1&per_page=2', params: {}, headers: @headers.merge('Authorization' => credentials)
  551. assert_response(200)
  552. result = JSON.parse(@response.body)
  553. assert_equal(Array, result.class)
  554. organizations = Organization.order(:id).limit(2)
  555. assert_equal(organizations[0].id, result[0]['id'])
  556. assert_equal(organizations[0].member_ids, result[0]['member_ids'])
  557. assert_equal(organizations[1].id, result[1]['id'])
  558. assert_equal(organizations[1].member_ids, result[1]['member_ids'])
  559. assert_equal(2, result.count)
  560. get '/api/v1/organizations?limit=40&page=2&per_page=2', params: {}, headers: @headers.merge('Authorization' => credentials)
  561. assert_response(200)
  562. result = JSON.parse(@response.body)
  563. assert_equal(Array, result.class)
  564. organizations = Organization.order(:id).limit(4)
  565. assert_equal(organizations[2].id, result[0]['id'])
  566. assert_equal(organizations[2].member_ids, result[0]['member_ids'])
  567. assert_equal(organizations[3].id, result[1]['id'])
  568. assert_equal(organizations[3].member_ids, result[1]['member_ids'])
  569. assert_equal(2, result.count)
  570. # show/:id
  571. get "/api/v1/organizations/#{@organization.id}", params: {}, headers: @headers.merge('Authorization' => credentials)
  572. assert_response(200)
  573. result = JSON.parse(@response.body)
  574. assert_equal(result.class, Hash)
  575. assert_equal(result['member_ids'].class, Array)
  576. assert_not(result['members'])
  577. assert_equal(result['name'], 'Rest Org')
  578. get "/api/v1/organizations/#{@organization2.id}", params: {}, headers: @headers.merge('Authorization' => credentials)
  579. assert_response(200)
  580. result = JSON.parse(@response.body)
  581. assert_equal(result.class, Hash)
  582. assert_equal(result['member_ids'].class, Array)
  583. assert_not(result['members'])
  584. assert_equal(result['name'], 'Rest Org #2')
  585. # search as agent
  586. Scheduler.worker(true)
  587. get "/api/v1/organizations/search?query=#{CGI.escape('Zammad')}", params: {}, headers: @headers.merge('Authorization' => credentials)
  588. assert_response(200)
  589. result = JSON.parse(@response.body)
  590. assert_equal(Array, result.class)
  591. assert_equal('Zammad Foundation', result[0]['name'])
  592. assert(result[0]['member_ids'])
  593. assert_not(result[0]['members'])
  594. get "/api/v1/organizations/search?query=#{CGI.escape('Zammad')}&expand=true", params: {}, headers: @headers.merge('Authorization' => credentials)
  595. assert_response(200)
  596. result = JSON.parse(@response.body)
  597. assert_equal(Array, result.class)
  598. assert_equal('Zammad Foundation', result[0]['name'])
  599. assert(result[0]['member_ids'])
  600. assert(result[0]['members'])
  601. get "/api/v1/organizations/search?query=#{CGI.escape('Zammad')}&label=true", params: {}, headers: @headers.merge('Authorization' => credentials)
  602. assert_response(200)
  603. result = JSON.parse(@response.body)
  604. assert_equal(Array, result.class)
  605. assert_equal('Zammad Foundation', result[0]['label'])
  606. assert_equal('Zammad Foundation', result[0]['value'])
  607. assert_not(result[0]['member_ids'])
  608. assert_not(result[0]['members'])
  609. end
  610. test 'organization index with customer1' do
  611. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('rest-customer1@example.com', 'customer1pw')
  612. # index
  613. get '/api/v1/organizations', params: {}, headers: @headers.merge('Authorization' => credentials)
  614. assert_response(200)
  615. result = JSON.parse(@response.body)
  616. assert_equal(result.class, Array)
  617. assert_equal(result.length, 0)
  618. # show/:id
  619. get "/api/v1/organizations/#{@organization.id}", params: {}, headers: @headers.merge('Authorization' => credentials)
  620. assert_response(200)
  621. result = JSON.parse(@response.body)
  622. assert_equal(result.class, Hash)
  623. assert_nil(result['name'])
  624. get "/api/v1/organizations/#{@organization2.id}", params: {}, headers: @headers.merge('Authorization' => credentials)
  625. assert_response(200)
  626. result = JSON.parse(@response.body)
  627. assert_equal(result.class, Hash)
  628. assert_nil(result['name'])
  629. # search
  630. Scheduler.worker(true)
  631. get "/api/v1/organizations/search?query=#{CGI.escape('Zammad')}", params: {}, headers: @headers.merge('Authorization' => credentials)
  632. assert_response(401)
  633. end
  634. test 'organization index with customer2' do
  635. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('rest-customer2@example.com', 'customer2pw')
  636. # index
  637. get '/api/v1/organizations', params: {}, headers: @headers.merge('Authorization' => credentials)
  638. assert_response(200)
  639. result = JSON.parse(@response.body)
  640. assert_equal(result.class, Array)
  641. assert_equal(result.length, 1)
  642. # show/:id
  643. get "/api/v1/organizations/#{@organization.id}", params: {}, headers: @headers.merge('Authorization' => credentials)
  644. assert_response(200)
  645. result = JSON.parse(@response.body)
  646. assert_equal(result.class, Hash)
  647. assert_equal(result['name'], 'Rest Org')
  648. get "/api/v1/organizations/#{@organization2.id}", params: {}, headers: @headers.merge('Authorization' => credentials)
  649. assert_response(401)
  650. result = JSON.parse(@response.body)
  651. assert_equal(result.class, Hash)
  652. assert_nil(result['name'])
  653. # search
  654. Scheduler.worker(true)
  655. get "/api/v1/organizations/search?query=#{CGI.escape('Zammad')}", params: {}, headers: @headers.merge('Authorization' => credentials)
  656. assert_response(401)
  657. end
  658. end