organization_spec.rb 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'Organization', type: :request, searchindex: true, performs_jobs: true do
  4. let!(:admin) do
  5. create(:admin, groups: Group.all)
  6. end
  7. let!(:agent) do
  8. create(:agent, firstname: 'Search 1234', groups: Group.all)
  9. end
  10. let!(:customer) do
  11. create(:customer)
  12. end
  13. let!(:organization) do
  14. create(
  15. :organization,
  16. name: 'Rest Org #1',
  17. note: 'Rest Org #1',
  18. created_at: '2017-09-05 10:00:00',
  19. )
  20. end
  21. let!(:organization2) do
  22. create(
  23. :organization,
  24. name: 'Rest Org #2',
  25. note: 'Rest Org #2',
  26. created_at: '2017-09-05 11:00:00',
  27. )
  28. end
  29. let!(:organization3) do
  30. create(
  31. :organization,
  32. name: 'Rest Org #3',
  33. note: 'Rest Org #3',
  34. created_at: '2017-09-05 12:00:00',
  35. )
  36. end
  37. let!(:customer2) do
  38. create(:customer, organization: organization)
  39. end
  40. before do
  41. configure_elasticsearch rebuild: true, required: true
  42. end
  43. describe 'request handling' do
  44. it 'does index with agent' do
  45. # index
  46. authenticated_as(agent)
  47. get '/api/v1/organizations', params: {}, as: :json
  48. expect(response).to have_http_status(:ok)
  49. expect(json_response).to be_a_kind_of(Array)
  50. expect(json_response[0]['member_ids']).to be_a_kind_of(Array)
  51. expect(json_response.length >= 3).to be_truthy
  52. get '/api/v1/organizations?limit=40&page=1&per_page=2', params: {}, as: :json
  53. expect(response).to have_http_status(:ok)
  54. expect(json_response).to be_a_kind_of(Array)
  55. organizations = Organization.order(:id).limit(2)
  56. expect(json_response[0]['id']).to eq(organizations[0].id)
  57. expect(json_response[0]['member_ids']).to eq(organizations[0].member_ids)
  58. expect(json_response[1]['id']).to eq(organizations[1].id)
  59. expect(json_response[1]['member_ids']).to eq(organizations[1].member_ids)
  60. expect(json_response.count).to eq(2)
  61. get '/api/v1/organizations?limit=40&page=2&per_page=2', params: {}, as: :json
  62. expect(response).to have_http_status(:ok)
  63. expect(json_response).to be_a_kind_of(Array)
  64. organizations = Organization.order(:id).limit(4)
  65. expect(json_response[0]['id']).to eq(organizations[2].id)
  66. expect(json_response[0]['member_ids']).to eq(organizations[2].member_ids)
  67. expect(json_response[1]['id']).to eq(organizations[3].id)
  68. expect(json_response[1]['member_ids']).to eq(organizations[3].member_ids)
  69. expect(json_response.count).to eq(2)
  70. # show/:id
  71. get "/api/v1/organizations/#{organization.id}", params: {}, as: :json
  72. expect(response).to have_http_status(:ok)
  73. expect(json_response).to be_a_kind_of(Hash)
  74. expect(json_response['member_ids']).to be_a_kind_of(Array)
  75. expect(json_response['members']).to be_falsey
  76. expect('Rest Org #1').to eq(json_response['name'])
  77. get "/api/v1/organizations/#{organization2.id}", params: {}, as: :json
  78. expect(response).to have_http_status(:ok)
  79. expect(json_response).to be_a_kind_of(Hash)
  80. expect(json_response['member_ids']).to be_a_kind_of(Array)
  81. expect(json_response['members']).to be_falsey
  82. expect('Rest Org #2').to eq(json_response['name'])
  83. # search as agent
  84. perform_enqueued_jobs
  85. get "/api/v1/organizations/search?query=#{CGI.escape('Zammad')}", params: {}, as: :json
  86. expect(response).to have_http_status(:ok)
  87. expect(json_response).to be_a_kind_of(Array)
  88. organization = json_response.detect { |object| object['name'] == 'Zammad Foundation' }
  89. expect(organization['name']).to eq('Zammad Foundation')
  90. expect(organization['member_ids']).to be_truthy
  91. expect(organization['members']).to be_falsey
  92. get "/api/v1/organizations/search?query=#{CGI.escape('Zammad')}&expand=true", params: {}, as: :json
  93. expect(response).to have_http_status(:ok)
  94. expect(json_response).to be_a_kind_of(Array)
  95. organization = json_response.detect { |object| object['name'] == 'Zammad Foundation' }
  96. expect(organization['name']).to eq('Zammad Foundation')
  97. expect(organization['member_ids']).to be_truthy
  98. expect(organization['members']).to be_truthy
  99. get "/api/v1/organizations/search?query=#{CGI.escape('Zammad')}&label=true", params: {}, as: :json
  100. expect(response).to have_http_status(:ok)
  101. expect(json_response).to be_a_kind_of(Array)
  102. organization = json_response.detect { |object| object['label'] == 'Zammad Foundation' }
  103. expect(organization['label']).to eq('Zammad Foundation')
  104. expect(organization['value']).to eq('Zammad Foundation')
  105. expect(organization['member_ids']).to be_falsey
  106. expect(organization['members']).to be_falsey
  107. end
  108. it 'does index with customer1' do
  109. # index
  110. authenticated_as(customer)
  111. get '/api/v1/organizations', params: {}, as: :json
  112. expect(response).to have_http_status(:ok)
  113. expect(json_response).to be_a_kind_of(Array)
  114. expect(json_response.length).to eq(0)
  115. # show/:id
  116. get "/api/v1/organizations/#{organization.id}", params: {}, as: :json
  117. expect(response).to have_http_status(:ok)
  118. expect(json_response).to be_a_kind_of(Hash)
  119. expect(json_response['name']).to be_nil
  120. get "/api/v1/organizations/#{organization2.id}", params: {}, as: :json
  121. expect(response).to have_http_status(:ok)
  122. expect(json_response).to be_a_kind_of(Hash)
  123. expect(json_response['name']).to be_nil
  124. # search
  125. perform_enqueued_jobs
  126. get "/api/v1/organizations/search?query=#{CGI.escape('Zammad')}", params: {}, as: :json
  127. expect(response).to have_http_status(:forbidden)
  128. end
  129. it 'does index with customer2' do
  130. # index
  131. authenticated_as(customer2)
  132. get '/api/v1/organizations', params: {}, as: :json
  133. expect(response).to have_http_status(:ok)
  134. expect(json_response).to be_a_kind_of(Array)
  135. expect(json_response.length).to eq(1)
  136. # show/:id
  137. get "/api/v1/organizations/#{organization.id}", params: {}, as: :json
  138. expect(response).to have_http_status(:ok)
  139. expect(json_response).to be_a_kind_of(Hash)
  140. expect('Rest Org #1').to eq(json_response['name'])
  141. get "/api/v1/organizations/#{organization2.id}", params: {}, as: :json
  142. expect(response).to have_http_status(:forbidden)
  143. expect(json_response).to be_a_kind_of(Hash)
  144. expect(json_response['name']).to be_nil
  145. # search
  146. perform_enqueued_jobs
  147. get "/api/v1/organizations/search?query=#{CGI.escape('Zammad')}", params: {}, as: :json
  148. expect(response).to have_http_status(:forbidden)
  149. end
  150. it 'does organization search sortable' do
  151. authenticated_as(admin)
  152. get "/api/v1/organizations/search?query=#{CGI.escape('Rest Org')}", params: {}, as: :json
  153. expect(response).to have_http_status(:ok)
  154. result = json_response
  155. result.collect! { |v| v['id'] }
  156. expect(result).to be_a_kind_of(Array)
  157. expect(result).to eq([ organization.id, organization3.id, organization2.id ])
  158. get "/api/v1/organizations/search?query=#{CGI.escape('Rest Org')}", params: { sort_by: 'created_at', order_by: 'asc' }, as: :json
  159. expect(response).to have_http_status(:ok)
  160. result = json_response
  161. result.collect! { |v| v['id'] }
  162. expect(result).to be_a_kind_of(Array)
  163. expect(result).to eq([ organization.id, organization2.id, organization3.id ])
  164. get "/api/v1/organizations/search?query=#{CGI.escape('Rest Org')}", params: { sort_by: 'note', order_by: 'asc' }, as: :json
  165. expect(response).to have_http_status(:ok)
  166. result = json_response
  167. result.collect! { |v| v['id'] }
  168. expect(result).to be_a_kind_of(Array)
  169. expect(result).to eq([ organization.id, organization2.id, organization3.id ])
  170. get "/api/v1/organizations/search?query=#{CGI.escape('Rest Org')}", params: { sort_by: 'note', order_by: 'desc' }, as: :json
  171. expect(response).to have_http_status(:ok)
  172. result = json_response
  173. result.collect! { |v| v['id'] }
  174. expect(result).to be_a_kind_of(Array)
  175. expect(result).to eq([ organization3.id, organization2.id, organization.id ])
  176. get "/api/v1/organizations/search?query=#{CGI.escape('Rest Org')}", params: { sort_by: %w[note created_at], order_by: %w[desc asc] }, as: :json
  177. expect(response).to have_http_status(:ok)
  178. result = json_response
  179. result.collect! { |v| v['id'] }
  180. expect(result).to be_a_kind_of(Array)
  181. expect(result).to eq([ organization3.id, organization2.id, organization.id ])
  182. end
  183. it 'does organization show and response format' do
  184. organization = create(
  185. :organization,
  186. name: 'Rest Org NEW',
  187. members: [customer],
  188. updated_by_id: admin.id,
  189. created_by_id: admin.id,
  190. )
  191. authenticated_as(admin)
  192. get "/api/v1/organizations/#{organization.id}", params: {}, as: :json
  193. expect(response).to have_http_status(:ok)
  194. expect(json_response).to be_a_kind_of(Hash)
  195. expect(json_response['id']).to eq(organization.id)
  196. expect(json_response['name']).to eq(organization.name)
  197. expect(json_response['members']).to be_falsey
  198. expect(json_response['member_ids']).to eq([customer.id])
  199. expect(json_response['updated_by_id']).to eq(admin.id)
  200. expect(json_response['created_by_id']).to eq(admin.id)
  201. get "/api/v1/organizations/#{organization.id}?expand=true", params: {}, as: :json
  202. expect(response).to have_http_status(:ok)
  203. expect(json_response).to be_a_kind_of(Hash)
  204. expect(json_response['id']).to eq(organization.id)
  205. expect(json_response['name']).to eq(organization.name)
  206. expect(json_response['members']).to be_truthy
  207. expect(json_response['member_ids']).to eq([customer.id])
  208. expect(json_response['updated_by_id']).to eq(admin.id)
  209. expect(json_response['created_by_id']).to eq(admin.id)
  210. get "/api/v1/organizations/#{organization.id}?expand=false", params: {}, as: :json
  211. expect(response).to have_http_status(:ok)
  212. expect(json_response).to be_a_kind_of(Hash)
  213. expect(json_response['id']).to eq(organization.id)
  214. expect(json_response['name']).to eq(organization.name)
  215. expect(json_response['members']).to be_falsey
  216. expect(json_response['member_ids']).to eq([customer.id])
  217. expect(json_response['updated_by_id']).to eq(admin.id)
  218. expect(json_response['created_by_id']).to eq(admin.id)
  219. get "/api/v1/organizations/#{organization.id}?full=true", params: {}, as: :json
  220. expect(response).to have_http_status(:ok)
  221. expect(json_response).to be_a_kind_of(Hash)
  222. expect(json_response['id']).to eq(organization.id)
  223. expect(json_response['assets']).to be_truthy
  224. expect(json_response['assets']['Organization']).to be_truthy
  225. expect(json_response['assets']['Organization'][organization.id.to_s]).to be_truthy
  226. expect(json_response['assets']['Organization'][organization.id.to_s]['id']).to eq(organization.id)
  227. expect(json_response['assets']['Organization'][organization.id.to_s]['name']).to eq(organization.name)
  228. expect(json_response['assets']['Organization'][organization.id.to_s]['member_ids']).to eq(organization.member_ids)
  229. expect(json_response['assets']['Organization'][organization.id.to_s]['members']).to be_falsey
  230. get "/api/v1/organizations/#{organization.id}?full=false", params: {}, as: :json
  231. expect(response).to have_http_status(:ok)
  232. expect(json_response).to be_a_kind_of(Hash)
  233. expect(json_response['id']).to eq(organization.id)
  234. expect(json_response['name']).to eq(organization.name)
  235. expect(json_response['members']).to be_falsey
  236. expect(json_response['member_ids']).to eq([customer.id])
  237. expect(json_response['updated_by_id']).to eq(admin.id)
  238. expect(json_response['created_by_id']).to eq(admin.id)
  239. end
  240. it 'does organization index and response format' do
  241. organization = create(
  242. :organization,
  243. name: 'Rest Org NEW',
  244. members: [customer],
  245. updated_by_id: admin.id,
  246. created_by_id: admin.id,
  247. )
  248. authenticated_as(admin)
  249. get '/api/v1/organizations', params: {}, as: :json
  250. expect(response).to have_http_status(:ok)
  251. expect(json_response).to be_a_kind_of(Array)
  252. expect(json_response[0].class).to eq(Hash)
  253. expect(json_response.last['id']).to eq(organization.id)
  254. expect(json_response.last['name']).to eq(organization.name)
  255. expect(json_response.last['members']).to be_falsey
  256. expect(json_response.last['member_ids']).to eq(organization.member_ids)
  257. expect(json_response.last['updated_by_id']).to eq(admin.id)
  258. expect(json_response.last['created_by_id']).to eq(admin.id)
  259. get '/api/v1/organizations?expand=true', params: {}, as: :json
  260. expect(response).to have_http_status(:ok)
  261. expect(json_response).to be_a_kind_of(Array)
  262. expect(json_response[0].class).to eq(Hash)
  263. expect(json_response.last['id']).to eq(organization.id)
  264. expect(json_response.last['name']).to eq(organization.name)
  265. expect(json_response.last['member_ids']).to eq(organization.member_ids)
  266. expect([customer.login]).to eq(organization.members.pluck(:login))
  267. expect(json_response.last['updated_by_id']).to eq(admin.id)
  268. expect(json_response.last['created_by_id']).to eq(admin.id)
  269. get '/api/v1/organizations?expand=false', params: {}, as: :json
  270. expect(response).to have_http_status(:ok)
  271. expect(json_response).to be_a_kind_of(Array)
  272. expect(json_response[0].class).to eq(Hash)
  273. expect(json_response.last['id']).to eq(organization.id)
  274. expect(json_response.last['name']).to eq(organization.name)
  275. expect(json_response.last['members']).to be_falsey
  276. expect(json_response.last['member_ids']).to eq(organization.member_ids)
  277. expect(json_response.last['updated_by_id']).to eq(admin.id)
  278. expect(json_response.last['created_by_id']).to eq(admin.id)
  279. get '/api/v1/organizations?full=true', params: {}, as: :json
  280. expect(response).to have_http_status(:ok)
  281. expect(json_response).to be_a_kind_of(Hash)
  282. expect(json_response['record_ids'].class).to eq(Array)
  283. expect(json_response['record_ids'][0]).to eq(1)
  284. expect(json_response['record_ids'].last).to eq(organization.id)
  285. expect(json_response['assets']).to be_truthy
  286. expect(json_response['assets']['Organization']).to be_truthy
  287. expect(json_response['assets']['Organization'][organization.id.to_s]).to be_truthy
  288. expect(json_response['assets']['Organization'][organization.id.to_s]['id']).to eq(organization.id)
  289. expect(json_response['assets']['Organization'][organization.id.to_s]['name']).to eq(organization.name)
  290. expect(json_response['assets']['Organization'][organization.id.to_s]['member_ids']).to eq(organization.member_ids)
  291. expect(json_response['assets']['Organization'][organization.id.to_s]['members']).to be_falsey
  292. get '/api/v1/organizations?full=false', params: {}, as: :json
  293. expect(response).to have_http_status(:ok)
  294. expect(json_response).to be_a_kind_of(Array)
  295. expect(json_response[0].class).to eq(Hash)
  296. expect(json_response.last['id']).to eq(organization.id)
  297. expect(json_response.last['name']).to eq(organization.name)
  298. expect(json_response.last['members']).to be_falsey
  299. expect(json_response.last['member_ids']).to eq(organization.member_ids)
  300. expect(json_response.last['updated_by_id']).to eq(admin.id)
  301. expect(json_response.last['created_by_id']).to eq(admin.id)
  302. end
  303. it 'does ticket create and response format' do
  304. params = {
  305. name: 'Rest Org NEW',
  306. members: [customer.login],
  307. }
  308. authenticated_as(admin)
  309. post '/api/v1/organizations', params: params, as: :json
  310. expect(response).to have_http_status(:created)
  311. expect(json_response).to be_a_kind_of(Hash)
  312. organization = Organization.find(json_response['id'])
  313. expect(json_response['name']).to eq(organization.name)
  314. expect(json_response['member_ids']).to eq(organization.member_ids)
  315. expect(json_response['members']).to be_falsey
  316. expect(json_response['updated_by_id']).to eq(admin.id)
  317. expect(json_response['created_by_id']).to eq(admin.id)
  318. params[:name] = 'Rest Org NEW #2'
  319. post '/api/v1/organizations?expand=true', params: params, as: :json
  320. expect(response).to have_http_status(:created)
  321. expect(json_response).to be_a_kind_of(Hash)
  322. organization = Organization.find(json_response['id'])
  323. expect(json_response['name']).to eq(organization.name)
  324. expect(json_response['member_ids']).to eq(organization.member_ids)
  325. expect(json_response['members']).to eq(organization.members.pluck(:login))
  326. expect(json_response['updated_by_id']).to eq(admin.id)
  327. expect(json_response['created_by_id']).to eq(admin.id)
  328. params[:name] = 'Rest Org NEW #3'
  329. post '/api/v1/organizations?full=true', params: params, as: :json
  330. expect(response).to have_http_status(:created)
  331. expect(json_response).to be_a_kind_of(Hash)
  332. organization = Organization.find(json_response['id'])
  333. expect(json_response['assets']).to be_truthy
  334. expect(json_response['assets']['Organization']).to be_truthy
  335. expect(json_response['assets']['Organization'][organization.id.to_s]).to be_truthy
  336. expect(json_response['assets']['Organization'][organization.id.to_s]['id']).to eq(organization.id)
  337. expect(json_response['assets']['Organization'][organization.id.to_s]['name']).to eq(organization.name)
  338. expect(json_response['assets']['Organization'][organization.id.to_s]['member_ids']).to eq(organization.member_ids)
  339. expect(json_response['assets']['Organization'][organization.id.to_s]['members']).to be_falsey
  340. end
  341. it 'does ticket update and response formats' do
  342. organization = create(
  343. :organization,
  344. name: 'Rest Org NEW',
  345. members: [customer],
  346. updated_by_id: admin.id,
  347. created_by_id: admin.id,
  348. )
  349. params = {
  350. name: 'a update name #1',
  351. }
  352. authenticated_as(admin)
  353. put "/api/v1/organizations/#{organization.id}", params: params, as: :json
  354. expect(response).to have_http_status(:ok)
  355. expect(json_response).to be_a_kind_of(Hash)
  356. organization = Organization.find(json_response['id'])
  357. expect(json_response['name']).to eq(params[:name])
  358. expect(json_response['member_ids']).to eq(organization.member_ids)
  359. expect(json_response['members']).to be_falsey
  360. expect(json_response['updated_by_id']).to eq(admin.id)
  361. expect(json_response['created_by_id']).to eq(admin.id)
  362. params = {
  363. name: 'a update name #2',
  364. }
  365. put "/api/v1/organizations/#{organization.id}?expand=true", params: params, as: :json
  366. expect(response).to have_http_status(:ok)
  367. expect(json_response).to be_a_kind_of(Hash)
  368. organization = Organization.find(json_response['id'])
  369. expect(json_response['name']).to eq(params[:name])
  370. expect(json_response['member_ids']).to eq(organization.member_ids)
  371. expect([customer.login]).to eq(organization.members.pluck(:login))
  372. expect(json_response['updated_by_id']).to eq(admin.id)
  373. expect(json_response['created_by_id']).to eq(admin.id)
  374. params = {
  375. name: 'a update name #3',
  376. }
  377. put "/api/v1/organizations/#{organization.id}?full=true", params: params, as: :json
  378. expect(response).to have_http_status(:ok)
  379. expect(json_response).to be_a_kind_of(Hash)
  380. organization = Organization.find(json_response['id'])
  381. expect(json_response['assets']).to be_truthy
  382. expect(json_response['assets']['Organization']).to be_truthy
  383. expect(json_response['assets']['Organization'][organization.id.to_s]).to be_truthy
  384. expect(json_response['assets']['Organization'][organization.id.to_s]['id']).to eq(organization.id)
  385. expect(json_response['assets']['Organization'][organization.id.to_s]['name']).to eq(params[:name])
  386. expect(json_response['assets']['Organization'][organization.id.to_s]['member_ids']).to eq(organization.member_ids)
  387. expect(json_response['assets']['Organization'][organization.id.to_s]['members']).to be_falsey
  388. end
  389. it 'does organization history' do
  390. organization1 = create(
  391. :organization,
  392. name: 'some org',
  393. )
  394. authenticated_as(agent)
  395. get "/api/v1/organizations/history/#{organization1.id}", params: {}, as: :json
  396. expect(response).to have_http_status(:ok)
  397. expect(json_response).to be_a_kind_of(Hash)
  398. expect(json_response['history'].class).to eq(Array)
  399. expect(json_response['assets'].class).to eq(Hash)
  400. expect(json_response['assets']['Ticket']).to be_nil
  401. expect(json_response['assets']['Organization'][organization1.id.to_s]).not_to be_nil
  402. end
  403. it 'does csv example - customer no access' do
  404. authenticated_as(customer)
  405. get '/api/v1/organizations/import_example', params: {}, as: :json
  406. expect(response).to have_http_status(:forbidden)
  407. expect(json_response['error']).to eq('Not authorized (user)!')
  408. end
  409. it 'does csv example - admin access' do
  410. authenticated_as(admin)
  411. get '/api/v1/organizations/import_example', params: {}, as: :json
  412. expect(response).to have_http_status(:ok)
  413. rows = CSV.parse(@response.body)
  414. header = rows.shift
  415. expect(header[0]).to eq('id')
  416. expect(header[1]).to eq('name')
  417. expect(header[2]).to eq('shared')
  418. expect(header[3]).to eq('domain')
  419. expect(header[4]).to eq('domain_assignment')
  420. expect(header[5]).to eq('active')
  421. expect(header[6]).to eq('note')
  422. expect(header).to include('members')
  423. end
  424. it 'does csv import - admin access' do
  425. UserInfo.current_user_id = 1
  426. customer1 = create(
  427. :customer,
  428. login: 'customer1-members@example.com',
  429. firstname: 'Member',
  430. lastname: 'Customer',
  431. email: 'customer1-members@example.com',
  432. password: 'customerpw',
  433. active: true,
  434. )
  435. customer2 = create(
  436. :customer,
  437. login: 'customer2-members@example.com',
  438. firstname: 'Member',
  439. lastname: 'Customer',
  440. email: 'customer2-members@example.com',
  441. password: 'customerpw',
  442. active: true,
  443. )
  444. UserInfo.current_user_id = nil
  445. # invalid file
  446. authenticated_as(admin)
  447. csv_file = fixture_file_upload('csv_import/organization/simple_col_not_existing.csv', 'text/csv')
  448. post '/api/v1/organizations/import?try=true', params: { file: csv_file, col_sep: ';' }
  449. expect(response).to have_http_status(:ok)
  450. expect(json_response).to be_a_kind_of(Hash)
  451. expect(json_response['try']).to be(true)
  452. expect(json_response['records']).to be_empty
  453. expect(json_response['result']).to eq('failed')
  454. expect(json_response['errors'].count).to eq(2)
  455. expect(json_response['errors'][0]).to eq("Line 1: Unable to create record - unknown attribute 'name2' for Organization.")
  456. expect(json_response['errors'][1]).to eq("Line 2: Unable to create record - unknown attribute 'name2' for Organization.")
  457. # valid file try
  458. csv_file = fixture_file_upload('csv_import/organization/simple.csv', 'text/csv')
  459. post '/api/v1/organizations/import?try=true', params: { file: csv_file, col_sep: ';' }
  460. expect(response).to have_http_status(:ok)
  461. expect(json_response).to be_a_kind_of(Hash)
  462. expect(json_response['try']).to be(true)
  463. expect(json_response['records'].count).to eq(2)
  464. expect(json_response['result']).to eq('success')
  465. expect(Organization.find_by(name: 'organization-member-import1')).to be_nil
  466. expect(Organization.find_by(name: 'organization-member-import2')).to be_nil
  467. # valid file
  468. csv_file = fixture_file_upload('csv_import/organization/simple.csv', 'text/csv')
  469. post '/api/v1/organizations/import', params: { file: csv_file, col_sep: ';' }
  470. expect(response).to have_http_status(:ok)
  471. expect(json_response).to be_a_kind_of(Hash)
  472. expect(json_response['try']).to be(false)
  473. expect(json_response['records'].count).to eq(2)
  474. expect(json_response['result']).to eq('success')
  475. organization1 = Organization.find_by(name: 'organization-member-import1')
  476. expect(organization1).to be_truthy
  477. expect(organization1.name).to eq('organization-member-import1')
  478. expect(organization1.members.count).to eq(1)
  479. expect(organization1.members.first.login).to eq(customer1.login)
  480. expect(organization1.active).to be(true)
  481. organization2 = Organization.find_by(name: 'organization-member-import2')
  482. expect(organization2).to be_truthy
  483. expect(organization2.name).to eq('organization-member-import2')
  484. expect(organization2.members.count).to eq(1)
  485. expect(organization2.members.first.login).to eq(customer2.login)
  486. expect(organization2.active).to be(false)
  487. end
  488. end
  489. end