domain_blocks_spec.rb 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe 'Domain Blocks' do
  4. let(:role) { UserRole.find_by(name: 'Admin') }
  5. let(:user) { Fabricate(:user, role: role) }
  6. let(:scopes) { 'admin:read:domain_blocks admin:write:domain_blocks' }
  7. let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
  8. let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
  9. describe 'GET /api/v1/admin/domain_blocks' do
  10. subject do
  11. get '/api/v1/admin/domain_blocks', headers: headers, params: params
  12. end
  13. let(:params) { {} }
  14. it_behaves_like 'forbidden for wrong scope', 'write: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 are no domain blocks' 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 domain blocks' do
  28. let!(:domain_blocks) do
  29. [
  30. Fabricate(:domain_block, severity: :silence, reject_media: true),
  31. Fabricate(:domain_block, severity: :suspend, obfuscate: true),
  32. Fabricate(:domain_block, severity: :noop, reject_reports: true),
  33. Fabricate(:domain_block, public_comment: 'Spam'),
  34. Fabricate(:domain_block, private_comment: 'Spam'),
  35. ]
  36. end
  37. let(:expected_responde) do
  38. domain_blocks.map do |domain_block|
  39. {
  40. id: domain_block.id.to_s,
  41. domain: domain_block.domain,
  42. digest: domain_block.domain_digest,
  43. created_at: domain_block.created_at.strftime('%Y-%m-%dT%H:%M:%S.%LZ'),
  44. severity: domain_block.severity.to_s,
  45. reject_media: domain_block.reject_media,
  46. reject_reports: domain_block.reject_reports,
  47. private_comment: domain_block.private_comment,
  48. public_comment: domain_block.public_comment,
  49. obfuscate: domain_block.obfuscate,
  50. }
  51. end
  52. end
  53. it 'returns the expected domain blocks' do
  54. subject
  55. expect(body_as_json).to match_array(expected_responde)
  56. end
  57. context 'with limit param' do
  58. let(:params) { { limit: 2 } }
  59. it 'returns only the requested number of domain blocks' do
  60. subject
  61. expect(body_as_json.size).to eq(params[:limit])
  62. end
  63. end
  64. end
  65. end
  66. describe 'GET /api/v1/admin/domain_blocks/:id' do
  67. subject do
  68. get "/api/v1/admin/domain_blocks/#{domain_block.id}", headers: headers
  69. end
  70. let!(:domain_block) { Fabricate(:domain_block) }
  71. it_behaves_like 'forbidden for wrong scope', 'write:statuses'
  72. it_behaves_like 'forbidden for wrong role', ''
  73. it_behaves_like 'forbidden for wrong role', 'Moderator'
  74. it 'returns the expected domain block content', :aggregate_failures do
  75. subject
  76. expect(response).to have_http_status(200)
  77. expect(body_as_json).to eq(
  78. {
  79. id: domain_block.id.to_s,
  80. domain: domain_block.domain,
  81. digest: domain_block.domain_digest,
  82. created_at: domain_block.created_at.strftime('%Y-%m-%dT%H:%M:%S.%LZ'),
  83. severity: domain_block.severity.to_s,
  84. reject_media: domain_block.reject_media,
  85. reject_reports: domain_block.reject_reports,
  86. private_comment: domain_block.private_comment,
  87. public_comment: domain_block.public_comment,
  88. obfuscate: domain_block.obfuscate,
  89. }
  90. )
  91. end
  92. context 'when the requested domain block does not exist' do
  93. it 'returns http not found' do
  94. get '/api/v1/admin/domain_blocks/-1', headers: headers
  95. expect(response).to have_http_status(404)
  96. end
  97. end
  98. end
  99. describe 'POST /api/v1/admin/domain_blocks' do
  100. subject do
  101. post '/api/v1/admin/domain_blocks', headers: headers, params: params
  102. end
  103. let(:params) { { domain: 'foo.bar.com', severity: :silence } }
  104. it_behaves_like 'forbidden for wrong scope', 'write:statuses'
  105. it_behaves_like 'forbidden for wrong role', ''
  106. it_behaves_like 'forbidden for wrong role', 'Moderator'
  107. it 'creates a domain block with the expected domain name and severity', :aggregate_failures do
  108. subject
  109. body = body_as_json
  110. expect(response).to have_http_status(200)
  111. expect(body).to match a_hash_including(
  112. {
  113. domain: 'foo.bar.com',
  114. severity: 'silence',
  115. }
  116. )
  117. expect(DomainBlock.find_by(domain: 'foo.bar.com')).to be_present
  118. end
  119. context 'when a looser domain block already exists on a higher level domain' do
  120. let(:params) { { domain: 'foo.bar.com', severity: :suspend } }
  121. before do
  122. Fabricate(:domain_block, domain: 'bar.com', severity: :silence)
  123. end
  124. it 'creates a domain block with the expected domain name and severity', :aggregate_failures do
  125. subject
  126. body = body_as_json
  127. expect(response).to have_http_status(200)
  128. expect(body).to match a_hash_including(
  129. {
  130. domain: 'foo.bar.com',
  131. severity: 'suspend',
  132. }
  133. )
  134. expect(DomainBlock.find_by(domain: 'foo.bar.com')).to be_present
  135. end
  136. end
  137. context 'when a domain block already exists on the same domain' do
  138. before do
  139. Fabricate(:domain_block, domain: 'foo.bar.com', severity: :silence)
  140. end
  141. it 'returns existing domain block in error', :aggregate_failures do
  142. subject
  143. expect(response).to have_http_status(422)
  144. expect(body_as_json[:existing_domain_block][:domain]).to eq('foo.bar.com')
  145. end
  146. end
  147. context 'when a stricter domain block already exists on a higher level domain' do
  148. before do
  149. Fabricate(:domain_block, domain: 'bar.com', severity: :suspend)
  150. end
  151. it 'returns existing domain block in error', :aggregate_failures do
  152. subject
  153. expect(response).to have_http_status(422)
  154. expect(body_as_json[:existing_domain_block][:domain]).to eq('bar.com')
  155. end
  156. end
  157. context 'when given domain name is invalid' do
  158. let(:params) { { domain: 'foo bar', severity: :silence } }
  159. it 'returns http unprocessable entity' do
  160. subject
  161. expect(response).to have_http_status(422)
  162. end
  163. end
  164. end
  165. describe 'PUT /api/v1/admin/domain_blocks/:id' do
  166. subject do
  167. put "/api/v1/admin/domain_blocks/#{domain_block.id}", headers: headers, params: params
  168. end
  169. let!(:domain_block) { Fabricate(:domain_block, domain: 'example.com', severity: :silence) }
  170. let(:params) { { domain: 'example.com', severity: 'suspend' } }
  171. it_behaves_like 'forbidden for wrong scope', 'write:statuses'
  172. it_behaves_like 'forbidden for wrong role', ''
  173. it_behaves_like 'forbidden for wrong role', 'Moderator'
  174. it 'returns the updated domain block', :aggregate_failures do
  175. subject
  176. expect(response).to have_http_status(200)
  177. expect(body_as_json).to match a_hash_including(
  178. {
  179. id: domain_block.id.to_s,
  180. domain: domain_block.domain,
  181. digest: domain_block.domain_digest,
  182. severity: 'suspend',
  183. }
  184. )
  185. end
  186. it 'updates the block severity' do
  187. expect { subject }.to change { domain_block.reload.severity }.from('silence').to('suspend')
  188. end
  189. context 'when domain block does not exist' do
  190. it 'returns http not found' do
  191. put '/api/v1/admin/domain_blocks/-1', headers: headers
  192. expect(response).to have_http_status(404)
  193. end
  194. end
  195. end
  196. describe 'DELETE /api/v1/admin/domain_blocks/:id' do
  197. subject do
  198. delete "/api/v1/admin/domain_blocks/#{domain_block.id}", headers: headers
  199. end
  200. let!(:domain_block) { Fabricate(:domain_block) }
  201. it_behaves_like 'forbidden for wrong scope', 'write:statuses'
  202. it_behaves_like 'forbidden for wrong role', ''
  203. it_behaves_like 'forbidden for wrong role', 'Moderator'
  204. it 'deletes the domain block', :aggregate_failures do
  205. subject
  206. expect(response).to have_http_status(200)
  207. expect(DomainBlock.find_by(id: domain_block.id)).to be_nil
  208. end
  209. context 'when domain block does not exist' do
  210. it 'returns http not found' do
  211. delete '/api/v1/admin/domain_blocks/-1', headers: headers
  212. expect(response).to have_http_status(404)
  213. end
  214. end
  215. end
  216. end