organization_spec.rb 6.4 KB

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