organization_spec.rb 25 KB

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