0
0
Fork 0

Formalize some patterns in cli specs (#28255)

This commit is contained in:
Matt Jankowski 2023-12-07 08:49:14 -05:00 committed by GitHub
parent 5d97a897c8
commit ad34d33bfd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 492 additions and 449 deletions

View file

@ -4,96 +4,99 @@ require 'rails_helper'
require 'mastodon/cli/email_domain_blocks'
describe Mastodon::CLI::EmailDomainBlocks do
subject { cli.invoke(action, arguments, options) }
let(:cli) { described_class.new }
let(:arguments) { [] }
let(:options) { {} }
it_behaves_like 'CLI Command'
describe '#list' do
let(:action) { :list }
context 'with email domain block records' do
let!(:parent_block) { Fabricate(:email_domain_block) }
let!(:child_block) { Fabricate(:email_domain_block, parent: parent_block) }
let(:options) { {} }
it 'lists the blocks' do
expect { cli.invoke(:list, [], options) }.to output(
a_string_including(parent_block.domain)
.and(a_string_including(child_block.domain))
).to_stdout
expect { subject }
.to output_results(
parent_block.domain,
child_block.domain
)
end
end
end
describe '#add' do
context 'without any options' do
let(:options) { {} }
let(:action) { :add }
context 'without any options' do
it 'warns about usage and exits' do
expect { cli.invoke(:add, [], options) }.to output(
a_string_including('No domain(s) given')
).to_stdout.and raise_error(SystemExit)
expect { subject }
.to output_results('No domain(s) given')
.and raise_error(SystemExit)
end
end
context 'when blocks exist' do
let(:options) { {} }
let(:domain) { 'host.example' }
let(:arguments) { [domain] }
before { Fabricate(:email_domain_block, domain: domain) }
it 'does not add a new block' do
expect { cli.invoke(:add, [domain], options) }.to output(
a_string_including('is already blocked')
).to_stdout
expect { subject }
.to output_results('is already blocked')
.and(not_change(EmailDomainBlock, :count))
end
end
context 'when no blocks exist' do
let(:options) { {} }
let(:domain) { 'host.example' }
let(:arguments) { [domain] }
it 'adds a new block' do
expect { cli.invoke(:add, [domain], options) }.to output(
a_string_including('Added 1')
).to_stdout
expect { subject }
.to output_results('Added 1')
.and(change(EmailDomainBlock, :count).by(1))
end
end
end
describe '#remove' do
context 'without any options' do
let(:options) { {} }
let(:action) { :remove }
context 'without any options' do
it 'warns about usage and exits' do
expect { cli.invoke(:remove, [], options) }.to output(
a_string_including('No domain(s) given')
).to_stdout.and raise_error(SystemExit)
expect { subject }
.to output_results('No domain(s) given')
.and raise_error(SystemExit)
end
end
context 'when blocks exist' do
let(:options) { {} }
let(:domain) { 'host.example' }
let(:arguments) { [domain] }
before { Fabricate(:email_domain_block, domain: domain) }
it 'removes the block' do
expect { cli.invoke(:remove, [domain], options) }.to output(
a_string_including('Removed 1')
).to_stdout
expect { subject }
.to output_results('Removed 1')
.and(change(EmailDomainBlock, :count).by(-1))
end
end
context 'when no blocks exist' do
let(:options) { {} }
let(:domain) { 'host.example' }
let(:arguments) { [domain] }
it 'does not remove a block' do
expect { cli.invoke(:remove, [domain], options) }.to output(
a_string_including('is not yet blocked')
).to_stdout
expect { subject }
.to output_results('is not yet blocked')
.and(not_change(EmailDomainBlock, :count))
end
end