organizations_controller_test.rb 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. require 'test_helper'
  2. class OrganizationsControllerTest < 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. @admin = User.create!(
  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!(
  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!(
  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!(
  46. name: 'Rest Org #1',
  47. note: 'Rest Org A',
  48. created_at: '2018-02-05 17:42:00',
  49. updated_at: '2018-02-05 20:42:00',
  50. )
  51. @organization2 = Organization.create!(
  52. name: 'Rest Org #2',
  53. note: 'Rest Org B',
  54. created_at: '2018-02-05 18:42:00',
  55. updated_at: '2018-02-05 18:42:00',
  56. )
  57. @organization3 = Organization.create!(
  58. name: 'Rest Org #3',
  59. note: 'Rest Org C',
  60. created_at: '2018-02-05 19:42:00',
  61. updated_at: '2018-02-05 19:42:00',
  62. )
  63. # create customer with org
  64. @customer_with_org = User.create!(
  65. login: 'rest-customer2@example.com',
  66. firstname: 'Rest',
  67. lastname: 'Customer2',
  68. email: 'rest-customer2@example.com',
  69. password: 'customer2pw',
  70. active: true,
  71. roles: roles,
  72. organization_id: @organization.id,
  73. )
  74. configure_elasticsearch do
  75. travel 1.minute
  76. rebuild_searchindex
  77. # execute background jobs
  78. Scheduler.worker(true)
  79. sleep 6
  80. end
  81. UserInfo.current_user_id = nil
  82. end
  83. test 'organization index with agent' do
  84. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('rest-agent@example.com', 'agentpw')
  85. # index
  86. get '/api/v1/organizations', params: {}, headers: @headers.merge('Authorization' => credentials)
  87. assert_response(200)
  88. result = JSON.parse(@response.body)
  89. assert_equal(result.class, Array)
  90. assert_equal(result[0]['member_ids'].class, Array)
  91. assert(result.length >= 3)
  92. get '/api/v1/organizations?limit=40&page=1&per_page=2', params: {}, headers: @headers.merge('Authorization' => credentials)
  93. assert_response(200)
  94. result = JSON.parse(@response.body)
  95. assert_equal(Array, result.class)
  96. organizations = Organization.order(:id).limit(2)
  97. assert_equal(organizations[0].id, result[0]['id'])
  98. assert_equal(organizations[0].member_ids, result[0]['member_ids'])
  99. assert_equal(organizations[1].id, result[1]['id'])
  100. assert_equal(organizations[1].member_ids, result[1]['member_ids'])
  101. assert_equal(2, result.count)
  102. get '/api/v1/organizations?limit=40&page=2&per_page=2', params: {}, headers: @headers.merge('Authorization' => credentials)
  103. assert_response(200)
  104. result = JSON.parse(@response.body)
  105. assert_equal(Array, result.class)
  106. organizations = Organization.order(:id).limit(4)
  107. assert_equal(organizations[2].id, result[0]['id'])
  108. assert_equal(organizations[2].member_ids, result[0]['member_ids'])
  109. assert_equal(organizations[3].id, result[1]['id'])
  110. assert_equal(organizations[3].member_ids, result[1]['member_ids'])
  111. assert_equal(2, result.count)
  112. # show/:id
  113. get "/api/v1/organizations/#{@organization.id}", params: {}, headers: @headers.merge('Authorization' => credentials)
  114. assert_response(200)
  115. result = JSON.parse(@response.body)
  116. assert_equal(result.class, Hash)
  117. assert_equal(result['member_ids'].class, Array)
  118. assert_not(result['members'])
  119. assert_equal(result['name'], 'Rest Org #1')
  120. get "/api/v1/organizations/#{@organization2.id}", params: {}, headers: @headers.merge('Authorization' => credentials)
  121. assert_response(200)
  122. result = JSON.parse(@response.body)
  123. assert_equal(result.class, Hash)
  124. assert_equal(result['member_ids'].class, Array)
  125. assert_not(result['members'])
  126. assert_equal(result['name'], 'Rest Org #2')
  127. # search as agent
  128. Scheduler.worker(true)
  129. get "/api/v1/organizations/search?query=#{CGI.escape('Zammad')}", params: {}, headers: @headers.merge('Authorization' => credentials)
  130. assert_response(200)
  131. result = JSON.parse(@response.body)
  132. assert_equal(Array, result.class)
  133. assert_equal('Zammad Foundation', result[0]['name'])
  134. assert(result[0]['member_ids'])
  135. assert_not(result[0]['members'])
  136. get "/api/v1/organizations/search?query=#{CGI.escape('Zammad')}&expand=true", params: {}, headers: @headers.merge('Authorization' => credentials)
  137. assert_response(200)
  138. result = JSON.parse(@response.body)
  139. assert_equal(Array, result.class)
  140. assert_equal('Zammad Foundation', result[0]['name'])
  141. assert(result[0]['member_ids'])
  142. assert(result[0]['members'])
  143. get "/api/v1/organizations/search?query=#{CGI.escape('Zammad')}&label=true", params: {}, headers: @headers.merge('Authorization' => credentials)
  144. assert_response(200)
  145. result = JSON.parse(@response.body)
  146. assert_equal(Array, result.class)
  147. assert_equal('Zammad Foundation', result[0]['label'])
  148. assert_equal('Zammad Foundation', result[0]['value'])
  149. assert_not(result[0]['member_ids'])
  150. assert_not(result[0]['members'])
  151. end
  152. test 'organization index with customer1' do
  153. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('rest-customer1@example.com', 'customer1pw')
  154. # index
  155. get '/api/v1/organizations', params: {}, headers: @headers.merge('Authorization' => credentials)
  156. assert_response(200)
  157. result = JSON.parse(@response.body)
  158. assert_equal(result.class, Array)
  159. assert_equal(result.length, 0)
  160. # show/:id
  161. get "/api/v1/organizations/#{@organization.id}", params: {}, headers: @headers.merge('Authorization' => credentials)
  162. assert_response(200)
  163. result = JSON.parse(@response.body)
  164. assert_equal(result.class, Hash)
  165. assert_nil(result['name'])
  166. get "/api/v1/organizations/#{@organization2.id}", params: {}, headers: @headers.merge('Authorization' => credentials)
  167. assert_response(200)
  168. result = JSON.parse(@response.body)
  169. assert_equal(result.class, Hash)
  170. assert_nil(result['name'])
  171. # search
  172. Scheduler.worker(true)
  173. get "/api/v1/organizations/search?query=#{CGI.escape('Zammad')}", params: {}, headers: @headers.merge('Authorization' => credentials)
  174. assert_response(401)
  175. end
  176. test 'organization index with customer2' do
  177. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('rest-customer2@example.com', 'customer2pw')
  178. # index
  179. get '/api/v1/organizations', params: {}, headers: @headers.merge('Authorization' => credentials)
  180. assert_response(200)
  181. result = JSON.parse(@response.body)
  182. assert_equal(result.class, Array)
  183. assert_equal(result.length, 1)
  184. # show/:id
  185. get "/api/v1/organizations/#{@organization.id}", params: {}, headers: @headers.merge('Authorization' => credentials)
  186. assert_response(200)
  187. result = JSON.parse(@response.body)
  188. assert_equal(result.class, Hash)
  189. assert_equal(result['name'], 'Rest Org #1')
  190. get "/api/v1/organizations/#{@organization2.id}", params: {}, headers: @headers.merge('Authorization' => credentials)
  191. assert_response(401)
  192. result = JSON.parse(@response.body)
  193. assert_equal(result.class, Hash)
  194. assert_nil(result['name'])
  195. # search
  196. Scheduler.worker(true)
  197. get "/api/v1/organizations/search?query=#{CGI.escape('Zammad')}", params: {}, headers: @headers.merge('Authorization' => credentials)
  198. assert_response(401)
  199. end
  200. test 'organization search sortable' do
  201. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('rest-admin', 'adminpw')
  202. get "/api/v1/organizations/search?query=#{CGI.escape('Rest Org')}", params: {}, headers: @headers.merge('Authorization' => credentials)
  203. assert_response(200)
  204. result = JSON.parse(@response.body)
  205. result.collect! { |v| v['id'] }
  206. assert_equal(Array, result.class)
  207. assert_equal([ @organization.id, @organization3.id, @organization2.id ], result)
  208. get "/api/v1/organizations/search?query=#{CGI.escape('Rest Org')}", params: { sort_by: 'created_at', order_by: 'asc' }, headers: @headers.merge('Authorization' => credentials)
  209. assert_response(200)
  210. result = JSON.parse(@response.body)
  211. result.collect! { |v| v['id'] }
  212. assert_equal(Array, result.class)
  213. assert_equal([ @organization.id, @organization2.id, @organization3.id ], result)
  214. get "/api/v1/organizations/search?query=#{CGI.escape('Rest Org')}", params: { sort_by: 'note', order_by: 'asc' }, headers: @headers.merge('Authorization' => credentials)
  215. assert_response(200)
  216. result = JSON.parse(@response.body)
  217. result.collect! { |v| v['id'] }
  218. assert_equal(Array, result.class)
  219. assert_equal([ @organization.id, @organization2.id, @organization3.id ], result)
  220. get "/api/v1/organizations/search?query=#{CGI.escape('Rest Org')}", params: { sort_by: 'note', order_by: 'desc' }, headers: @headers.merge('Authorization' => credentials)
  221. assert_response(200)
  222. result = JSON.parse(@response.body)
  223. result.collect! { |v| v['id'] }
  224. assert_equal(Array, result.class)
  225. assert_equal([ @organization3.id, @organization2.id, @organization.id ], result)
  226. get "/api/v1/organizations/search?query=#{CGI.escape('Rest Org')}", params: { sort_by: %w[note created_at], order_by: %w[desc asc] }, headers: @headers.merge('Authorization' => credentials)
  227. assert_response(200)
  228. result = JSON.parse(@response.body)
  229. result.collect! { |v| v['id'] }
  230. assert_equal(Array, result.class)
  231. assert_equal([ @organization3.id, @organization2.id, @organization.id ], result)
  232. end
  233. test '04.01 organization show and response format' do
  234. organization = Organization.create!(
  235. name: 'Rest Org NEW',
  236. members: [@customer_without_org],
  237. updated_by_id: @admin.id,
  238. created_by_id: @admin.id,
  239. )
  240. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('rest-admin@example.com', 'adminpw')
  241. get "/api/v1/organizations/#{organization.id}", params: {}, headers: @headers.merge('Authorization' => credentials)
  242. assert_response(200)
  243. result = JSON.parse(@response.body)
  244. assert_equal(Hash, result.class)
  245. assert_equal(organization.id, result['id'])
  246. assert_equal(organization.name, result['name'])
  247. assert_not(result['members'])
  248. assert_equal([@customer_without_org.id], result['member_ids'])
  249. assert_equal(@admin.id, result['updated_by_id'])
  250. assert_equal(@admin.id, result['created_by_id'])
  251. get "/api/v1/organizations/#{organization.id}?expand=true", params: {}, headers: @headers.merge('Authorization' => credentials)
  252. assert_response(200)
  253. result = JSON.parse(@response.body)
  254. assert_equal(Hash, result.class)
  255. assert_equal(organization.id, result['id'])
  256. assert_equal(organization.name, result['name'])
  257. assert(result['members'])
  258. assert_equal([@customer_without_org.id], result['member_ids'])
  259. assert_equal(@admin.id, result['updated_by_id'])
  260. assert_equal(@admin.id, result['created_by_id'])
  261. get "/api/v1/organizations/#{organization.id}?expand=false", params: {}, headers: @headers.merge('Authorization' => credentials)
  262. assert_response(200)
  263. result = JSON.parse(@response.body)
  264. assert_equal(Hash, result.class)
  265. assert_equal(organization.id, result['id'])
  266. assert_equal(organization.name, result['name'])
  267. assert_not(result['members'])
  268. assert_equal([@customer_without_org.id], result['member_ids'])
  269. assert_equal(@admin.id, result['updated_by_id'])
  270. assert_equal(@admin.id, result['created_by_id'])
  271. get "/api/v1/organizations/#{organization.id}?full=true", params: {}, headers: @headers.merge('Authorization' => credentials)
  272. assert_response(200)
  273. result = JSON.parse(@response.body)
  274. assert_equal(Hash, result.class)
  275. assert_equal(organization.id, result['id'])
  276. assert(result['assets'])
  277. assert(result['assets']['Organization'])
  278. assert(result['assets']['Organization'][organization.id.to_s])
  279. assert_equal(organization.id, result['assets']['Organization'][organization.id.to_s]['id'])
  280. assert_equal(organization.name, result['assets']['Organization'][organization.id.to_s]['name'])
  281. assert_equal(organization.member_ids, result['assets']['Organization'][organization.id.to_s]['member_ids'])
  282. assert_not(result['assets']['Organization'][organization.id.to_s]['members'])
  283. get "/api/v1/organizations/#{organization.id}?full=false", params: {}, headers: @headers.merge('Authorization' => credentials)
  284. assert_response(200)
  285. result = JSON.parse(@response.body)
  286. assert_equal(Hash, result.class)
  287. assert_equal(organization.id, result['id'])
  288. assert_equal(organization.name, result['name'])
  289. assert_not(result['members'])
  290. assert_equal([@customer_without_org.id], result['member_ids'])
  291. assert_equal(@admin.id, result['updated_by_id'])
  292. assert_equal(@admin.id, result['created_by_id'])
  293. end
  294. test '04.02 organization index and response format' do
  295. organization = Organization.create!(
  296. name: 'Rest Org NEW',
  297. members: [@customer_without_org],
  298. updated_by_id: @admin.id,
  299. created_by_id: @admin.id,
  300. )
  301. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('rest-admin@example.com', 'adminpw')
  302. get '/api/v1/organizations', params: {}, headers: @headers.merge('Authorization' => credentials)
  303. assert_response(200)
  304. result = JSON.parse(@response.body)
  305. assert_equal(Array, result.class)
  306. assert_equal(Hash, result[0].class)
  307. assert_equal(organization.id, result.last['id'])
  308. assert_equal(organization.name, result.last['name'])
  309. assert_not(result.last['members'])
  310. assert_equal(organization.member_ids, result.last['member_ids'])
  311. assert_equal(@admin.id, result.last['updated_by_id'])
  312. assert_equal(@admin.id, result.last['created_by_id'])
  313. get '/api/v1/organizations?expand=true', params: {}, headers: @headers.merge('Authorization' => credentials)
  314. assert_response(200)
  315. result = JSON.parse(@response.body)
  316. assert_equal(Array, result.class)
  317. assert_equal(Hash, result[0].class)
  318. assert_equal(organization.id, result.last['id'])
  319. assert_equal(organization.name, result.last['name'])
  320. assert_equal(organization.member_ids, result.last['member_ids'])
  321. assert_equal(organization.members.pluck(:login), [@customer_without_org.login])
  322. assert_equal(@admin.id, result.last['updated_by_id'])
  323. assert_equal(@admin.id, result.last['created_by_id'])
  324. get '/api/v1/organizations?expand=false', params: {}, headers: @headers.merge('Authorization' => credentials)
  325. assert_response(200)
  326. result = JSON.parse(@response.body)
  327. assert_equal(Array, result.class)
  328. assert_equal(Hash, result[0].class)
  329. assert_equal(organization.id, result.last['id'])
  330. assert_equal(organization.name, result.last['name'])
  331. assert_not(result.last['members'])
  332. assert_equal(organization.member_ids, result.last['member_ids'])
  333. assert_equal(@admin.id, result.last['updated_by_id'])
  334. assert_equal(@admin.id, result.last['created_by_id'])
  335. get '/api/v1/organizations?full=true', params: {}, headers: @headers.merge('Authorization' => credentials)
  336. assert_response(200)
  337. result = JSON.parse(@response.body)
  338. assert_equal(Hash, result.class)
  339. assert_equal(Array, result['record_ids'].class)
  340. assert_equal(1, result['record_ids'][0])
  341. assert_equal(organization.id, result['record_ids'].last)
  342. assert(result['assets'])
  343. assert(result['assets']['Organization'])
  344. assert(result['assets']['Organization'][organization.id.to_s])
  345. assert_equal(organization.id, result['assets']['Organization'][organization.id.to_s]['id'])
  346. assert_equal(organization.name, result['assets']['Organization'][organization.id.to_s]['name'])
  347. assert_equal(organization.member_ids, result['assets']['Organization'][organization.id.to_s]['member_ids'])
  348. assert_not(result['assets']['Organization'][organization.id.to_s]['members'])
  349. get '/api/v1/organizations?full=false', params: {}, headers: @headers.merge('Authorization' => credentials)
  350. assert_response(200)
  351. result = JSON.parse(@response.body)
  352. assert_equal(Array, result.class)
  353. assert_equal(Hash, result[0].class)
  354. assert_equal(organization.id, result.last['id'])
  355. assert_equal(organization.name, result.last['name'])
  356. assert_not(result.last['members'])
  357. assert_equal(organization.member_ids, result.last['member_ids'])
  358. assert_equal(@admin.id, result.last['updated_by_id'])
  359. assert_equal(@admin.id, result.last['created_by_id'])
  360. end
  361. test '04.03 ticket create and response format' do
  362. params = {
  363. name: 'Rest Org NEW',
  364. members: [@customer_without_org.login],
  365. }
  366. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('rest-admin@example.com', 'adminpw')
  367. post '/api/v1/organizations', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  368. assert_response(201)
  369. result = JSON.parse(@response.body)
  370. assert_equal(Hash, result.class)
  371. organization = Organization.find(result['id'])
  372. assert_equal(organization.name, result['name'])
  373. assert_equal(organization.member_ids, result['member_ids'])
  374. assert_not(result['members'])
  375. assert_equal(@admin.id, result['updated_by_id'])
  376. assert_equal(@admin.id, result['created_by_id'])
  377. params[:name] = 'Rest Org NEW #2'
  378. post '/api/v1/organizations?expand=true', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  379. assert_response(201)
  380. result = JSON.parse(@response.body)
  381. assert_equal(Hash, result.class)
  382. organization = Organization.find(result['id'])
  383. assert_equal(organization.name, result['name'])
  384. assert_equal(organization.member_ids, result['member_ids'])
  385. assert_equal(organization.members.pluck(:login), result['members'])
  386. assert_equal(@admin.id, result['updated_by_id'])
  387. assert_equal(@admin.id, result['created_by_id'])
  388. params[:name] = 'Rest Org NEW #3'
  389. post '/api/v1/organizations?full=true', params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  390. assert_response(201)
  391. result = JSON.parse(@response.body)
  392. assert_equal(Hash, result.class)
  393. organization = Organization.find(result['id'])
  394. assert(result['assets'])
  395. assert(result['assets']['Organization'])
  396. assert(result['assets']['Organization'][organization.id.to_s])
  397. assert_equal(organization.id, result['assets']['Organization'][organization.id.to_s]['id'])
  398. assert_equal(organization.name, result['assets']['Organization'][organization.id.to_s]['name'])
  399. assert_equal(organization.member_ids, result['assets']['Organization'][organization.id.to_s]['member_ids'])
  400. assert_not(result['assets']['Organization'][organization.id.to_s]['members'])
  401. end
  402. test '04.04 ticket update and response formats' do
  403. organization = Organization.create!(
  404. name: 'Rest Org NEW',
  405. members: [@customer_without_org],
  406. updated_by_id: @admin.id,
  407. created_by_id: @admin.id,
  408. )
  409. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('rest-admin@example.com', 'adminpw')
  410. params = {
  411. name: 'a update name #1',
  412. }
  413. put "/api/v1/organizations/#{organization.id}", params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  414. assert_response(200)
  415. result = JSON.parse(@response.body)
  416. assert_equal(Hash, result.class)
  417. organization = Organization.find(result['id'])
  418. assert_equal(params[:name], result['name'])
  419. assert_equal(organization.member_ids, result['member_ids'])
  420. assert_not(result['members'])
  421. assert_equal(@admin.id, result['updated_by_id'])
  422. assert_equal(@admin.id, result['created_by_id'])
  423. params = {
  424. name: 'a update name #2',
  425. }
  426. put "/api/v1/organizations/#{organization.id}?expand=true", params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  427. assert_response(200)
  428. result = JSON.parse(@response.body)
  429. assert_equal(Hash, result.class)
  430. organization = Organization.find(result['id'])
  431. assert_equal(params[:name], result['name'])
  432. assert_equal(organization.member_ids, result['member_ids'])
  433. assert_equal(organization.members.pluck(:login), [@customer_without_org.login])
  434. assert_equal(@admin.id, result['updated_by_id'])
  435. assert_equal(@admin.id, result['created_by_id'])
  436. params = {
  437. name: 'a update name #3',
  438. }
  439. put "/api/v1/organizations/#{organization.id}?full=true", params: params.to_json, headers: @headers.merge('Authorization' => credentials)
  440. assert_response(200)
  441. result = JSON.parse(@response.body)
  442. assert_equal(Hash, result.class)
  443. organization = Organization.find(result['id'])
  444. assert(result['assets'])
  445. assert(result['assets']['Organization'])
  446. assert(result['assets']['Organization'][organization.id.to_s])
  447. assert_equal(organization.id, result['assets']['Organization'][organization.id.to_s]['id'])
  448. assert_equal(params[:name], result['assets']['Organization'][organization.id.to_s]['name'])
  449. assert_equal(organization.member_ids, result['assets']['Organization'][organization.id.to_s]['member_ids'])
  450. assert_not(result['assets']['Organization'][organization.id.to_s]['members'])
  451. end
  452. test '05.01 csv example - customer no access' do
  453. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('rest-customer1@example.com', 'customer1pw')
  454. get '/api/v1/organizations/import_example', params: {}, headers: @headers.merge('Authorization' => credentials)
  455. assert_response(401)
  456. result = JSON.parse(@response.body)
  457. assert_equal('Not authorized (user)!', result['error'])
  458. end
  459. test '05.02 csv example - admin access' do
  460. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('rest-admin@example.com', 'adminpw')
  461. get '/api/v1/organizations/import_example', params: {}, headers: @headers.merge('Authorization' => credentials)
  462. assert_response(200)
  463. rows = CSV.parse(@response.body)
  464. header = rows.shift
  465. assert_equal('id', header[0])
  466. assert_equal('name', header[1])
  467. assert_equal('shared', header[2])
  468. assert_equal('domain', header[3])
  469. assert_equal('domain_assignment', header[4])
  470. assert_equal('active', header[5])
  471. assert_equal('note', header[6])
  472. assert(header.include?('members'))
  473. end
  474. test '05.03 csv import - admin access' do
  475. UserInfo.current_user_id = 1
  476. customer1 = User.create!(
  477. login: 'customer1-members@example.com',
  478. firstname: 'Member',
  479. lastname: 'Customer',
  480. email: 'customer1-members@example.com',
  481. password: 'customerpw',
  482. active: true,
  483. )
  484. customer2 = User.create!(
  485. login: 'customer2-members@example.com',
  486. firstname: 'Member',
  487. lastname: 'Customer',
  488. email: 'customer2-members@example.com',
  489. password: 'customerpw',
  490. active: true,
  491. )
  492. UserInfo.current_user_id = nil
  493. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('rest-admin@example.com', 'adminpw')
  494. # invalid file
  495. csv_file_path = Rails.root.join('test', 'data', 'csv', 'organization_simple_col_not_existing.csv')
  496. csv_file = ::Rack::Test::UploadedFile.new(csv_file_path, 'text/csv')
  497. post '/api/v1/organizations/import?try=true', params: { file: csv_file, col_sep: ';' }, headers: { 'Authorization' => credentials }
  498. assert_response(200)
  499. result = JSON.parse(@response.body)
  500. assert_equal(Hash, result.class)
  501. assert_equal(true, result['try'])
  502. assert_equal(2, result['records'].count)
  503. assert_equal('failed', result['result'])
  504. assert_equal(2, result['errors'].count)
  505. assert_equal("Line 1: unknown attribute 'name2' for Organization.", result['errors'][0])
  506. assert_equal("Line 2: unknown attribute 'name2' for Organization.", result['errors'][1])
  507. # valid file try
  508. csv_file_path = Rails.root.join('test', 'data', 'csv', 'organization_simple.csv')
  509. csv_file = ::Rack::Test::UploadedFile.new(csv_file_path, 'text/csv')
  510. post '/api/v1/organizations/import?try=true', params: { file: csv_file, col_sep: ';' }, headers: { 'Authorization' => credentials }
  511. assert_response(200)
  512. result = JSON.parse(@response.body)
  513. assert_equal(Hash, result.class)
  514. assert_equal(true, result['try'])
  515. assert_equal(2, result['records'].count)
  516. assert_equal('success', result['result'])
  517. assert_nil(Organization.find_by(name: 'organization-member-import1'))
  518. assert_nil(Organization.find_by(name: 'organization-member-import2'))
  519. # valid file
  520. csv_file_path = Rails.root.join('test', 'data', 'csv', 'organization_simple.csv')
  521. csv_file = ::Rack::Test::UploadedFile.new(csv_file_path, 'text/csv')
  522. post '/api/v1/organizations/import', params: { file: csv_file, col_sep: ';' }, headers: { 'Authorization' => credentials }
  523. assert_response(200)
  524. result = JSON.parse(@response.body)
  525. assert_equal(Hash, result.class)
  526. assert_equal(false, result['try'])
  527. assert_equal(2, result['records'].count)
  528. assert_equal('success', result['result'])
  529. organization1 = Organization.find_by(name: 'organization-member-import1')
  530. assert(organization1)
  531. assert_equal(organization1.name, 'organization-member-import1')
  532. assert_equal(organization1.members.count, 1)
  533. assert_equal(organization1.members.first.login, customer1.login)
  534. assert_equal(organization1.active, true)
  535. organization2 = Organization.find_by(name: 'organization-member-import2')
  536. assert(organization2)
  537. assert_equal(organization2.name, 'organization-member-import2')
  538. assert_equal(organization2.members.count, 1)
  539. assert_equal(organization2.members.first.login, customer2.login)
  540. assert_equal(organization2.active, false)
  541. end
  542. end