export_domain_blocks_controller_spec.rb 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. require 'rails_helper'
  2. RSpec.describe Admin::ExportDomainBlocksController, type: :controller do
  3. render_views
  4. before do
  5. sign_in Fabricate(:user, role: UserRole.find_by(name: 'Admin')), scope: :user
  6. end
  7. describe 'GET #export' do
  8. it 'renders instances' do
  9. Fabricate(:domain_block, domain: 'bad.domain', severity: 'silence', public_comment: 'bad server')
  10. Fabricate(:domain_block, domain: 'worse.domain', severity: 'suspend', reject_media: true, reject_reports: true, public_comment: 'worse server', obfuscate: true)
  11. Fabricate(:domain_block, domain: 'reject.media', severity: 'noop', reject_media: true, public_comment: 'reject media and test unicode characters ♥')
  12. Fabricate(:domain_block, domain: 'no.op', severity: 'noop', public_comment: 'noop')
  13. get :export, params: { format: :csv }
  14. expect(response).to have_http_status(200)
  15. expect(response.body).to eq(IO.read(File.join(file_fixture_path, 'domain_blocks.csv')))
  16. end
  17. end
  18. describe 'POST #import' do
  19. context 'with complete domain blocks CSV' do
  20. before do
  21. post :import, params: { admin_import: { data: fixture_file_upload('domain_blocks.csv') } }
  22. end
  23. it 'renders page with expected domain blocks' do
  24. expect(assigns(:domain_blocks).map { |block| [block.domain, block.severity.to_sym] }).to match_array [['bad.domain', :silence], ['worse.domain', :suspend], ['reject.media', :noop]]
  25. end
  26. it 'returns http success' do
  27. expect(response).to have_http_status(200)
  28. end
  29. end
  30. context 'with a list of only domains' do
  31. before do
  32. post :import, params: { admin_import: { data: fixture_file_upload('domain_blocks_list.txt') } }
  33. end
  34. it 'renders page with expected domain blocks' do
  35. expect(assigns(:domain_blocks).map { |block| [block.domain, block.severity.to_sym] }).to match_array [['bad.domain', :suspend], ['worse.domain', :suspend], ['reject.media', :suspend]]
  36. end
  37. it 'returns http success' do
  38. expect(response).to have_http_status(200)
  39. end
  40. end
  41. end
  42. it 'displays error on no file selected' do
  43. post :import, params: { admin_import: {} }
  44. expect(flash[:alert]).to eq(I18n.t('admin.export_domain_blocks.no_file'))
  45. end
  46. end