Use pluck on DomainAllow.allowed_domains for export (#36019)

This commit is contained in:
Matt Jankowski 2025-09-05 08:30:18 -04:00 committed by GitHub
parent 350a802851
commit 3efba15b3c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 44 additions and 22 deletions

View File

@ -49,8 +49,8 @@ module Admin
def export_data def export_data
CSV.generate(headers: export_headers, write_headers: true) do |content| CSV.generate(headers: export_headers, write_headers: true) do |content|
DomainAllow.allowed_domains.each do |instance| DomainAllow.allowed_domains.each do |domain|
content << [instance.domain] content << [domain]
end end
end end
end end

View File

@ -27,7 +27,7 @@ class DomainAllow < ApplicationRecord
end end
def allowed_domains def allowed_domains
select(:domain) pluck(:domain)
end end
def rule_for(domain) def rule_for(domain)

View File

@ -17,17 +17,6 @@ RSpec.describe Admin::ExportDomainAllowsController do
end end
end end
describe 'GET #export' do
it 'renders instances' do
Fabricate(:domain_allow, domain: 'good.domain')
Fabricate(:domain_allow, domain: 'better.domain')
get :export, params: { format: :csv }
expect(response).to have_http_status(200)
expect(response.body).to eq(domain_allows_csv_file)
end
end
describe 'POST #import' do describe 'POST #import' do
it 'allows imported domains' do it 'allows imported domains' do
post :import, params: { admin_import: { data: fixture_file_upload('domain_allows.csv') } } post :import, params: { admin_import: { data: fixture_file_upload('domain_allows.csv') } }
@ -50,10 +39,4 @@ RSpec.describe Admin::ExportDomainAllowsController do
expect(flash[:error]).to eq(I18n.t('admin.export_domain_allows.no_file')) expect(flash[:error]).to eq(I18n.t('admin.export_domain_allows.no_file'))
end end
end end
private
def domain_allows_csv_file
File.read(File.join(file_fixture_path, 'domain_allows.csv'))
end
end end

View File

@ -12,4 +12,19 @@ RSpec.describe DomainAllow do
it { is_expected.to_not allow_value('xn--r9j5b5b').for(:domain) } it { is_expected.to_not allow_value('xn--r9j5b5b').for(:domain) }
end end
end end
describe '.allowed_domains' do
subject { described_class.allowed_domains }
context 'without domain allows' do
it { is_expected.to be_an(Array).and(be_empty) }
end
context 'with domain allows' do
let!(:allowed_domain) { Fabricate :domain_allow }
let!(:other_allowed_domain) { Fabricate :domain_allow }
it { is_expected.to contain_exactly(allowed_domain.domain, other_allowed_domain.domain) }
end
end
end end

View File

@ -3,9 +3,9 @@
require 'rails_helper' require 'rails_helper'
RSpec.describe 'Admin Export Domain Allows' do RSpec.describe 'Admin Export Domain Allows' do
describe 'POST /admin/export_domain_allows/import' do before { sign_in Fabricate(:admin_user) }
before { sign_in Fabricate(:admin_user) }
describe 'POST /admin/export_domain_allows/import' do
it 'gracefully handles invalid nested params' do it 'gracefully handles invalid nested params' do
post import_admin_export_domain_allows_path(admin_import: 'invalid') post import_admin_export_domain_allows_path(admin_import: 'invalid')
@ -13,4 +13,28 @@ RSpec.describe 'Admin Export Domain Allows' do
.to redirect_to(admin_instances_path) .to redirect_to(admin_instances_path)
end end
end end
describe 'GET /admin/export_domain_allows/export.csv' do
before do
Fabricate(:domain_allow, domain: 'good.domain')
Fabricate(:domain_allow, domain: 'better.domain')
end
it 'returns CSV response with instance domain values' do
get export_admin_export_domain_allows_path(format: :csv)
expect(response)
.to have_http_status(200)
expect(response.body)
.to eq(domain_allows_csv_file)
expect(response.media_type)
.to eq('text/csv')
end
end
private
def domain_allows_csv_file
File.read(File.join(file_fixture_path, 'domain_allows.csv'))
end
end end