organization_controller_test.rb 24 KB

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