canonical_email_blocks_spec.rb 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe 'Canonical Email Blocks' do
  4. let(:role) { UserRole.find_by(name: 'Admin') }
  5. let(:user) { Fabricate(:user, role: role) }
  6. let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
  7. let(:scopes) { 'admin:read:canonical_email_blocks admin:write:canonical_email_blocks' }
  8. let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
  9. describe 'GET /api/v1/admin/canonical_email_blocks' do
  10. subject do
  11. get '/api/v1/admin/canonical_email_blocks', headers: headers, params: params
  12. end
  13. let(:params) { {} }
  14. it_behaves_like 'forbidden for wrong scope', 'read:statuses'
  15. it_behaves_like 'forbidden for wrong role', ''
  16. it_behaves_like 'forbidden for wrong role', 'Moderator'
  17. it 'returns http success' do
  18. subject
  19. expect(response).to have_http_status(200)
  20. end
  21. context 'when there is no canonical email block' do
  22. it 'returns an empty list' do
  23. subject
  24. expect(body_as_json).to be_empty
  25. end
  26. end
  27. context 'when there are canonical email blocks' do
  28. let!(:canonical_email_blocks) { Fabricate.times(5, :canonical_email_block) }
  29. let(:expected_email_hashes) { canonical_email_blocks.pluck(:canonical_email_hash) }
  30. it 'returns the correct canonical email hashes' do
  31. subject
  32. expect(body_as_json.pluck(:canonical_email_hash)).to match_array(expected_email_hashes)
  33. end
  34. context 'with limit param' do
  35. let(:params) { { limit: 2 } }
  36. it 'returns only the requested number of canonical email blocks' do
  37. subject
  38. expect(body_as_json.size).to eq(params[:limit])
  39. end
  40. end
  41. context 'with since_id param' do
  42. let(:params) { { since_id: canonical_email_blocks[1].id } }
  43. it 'returns only the canonical email blocks after since_id' do
  44. subject
  45. canonical_email_blocks_ids = canonical_email_blocks.pluck(:id).map(&:to_s)
  46. expect(body_as_json.pluck(:id)).to match_array(canonical_email_blocks_ids[2..])
  47. end
  48. end
  49. context 'with max_id param' do
  50. let(:params) { { max_id: canonical_email_blocks[3].id } }
  51. it 'returns only the canonical email blocks before max_id' do
  52. subject
  53. canonical_email_blocks_ids = canonical_email_blocks.pluck(:id).map(&:to_s)
  54. expect(body_as_json.pluck(:id)).to match_array(canonical_email_blocks_ids[..2])
  55. end
  56. end
  57. end
  58. end
  59. describe 'GET /api/v1/admin/canonical_email_blocks/:id' do
  60. subject do
  61. get "/api/v1/admin/canonical_email_blocks/#{canonical_email_block.id}", headers: headers
  62. end
  63. let!(:canonical_email_block) { Fabricate(:canonical_email_block) }
  64. it_behaves_like 'forbidden for wrong scope', 'read:statuses'
  65. it_behaves_like 'forbidden for wrong role', ''
  66. it_behaves_like 'forbidden for wrong role', 'Moderator'
  67. context 'when the requested canonical email block exists' do
  68. it 'returns the requested canonical email block data correctly', :aggregate_failures do
  69. subject
  70. expect(response).to have_http_status(200)
  71. json = body_as_json
  72. expect(json[:id]).to eq(canonical_email_block.id.to_s)
  73. expect(json[:canonical_email_hash]).to eq(canonical_email_block.canonical_email_hash)
  74. end
  75. end
  76. context 'when the requested canonical block does not exist' do
  77. it 'returns http not found' do
  78. get '/api/v1/admin/canonical_email_blocks/-1', headers: headers
  79. expect(response).to have_http_status(404)
  80. end
  81. end
  82. end
  83. describe 'POST /api/v1/admin/canonical_email_blocks/test' do
  84. subject do
  85. post '/api/v1/admin/canonical_email_blocks/test', headers: headers, params: params
  86. end
  87. let(:params) { { email: 'email@example.com' } }
  88. it_behaves_like 'forbidden for wrong scope', 'read:statuses'
  89. it_behaves_like 'forbidden for wrong role', ''
  90. it_behaves_like 'forbidden for wrong role', 'Moderator'
  91. context 'when the required email param is not provided' do
  92. let(:params) { {} }
  93. it 'returns http bad request' do
  94. subject
  95. expect(response).to have_http_status(400)
  96. end
  97. end
  98. context 'when the required email param is provided' do
  99. context 'when there is a matching canonical email block' do
  100. let!(:canonical_email_block) { CanonicalEmailBlock.create(params) }
  101. it 'returns the expected canonical email hash', :aggregate_failures do
  102. subject
  103. expect(response).to have_http_status(200)
  104. expect(body_as_json[0][:canonical_email_hash]).to eq(canonical_email_block.canonical_email_hash)
  105. end
  106. end
  107. context 'when there is no matching canonical email block' do
  108. it 'returns an empty list', :aggregate_failures do
  109. subject
  110. expect(response).to have_http_status(200)
  111. expect(body_as_json).to be_empty
  112. end
  113. end
  114. end
  115. end
  116. describe 'POST /api/v1/admin/canonical_email_blocks' do
  117. subject do
  118. post '/api/v1/admin/canonical_email_blocks', headers: headers, params: params
  119. end
  120. let(:params) { { email: 'example@email.com' } }
  121. let(:canonical_email_block) { CanonicalEmailBlock.new(email: params[:email]) }
  122. it_behaves_like 'forbidden for wrong scope', 'read:statuses'
  123. it_behaves_like 'forbidden for wrong role', ''
  124. it_behaves_like 'forbidden for wrong role', 'Moderator'
  125. it 'returns the canonical_email_hash correctly', :aggregate_failures do
  126. subject
  127. expect(response).to have_http_status(200)
  128. expect(body_as_json[:canonical_email_hash]).to eq(canonical_email_block.canonical_email_hash)
  129. end
  130. context 'when the required email param is not provided' do
  131. let(:params) { {} }
  132. it 'returns http unprocessable entity' do
  133. subject
  134. expect(response).to have_http_status(422)
  135. end
  136. end
  137. context 'when the canonical_email_hash param is provided instead of email' do
  138. let(:params) { { canonical_email_hash: 'dd501ce4e6b08698f19df96f2f15737e48a75660b1fa79b6ff58ea25ee4851a4' } }
  139. it 'returns the correct canonical_email_hash', :aggregate_failures do
  140. subject
  141. expect(response).to have_http_status(200)
  142. expect(body_as_json[:canonical_email_hash]).to eq(params[:canonical_email_hash])
  143. end
  144. end
  145. context 'when both email and canonical_email_hash params are provided' do
  146. let(:params) { { email: 'example@email.com', canonical_email_hash: 'dd501ce4e6b08698f19df96f2f15737e48a75660b1fa79b6ff58ea25ee4851a4' } }
  147. it 'ignores the canonical_email_hash param', :aggregate_failures do
  148. subject
  149. expect(response).to have_http_status(200)
  150. expect(body_as_json[:canonical_email_hash]).to eq(canonical_email_block.canonical_email_hash)
  151. end
  152. end
  153. context 'when the given canonical email was already blocked' do
  154. before do
  155. canonical_email_block.save
  156. end
  157. it 'returns http unprocessable entity' do
  158. subject
  159. expect(response).to have_http_status(422)
  160. end
  161. end
  162. end
  163. describe 'DELETE /api/v1/admin/canonical_email_blocks/:id' do
  164. subject do
  165. delete "/api/v1/admin/canonical_email_blocks/#{canonical_email_block.id}", headers: headers
  166. end
  167. let!(:canonical_email_block) { Fabricate(:canonical_email_block) }
  168. it_behaves_like 'forbidden for wrong scope', 'read:statuses'
  169. it_behaves_like 'forbidden for wrong role', ''
  170. it_behaves_like 'forbidden for wrong role', 'Moderator'
  171. it 'deletes the canonical email block', :aggregate_failures do
  172. subject
  173. expect(response).to have_http_status(200)
  174. expect(CanonicalEmailBlock.find_by(id: canonical_email_block.id)).to be_nil
  175. end
  176. context 'when the canonical email block is not found' do
  177. it 'returns http not found' do
  178. delete '/api/v1/admin/canonical_email_blocks/0', headers: headers
  179. expect(response).to have_http_status(404)
  180. end
  181. end
  182. end
  183. end