Reduce factory creation (65 -> 49) in AP::ProcessStatusUpdateService spec

This commit is contained in:
Matt Jankowski 2024-09-20 21:35:54 -04:00
parent 6d42c2aa41
commit d14d295bd0

View File

@ -2,10 +2,6 @@
require 'rails_helper'
def poll_option_json(name, votes)
{ type: 'Note', name: name, replies: { type: 'Collection', totalItems: votes } }
end
RSpec.describe ActivityPub::ProcessStatusUpdateService do
subject { described_class.new }
@ -294,7 +290,6 @@ RSpec.describe ActivityPub::ProcessStatusUpdateService do
context 'when originally without media attachments' do
before do
stub_request(:get, 'https://example.com/foo.png').to_return(body: attachment_fixture('emojo.png'))
subject.call(status, json, json)
end
let(:payload) do
@ -310,19 +305,18 @@ RSpec.describe ActivityPub::ProcessStatusUpdateService do
}
end
it 'updates media attachments' do
media_attachment = status.reload.ordered_media_attachments.first
it 'updates media attachments, fetches attachment, records media change in edit' do
subject.call(status, json, json)
expect(media_attachment).to_not be_nil
expect(media_attachment.remote_url).to eq 'https://example.com/foo.png'
end
expect(status.reload.ordered_media_attachments.first)
.to be_present
.and(have_attributes(remote_url: 'https://example.com/foo.png'))
it 'fetches the attachment' do
expect(a_request(:get, 'https://example.com/foo.png')).to have_been_made
end
expect(a_request(:get, 'https://example.com/foo.png'))
.to have_been_made
it 'records media change in edit' do
expect(status.edits.reload.last.ordered_media_attachment_ids).to_not be_empty
expect(status.edits.reload.last.ordered_media_attachment_ids)
.to_not be_empty
end
end
@ -344,27 +338,26 @@ RSpec.describe ActivityPub::ProcessStatusUpdateService do
before do
allow(RedownloadMediaWorker).to receive(:perform_async)
end
it 'updates the existing media attachment in-place, does not queue redownload, updates media, records media change' do
subject.call(status, json, json)
end
it 'updates the existing media attachment in-place' do
media_attachment = status.media_attachments.ordered.reload.first
expect(status.media_attachments.ordered.reload.first)
.to be_present
.and have_attributes(
remote_url: 'https://example.com/foo.png',
description: 'A picture'
)
expect(media_attachment).to_not be_nil
expect(media_attachment.remote_url).to eq 'https://example.com/foo.png'
expect(media_attachment.description).to eq 'A picture'
end
expect(RedownloadMediaWorker)
.to_not have_received(:perform_async)
it 'does not queue redownload for the existing media attachment' do
expect(RedownloadMediaWorker).to_not have_received(:perform_async)
end
expect(status.ordered_media_attachments.map(&:remote_url))
.to eq %w(https://example.com/foo.png)
it 'updates media attachments' do
expect(status.ordered_media_attachments.map(&:remote_url)).to eq %w(https://example.com/foo.png)
end
it 'records media change in edit' do
expect(status.edits.reload.last.ordered_media_attachment_ids).to_not be_empty
expect(status.edits.reload.last.ordered_media_attachment_ids)
.to_not be_empty
end
end
@ -372,10 +365,11 @@ RSpec.describe ActivityPub::ProcessStatusUpdateService do
before do
poll = Fabricate(:poll, status: status)
status.update(preloadable_poll: poll)
subject.call(status, json, json)
end
it 'removes poll and records media change in edit' do
subject.call(status, json, json)
expect(status.reload.poll).to be_nil
expect(status.edits.reload.last.poll_options).to be_nil
end
@ -398,15 +392,13 @@ RSpec.describe ActivityPub::ProcessStatusUpdateService do
}
end
before do
subject.call(status, json, json)
end
it 'creates a poll and records media change in edit' do
poll = status.reload.poll
subject.call(status, json, json)
expect(status.reload.poll)
.to be_present
.and have_attributes(options: %w(Foo Bar Baz))
expect(poll).to_not be_nil
expect(poll.options).to eq %w(Foo Bar Baz)
expect(status.edits.reload.last.poll_options).to eq %w(Foo Bar Baz)
end
end
@ -419,4 +411,8 @@ RSpec.describe ActivityPub::ProcessStatusUpdateService do
.to eq '2021-09-08 22:39:25 UTC'
end
end
def poll_option_json(name, votes)
{ type: 'Note', name: name, replies: { type: 'Collection', totalItems: votes } }
end
end