organization_spec.rb 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. require 'rails_helper'
  2. RSpec.describe 'User 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, groups: Group.all)
  8. end
  9. let!(:customer) do
  10. create(:customer)
  11. end
  12. let!(:organization) do
  13. create(:organization, name: 'Rest Org', note: 'Rest Org A')
  14. end
  15. let!(:organization2) do
  16. create(:organization, name: 'Rest Org #2', note: 'Rest Org B')
  17. end
  18. let!(:organization3) do
  19. create(:organization, name: 'Rest Org #3', note: 'Rest Org C')
  20. end
  21. let!(:customer2) do
  22. create(:customer, organization: organization)
  23. end
  24. before do
  25. configure_elasticsearch do
  26. travel 1.minute
  27. rebuild_searchindex
  28. # execute background jobs
  29. Scheduler.worker(true)
  30. sleep 6
  31. end
  32. end
  33. describe 'request handling' do
  34. it 'does organization index with agent' do
  35. authenticated_as(agent)
  36. get '/api/v1/organizations', params: {}, as: :json
  37. expect(response).to have_http_status(:ok)
  38. expect(json_response).to be_a_kind_of(Array)
  39. expect(json_response[0]['member_ids']).to be_a_kind_of(Array)
  40. expect(json_response.length >= 3).to be_truthy
  41. get '/api/v1/organizations?limit=40&page=1&per_page=2', params: {}, as: :json
  42. expect(response).to have_http_status(:ok)
  43. expect(json_response.class).to eq(Array)
  44. organizations = Organization.order(:id).limit(2)
  45. expect(json_response[0]['id']).to eq(organizations[0].id)
  46. expect(json_response[0]['member_ids']).to eq(organizations[0].member_ids)
  47. expect(json_response[1]['id']).to eq(organizations[1].id)
  48. expect(json_response[1]['member_ids']).to eq(organizations[1].member_ids)
  49. expect(json_response.count).to eq(2)
  50. get '/api/v1/organizations?limit=40&page=2&per_page=2', params: {}, as: :json
  51. expect(response).to have_http_status(:ok)
  52. expect(json_response.class).to eq(Array)
  53. organizations = Organization.order(:id).limit(4)
  54. expect(json_response[0]['id']).to eq(organizations[2].id)
  55. expect(json_response[0]['member_ids']).to eq(organizations[2].member_ids)
  56. expect(json_response[1]['id']).to eq(organizations[3].id)
  57. expect(json_response[1]['member_ids']).to eq(organizations[3].member_ids)
  58. expect(json_response.count).to eq(2)
  59. # show/:id
  60. get "/api/v1/organizations/#{organization.id}", params: {}, as: :json
  61. expect(response).to have_http_status(:ok)
  62. expect(json_response).to be_a_kind_of(Hash)
  63. expect(json_response['member_ids']).to be_a_kind_of(Array)
  64. expect(json_response['members']).to be_falsey
  65. expect('Rest Org').to eq(json_response['name'])
  66. get "/api/v1/organizations/#{organization2.id}", params: {}, as: :json
  67. expect(response).to have_http_status(:ok)
  68. expect(json_response).to be_a_kind_of(Hash)
  69. expect(json_response['member_ids']).to be_a_kind_of(Array)
  70. expect(json_response['members']).to be_falsey
  71. expect('Rest Org #2').to eq(json_response['name'])
  72. # search as agent
  73. Scheduler.worker(true)
  74. get "/api/v1/organizations/search?query=#{CGI.escape('Zammad')}", params: {}, as: :json
  75. expect(response).to have_http_status(:ok)
  76. expect(json_response.class).to eq(Array)
  77. organization = json_response.detect { |object| object['name'] == 'Zammad Foundation' }
  78. expect(organization['name']).to eq('Zammad Foundation')
  79. expect(organization['member_ids']).to be_truthy
  80. expect(organization['members']).to be_falsey
  81. get "/api/v1/organizations/search?query=#{CGI.escape('Zammad')}&expand=true", params: {}, as: :json
  82. expect(response).to have_http_status(:ok)
  83. expect(json_response.class).to eq(Array)
  84. organization = json_response.detect { |object| object['name'] == 'Zammad Foundation' }
  85. expect(organization['name']).to eq('Zammad Foundation')
  86. expect(organization['member_ids']).to be_truthy
  87. expect(organization['members']).to be_truthy
  88. get "/api/v1/organizations/search?query=#{CGI.escape('Zammad')}&label=true", params: {}, as: :json
  89. expect(response).to have_http_status(:ok)
  90. expect(json_response.class).to eq(Array)
  91. organization = json_response.detect { |object| object['label'] == 'Zammad Foundation' }
  92. expect(organization['label']).to eq('Zammad Foundation')
  93. expect(organization['value']).to eq('Zammad Foundation')
  94. expect(organization['member_ids']).to be_falsey
  95. expect(organization['members']).to be_falsey
  96. end
  97. it 'does organization index with customer1' do
  98. authenticated_as(customer)
  99. get '/api/v1/organizations', params: {}, as: :json
  100. expect(response).to have_http_status(:ok)
  101. expect(json_response).to be_a_kind_of(Array)
  102. expect(json_response.length).to eq(0)
  103. # show/:id
  104. get "/api/v1/organizations/#{organization.id}", params: {}, as: :json
  105. expect(response).to have_http_status(:ok)
  106. expect(json_response).to be_a_kind_of(Hash)
  107. expect(json_response['name']).to be_nil
  108. get "/api/v1/organizations/#{organization2.id}", params: {}, as: :json
  109. expect(response).to have_http_status(:ok)
  110. expect(json_response).to be_a_kind_of(Hash)
  111. expect(json_response['name']).to be_nil
  112. # search
  113. Scheduler.worker(true)
  114. get "/api/v1/organizations/search?query=#{CGI.escape('Zammad')}", params: {}, as: :json
  115. expect(response).to have_http_status(:forbidden)
  116. end
  117. it 'does organization index with customer2' do
  118. authenticated_as(customer2)
  119. get '/api/v1/organizations', params: {}, as: :json
  120. expect(response).to have_http_status(:ok)
  121. expect(json_response).to be_a_kind_of(Array)
  122. expect(json_response.length).to eq(1)
  123. # show/:id
  124. get "/api/v1/organizations/#{organization.id}", params: {}, as: :json
  125. expect(response).to have_http_status(:ok)
  126. expect(json_response).to be_a_kind_of(Hash)
  127. expect('Rest Org').to eq(json_response['name'])
  128. get "/api/v1/organizations/#{organization2.id}", params: {}, as: :json
  129. expect(response).to have_http_status(:forbidden)
  130. expect(json_response).to be_a_kind_of(Hash)
  131. expect(json_response['name']).to be_nil
  132. # search
  133. Scheduler.worker(true)
  134. get "/api/v1/organizations/search?query=#{CGI.escape('Zammad')}", params: {}, as: :json
  135. expect(response).to have_http_status(:forbidden)
  136. end
  137. end
  138. end