organization_spec.rb 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'Organization', performs_jobs: true, searchindex: true, type: :request 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. searchindex_model_reload([Organization])
  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(Array)
  50. expect(json_response[0]['member_ids']).to be_a(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(Array)
  55. organizations = Organization.reorder(: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(Array)
  64. organizations = Organization.reorder(: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(Hash)
  74. expect(json_response['member_ids']).to be_a(Array)
  75. expect(json_response['members']).to be_falsey
  76. expect(json_response['name']).to eq('Rest Org #1')
  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(Hash)
  80. expect(json_response['member_ids']).to be_a(Array)
  81. expect(json_response['members']).to be_falsey
  82. expect(json_response['name']).to eq('Rest Org #2')
  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(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(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(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(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(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(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(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(Hash)
  140. expect(json_response['name']).to eq('Rest Org #1')
  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(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(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(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(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(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(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(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(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(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(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(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(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(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(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(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(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(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(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(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(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(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(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(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).to start_with('id', 'name', 'shared', 'domain', 'domain_assignment', 'active', 'vip', 'note')
  416. expect(header).to include('members')
  417. end
  418. it 'does csv import - admin access' do
  419. UserInfo.current_user_id = 1
  420. customer1 = create(
  421. :customer,
  422. login: 'customer1-members@example.com',
  423. firstname: 'Member',
  424. lastname: 'Customer',
  425. email: 'customer1-members@example.com',
  426. password: 'customerpw',
  427. active: true,
  428. )
  429. customer2 = create(
  430. :customer,
  431. login: 'customer2-members@example.com',
  432. firstname: 'Member',
  433. lastname: 'Customer',
  434. email: 'customer2-members@example.com',
  435. password: 'customerpw',
  436. active: true,
  437. )
  438. UserInfo.current_user_id = nil
  439. # invalid file
  440. authenticated_as(admin)
  441. csv_file = fixture_file_upload('csv_import/organization/simple_col_not_existing.csv', 'text/csv')
  442. post '/api/v1/organizations/import?try=true', params: { file: csv_file, col_sep: ';' }
  443. expect(response).to have_http_status(:ok)
  444. expect(json_response).to be_a(Hash)
  445. expect(json_response['try']).to be(true)
  446. expect(json_response['records']).to be_empty
  447. expect(json_response['result']).to eq('failed')
  448. expect(json_response['errors'].count).to eq(2)
  449. expect(json_response['errors'][0]).to eq("Line 1: Unable to create record - unknown attribute 'name2' for Organization.")
  450. expect(json_response['errors'][1]).to eq("Line 2: Unable to create record - unknown attribute 'name2' for Organization.")
  451. # valid file try
  452. csv_file = fixture_file_upload('csv_import/organization/simple.csv', 'text/csv')
  453. post '/api/v1/organizations/import?try=true', params: { file: csv_file, col_sep: ';' }
  454. expect(response).to have_http_status(:ok)
  455. expect(json_response).to be_a(Hash)
  456. expect(json_response['try']).to be(true)
  457. expect(json_response['records'].count).to eq(2)
  458. expect(json_response['result']).to eq('success')
  459. expect(Organization.find_by(name: 'organization-member-import1')).to be_nil
  460. expect(Organization.find_by(name: 'organization-member-import2')).to be_nil
  461. # valid file
  462. csv_file = fixture_file_upload('csv_import/organization/simple.csv', 'text/csv')
  463. post '/api/v1/organizations/import', params: { file: csv_file, col_sep: ';' }
  464. expect(response).to have_http_status(:ok)
  465. expect(json_response).to be_a(Hash)
  466. expect(json_response['try']).to be(false)
  467. expect(json_response['records'].count).to eq(2)
  468. expect(json_response['result']).to eq('success')
  469. organization1 = Organization.find_by(name: 'organization-member-import1')
  470. expect(organization1).to be_truthy
  471. expect(organization1.name).to eq('organization-member-import1')
  472. expect(organization1.members.count).to eq(1)
  473. expect(organization1.members.first.login).to eq(customer1.login)
  474. expect(organization1.active).to be(true)
  475. organization2 = Organization.find_by(name: 'organization-member-import2')
  476. expect(organization2).to be_truthy
  477. expect(organization2.name).to eq('organization-member-import2')
  478. expect(organization2.members.count).to eq(1)
  479. expect(organization2.members.first.login).to eq(customer2.login)
  480. expect(organization2.active).to be(false)
  481. end
  482. end
  483. end