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

releases_controller_spec.rb « projects « activity_pub « controllers « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4102789ee431ac4582ebcae4eb6130ca842bcb19 (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe ActivityPub::Projects::ReleasesController, feature_category: :groups_and_projects do
  include AccessMatchersForController

  let_it_be(:project) { create(:project, :repository, :public) }
  let_it_be(:private_project) { create(:project, :repository, :private) }
  let_it_be(:developer) { create(:user) }
  let_it_be(:release_1) { create(:release, project: project, released_at: Time.zone.parse('2018-10-18')) }
  let_it_be(:release_2) { create(:release, project: project, released_at: Time.zone.parse('2019-10-19')) }

  let(:request_body) { '' }

  before_all do
    project.add_developer(developer)
  end

  shared_examples 'common access controls' do
    it 'renders a 200' do
      perform_action(verb, action, params, request_body)

      expect(response).to have_gitlab_http_status(:ok)
    end

    context 'when the project is private' do
      let(:project) { private_project }

      context 'when user is not logged in' do
        it 'renders a 404' do
          perform_action(verb, action, params, request_body)

          expect(response).to have_gitlab_http_status(:not_found)
        end
      end

      context 'when user is a developer' do
        before do
          sign_in(developer)
        end

        it 'still renders a 404' do
          perform_action(verb, action, params, request_body)

          expect(response).to have_gitlab_http_status(:not_found)
        end
      end
    end

    context 'when activity_pub feature flag is disabled' do
      before do
        stub_feature_flags(activity_pub: false)
      end

      it 'renders a 404' do
        perform_action(verb, action, params, request_body)

        expect(response).to have_gitlab_http_status(:not_found)
      end
    end

    context 'when activity_pub_project feature flag is disabled' do
      before do
        stub_feature_flags(activity_pub_project: false)
      end

      it 'renders a 404' do
        perform_action(verb, action, params, request_body)

        expect(response).to have_gitlab_http_status(:not_found)
      end
    end
  end

  shared_examples_for 'ActivityPub response' do
    it 'returns an application/activity+json content_type' do
      expect(response.media_type).to eq 'application/activity+json'
    end

    it 'is formated as an ActivityStream document' do
      expect(json_response['@context']).to eq 'https://www.w3.org/ns/activitystreams'
    end
  end

  describe 'GET #index' do
    before do
      perform_action(verb, action, params)
    end

    let(:verb) { :get }
    let(:action) { :index }
    let(:params) { { namespace_id: project.namespace, project_id: project } }

    it_behaves_like 'common access controls'
    it_behaves_like 'ActivityPub response'

    it "returns the project's releases actor profile data" do
      expect(json_response['id']).to include project_releases_path(project)
    end
  end

  describe 'GET #outbox' do
    before do
      perform_action(verb, action, params)
    end

    let(:verb) { :get }
    let(:action) { :outbox }
    let(:params) { { namespace_id: project.namespace, project_id: project, page: page } }

    context 'with no page parameter' do
      let(:page) { nil }

      it_behaves_like 'common access controls'
      it_behaves_like 'ActivityPub response'

      it "returns the project's releases collection index" do
        expect(json_response['id']).to include outbox_project_releases_path(project)
        expect(json_response['totalItems']).to eq 2
      end
    end

    context 'with a page parameter' do
      let(:page) { 1 }

      it_behaves_like 'common access controls'
      it_behaves_like 'ActivityPub response'

      it "returns the project's releases list" do
        expect(json_response['id']).to include outbox_project_releases_path(project, page: 1)

        names = json_response['orderedItems'].map { |release| release['object']['name'] }
        expect(names).to match_array([release_2.name, release_1.name])
      end
    end
  end

  describe 'POST #inbox' do
    before do
      allow(ActivityPub::Projects::ReleasesFollowService).to receive(:new) { follow_service }
      allow(ActivityPub::Projects::ReleasesUnfollowService).to receive(:new) { unfollow_service }
    end

    let(:verb) { :post }
    let(:action) { :inbox }
    let(:params) { { namespace_id: project.namespace, project_id: project } }

    let(:follow_service) do
      instance_double(ActivityPub::Projects::ReleasesFollowService, execute: true, errors: ['an error'])
    end

    let(:unfollow_service) do
      instance_double(ActivityPub::Projects::ReleasesUnfollowService, execute: true, errors: ['an error'])
    end

    context 'with a follow activity' do
      before do
        perform_action(verb, action, params, request_body)
      end

      let(:request_body) do
        {
          "@context": "https://www.w3.org/ns/activitystreams",
          id: "http://localhost:3001/6233e6c2-d285-4aa4-bd71-ddf1824d87f8",
          type: "Follow",
          actor: "http://localhost:3001/users/admin",
          object: "http://127.0.0.1:3000/flightjs/Flight/-/releases"
        }.to_json
      end

      it_behaves_like 'common access controls'

      context 'with successful subscription initialization' do
        it 'calls the subscription service' do
          expect(follow_service).to have_received :execute
        end

        it 'returns a successful response' do
          expect(json_response['success']).to be_truthy
        end

        it 'does not fill any error' do
          expect(json_response).not_to have_key 'errors'
        end
      end

      context 'with unsuccessful subscription initialization' do
        let(:follow_service) do
          instance_double(ActivityPub::Projects::ReleasesFollowService, execute: false, errors: ['an error'])
        end

        it 'calls the subscription service' do
          expect(follow_service).to have_received :execute
        end

        it 'returns a successful response' do
          expect(json_response['success']).to be_falsey
        end

        it 'fills an error' do
          expect(json_response['errors']).to include 'an error'
        end
      end
    end

    context 'with an unfollow activity' do
      before do
        perform_action(verb, action, params, request_body)
      end

      let(:unfollow_service) do
        instance_double(ActivityPub::Projects::ReleasesSubscriptionService, execute: true, errors: ['an error'])
      end

      let(:request_body) do
        {
          "@context": "https://www.w3.org/ns/activitystreams",
          id: "http://localhost:3001/users/admin#follows/8/undo",
          type: "Undo",
          actor: "http://localhost:3001/users/admin",
          object: {
            id: "http://localhost:3001/d4358269-71a9-4746-ac16-9a909f12ee5b",
            type: "Follow",
            actor: "http://localhost:3001/users/admin",
            object: "http://127.0.0.1:3000/flightjs/Flight/-/releases"
          }
        }.to_json
      end

      it_behaves_like 'common access controls'

      context 'with successful unfollow' do
        it 'calls the subscription service' do
          expect(unfollow_service).to have_received :execute
        end

        it 'returns a successful response' do
          expect(json_response['success']).to be_truthy
        end

        it 'does not fill any error' do
          expect(json_response).not_to have_key 'errors'
        end
      end

      context 'with unsuccessful unfollow' do
        let(:unfollow_service) do
          instance_double(ActivityPub::Projects::ReleasesUnfollowService, execute: false, errors: ['an error'])
        end

        it 'calls the subscription service' do
          expect(unfollow_service).to have_received :execute
        end

        it 'returns a successful response' do
          expect(json_response['success']).to be_falsey
        end

        it 'fills an error' do
          expect(json_response['errors']).to include 'an error'
        end
      end
    end

    context 'with an unknown activity' do
      before do
        perform_action(verb, action, params, request_body)
      end

      let(:request_body) do
        {
          "@context": "https://www.w3.org/ns/activitystreams",
          id: "http://localhost:3001/6233e6c2-d285-4aa4-bd71-ddf1824d87f8",
          type: "Like",
          actor: "http://localhost:3001/users/admin",
          object: "http://127.0.0.1:3000/flightjs/Flight/-/releases"
        }.to_json
      end

      it 'does not call the subscription service' do
        expect(follow_service).not_to have_received :execute
        expect(unfollow_service).not_to have_received :execute
      end

      it 'returns a successful response' do
        expect(json_response['success']).to be_truthy
      end

      it 'does not fill any error' do
        expect(json_response).not_to have_key 'errors'
      end
    end

    context 'with no activity' do
      it 'renders a 422' do
        perform_action(verb, action, params, request_body)
        expect(response).to have_gitlab_http_status(:unprocessable_entity)
      end
    end
  end
end

def perform_action(verb, action, params, body = nil)
  send(verb, action, params: params, body: body)
end