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

draft_notes_spec.rb « api « requests « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3911bb8bc0061022e0b255406406e0bd2076137d (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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe API::DraftNotes, feature_category: :code_review_workflow do
  let_it_be(:user) { create(:user) }
  let_it_be(:user_2) { create(:user) }
  let_it_be(:project) { create(:project, :public) }
  let_it_be(:merge_request) { create(:merge_request, source_project: project, target_project: project, author: user) }

  let_it_be(:private_project) { create(:project, :private) }
  let_it_be(:private_merge_request) do
    create(:merge_request, source_project: private_project, target_project: private_project)
  end

  let_it_be(:merge_request_note) { create(:note, noteable: merge_request, project: project, author: user) }
  let!(:draft_note_by_current_user) { create(:draft_note, merge_request: merge_request, author: user) }
  let!(:draft_note_by_random_user) { create(:draft_note, merge_request: merge_request) }

  let_it_be(:base_url) { "/projects/#{project.id}/merge_requests/#{merge_request.iid}/draft_notes" }

  before do
    project.add_developer(user)
  end

  describe "Get a list of merge request draft notes" do
    it "returns 200 OK status" do
      get api(base_url, user)

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

    it "returns only draft notes authored by the current user" do
      get api(base_url, user)

      draft_note_ids = json_response.pluck("id")

      expect(draft_note_ids).to include(draft_note_by_current_user.id)
      expect(draft_note_ids).not_to include(draft_note_by_random_user.id)
      expect(draft_note_ids).not_to include(merge_request_note.id)
    end
  end

  describe "Get a single draft note" do
    context "when requesting an existing draft note by the user" do
      before do
        get api(
          "#{base_url}/#{draft_note_by_current_user.id}",
          user
        )
      end

      it "returns 200 OK status" do
        expect(response).to have_gitlab_http_status(:ok)
      end

      it "returns the requested draft note" do
        expect(json_response["id"]).to eq(draft_note_by_current_user.id)
      end

      context "when requesting a non-existent draft note" do
        it "returns a 404 Not Found response" do
          get api(
            "#{base_url}/#{DraftNote.last.id + 1}",
            user
          )

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

      context "when requesting an existing draft note by another user" do
        it "returns a 404 Not Found response" do
          get api(
            "#{base_url}/#{draft_note_by_random_user.id}",
            user
          )

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

  describe "delete a draft note" do
    context "when deleting an existing draft note by the user" do
      let!(:deleted_draft_note_id) { draft_note_by_current_user.id }

      before do
        delete api(
          "#{base_url}/#{draft_note_by_current_user.id}",
          user
        )
      end

      it "returns 204 No Content status" do
        expect(response).to have_gitlab_http_status(:no_content)
      end

      it "deletes the specified draft note" do
        expect(DraftNote.exists?(deleted_draft_note_id)).to eq(false)
      end
    end

    context "when deleting a non-existent draft note" do
      it "returns a 404 Not Found" do
        delete api(
          "#{base_url}/#{non_existing_record_id}",
          user
        )

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

    context "when deleting a draft note by a different user" do
      it "returns a 404 Not Found" do
        delete api(
          "#{base_url}/#{draft_note_by_random_user.id}",
          user
        )

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

  def create_draft_note(params = {}, url = base_url)
    post api(url, user), params: params
  end

  describe "Create a new draft note" do
    let(:basic_create_params) do
      {
        note: "Example body string"
      }
    end

    context "when creating a new draft note" do
      context "with required params" do
        it "returns 201 Created status" do
          create_draft_note(basic_create_params)

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

        it "creates a new draft note with the submitted params" do
          expect { create_draft_note(basic_create_params) }.to change { DraftNote.count }.by(1)

          expect(json_response["note"]).to eq(basic_create_params[:note])
          expect(json_response["merge_request_id"]).to eq(merge_request.id)
          expect(json_response["author_id"]).to eq(user.id)
        end
      end

      context "without required params" do
        it "returns 400 Bad Request status" do
          create_draft_note({})

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

      context "when providing a non-existing commit_id" do
        it "returns a 400 Bad Request" do
          create_draft_note(
            basic_create_params.merge(
              commit_id: 'bad SHA'
            )
          )

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

      context "when targeting a merge request the user doesn't have access to" do
        it "returns a 404 Not Found" do
          create_draft_note(
            basic_create_params,
            "/projects/#{private_project.id}/merge_requests/#{private_merge_request.iid}"
          )

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

      context "when attempting to resolve a disscussion" do
        context "when providing a non-existant ID" do
          it "returns a 400 Bad Request" do
            create_draft_note(
              basic_create_params.merge(
                resolve_discussion: true,
                in_reply_to_discussion_id: non_existing_record_id
              )
            )

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

        context "when not providing an ID" do
          it "returns a 400 Bad Request" do
            create_draft_note(basic_create_params.merge(resolve_discussion: true))

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

          it "returns a validation error message" do
            create_draft_note(basic_create_params.merge(resolve_discussion: true))

            expect(response.body)
              .to eq("{\"message\":{\"base\":[\"User is not allowed to resolve thread\"]}}")
          end
        end
      end
    end
  end

  def update_draft_note(params = {}, url = base_url)
    put api("#{url}/#{draft_note_by_current_user.id}", user), params: params
  end

  describe "Update a draft note" do
    let(:basic_update_params) do
      {
        note: "Example updated body string"
      }
    end

    context "when updating an existing draft note" do
      context "with required params" do
        it "returns 200 Success status" do
          update_draft_note(basic_update_params)

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

        it "updates draft note with the new content" do
          update_draft_note(basic_update_params)

          expect(json_response["note"]).to eq(basic_update_params[:note])
        end
      end

      context "without including an update to the note body" do
        it "returns the draft note with no changes" do
          expect { update_draft_note({}) }
            .not_to change { draft_note_by_current_user.note }
        end
      end

      context "when updating a non-existent draft note" do
        it "returns a 404 Not Found" do
          put api("#{base_url}/#{non_existing_record_id}", user), params: basic_update_params

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

      context "when updating a draft note by a different user" do
        it "returns a 404 Not Found" do
          put api("#{base_url}/#{draft_note_by_random_user.id}", user), params: basic_update_params

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

  describe "Publishing a draft note" do
    let(:publish_draft_note) do
      put api(
        "#{base_url}/#{draft_note_by_current_user.id}/publish",
        user
      )
    end

    context "when publishing an existing draft note by the user" do
      it "returns 204 No Content status" do
        publish_draft_note

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

      it "publishes the specified draft note" do
        expect { publish_draft_note }.to change { Note.count }.by(1)
        expect(DraftNote.exists?(draft_note_by_current_user.id)).to eq(false)
      end
    end

    context "when publishing a non-existent draft note" do
      it "returns a 404 Not Found" do
        put api(
          "#{base_url}/#{non_existing_record_id}/publish",
          user
        )

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

    context "when publishing a draft note by a different user" do
      it "returns a 404 Not Found" do
        put api(
          "#{base_url}/#{draft_note_by_random_user.id}/publish",
          user
        )

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

    context "when DraftNotes::PublishService returns a non-success" do
      it "returns an :internal_server_error and a message" do
        expect_next_instance_of(DraftNotes::PublishService) do |instance|
          expect(instance).to receive(:execute).and_return({ status: :failure, message: "Error message" })
        end

        publish_draft_note

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

  describe "Bulk publishing draft notes" do
    let(:bulk_publish_draft_notes) do
      post api(
        "#{base_url}/bulk_publish",
        user
      )
    end

    let!(:draft_note_by_current_user_2) { create(:draft_note, merge_request: merge_request, author: user) }

    context "when publishing an existing draft note by the user" do
      it "returns 204 No Content status" do
        bulk_publish_draft_notes

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

      it "publishes the specified draft notes" do
        expect { bulk_publish_draft_notes }.to change { Note.count }.by(2)
        expect(DraftNote.exists?(draft_note_by_current_user.id)).to eq(false)
        expect(DraftNote.exists?(draft_note_by_current_user_2.id)).to eq(false)
      end

      it "only publishes the user's draft notes" do
        bulk_publish_draft_notes

        expect(DraftNote.exists?(draft_note_by_random_user.id)).to eq(true)
      end
    end

    context "when DraftNotes::PublishService returns a non-success" do
      it "returns an :internal_server_error and a message" do
        expect_next_instance_of(DraftNotes::PublishService) do |instance|
          expect(instance).to receive(:execute).and_return({ status: :failure, message: "Error message" })
        end

        bulk_publish_draft_notes

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