Welcome to mirror list, hosted at ThFree Co, Russian Federation.

releases_follow_service_spec.rb « projects « activity_pub « services « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6d0d400b9c66a66d09f1e3a08dced1922200ae03 (plain)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe ActivityPub::Projects::ReleasesFollowService, feature_category: :release_orchestration do
  let_it_be(:project) { create(:project, :public) }
  let_it_be_with_reload(:existing_subscription) { create(:activity_pub_releases_subscription, project: project) }

  describe '#execute' do
    let(:service) { described_class.new(project, payload) }
    let(:payload) { nil }

    before do
      allow(ActivityPub::Projects::ReleasesSubscriptionWorker).to receive(:perform_async)
    end

    context 'with a valid payload' do
      let(:payload) do
        {
          '@context': 'https://www.w3.org/ns/activitystreams',
          id: 'https://example.com/new-actor#follow-1',
          type: 'Follow',
          actor: actor,
          object: 'https://localhost/our/project/-/releases'
        }.with_indifferent_access
      end

      let(:actor) { 'https://example.com/new-actor' }

      context 'when there is no subscription for that actor' do
        before do
          allow(ActivityPub::ReleasesSubscription).to receive(:find_by_project_and_subscriber).and_return(nil)
        end

        it 'sets the subscriber url' do
          service.execute
          expect(ActivityPub::ReleasesSubscription.last.subscriber_url).to eq 'https://example.com/new-actor'
        end

        it 'sets the payload' do
          service.execute
          expect(ActivityPub::ReleasesSubscription.last.payload).to eq payload
        end

        it 'sets the project' do
          service.execute
          expect(ActivityPub::ReleasesSubscription.last.project_id).to eq project.id
        end

        it 'saves the subscription' do
          expect { service.execute }.to change { ActivityPub::ReleasesSubscription.count }.by(1)
        end

        it 'queues the subscription job' do
          service.execute
          expect(ActivityPub::Projects::ReleasesSubscriptionWorker).to have_received(:perform_async)
        end

        it 'returns true' do
          expect(service.execute).to be_truthy
        end
      end

      context 'when there is already a subscription for that actor' do
        before do
          allow(ActivityPub::ReleasesSubscription).to receive(:find_by_project_and_subscriber) { existing_subscription }
        end

        it 'does not save the subscription' do
          expect { service.execute }.not_to change { ActivityPub::ReleasesSubscription.count }
        end

        it 'does not queue the subscription job' do
          service.execute
          expect(ActivityPub::Projects::ReleasesSubscriptionWorker).not_to have_received(:perform_async)
        end

        it 'returns true' do
          expect(service.execute).to be_truthy
        end
      end
    end

    shared_examples 'invalid follow request' do
      it 'does not save the subscription' do
        expect { service.execute }.not_to change { ActivityPub::ReleasesSubscription.count }
      end

      it 'does not queue the subscription job' do
        service.execute
        expect(ActivityPub::Projects::ReleasesSubscriptionWorker).not_to have_received(:perform_async)
      end

      it 'sets an error' do
        service.execute
        expect(service.errors).not_to be_empty
      end

      it 'returns false' do
        expect(service.execute).to be_falsey
      end
    end

    context 'when actor is missing' do
      let(:payload) do
        {
          '@context': 'https://www.w3.org/ns/activitystreams',
          id: 'https://example.com/new-actor',
          type: 'Follow',
          object: 'https://localhost/our/project/-/releases'
        }.with_indifferent_access
      end

      it_behaves_like 'invalid follow request'
    end

    context 'when actor is an object with no id attribute' do
      let(:payload) do
        {
          '@context': 'https://www.w3.org/ns/activitystreams',
          id: 'https://example.com/new-actor',
          type: 'Follow',
          actor: { type: 'Person' },
          object: 'https://localhost/our/project/-/releases'
        }.with_indifferent_access
      end

      it_behaves_like 'invalid follow request'
    end

    context 'when actor is neither a string nor an object' do
      let(:payload) do
        {
          '@context': 'https://www.w3.org/ns/activitystreams',
          id: 'https://example.com/new-actor',
          type: 'Follow',
          actor: 27.13,
          object: 'https://localhost/our/project/-/releases'
        }.with_indifferent_access
      end

      it_behaves_like 'invalid follow request'
    end
  end
end