domain_blocks_controller_spec.rb 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 'PUT #update' do
  134. subject do
  135. post :update, params: { :id => domain_block.id, :domain_block => { domain: 'example.com', severity: new_severity }, 'confirm' => '' }
  136. end
  137. let!(:remote_account) { Fabricate(:account, domain: 'example.com') }
  138. let(:domain_block) { Fabricate(:domain_block, domain: 'example.com', severity: original_severity) }
  139. before do
  140. BlockDomainService.new.call(domain_block)
  141. end
  142. context 'when downgrading a domain suspension to silence' do
  143. let(:original_severity) { 'suspend' }
  144. let(:new_severity) { 'silence' }
  145. it 'changes the block severity' do
  146. expect { subject }.to change { domain_block.reload.severity }.from('suspend').to('silence')
  147. end
  148. it 'undoes individual suspensions' do
  149. expect { subject }.to change { remote_account.reload.suspended? }.from(true).to(false)
  150. end
  151. it 'performs individual silences' do
  152. expect { subject }.to change { remote_account.reload.silenced? }.from(false).to(true)
  153. end
  154. end
  155. context 'when upgrading a domain silence to suspend' do
  156. let(:original_severity) { 'silence' }
  157. let(:new_severity) { 'suspend' }
  158. it 'changes the block severity' do
  159. expect { subject }.to change { domain_block.reload.severity }.from('silence').to('suspend')
  160. end
  161. it 'undoes individual silences' do
  162. expect { subject }.to change { remote_account.reload.silenced? }.from(true).to(false)
  163. end
  164. it 'performs individual suspends' do
  165. expect { subject }.to change { remote_account.reload.suspended? }.from(false).to(true)
  166. end
  167. end
  168. end
  169. describe 'DELETE #destroy' do
  170. it 'unblocks the domain' do
  171. service = instance_double(UnblockDomainService, call: true)
  172. allow(UnblockDomainService).to receive(:new).and_return(service)
  173. domain_block = Fabricate(:domain_block)
  174. delete :destroy, params: { id: domain_block.id }
  175. expect(service).to have_received(:call).with(domain_block)
  176. expect(flash[:notice]).to eq I18n.t('admin.domain_blocks.destroyed_msg')
  177. expect(response).to redirect_to(admin_instances_path(limited: '1'))
  178. end
  179. end
  180. end