organization_spec.rb 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'User 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, 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. searchindex_model_reload([Organization])
  27. end
  28. describe 'request handling' do
  29. it 'does organization index with agent' do
  30. authenticated_as(agent)
  31. get '/api/v1/organizations', params: {}, as: :json
  32. expect(response).to have_http_status(:ok)
  33. expect(json_response).to be_a(Array)
  34. expect(json_response[0]['member_ids']).to be_a(Array)
  35. expect(json_response.length >= 3).to be_truthy
  36. get '/api/v1/organizations?limit=40&page=1&per_page=2', params: {}, as: :json
  37. expect(response).to have_http_status(:ok)
  38. expect(json_response.class).to eq(Array)
  39. organizations = Organization.reorder(:id).limit(2)
  40. expect(json_response[0]['id']).to eq(organizations[0].id)
  41. expect(json_response[0]['member_ids']).to eq(organizations[0].member_ids)
  42. expect(json_response[1]['id']).to eq(organizations[1].id)
  43. expect(json_response[1]['member_ids']).to eq(organizations[1].member_ids)
  44. expect(json_response.count).to eq(2)
  45. get '/api/v1/organizations?limit=40&page=2&per_page=2', params: {}, as: :json
  46. expect(response).to have_http_status(:ok)
  47. expect(json_response.class).to eq(Array)
  48. organizations = Organization.reorder(:id).limit(4)
  49. expect(json_response[0]['id']).to eq(organizations[2].id)
  50. expect(json_response[0]['member_ids']).to eq(organizations[2].member_ids)
  51. expect(json_response[1]['id']).to eq(organizations[3].id)
  52. expect(json_response[1]['member_ids']).to eq(organizations[3].member_ids)
  53. expect(json_response.count).to eq(2)
  54. # show/:id
  55. get "/api/v1/organizations/#{organization.id}", params: {}, as: :json
  56. expect(response).to have_http_status(:ok)
  57. expect(json_response).to be_a(Hash)
  58. expect(json_response['member_ids']).to be_a(Array)
  59. expect(json_response['members']).to be_falsey
  60. expect(json_response['name']).to eq('Rest Org')
  61. get "/api/v1/organizations/#{organization2.id}", params: {}, as: :json
  62. expect(response).to have_http_status(:ok)
  63. expect(json_response).to be_a(Hash)
  64. expect(json_response['member_ids']).to be_a(Array)
  65. expect(json_response['members']).to be_falsey
  66. expect(json_response['name']).to eq('Rest Org #2')
  67. # search as agent
  68. perform_enqueued_jobs
  69. get "/api/v1/organizations/search?query=#{CGI.escape('Zammad')}", params: {}, as: :json
  70. expect(response).to have_http_status(:ok)
  71. expect(json_response.class).to eq(Array)
  72. organization = json_response.detect { |object| object['name'] == 'Zammad Foundation' }
  73. expect(organization['name']).to eq('Zammad Foundation')
  74. expect(organization['member_ids']).to be_truthy
  75. expect(organization['members']).to be_falsey
  76. get "/api/v1/organizations/search?query=#{CGI.escape('Zammad')}&expand=true", params: {}, as: :json
  77. expect(response).to have_http_status(:ok)
  78. expect(json_response.class).to eq(Array)
  79. organization = json_response.detect { |object| object['name'] == 'Zammad Foundation' }
  80. expect(organization['name']).to eq('Zammad Foundation')
  81. expect(organization['member_ids']).to be_truthy
  82. expect(organization['members']).to be_truthy
  83. get "/api/v1/organizations/search?query=#{CGI.escape('Zammad')}&label=true", params: {}, as: :json
  84. expect(response).to have_http_status(:ok)
  85. expect(json_response.class).to eq(Array)
  86. organization = json_response.detect { |object| object['label'] == 'Zammad Foundation' }
  87. expect(organization['label']).to eq('Zammad Foundation')
  88. expect(organization['value']).to eq('Zammad Foundation')
  89. expect(organization['member_ids']).to be_falsey
  90. expect(organization['members']).to be_falsey
  91. end
  92. it 'does organization index with customer1' do
  93. authenticated_as(customer)
  94. get '/api/v1/organizations', params: {}, as: :json
  95. expect(response).to have_http_status(:ok)
  96. expect(json_response).to be_a(Array)
  97. expect(json_response.length).to eq(0)
  98. # show/:id
  99. get "/api/v1/organizations/#{organization.id}", params: {}, as: :json
  100. expect(response).to have_http_status(:ok)
  101. expect(json_response).to be_a(Hash)
  102. expect(json_response['name']).to be_nil
  103. get "/api/v1/organizations/#{organization2.id}", params: {}, as: :json
  104. expect(response).to have_http_status(:ok)
  105. expect(json_response).to be_a(Hash)
  106. expect(json_response['name']).to be_nil
  107. # search
  108. perform_enqueued_jobs
  109. get "/api/v1/organizations/search?query=#{CGI.escape('Zammad')}", params: {}, as: :json
  110. expect(response).to have_http_status(:forbidden)
  111. end
  112. it 'does organization index with customer2' do
  113. authenticated_as(customer2)
  114. get '/api/v1/organizations', params: {}, as: :json
  115. expect(response).to have_http_status(:ok)
  116. expect(json_response).to be_a(Array)
  117. expect(json_response.length).to eq(1)
  118. # show/:id
  119. get "/api/v1/organizations/#{organization.id}", params: {}, as: :json
  120. expect(response).to have_http_status(:ok)
  121. expect(json_response).to be_a(Hash)
  122. expect(json_response['name']).to eq('Rest Org')
  123. get "/api/v1/organizations/#{organization2.id}", params: {}, as: :json
  124. expect(response).to have_http_status(:forbidden)
  125. expect(json_response).to be_a(Hash)
  126. expect(json_response['name']).to be_nil
  127. # search
  128. perform_enqueued_jobs
  129. get "/api/v1/organizations/search?query=#{CGI.escape('Zammad')}", params: {}, as: :json
  130. expect(response).to have_http_status(:forbidden)
  131. end
  132. end
  133. end