domain_blocks_controller_spec.rb 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe Admin::DomainBlocksController do
  4. render_views
  5. before do
  6. sign_in Fabricate(:user, role: UserRole.find_by(name: 'Admin')), scope: :user
  7. end
  8. describe 'GET #new' do
  9. it 'assigns a new domain block' do
  10. get :new
  11. expect(assigns(:domain_block)).to be_instance_of(DomainBlock)
  12. expect(response).to have_http_status(200)
  13. end
  14. end
  15. describe 'POST #batch' do
  16. it 'blocks the domains when succeeded to save' do
  17. allow(DomainBlockWorker).to receive(:perform_async).and_return(true)
  18. post :batch, params: {
  19. save: '',
  20. form_domain_block_batch: {
  21. domain_blocks_attributes: {
  22. '0' => { enabled: '1', domain: 'example.com', severity: 'silence' },
  23. '1' => { enabled: '0', domain: 'mastodon.social', severity: 'suspend' },
  24. '2' => { enabled: '1', domain: 'mastodon.online', severity: 'suspend' },
  25. },
  26. },
  27. }
  28. expect(DomainBlockWorker).to have_received(:perform_async).exactly(2).times
  29. expect(flash[:notice]).to eq I18n.t('admin.domain_blocks.created_msg')
  30. expect(response).to redirect_to(admin_instances_path(limited: '1'))
  31. end
  32. end
  33. describe 'POST #create' do
  34. before do
  35. allow(DomainBlockWorker).to receive(:perform_async).and_return(true)
  36. end
  37. context 'with "silence" severity and no conflict' do
  38. before do
  39. post :create, params: { domain_block: { domain: 'example.com', severity: 'silence' } }
  40. end
  41. it 'records a block' do
  42. expect(DomainBlock.exists?(domain: 'example.com', severity: 'silence')).to be true
  43. end
  44. it 'calls DomainBlockWorker' do
  45. expect(DomainBlockWorker).to have_received(:perform_async)
  46. end
  47. it 'redirects with a success message' do
  48. expect(flash[:notice]).to eq I18n.t('admin.domain_blocks.created_msg')
  49. expect(response).to redirect_to(admin_instances_path(limited: '1'))
  50. end
  51. end
  52. context 'when the new domain block conflicts with an existing one' do
  53. before do
  54. Fabricate(:domain_block, domain: 'example.com', severity: 'suspend')
  55. post :create, params: { domain_block: { domain: 'example.com', severity: 'silence' } }
  56. end
  57. it 'does not record a block' do
  58. expect(DomainBlock.exists?(domain: 'example.com', severity: 'silence')).to be false
  59. end
  60. it 'does not call DomainBlockWorker' do
  61. expect(DomainBlockWorker).to_not have_received(:perform_async)
  62. end
  63. it 'renders new' do
  64. expect(response).to render_template :new
  65. end
  66. end
  67. context 'with "suspend" severity and no conflict' do
  68. context 'without a confirmation' do
  69. before do
  70. post :create, params: { domain_block: { domain: 'example.com', severity: 'suspend', reject_media: true, reject_reports: true } }
  71. end
  72. it 'does not record a block' do
  73. expect(DomainBlock.exists?(domain: 'example.com', severity: 'suspend')).to be false
  74. end
  75. it 'does not call DomainBlockWorker' do
  76. expect(DomainBlockWorker).to_not have_received(:perform_async)
  77. end
  78. it 'renders confirm_suspension' do
  79. expect(response).to render_template :confirm_suspension
  80. end
  81. end
  82. context 'with a confirmation' do
  83. before do
  84. post :create, params: { :domain_block => { domain: 'example.com', severity: 'suspend', reject_media: true, reject_reports: true }, 'confirm' => '' }
  85. end
  86. it 'records a block' do
  87. expect(DomainBlock.exists?(domain: 'example.com', severity: 'suspend')).to be true
  88. end
  89. it 'calls DomainBlockWorker' do
  90. expect(DomainBlockWorker).to have_received(:perform_async)
  91. end
  92. it 'redirects with a success message' do
  93. expect(flash[:notice]).to eq I18n.t('admin.domain_blocks.created_msg')
  94. expect(response).to redirect_to(admin_instances_path(limited: '1'))
  95. end
  96. end
  97. end
  98. context 'when upgrading an existing block' do
  99. before do
  100. Fabricate(:domain_block, domain: 'example.com', severity: 'silence')
  101. end
  102. context 'without a confirmation' do
  103. before do
  104. post :create, params: { domain_block: { domain: 'example.com', severity: 'suspend', reject_media: true, reject_reports: true } }
  105. end
  106. it 'does not record a block' do
  107. expect(DomainBlock.exists?(domain: 'example.com', severity: 'suspend')).to be false
  108. end
  109. it 'does not call DomainBlockWorker' do
  110. expect(DomainBlockWorker).to_not have_received(:perform_async)
  111. end
  112. it 'renders confirm_suspension' do
  113. expect(response).to render_template :confirm_suspension
  114. end
  115. end
  116. context 'with a confirmation' do
  117. before do
  118. post :create, params: { :domain_block => { domain: 'example.com', severity: 'suspend', reject_media: true, reject_reports: true }, 'confirm' => '' }
  119. end
  120. it 'updates the record' do
  121. expect(DomainBlock.exists?(domain: 'example.com', severity: 'suspend')).to be true
  122. end
  123. it 'calls DomainBlockWorker' do
  124. expect(DomainBlockWorker).to have_received(:perform_async)
  125. end
  126. it 'redirects with a success message' do
  127. expect(flash[:notice]).to eq I18n.t('admin.domain_blocks.created_msg')
  128. expect(response).to redirect_to(admin_instances_path(limited: '1'))
  129. end
  130. end
  131. end
  132. end
  133. describe 'GET #edit' do
  134. let(:domain_block) { Fabricate(:domain_block) }
  135. it 'returns http success' do
  136. get :edit, params: { id: domain_block.id }
  137. expect(assigns(:domain_block)).to be_instance_of(DomainBlock)
  138. expect(response).to have_http_status(200)
  139. end
  140. end
  141. describe 'PUT #update' do
  142. subject do
  143. post :update, params: { :id => domain_block.id, :domain_block => { domain: 'example.com', severity: new_severity }, 'confirm' => '' }
  144. end
  145. let!(:remote_account) { Fabricate(:account, domain: 'example.com') }
  146. let(:domain_block) { Fabricate(:domain_block, domain: 'example.com', severity: original_severity) }
  147. before do
  148. BlockDomainService.new.call(domain_block)
  149. end
  150. context 'when downgrading a domain suspension to silence' do
  151. let(:original_severity) { 'suspend' }
  152. let(:new_severity) { 'silence' }
  153. it 'changes the block severity, suspensions, and silences' do
  154. expect { subject }
  155. .to change_severity('suspend', 'silence')
  156. .and change_suspended(true, false)
  157. .and change_silenced(false, true)
  158. end
  159. end
  160. context 'when upgrading a domain silence to suspend' do
  161. let(:original_severity) { 'silence' }
  162. let(:new_severity) { 'suspend' }
  163. it 'changes the block severity, silences, and suspensions' do
  164. expect { subject }
  165. .to change_severity('silence', 'suspend')
  166. .and change_silenced(true, false)
  167. .and change_suspended(false, true)
  168. end
  169. end
  170. private
  171. def change_severity(from, to)
  172. change { domain_block.reload.severity }.from(from).to(to)
  173. end
  174. def change_silenced(from, to)
  175. change { remote_account.reload.silenced? }.from(from).to(to)
  176. end
  177. def change_suspended(from, to)
  178. change { remote_account.reload.suspended? }.from(from).to(to)
  179. end
  180. end
  181. describe 'DELETE #destroy' do
  182. it 'unblocks the domain' do
  183. service = instance_double(UnblockDomainService, call: true)
  184. allow(UnblockDomainService).to receive(:new).and_return(service)
  185. domain_block = Fabricate(:domain_block)
  186. delete :destroy, params: { id: domain_block.id }
  187. expect(service).to have_received(:call).with(domain_block)
  188. expect(flash[:notice]).to eq I18n.t('admin.domain_blocks.destroyed_msg')
  189. expect(response).to redirect_to(admin_instances_path(limited: '1'))
  190. end
  191. end
  192. end