2023-02-22 09:55:31 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-11-17 19:05:09 +09:00
|
|
|
require 'rails_helper'
|
|
|
|
|
2023-05-04 12:49:53 +09:00
|
|
|
RSpec.describe Admin::ExportDomainBlocksController do
|
2022-11-17 19:05:09 +09:00
|
|
|
render_views
|
|
|
|
|
|
|
|
before do
|
|
|
|
sign_in Fabricate(:user, role: UserRole.find_by(name: 'Admin')), scope: :user
|
|
|
|
end
|
|
|
|
|
2023-10-17 00:20:28 +09:00
|
|
|
describe 'GET #new' do
|
|
|
|
it 'returns http success' do
|
|
|
|
get :new
|
|
|
|
|
|
|
|
expect(response).to have_http_status(200)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-17 19:05:09 +09:00
|
|
|
describe 'GET #export' do
|
|
|
|
it 'renders instances' do
|
2023-01-19 00:20:52 +09:00
|
|
|
Fabricate(:domain_block, domain: 'bad.domain', severity: 'silence', public_comment: 'bad server')
|
|
|
|
Fabricate(:domain_block, domain: 'worse.domain', severity: 'suspend', reject_media: true, reject_reports: true, public_comment: 'worse server', obfuscate: true)
|
|
|
|
Fabricate(:domain_block, domain: 'reject.media', severity: 'noop', reject_media: true, public_comment: 'reject media and test unicode characters ♥')
|
2022-11-17 19:05:09 +09:00
|
|
|
Fabricate(:domain_block, domain: 'no.op', severity: 'noop', public_comment: 'noop')
|
|
|
|
|
|
|
|
get :export, params: { format: :csv }
|
|
|
|
expect(response).to have_http_status(200)
|
2023-12-04 17:52:21 +09:00
|
|
|
expect(response.body).to eq(domain_blocks_csv_file)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def domain_blocks_csv_file
|
|
|
|
File.read(File.join(file_fixture_path, 'domain_blocks.csv'))
|
2022-11-17 19:05:09 +09:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'POST #import' do
|
2023-01-19 00:20:52 +09:00
|
|
|
context 'with complete domain blocks CSV' do
|
|
|
|
before do
|
|
|
|
post :import, params: { admin_import: { data: fixture_file_upload('domain_blocks.csv') } }
|
|
|
|
end
|
2022-11-17 19:05:09 +09:00
|
|
|
|
2024-09-04 00:23:16 +09:00
|
|
|
it 'renders page with expected domain blocks and returns http success' do
|
|
|
|
expect(mapped_batch_table_rows).to contain_exactly(['bad.domain', :silence], ['worse.domain', :suspend], ['reject.media', :noop])
|
2023-01-19 00:20:52 +09:00
|
|
|
expect(response).to have_http_status(200)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a list of only domains' do
|
|
|
|
before do
|
|
|
|
post :import, params: { admin_import: { data: fixture_file_upload('domain_blocks_list.txt') } }
|
|
|
|
end
|
|
|
|
|
2024-09-04 00:23:16 +09:00
|
|
|
it 'renders page with expected domain blocks and returns http success' do
|
|
|
|
expect(mapped_batch_table_rows).to contain_exactly(['bad.domain', :suspend], ['worse.domain', :suspend], ['reject.media', :suspend])
|
2023-01-19 00:20:52 +09:00
|
|
|
expect(response).to have_http_status(200)
|
|
|
|
end
|
2022-11-17 19:05:09 +09:00
|
|
|
end
|
2024-09-04 00:23:16 +09:00
|
|
|
|
|
|
|
def mapped_batch_table_rows
|
|
|
|
batch_table_rows.map { |row| [row.at_css('[id$=_domain]')['value'], row.at_css('[id$=_severity]')['value'].to_sym] }
|
|
|
|
end
|
|
|
|
|
|
|
|
def batch_table_rows
|
2024-09-05 02:29:05 +09:00
|
|
|
response.parsed_body.css('body div.batch-table__row')
|
2024-09-04 00:23:16 +09:00
|
|
|
end
|
2022-11-17 19:05:09 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'displays error on no file selected' do
|
|
|
|
post :import, params: { admin_import: {} }
|
|
|
|
expect(flash[:alert]).to eq(I18n.t('admin.export_domain_blocks.no_file'))
|
|
|
|
end
|
|
|
|
end
|