Reduce factory creation (14 -> 8) in `ActivityPub::Activity::Block` spec (#32488)

This commit is contained in:
Matt Jankowski 2024-10-15 09:51:52 -04:00 committed by Claire
parent ae676edc2b
commit 527d1253bf
1 changed files with 51 additions and 77 deletions

View File

@ -3,6 +3,8 @@
require 'rails_helper'
RSpec.describe ActivityPub::Activity::Block do
subject { described_class.new(json, sender) }
let(:sender) { Fabricate(:account) }
let(:recipient) { Fabricate(:account) }
@ -16,93 +18,65 @@ RSpec.describe ActivityPub::Activity::Block do
}.with_indifferent_access
end
context 'when the recipient does not follow the sender' do
describe '#perform' do
subject { described_class.new(json, sender) }
before do
subject.perform
end
describe '#perform' do
context 'when the recipient does not follow the sender' do
it 'creates a block from sender to recipient' do
expect(sender.blocking?(recipient)).to be true
subject.perform
expect(sender)
.to be_blocking(recipient)
end
end
end
context 'when the recipient is already blocked' do
before do
sender.block!(recipient, uri: 'old')
context 'when the recipient is already blocked' do
before { sender.block!(recipient, uri: 'old') }
it 'creates a block from sender to recipient and sets uri to last received block activity' do
subject.perform
expect(sender)
.to be_blocking(recipient)
expect(sender.block_relationships.find_by(target_account: recipient).uri)
.to eq 'foo'
end
end
describe '#perform' do
subject { described_class.new(json, sender) }
context 'when the recipient follows the sender' do
before { recipient.follow!(sender) }
it 'creates a block from sender to recipient and ensures recipient not following sender' do
subject.perform
expect(sender)
.to be_blocking(recipient)
expect(recipient)
.to_not be_following(sender)
end
end
context 'when a matching undo has been received first' do
let(:undo_json) do
{
'@context': 'https://www.w3.org/ns/activitystreams',
id: 'bar',
type: 'Undo',
actor: ActivityPub::TagManager.instance.uri_for(sender),
object: json,
}.with_indifferent_access
end
before do
recipient.follow!(sender)
ActivityPub::Activity::Undo.new(undo_json, sender).perform
end
it 'does not create a block from sender to recipient and ensures recipient not following sender' do
subject.perform
end
it 'creates a block from sender to recipient' do
expect(sender.blocking?(recipient)).to be true
end
it 'sets the uri to that of last received block activity' do
expect(sender.block_relationships.find_by(target_account: recipient).uri).to eq 'foo'
end
end
end
context 'when the recipient follows the sender' do
before do
recipient.follow!(sender)
end
describe '#perform' do
subject { described_class.new(json, sender) }
before do
subject.perform
end
it 'creates a block from sender to recipient' do
expect(sender.blocking?(recipient)).to be true
end
it 'ensures recipient is not following sender' do
expect(recipient.following?(sender)).to be false
end
end
end
context 'when a matching undo has been received first' do
let(:undo_json) do
{
'@context': 'https://www.w3.org/ns/activitystreams',
id: 'bar',
type: 'Undo',
actor: ActivityPub::TagManager.instance.uri_for(sender),
object: json,
}.with_indifferent_access
end
before do
recipient.follow!(sender)
ActivityPub::Activity::Undo.new(undo_json, sender).perform
end
describe '#perform' do
subject { described_class.new(json, sender) }
before do
subject.perform
end
it 'does not create a block from sender to recipient' do
expect(sender.blocking?(recipient)).to be false
end
it 'ensures recipient is not following sender' do
expect(recipient.following?(sender)).to be false
expect(sender)
.to_not be_blocking(recipient)
expect(recipient)
.to_not be_following(sender)
end
end
end