From b3b009e6aa29d9051aa61113da344b3ab26d98e4 Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Mon, 4 Dec 2023 04:44:54 -0500 Subject: [PATCH] Add spec coverage for `CLI::EmailDomainBlocks` commands (#28181) --- lib/mastodon/cli/email_domain_blocks.rb | 4 +- .../mastodon/cli/email_domain_blocks_spec.rb | 93 +++++++++++++++++++ 2 files changed, 95 insertions(+), 2 deletions(-) diff --git a/lib/mastodon/cli/email_domain_blocks.rb b/lib/mastodon/cli/email_domain_blocks.rb index 88a84ecb4..022b1dcbb 100644 --- a/lib/mastodon/cli/email_domain_blocks.rb +++ b/lib/mastodon/cli/email_domain_blocks.rb @@ -7,10 +7,10 @@ module Mastodon::CLI class EmailDomainBlocks < Base desc 'list', 'List blocked e-mail domains' def list - EmailDomainBlock.where(parent_id: nil).order(id: 'DESC').find_each do |entry| + EmailDomainBlock.where(parent_id: nil).find_each do |entry| say(entry.domain.to_s, :white) - EmailDomainBlock.where(parent_id: entry.id).order(id: 'DESC').find_each do |child| + EmailDomainBlock.where(parent_id: entry.id).find_each do |child| say(" #{child.domain}", :cyan) end end diff --git a/spec/lib/mastodon/cli/email_domain_blocks_spec.rb b/spec/lib/mastodon/cli/email_domain_blocks_spec.rb index 060943b18..f5cb6c332 100644 --- a/spec/lib/mastodon/cli/email_domain_blocks_spec.rb +++ b/spec/lib/mastodon/cli/email_domain_blocks_spec.rb @@ -4,5 +4,98 @@ require 'rails_helper' require 'mastodon/cli/email_domain_blocks' describe Mastodon::CLI::EmailDomainBlocks do + let(:cli) { described_class.new } + it_behaves_like 'CLI Command' + + describe '#list' do + 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 + end + end + end + + describe '#add' do + context 'without any options' do + let(:options) { {} } + + 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) + end + end + + context 'when blocks exist' do + let(:options) { {} } + let(:domain) { 'host.example' } + + 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 + .and(not_change(EmailDomainBlock, :count)) + end + end + + context 'when no blocks exist' do + let(:options) { {} } + let(:domain) { 'host.example' } + + it 'adds a new block' do + expect { cli.invoke(:add, [domain], options) }.to output( + a_string_including('Added 1') + ).to_stdout + .and(change(EmailDomainBlock, :count).by(1)) + end + end + end + + describe '#remove' do + context 'without any options' do + let(:options) { {} } + + 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) + end + end + + context 'when blocks exist' do + let(:options) { {} } + let(:domain) { 'host.example' } + + 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 + .and(change(EmailDomainBlock, :count).by(-1)) + end + end + + context 'when no blocks exist' do + let(:options) { {} } + let(:domain) { 'host.example' } + + it 'does not remove a block' do + expect { cli.invoke(:remove, [domain], options) }.to output( + a_string_including('is not yet blocked') + ).to_stdout + .and(not_change(EmailDomainBlock, :count)) + end + end + end end