Extract some contexts

This commit is contained in:
Matt Jankowski 2024-10-08 13:18:21 -04:00
parent 9c04c0f0be
commit 03a17396e6
2 changed files with 47 additions and 21 deletions

View file

@ -20,21 +20,41 @@ RSpec.describe ActivityPub::FetchRepliesWorker do
let(:json) { Oj.dump(payload) } let(:json) { Oj.dump(payload) }
describe 'perform' do describe 'perform' do
it 'performs a request if the collection URI is from the same host' do context 'when the collection URI is from the same host' do
before do
stub_request(:get, 'https://example.com/statuses_replies/1').to_return(status: 200, body: json, headers: { 'Content-Type': 'application/activity+json' }) stub_request(:get, 'https://example.com/statuses_replies/1').to_return(status: 200, body: json, headers: { 'Content-Type': 'application/activity+json' })
subject.perform(status.id, 'https://example.com/statuses_replies/1')
expect(a_request(:get, 'https://example.com/statuses_replies/1')).to have_been_made.once
end end
it 'does not perform a request if the collection URI is from a different host' do it 'performs a request' do
subject.perform(status.id, 'https://example.com/statuses_replies/1')
expect(a_request(:get, 'https://example.com/statuses_replies/1'))
.to have_been_made.once
end
end
context 'when the collection URI is from a different host' do
before do
stub_request(:get, 'https://other.com/statuses_replies/1').to_return(status: 200) stub_request(:get, 'https://other.com/statuses_replies/1').to_return(status: 200)
end
it 'does not perform a request' do
subject.perform(status.id, 'https://other.com/statuses_replies/1') subject.perform(status.id, 'https://other.com/statuses_replies/1')
expect(a_request(:get, 'https://other.com/statuses_replies/1')).to_not have_been_made
expect(a_request(:get, 'https://other.com/statuses_replies/1'))
.to_not have_been_made
end
end
context 'when the request fails' do
before do
stub_request(:get, 'https://example.com/statuses_replies/1').to_return(status: 500)
end end
it 'raises when request fails' do it 'raises when request fails' do
stub_request(:get, 'https://example.com/statuses_replies/1').to_return(status: 500) expect { subject.perform(status.id, 'https://example.com/statuses_replies/1') }
expect { subject.perform(status.id, 'https://example.com/statuses_replies/1') }.to raise_error Mastodon::UnexpectedResponseError .to raise_error Mastodon::UnexpectedResponseError
end
end end
end end
end end

View file

@ -11,23 +11,29 @@ RSpec.describe PublishAnnouncementReactionWorker do
let(:announcement) { Fabricate(:announcement) } let(:announcement) { Fabricate(:announcement) }
let(:name) { 'name value' } let(:name) { 'name value' }
it 'sends the announcement and name to the service when subscribed' do context 'when subscribed' do
allow(redis).to receive(:exists?).and_return(true) before { allow(redis).to receive(:exists?).and_return(true) }
it 'sends the announcement and name to the service' do
allow(redis).to receive(:publish) allow(redis).to receive(:publish)
worker.perform(announcement.id, name) worker.perform(announcement.id, name)
expect(redis).to have_received(:publish) expect(redis).to have_received(:publish)
end end
end
it 'does not send the announcement and name to the service when not subscribed' do context 'when not subscribed' do
allow(redis).to receive(:exists?).and_return(false) before { allow(redis).to receive(:exists?).and_return(false) }
it 'does not send the announcement and name to the service' do
allow(redis).to receive(:publish) allow(redis).to receive(:publish)
worker.perform(announcement.id, name) worker.perform(announcement.id, name)
expect(redis).to_not have_received(:publish) expect(redis).to_not have_received(:publish)
end end
end
it 'returns true for non-existent record' do it 'returns true for non-existent record' do
result = worker.perform(123_123_123, name) result = worker.perform(123_123_123, name)