From 527d1253bf4e49c1255f65f213c2ef01ae14a0e9 Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Tue, 15 Oct 2024 09:51:52 -0400 Subject: [PATCH] Reduce factory creation (14 -> 8) in `ActivityPub::Activity::Block` spec (#32488) --- spec/lib/activitypub/activity/block_spec.rb | 128 ++++++++------------ 1 file changed, 51 insertions(+), 77 deletions(-) diff --git a/spec/lib/activitypub/activity/block_spec.rb b/spec/lib/activitypub/activity/block_spec.rb index 6f6898401..385628852 100644 --- a/spec/lib/activitypub/activity/block_spec.rb +++ b/spec/lib/activitypub/activity/block_spec.rb @@ -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