Skip to content

Commit

Permalink
Fix handling of Reject Follow when a matching follow relationship exi…
Browse files Browse the repository at this point in the history
…sts (mastodon#14479)

* Add tests

* Fix handling of Reject Follow when a matching follow relationship exists

Regression from mastodon#12199
  • Loading branch information
ClearlyClaire authored and Mage committed Jan 14, 2022
1 parent 024d80e commit c9b824f
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 14 deletions.
2 changes: 1 addition & 1 deletion app/lib/activitypub/activity/reject.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class ActivityPub::Activity::Reject < ActivityPub::Activity
def perform
return reject_follow_for_relay if relay_follow?
return follow_request_from_object.reject! unless follow_request_from_object.nil?
return UnfollowService.new.call(follow_from_object.target_account, @account) unless follow_from_object.nil?
return UnfollowService.new.call(follow_from_object.account, @account) unless follow_from_object.nil?

case @object['type']
when 'Follow'
Expand Down
110 changes: 97 additions & 13 deletions spec/lib/activitypub/activity/reject_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,120 @@
RSpec.describe ActivityPub::Activity::Reject do
let(:sender) { Fabricate(:account) }
let(:recipient) { Fabricate(:account) }
let(:object_json) do
{
id: 'bar',
type: 'Follow',
actor: ActivityPub::TagManager.instance.uri_for(recipient),
object: ActivityPub::TagManager.instance.uri_for(sender),
}
end

let(:json) do
{
'@context': 'https://www.w3.org/ns/activitystreams',
id: 'foo',
type: 'Reject',
actor: ActivityPub::TagManager.instance.uri_for(sender),
object: {
id: 'bar',
type: 'Follow',
actor: ActivityPub::TagManager.instance.uri_for(recipient),
object: ActivityPub::TagManager.instance.uri_for(sender),
},
object: object_json,
}.with_indifferent_access
end

describe '#perform' do
subject { described_class.new(json, sender) }

before do
Fabricate(:follow_request, account: recipient, target_account: sender)
subject.perform
context 'rejecting a pending follow request by target' do
before do
Fabricate(:follow_request, account: recipient, target_account: sender)
subject.perform
end

it 'does not create a follow relationship' do
expect(recipient.following?(sender)).to be false
end

it 'removes the follow request' do
expect(recipient.requested?(sender)).to be false
end
end

context 'rejecting a pending follow request by uri' do
before do
Fabricate(:follow_request, account: recipient, target_account: sender, uri: 'bar')
subject.perform
end

it 'does not create a follow relationship' do
expect(recipient.following?(sender)).to be false
end

it 'removes the follow request' do
expect(recipient.requested?(sender)).to be false
end
end

it 'does not create a follow relationship' do
expect(recipient.following?(sender)).to be false
context 'rejecting a pending follow request by uri only' do
let(:object_json) { 'bar' }

before do
Fabricate(:follow_request, account: recipient, target_account: sender, uri: 'bar')
subject.perform
end

it 'does not create a follow relationship' do
expect(recipient.following?(sender)).to be false
end

it 'removes the follow request' do
expect(recipient.requested?(sender)).to be false
end
end

it 'removes the follow request' do
expect(recipient.requested?(sender)).to be false
context 'rejecting an existing follow relationship by target' do
before do
Fabricate(:follow, account: recipient, target_account: sender)
subject.perform
end

it 'removes the follow relationship' do
expect(recipient.following?(sender)).to be false
end

it 'does not create a follow request' do
expect(recipient.requested?(sender)).to be false
end
end

context 'rejecting an existing follow relationship by uri' do
before do
Fabricate(:follow, account: recipient, target_account: sender, uri: 'bar')
subject.perform
end

it 'removes the follow relationship' do
expect(recipient.following?(sender)).to be false
end

it 'does not create a follow request' do
expect(recipient.requested?(sender)).to be false
end
end

context 'rejecting an existing follow relationship by uri only' do
let(:object_json) { 'bar' }

before do
Fabricate(:follow, account: recipient, target_account: sender, uri: 'bar')
subject.perform
end

it 'removes the follow relationship' do
expect(recipient.following?(sender)).to be false
end

it 'does not create a follow request' do
expect(recipient.requested?(sender)).to be false
end
end
end

Expand Down

0 comments on commit c9b824f

Please sign in to comment.