2023-05-24 19:55:40 +10:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'rails_helper'
|
|
|
|
require 'mastodon/cli/media'
|
|
|
|
|
|
|
|
describe Mastodon::CLI::Media do
|
2023-12-08 00:49:14 +11:00
|
|
|
subject { cli.invoke(action, arguments, options) }
|
|
|
|
|
2023-10-17 03:20:58 +11:00
|
|
|
let(:cli) { described_class.new }
|
2023-12-08 00:49:14 +11:00
|
|
|
let(:arguments) { [] }
|
|
|
|
let(:options) { {} }
|
2023-10-17 03:20:58 +11:00
|
|
|
|
2023-12-01 23:00:34 +11:00
|
|
|
it_behaves_like 'CLI Command'
|
2023-10-17 03:20:58 +11:00
|
|
|
|
|
|
|
describe '#remove' do
|
2023-12-08 00:49:14 +11:00
|
|
|
let(:action) { :remove }
|
|
|
|
|
2023-10-17 03:20:58 +11:00
|
|
|
context 'with --prune-profiles and --remove-headers' do
|
|
|
|
let(:options) { { prune_profiles: true, remove_headers: true } }
|
|
|
|
|
|
|
|
it 'warns about usage and exits' do
|
2023-12-08 00:49:14 +11:00
|
|
|
expect { subject }
|
|
|
|
.to output_results('--prune-profiles and --remove-headers should not be specified simultaneously')
|
|
|
|
.and raise_error(SystemExit)
|
2023-10-17 03:20:58 +11:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with --include-follows but not including --prune-profiles and --remove-headers' do
|
|
|
|
let(:options) { { include_follows: true } }
|
|
|
|
|
|
|
|
it 'warns about usage and exits' do
|
2023-12-08 00:49:14 +11:00
|
|
|
expect { subject }
|
|
|
|
.to output_results('--include-follows can only be used with --prune-profiles or --remove-headers')
|
|
|
|
.and raise_error(SystemExit)
|
2023-10-17 03:20:58 +11:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a relevant account' do
|
|
|
|
let!(:account) do
|
|
|
|
Fabricate(:account, domain: 'example.com', updated_at: 1.month.ago, last_webfingered_at: 1.month.ago, avatar: attachment_fixture('attachment.jpg'), header: attachment_fixture('attachment.jpg'))
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with --prune-profiles' do
|
|
|
|
let(:options) { { prune_profiles: true } }
|
|
|
|
|
|
|
|
it 'removes account avatars' do
|
2023-12-08 00:49:14 +11:00
|
|
|
expect { subject }
|
|
|
|
.to output_results('Visited 1')
|
2023-10-17 03:20:58 +11:00
|
|
|
|
|
|
|
expect(account.reload.avatar).to be_blank
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with --remove-headers' do
|
|
|
|
let(:options) { { remove_headers: true } }
|
|
|
|
|
|
|
|
it 'removes account header' do
|
2023-12-08 00:49:14 +11:00
|
|
|
expect { subject }
|
|
|
|
.to output_results('Visited 1')
|
2023-10-17 03:20:58 +11:00
|
|
|
|
|
|
|
expect(account.reload.header).to be_blank
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a relevant media attachment' do
|
|
|
|
let!(:media_attachment) { Fabricate(:media_attachment, remote_url: 'https://example.com/image.jpg', created_at: 1.month.ago) }
|
|
|
|
|
|
|
|
context 'without options' do
|
|
|
|
it 'removes account avatars' do
|
2023-12-08 00:49:14 +11:00
|
|
|
expect { subject }
|
|
|
|
.to output_results('Removed 1')
|
2023-10-17 03:20:58 +11:00
|
|
|
|
|
|
|
expect(media_attachment.reload.file).to be_blank
|
|
|
|
expect(media_attachment.reload.thumbnail).to be_blank
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2023-12-01 22:56:47 +11:00
|
|
|
|
|
|
|
describe '#usage' do
|
2023-12-08 00:49:14 +11:00
|
|
|
let(:action) { :usage }
|
2023-12-01 22:56:47 +11:00
|
|
|
|
2023-12-08 00:49:14 +11:00
|
|
|
context 'without options' do
|
2023-12-01 22:56:47 +11:00
|
|
|
it 'reports about storage size' do
|
2023-12-08 00:49:14 +11:00
|
|
|
expect { subject }
|
|
|
|
.to output_results('0 Bytes')
|
2023-12-01 22:56:47 +11:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2023-12-02 01:18:45 +11:00
|
|
|
|
2023-12-08 02:27:41 +11:00
|
|
|
describe '#lookup' do
|
|
|
|
let(:action) { :lookup }
|
|
|
|
let(:arguments) { [url] }
|
|
|
|
|
|
|
|
context 'with valid url not connected to a record' do
|
|
|
|
let(:url) { 'https://example.host/assets/1' }
|
|
|
|
|
|
|
|
it 'warns about url and exits' do
|
|
|
|
expect { subject }
|
|
|
|
.to output_results('Not a media URL')
|
|
|
|
.and raise_error(SystemExit)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a valid media url' do
|
|
|
|
let(:status) { Fabricate(:status) }
|
|
|
|
let(:media_attachment) { Fabricate(:media_attachment, status: status) }
|
|
|
|
let(:url) { media_attachment.file.url(:original) }
|
|
|
|
|
|
|
|
it 'displays the url of a connected status' do
|
|
|
|
expect { subject }
|
|
|
|
.to output_results(status.id.to_s)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-12-02 01:18:45 +11:00
|
|
|
describe '#refresh' do
|
2023-12-08 00:49:14 +11:00
|
|
|
let(:action) { :refresh }
|
2023-12-02 01:18:45 +11:00
|
|
|
|
2023-12-08 00:49:14 +11:00
|
|
|
context 'without any options' do
|
2023-12-02 01:18:45 +11:00
|
|
|
it 'warns about usage and exits' do
|
2023-12-08 00:49:14 +11:00
|
|
|
expect { subject }
|
|
|
|
.to output_results('Specify the source')
|
|
|
|
.and raise_error(SystemExit)
|
2023-12-02 01:18:45 +11:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with --status option' do
|
|
|
|
before do
|
|
|
|
media_attachment.update(file_file_name: nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:media_attachment) { Fabricate(:media_attachment, status: status, remote_url: 'https://host.example/asset.jpg') }
|
|
|
|
let(:options) { { status: status.id } }
|
|
|
|
let(:status) { Fabricate(:status) }
|
|
|
|
|
|
|
|
it 'redownloads the attachment file' do
|
2023-12-08 00:49:14 +11:00
|
|
|
expect { subject }
|
|
|
|
.to output_results('Downloaded 1 media')
|
2023-12-02 01:18:45 +11:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with --account option' do
|
|
|
|
context 'when the account does not exist' do
|
|
|
|
let(:options) { { account: 'not-real-user@example.host' } }
|
|
|
|
|
|
|
|
it 'warns about usage and exits' do
|
2023-12-08 00:49:14 +11:00
|
|
|
expect { subject }
|
|
|
|
.to output_results('No such account')
|
|
|
|
.and raise_error(SystemExit)
|
2023-12-02 01:18:45 +11:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the account exists' do
|
|
|
|
before do
|
|
|
|
media_attachment.update(file_file_name: nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:media_attachment) { Fabricate(:media_attachment, account: account) }
|
|
|
|
let(:options) { { account: account.acct } }
|
|
|
|
let(:account) { Fabricate(:account) }
|
|
|
|
|
|
|
|
it 'redownloads the attachment file' do
|
2023-12-08 00:49:14 +11:00
|
|
|
expect { subject }
|
|
|
|
.to output_results('Downloaded 1 media')
|
2023-12-02 01:18:45 +11:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with --domain option' do
|
|
|
|
before do
|
|
|
|
media_attachment.update(file_file_name: nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:domain) { 'example.host' }
|
|
|
|
let(:media_attachment) { Fabricate(:media_attachment, account: account) }
|
|
|
|
let(:options) { { domain: domain } }
|
|
|
|
let(:account) { Fabricate(:account, domain: domain) }
|
|
|
|
|
|
|
|
it 'redownloads the attachment file' do
|
2023-12-08 00:49:14 +11:00
|
|
|
expect { subject }
|
|
|
|
.to output_results('Downloaded 1 media')
|
2023-12-02 01:18:45 +11:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2024-01-04 01:12:56 +11:00
|
|
|
|
|
|
|
describe '#remove_orphans' do
|
|
|
|
let(:action) { :remove_orphans }
|
|
|
|
|
|
|
|
before do
|
|
|
|
FileUtils.mkdir_p Rails.public_path.join('system')
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'without any options' do
|
|
|
|
it 'runs without error' do
|
|
|
|
expect { subject }
|
|
|
|
.to output_results('Removed', 'orphans (approx')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when in azure mode' do
|
|
|
|
before do
|
|
|
|
allow(Paperclip::Attachment).to receive(:default_options).and_return(storage: :azure)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'warns about usage and exits' do
|
|
|
|
expect { subject }
|
|
|
|
.to output_results('azure storage driver is not supported')
|
|
|
|
.and raise_error(SystemExit)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when in fog mode' do
|
|
|
|
before do
|
|
|
|
allow(Paperclip::Attachment).to receive(:default_options).and_return(storage: :fog)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'warns about usage and exits' do
|
|
|
|
expect { subject }
|
|
|
|
.to output_results('fog storage driver is not supported')
|
|
|
|
.and raise_error(SystemExit)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when in filesystem mode' do
|
|
|
|
before do
|
|
|
|
allow(File).to receive(:delete).and_return(true)
|
|
|
|
media_attachment.delete
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:media_attachment) { Fabricate(:media_attachment) }
|
|
|
|
|
|
|
|
it 'removes the unlinked files' do
|
|
|
|
expect { subject }
|
|
|
|
.to output_results('Removed', 'orphans (approx')
|
|
|
|
expect(File).to have_received(:delete).with(media_attachment.file.path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2023-05-24 19:55:40 +10:00
|
|
|
end
|