domain_blocks_controller_spec.rb 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. require 'rails_helper'
  2. RSpec.describe Admin::DomainBlocksController, type: :controller do
  3. render_views
  4. before do
  5. sign_in Fabricate(:user, admin: true), scope: :user
  6. end
  7. describe 'GET #new' do
  8. it 'assigns a new domain block' do
  9. get :new
  10. expect(assigns(:domain_block)).to be_instance_of(DomainBlock)
  11. expect(response).to have_http_status(200)
  12. end
  13. end
  14. describe 'POST #create' do
  15. it 'blocks the domain when succeeded to save' do
  16. allow(DomainBlockWorker).to receive(:perform_async).and_return(true)
  17. post :create, params: { domain_block: { domain: 'example.com', severity: 'silence' } }
  18. expect(DomainBlockWorker).to have_received(:perform_async)
  19. expect(flash[:notice]).to eq I18n.t('admin.domain_blocks.created_msg')
  20. expect(response).to redirect_to(admin_instances_path(limited: '1'))
  21. end
  22. it 'renders new when failed to save' do
  23. Fabricate(:domain_block, domain: 'example.com', severity: 'suspend')
  24. allow(DomainBlockWorker).to receive(:perform_async).and_return(true)
  25. post :create, params: { domain_block: { domain: 'example.com', severity: 'silence' } }
  26. expect(DomainBlockWorker).not_to have_received(:perform_async)
  27. expect(response).to render_template :new
  28. end
  29. it 'allows upgrading a block' do
  30. Fabricate(:domain_block, domain: 'example.com', severity: 'silence')
  31. allow(DomainBlockWorker).to receive(:perform_async).and_return(true)
  32. post :create, params: { domain_block: { domain: 'example.com', severity: 'silence', reject_media: true, reject_reports: true } }
  33. expect(DomainBlockWorker).to have_received(:perform_async)
  34. expect(flash[:notice]).to eq I18n.t('admin.domain_blocks.created_msg')
  35. expect(response).to redirect_to(admin_instances_path(limited: '1'))
  36. end
  37. end
  38. describe 'DELETE #destroy' do
  39. it 'unblocks the domain' do
  40. service = double(call: true)
  41. allow(UnblockDomainService).to receive(:new).and_return(service)
  42. domain_block = Fabricate(:domain_block)
  43. delete :destroy, params: { id: domain_block.id }
  44. expect(service).to have_received(:call).with(domain_block)
  45. expect(flash[:notice]).to eq I18n.t('admin.domain_blocks.destroyed_msg')
  46. expect(response).to redirect_to(admin_instances_path(limited: '1'))
  47. end
  48. end
  49. end