export_domain_blocks_controller_spec.rb 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe Admin::ExportDomainBlocksController 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 'returns http success' do
  10. get :new
  11. expect(response).to have_http_status(200)
  12. end
  13. end
  14. describe 'GET #export' do
  15. it 'renders instances' do
  16. Fabricate(:domain_block, domain: 'bad.domain', severity: 'silence', public_comment: 'bad server')
  17. Fabricate(:domain_block, domain: 'worse.domain', severity: 'suspend', reject_media: true, reject_reports: true, public_comment: 'worse server', obfuscate: true)
  18. Fabricate(:domain_block, domain: 'reject.media', severity: 'noop', reject_media: true, public_comment: 'reject media and test unicode characters ♥')
  19. Fabricate(:domain_block, domain: 'no.op', severity: 'noop', public_comment: 'noop')
  20. get :export, params: { format: :csv }
  21. expect(response).to have_http_status(200)
  22. expect(response.body).to eq(domain_blocks_csv_file)
  23. end
  24. private
  25. def domain_blocks_csv_file
  26. File.read(File.join(file_fixture_path, 'domain_blocks.csv'))
  27. end
  28. end
  29. describe 'POST #import' do
  30. context 'with complete domain blocks CSV' do
  31. before do
  32. post :import, params: { admin_import: { data: fixture_file_upload('domain_blocks.csv') } }
  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 contain_exactly(['bad.domain', :silence], ['worse.domain', :suspend], ['reject.media', :noop])
  36. end
  37. it 'returns http success' do
  38. expect(response).to have_http_status(200)
  39. end
  40. end
  41. context 'with a list of only domains' do
  42. before do
  43. post :import, params: { admin_import: { data: fixture_file_upload('domain_blocks_list.txt') } }
  44. end
  45. it 'renders page with expected domain blocks' do
  46. expect(assigns(:domain_blocks).map { |block| [block.domain, block.severity.to_sym] }).to contain_exactly(['bad.domain', :suspend], ['worse.domain', :suspend], ['reject.media', :suspend])
  47. end
  48. it 'returns http success' do
  49. expect(response).to have_http_status(200)
  50. end
  51. end
  52. end
  53. it 'displays error on no file selected' do
  54. post :import, params: { admin_import: {} }
  55. expect(flash[:alert]).to eq(I18n.t('admin.export_domain_blocks.no_file'))
  56. end
  57. end