export_domain_allows_controller_spec.rb 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe Admin::ExportDomainAllowsController 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_allow, domain: 'good.domain')
  17. Fabricate(:domain_allow, domain: 'better.domain')
  18. get :export, params: { format: :csv }
  19. expect(response).to have_http_status(200)
  20. expect(response.body).to eq(domain_allows_csv_file)
  21. end
  22. end
  23. describe 'POST #import' do
  24. it 'allows imported domains' do
  25. post :import, params: { admin_import: { data: fixture_file_upload('domain_allows.csv') } }
  26. expect(response).to redirect_to(admin_instances_path)
  27. # Header should not be imported
  28. expect(DomainAllow.where(domain: '#domain').present?).to be(false)
  29. # Domains should now be added
  30. get :export, params: { format: :csv }
  31. expect(response).to have_http_status(200)
  32. expect(response.body).to eq(domain_allows_csv_file)
  33. end
  34. it 'displays error on no file selected' do
  35. post :import, params: { admin_import: {} }
  36. expect(response).to redirect_to(admin_instances_path)
  37. expect(flash[:error]).to eq(I18n.t('admin.export_domain_allows.no_file'))
  38. end
  39. end
  40. private
  41. def domain_allows_csv_file
  42. File.read(File.join(file_fixture_path, 'domain_allows.csv'))
  43. end
  44. end