You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
2.5 KiB

  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 #index' do
  8. around do |example|
  9. default_per_page = DomainBlock.default_per_page
  10. DomainBlock.paginates_per 1
  11. example.run
  12. DomainBlock.paginates_per default_per_page
  13. end
  14. it 'renders domain blocks' do
  15. 2.times { Fabricate(:domain_block) }
  16. get :index, params: { page: 2 }
  17. assigned = assigns(:domain_blocks)
  18. expect(assigned.count).to eq 1
  19. expect(assigned.klass).to be DomainBlock
  20. expect(response).to have_http_status(200)
  21. end
  22. end
  23. describe 'GET #new' do
  24. it 'assigns a new domain block' do
  25. get :new
  26. expect(assigns(:domain_block)).to be_instance_of(DomainBlock)
  27. expect(response).to have_http_status(200)
  28. end
  29. end
  30. describe 'GET #show' do
  31. it 'returns http success' do
  32. domain_block = Fabricate(:domain_block)
  33. get :show, params: { id: domain_block.id }
  34. expect(response).to have_http_status(200)
  35. end
  36. end
  37. describe 'POST #create' do
  38. it 'blocks the domain when succeeded to save' do
  39. allow(DomainBlockWorker).to receive(:perform_async).and_return(true)
  40. post :create, params: { domain_block: { domain: 'example.com', severity: 'silence' } }
  41. expect(DomainBlockWorker).to have_received(:perform_async)
  42. expect(flash[:notice]).to eq I18n.t('admin.domain_blocks.created_msg')
  43. expect(response).to redirect_to(admin_domain_blocks_path)
  44. end
  45. it 'renders new when failed to save' do
  46. Fabricate(:domain_block, domain: 'example.com')
  47. allow(DomainBlockWorker).to receive(:perform_async).and_return(true)
  48. post :create, params: { domain_block: { domain: 'example.com', severity: 'silence' } }
  49. expect(DomainBlockWorker).not_to have_received(:perform_async)
  50. expect(response).to render_template :new
  51. end
  52. end
  53. describe 'DELETE #destroy' do
  54. it 'unblocks the domain' do
  55. service = double(call: true)
  56. allow(UnblockDomainService).to receive(:new).and_return(service)
  57. domain_block = Fabricate(:domain_block)
  58. delete :destroy, params: { id: domain_block.id, domain_block: { retroactive: '1' } }
  59. expect(service).to have_received(:call).with(domain_block, true)
  60. expect(flash[:notice]).to eq I18n.t('admin.domain_blocks.destroyed_msg')
  61. expect(response).to redirect_to(admin_domain_blocks_path)
  62. end
  63. end
  64. end