domain_blocks_controller_spec.rb 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 'GET #show' do
  15. it 'returns http success' do
  16. domain_block = Fabricate(:domain_block)
  17. get :show, params: { id: domain_block.id }
  18. expect(response).to have_http_status(200)
  19. end
  20. end
  21. describe 'POST #create' do
  22. it 'blocks the domain when succeeded to save' do
  23. allow(DomainBlockWorker).to receive(:perform_async).and_return(true)
  24. post :create, params: { domain_block: { domain: 'example.com', severity: 'silence' } }
  25. expect(DomainBlockWorker).to have_received(:perform_async)
  26. expect(flash[:notice]).to eq I18n.t('admin.domain_blocks.created_msg')
  27. expect(response).to redirect_to(admin_instances_path(limited: '1'))
  28. end
  29. it 'renders new when failed to save' do
  30. Fabricate(:domain_block, domain: 'example.com')
  31. allow(DomainBlockWorker).to receive(:perform_async).and_return(true)
  32. post :create, params: { domain_block: { domain: 'example.com', severity: 'silence' } }
  33. expect(DomainBlockWorker).not_to have_received(:perform_async)
  34. expect(response).to render_template :new
  35. end
  36. end
  37. describe 'DELETE #destroy' do
  38. it 'unblocks the domain' do
  39. service = double(call: true)
  40. allow(UnblockDomainService).to receive(:new).and_return(service)
  41. domain_block = Fabricate(:domain_block)
  42. delete :destroy, params: { id: domain_block.id, domain_block: { retroactive: '1' } }
  43. expect(service).to have_received(:call).with(domain_block, true)
  44. expect(flash[:notice]).to eq I18n.t('admin.domain_blocks.destroyed_msg')
  45. expect(response).to redirect_to(admin_instances_path(limited: '1'))
  46. end
  47. end
  48. end