user_organization_controller_test.rb 27 KB

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