organization_spec.rb 25 KB

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