about summary refs log tree commit diff
path: root/spec/lib/activitypub/activity/add_spec.rb
blob: 9c45e465e4897ae88ad645f112a39bd290bfcbdd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe ActivityPub::Activity::Add do
  let(:sender) { Fabricate(:account, featured_collection_url: 'https://example.com/featured', domain: 'example.com') }
  let(:status) { Fabricate(:status, account: sender, visibility: :private) }

  let(:json) do
    {
      '@context': 'https://www.w3.org/ns/activitystreams',
      id: 'foo',
      type: 'Add',
      actor: ActivityPub::TagManager.instance.uri_for(sender),
      object: ActivityPub::TagManager.instance.uri_for(status),
      target: sender.featured_collection_url,
    }.with_indifferent_access
  end

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

    it 'creates a pin' do
      subject.perform
      expect(sender.pinned?(status)).to be true
    end

    context 'when status was not known before' do
      let(:service_stub) { double }

      let(:json) do
        {
          '@context': 'https://www.w3.org/ns/activitystreams',
          id: 'foo',
          type: 'Add',
          actor: ActivityPub::TagManager.instance.uri_for(sender),
          object: 'https://example.com/unknown',
          target: sender.featured_collection_url,
        }.with_indifferent_access
      end

      before do
        allow(ActivityPub::FetchRemoteStatusService).to receive(:new).and_return(service_stub)
      end

      context 'when there is a local follower' do
        before do
          account = Fabricate(:account)
          account.follow!(sender)
        end

        it 'fetches the status and pins it' do
          allow(service_stub).to receive(:call) do |uri, id: true, on_behalf_of: nil, request_id: nil| # rubocop:disable Lint/UnusedBlockArgument
            expect(uri).to eq 'https://example.com/unknown'
            expect(id).to be true
            expect(on_behalf_of&.following?(sender)).to be true
            status
          end
          subject.perform
          expect(service_stub).to have_received(:call)
          expect(sender.pinned?(status)).to be true
        end
      end

      context 'when there is no local follower' do
        it 'tries to fetch the status' do
          allow(service_stub).to receive(:call) do |uri, id: true, on_behalf_of: nil, request_id: nil| # rubocop:disable Lint/UnusedBlockArgument
            expect(uri).to eq 'https://example.com/unknown'
            expect(id).to be true
            expect(on_behalf_of).to be_nil
            nil
          end
          subject.perform
          expect(service_stub).to have_received(:call)
          expect(sender.pinned?(status)).to be false
        end
      end
    end
  end
end