user_organization_controller_test.rb 25 KB

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