user_organization_controller_test.rb 24 KB

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