Convert admin/email_domain_blocks controller -> system spec (#33759)

This commit is contained in:
Matt Jankowski 2025-01-29 05:54:20 -05:00 committed by GitHub
parent 6aa565b319
commit 2beab34ca4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 56 additions and 68 deletions

View File

@ -1,68 +0,0 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe Admin::EmailDomainBlocksController do
render_views
before do
sign_in Fabricate(:admin_user), scope: :user
end
describe 'GET #index' do
around do |example|
default_per_page = EmailDomainBlock.default_per_page
EmailDomainBlock.paginates_per 2
example.run
EmailDomainBlock.paginates_per default_per_page
end
it 'returns http success' do
2.times { Fabricate(:email_domain_block) }
Fabricate(:email_domain_block, allow_with_approval: true)
get :index, params: { page: 2 }
expect(response).to have_http_status(200)
end
end
describe 'GET #new' do
it 'returns http success' do
get :new
expect(response).to have_http_status(200)
end
end
describe 'POST #create' do
context 'when resolve button is pressed' do
before do
resolver = instance_double(Resolv::DNS)
allow(resolver).to receive(:getresources)
.with('example.com', Resolv::DNS::Resource::IN::MX)
.and_return([])
allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::A).and_return([])
allow(resolver).to receive(:getresources).with('example.com', Resolv::DNS::Resource::IN::AAAA).and_return([])
allow(resolver).to receive(:timeouts=).and_return(nil)
allow(Resolv::DNS).to receive(:open).and_yield(resolver)
post :create, params: { email_domain_block: { domain: 'example.com' } }
end
it 'renders new template' do
expect(response).to render_template(:new)
end
end
context 'when save button is pressed' do
before do
post :create, params: { email_domain_block: { domain: 'example.com' }, save: '' }
end
it 'blocks the domain and redirects to email domain blocks' do
expect(EmailDomainBlock.find_by(domain: 'example.com')).to_not be_nil
expect(response).to redirect_to(admin_email_domain_blocks_path)
end
end
end
end

View File

@ -28,6 +28,25 @@ module DomainHelpers
.and_yield(resolver)
end
def configure_dns(domain:, results:)
resolver = instance_double(Resolv::DNS, :timeouts= => nil)
allow(resolver).to receive(:getresources)
.with(domain, Resolv::DNS::Resource::IN::MX)
.and_return(results)
allow(resolver)
.to receive(:getresources)
.with(domain, Resolv::DNS::Resource::IN::A)
.and_return(results)
allow(resolver)
.to receive(:getresources)
.with(domain, Resolv::DNS::Resource::IN::AAAA)
.and_return(results)
allow(Resolv::DNS)
.to receive(:open)
.and_yield(resolver)
end
private
def double_mx(exchange)

View File

@ -7,6 +7,43 @@ RSpec.describe 'Admin::EmailDomainBlocks' do
before { sign_in current_user }
describe 'Managing email domain blocks' do
before { configure_dns(domain: 'example.com', results: []) }
let!(:email_domain_block) { Fabricate :email_domain_block }
it 'views and creates new blocks' do
visit admin_email_domain_blocks_path
expect(page)
.to have_content(I18n.t('admin.email_domain_blocks.title'))
.and have_content(email_domain_block.domain)
click_on I18n.t('admin.email_domain_blocks.add_new')
expect(page)
.to have_content(I18n.t('admin.email_domain_blocks.new.title'))
fill_in I18n.t('admin.email_domain_blocks.domain'), with: 'example.com'
expect { submit_resolve }
.to_not change(EmailDomainBlock, :count)
expect(page)
.to have_content(I18n.t('admin.email_domain_blocks.new.title'))
expect { submit_create }
.to change(EmailDomainBlock.where(domain: 'example.com'), :count).by(1)
expect(page)
.to have_content(I18n.t('admin.email_domain_blocks.title'))
.and have_content(I18n.t('admin.email_domain_blocks.created_msg'))
end
def submit_resolve
click_on I18n.t('admin.email_domain_blocks.new.resolve')
end
def submit_create
click_on I18n.t('admin.email_domain_blocks.new.create')
end
end
describe 'Performing batch updates' do
before do
visit admin_email_domain_blocks_path