|
| 1 | +require 'rails_helper' |
| 2 | + |
| 3 | +RSpec.describe UnsuspendAccountService, type: :service do |
| 4 | + shared_examples 'common behavior' do |
| 5 | + let!(:local_follower) { Fabricate(:user, current_sign_in_at: 1.hour.ago).account } |
| 6 | + let!(:list) { Fabricate(:list, account: local_follower) } |
| 7 | + |
| 8 | + subject do |
| 9 | + -> { described_class.new.call(account) } |
| 10 | + end |
| 11 | + |
| 12 | + before do |
| 13 | + allow(FeedManager.instance).to receive(:merge_into_home).and_return(nil) |
| 14 | + allow(FeedManager.instance).to receive(:merge_into_list).and_return(nil) |
| 15 | + |
| 16 | + local_follower.follow!(account) |
| 17 | + list.accounts << account |
| 18 | + |
| 19 | + account.suspend!(origin: :local) |
| 20 | + end |
| 21 | + end |
| 22 | + |
| 23 | + describe 'unsuspending a local account' do |
| 24 | + def match_update_actor_request(req, account) |
| 25 | + json = JSON.parse(req.body) |
| 26 | + actor_id = ActivityPub::TagManager.instance.uri_for(account) |
| 27 | + json['type'] == 'Update' && json['actor'] == actor_id && json['object']['id'] == actor_id && !json['object']['suspended'] |
| 28 | + end |
| 29 | + |
| 30 | + before do |
| 31 | + stub_request(:post, 'https://alice.com/inbox').to_return(status: 201) |
| 32 | + stub_request(:post, 'https://bob.com/inbox').to_return(status: 201) |
| 33 | + end |
| 34 | + |
| 35 | + it 'marks account as unsuspended' do |
| 36 | + is_expected.to change { account.suspended? }.from(true).to(false) |
| 37 | + end |
| 38 | + |
| 39 | + include_examples 'common behavior' do |
| 40 | + let!(:account) { Fabricate(:account) } |
| 41 | + let!(:remote_follower) { Fabricate(:account, uri: 'https://alice.com', inbox_url: 'https://alice.com/inbox', protocol: :activitypub) } |
| 42 | + let!(:remote_reporter) { Fabricate(:account, uri: 'https://bob.com', inbox_url: 'https://bob.com/inbox', protocol: :activitypub) } |
| 43 | + let!(:report) { Fabricate(:report, account: remote_reporter, target_account: account) } |
| 44 | + |
| 45 | + before do |
| 46 | + remote_follower.follow!(account) |
| 47 | + end |
| 48 | + |
| 49 | + it "merges back into local followers' feeds" do |
| 50 | + subject.call |
| 51 | + expect(FeedManager.instance).to have_received(:merge_into_home).with(account, local_follower) |
| 52 | + expect(FeedManager.instance).to have_received(:merge_into_list).with(account, list) |
| 53 | + end |
| 54 | + |
| 55 | + it 'sends an update actor to followers and reporters' do |
| 56 | + subject.call |
| 57 | + expect(a_request(:post, remote_follower.inbox_url).with { |req| match_update_actor_request(req, account) }).to have_been_made.once |
| 58 | + expect(a_request(:post, remote_reporter.inbox_url).with { |req| match_update_actor_request(req, account) }).to have_been_made.once |
| 59 | + end |
| 60 | + end |
| 61 | + end |
| 62 | + |
| 63 | + describe 'unsuspending a remote account' do |
| 64 | + include_examples 'common behavior' do |
| 65 | + let!(:account) { Fabricate(:account, domain: 'bob.com', uri: 'https://bob.com', inbox_url: 'https://bob.com/inbox', protocol: :activitypub) } |
| 66 | + let!(:reslove_account_service) { double } |
| 67 | + |
| 68 | + before do |
| 69 | + allow(ResolveAccountService).to receive(:new).and_return(reslove_account_service) |
| 70 | + end |
| 71 | + |
| 72 | + context 'when the account is not remotely suspended' do |
| 73 | + before do |
| 74 | + allow(reslove_account_service).to receive(:call).with(account).and_return(account) |
| 75 | + end |
| 76 | + |
| 77 | + it 're-fetches the account' do |
| 78 | + subject.call |
| 79 | + expect(reslove_account_service).to have_received(:call).with(account) |
| 80 | + end |
| 81 | + |
| 82 | + it "merges back into local followers' feeds" do |
| 83 | + subject.call |
| 84 | + expect(FeedManager.instance).to have_received(:merge_into_home).with(account, local_follower) |
| 85 | + expect(FeedManager.instance).to have_received(:merge_into_list).with(account, list) |
| 86 | + end |
| 87 | + |
| 88 | + it 'marks account as unsuspended' do |
| 89 | + is_expected.to change { account.suspended? }.from(true).to(false) |
| 90 | + end |
| 91 | + end |
| 92 | + |
| 93 | + context 'when the account is remotely suspended' do |
| 94 | + before do |
| 95 | + allow(reslove_account_service).to receive(:call).with(account) do |account| |
| 96 | + account.suspend!(origin: :remote) |
| 97 | + account |
| 98 | + end |
| 99 | + end |
| 100 | + |
| 101 | + it 're-fetches the account' do |
| 102 | + subject.call |
| 103 | + expect(reslove_account_service).to have_received(:call).with(account) |
| 104 | + end |
| 105 | + |
| 106 | + it "does not merge back into local followers' feeds" do |
| 107 | + subject.call |
| 108 | + expect(FeedManager.instance).to_not have_received(:merge_into_home).with(account, local_follower) |
| 109 | + expect(FeedManager.instance).to_not have_received(:merge_into_list).with(account, list) |
| 110 | + end |
| 111 | + |
| 112 | + it 'does not mark the account as unsuspended' do |
| 113 | + is_expected.not_to change { account.suspended? } |
| 114 | + end |
| 115 | + end |
| 116 | + |
| 117 | + context 'when the account is remotely deleted' do |
| 118 | + before do |
| 119 | + allow(reslove_account_service).to receive(:call).with(account).and_return(nil) |
| 120 | + end |
| 121 | + |
| 122 | + it 're-fetches the account' do |
| 123 | + subject.call |
| 124 | + expect(reslove_account_service).to have_received(:call).with(account) |
| 125 | + end |
| 126 | + |
| 127 | + it "does not merge back into local followers' feeds" do |
| 128 | + subject.call |
| 129 | + expect(FeedManager.instance).to_not have_received(:merge_into_home).with(account, local_follower) |
| 130 | + expect(FeedManager.instance).to_not have_received(:merge_into_list).with(account, list) |
| 131 | + end |
| 132 | + end |
| 133 | + end |
| 134 | + end |
| 135 | +end |
0 commit comments