canonical_email_blocks_spec.rb 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. context 'when there is no canonical email block' do
  18. it 'returns an empty list' do
  19. subject
  20. expect(response)
  21. .to have_http_status(200)
  22. expect(response.content_type)
  23. .to start_with('application/json')
  24. expect(response.parsed_body)
  25. .to be_empty
  26. end
  27. end
  28. context 'when there are canonical email blocks' do
  29. let!(:canonical_email_blocks) { Fabricate.times(5, :canonical_email_block) }
  30. let(:expected_email_hashes) { canonical_email_blocks.pluck(:canonical_email_hash) }
  31. it 'returns the correct canonical email hashes' do
  32. subject
  33. expect(response)
  34. .to have_http_status(200)
  35. expect(response.content_type)
  36. .to start_with('application/json')
  37. expect(response.parsed_body.pluck(:canonical_email_hash))
  38. .to match_array(expected_email_hashes)
  39. end
  40. context 'with limit param' do
  41. let(:params) { { limit: 2 } }
  42. it 'returns only the requested number of canonical email blocks' do
  43. subject
  44. expect(response.parsed_body.size).to eq(params[:limit])
  45. end
  46. end
  47. context 'with since_id param' do
  48. let(:params) { { since_id: canonical_email_blocks[1].id } }
  49. it 'returns only the canonical email blocks after since_id' do
  50. subject
  51. canonical_email_blocks_ids = canonical_email_blocks.pluck(:id).map(&:to_s)
  52. expect(response.parsed_body.pluck(:id)).to match_array(canonical_email_blocks_ids[2..])
  53. end
  54. end
  55. context 'with max_id param' do
  56. let(:params) { { max_id: canonical_email_blocks[3].id } }
  57. it 'returns only the canonical email blocks before max_id' do
  58. subject
  59. canonical_email_blocks_ids = canonical_email_blocks.pluck(:id).map(&:to_s)
  60. expect(response.parsed_body.pluck(:id)).to match_array(canonical_email_blocks_ids[..2])
  61. end
  62. end
  63. end
  64. end
  65. describe 'GET /api/v1/admin/canonical_email_blocks/:id' do
  66. subject do
  67. get "/api/v1/admin/canonical_email_blocks/#{canonical_email_block.id}", headers: headers
  68. end
  69. let!(:canonical_email_block) { Fabricate(:canonical_email_block) }
  70. it_behaves_like 'forbidden for wrong scope', 'read:statuses'
  71. it_behaves_like 'forbidden for wrong role', ''
  72. it_behaves_like 'forbidden for wrong role', 'Moderator'
  73. context 'when the requested canonical email block exists' do
  74. it 'returns the requested canonical email block data correctly', :aggregate_failures do
  75. subject
  76. expect(response).to have_http_status(200)
  77. expect(response.content_type)
  78. .to start_with('application/json')
  79. expect(response.parsed_body)
  80. .to include(
  81. id: eq(canonical_email_block.id.to_s),
  82. canonical_email_hash: eq(canonical_email_block.canonical_email_hash)
  83. )
  84. end
  85. end
  86. context 'when the requested canonical block does not exist' do
  87. it 'returns http not found' do
  88. get '/api/v1/admin/canonical_email_blocks/-1', headers: headers
  89. expect(response).to have_http_status(404)
  90. expect(response.content_type)
  91. .to start_with('application/json')
  92. end
  93. end
  94. end
  95. describe 'POST /api/v1/admin/canonical_email_blocks/test' do
  96. subject do
  97. post '/api/v1/admin/canonical_email_blocks/test', headers: headers, params: params
  98. end
  99. let(:params) { { email: 'email@example.com' } }
  100. it_behaves_like 'forbidden for wrong scope', 'read:statuses'
  101. it_behaves_like 'forbidden for wrong role', ''
  102. it_behaves_like 'forbidden for wrong role', 'Moderator'
  103. context 'when the required email param is not provided' do
  104. let(:params) { {} }
  105. it 'returns http bad request' do
  106. subject
  107. expect(response).to have_http_status(400)
  108. expect(response.content_type)
  109. .to start_with('application/json')
  110. end
  111. end
  112. context 'when the required email param is provided' do
  113. context 'when there is a matching canonical email block' do
  114. let!(:canonical_email_block) { CanonicalEmailBlock.create(params) }
  115. it 'returns the expected canonical email hash', :aggregate_failures do
  116. subject
  117. expect(response).to have_http_status(200)
  118. expect(response.content_type)
  119. .to start_with('application/json')
  120. expect(response.parsed_body.first[:canonical_email_hash]).to eq(canonical_email_block.canonical_email_hash)
  121. end
  122. end
  123. context 'when there is no matching canonical email block' do
  124. it 'returns an empty list', :aggregate_failures do
  125. subject
  126. expect(response).to have_http_status(200)
  127. expect(response.content_type)
  128. .to start_with('application/json')
  129. expect(response.parsed_body).to be_empty
  130. end
  131. end
  132. end
  133. end
  134. describe 'POST /api/v1/admin/canonical_email_blocks' do
  135. subject do
  136. post '/api/v1/admin/canonical_email_blocks', headers: headers, params: params
  137. end
  138. let(:params) { { email: 'example@email.com' } }
  139. let(:canonical_email_block) { CanonicalEmailBlock.new(email: params[:email]) }
  140. it_behaves_like 'forbidden for wrong scope', 'read:statuses'
  141. it_behaves_like 'forbidden for wrong role', ''
  142. it_behaves_like 'forbidden for wrong role', 'Moderator'
  143. it 'returns the canonical_email_hash correctly', :aggregate_failures do
  144. subject
  145. expect(response).to have_http_status(200)
  146. expect(response.content_type)
  147. .to start_with('application/json')
  148. expect(response.parsed_body[:canonical_email_hash]).to eq(canonical_email_block.canonical_email_hash)
  149. end
  150. context 'when the required email param is not provided' do
  151. let(:params) { {} }
  152. it 'returns http unprocessable entity' do
  153. subject
  154. expect(response).to have_http_status(422)
  155. expect(response.content_type)
  156. .to start_with('application/json')
  157. end
  158. end
  159. context 'when the canonical_email_hash param is provided instead of email' do
  160. let(:params) { { canonical_email_hash: 'dd501ce4e6b08698f19df96f2f15737e48a75660b1fa79b6ff58ea25ee4851a4' } }
  161. it 'returns the correct canonical_email_hash', :aggregate_failures do
  162. subject
  163. expect(response).to have_http_status(200)
  164. expect(response.content_type)
  165. .to start_with('application/json')
  166. expect(response.parsed_body[:canonical_email_hash]).to eq(params[:canonical_email_hash])
  167. end
  168. end
  169. context 'when both email and canonical_email_hash params are provided' do
  170. let(:params) { { email: 'example@email.com', canonical_email_hash: 'dd501ce4e6b08698f19df96f2f15737e48a75660b1fa79b6ff58ea25ee4851a4' } }
  171. it 'ignores the canonical_email_hash param', :aggregate_failures do
  172. subject
  173. expect(response).to have_http_status(200)
  174. expect(response.content_type)
  175. .to start_with('application/json')
  176. expect(response.parsed_body[:canonical_email_hash]).to eq(canonical_email_block.canonical_email_hash)
  177. end
  178. end
  179. context 'when the given canonical email was already blocked' do
  180. before do
  181. canonical_email_block.save
  182. end
  183. it 'returns http unprocessable entity' do
  184. subject
  185. expect(response).to have_http_status(422)
  186. expect(response.content_type)
  187. .to start_with('application/json')
  188. end
  189. end
  190. end
  191. describe 'DELETE /api/v1/admin/canonical_email_blocks/:id' do
  192. subject do
  193. delete "/api/v1/admin/canonical_email_blocks/#{canonical_email_block.id}", headers: headers
  194. end
  195. let!(:canonical_email_block) { Fabricate(:canonical_email_block) }
  196. it_behaves_like 'forbidden for wrong scope', 'read:statuses'
  197. it_behaves_like 'forbidden for wrong role', ''
  198. it_behaves_like 'forbidden for wrong role', 'Moderator'
  199. it 'deletes the canonical email block', :aggregate_failures do
  200. subject
  201. expect(response).to have_http_status(200)
  202. expect(response.content_type)
  203. .to start_with('application/json')
  204. expect(CanonicalEmailBlock.find_by(id: canonical_email_block.id)).to be_nil
  205. end
  206. context 'when the canonical email block is not found' do
  207. it 'returns http not found' do
  208. delete '/api/v1/admin/canonical_email_blocks/0', headers: headers
  209. expect(response).to have_http_status(404)
  210. expect(response.content_type)
  211. .to start_with('application/json')
  212. end
  213. end
  214. end
  215. end