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: e8f519e004df816766a20df4e02c126604cc3b0b (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
# 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(: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(:api_stub) { "/projects/#{project.id}/merge_requests/#{merge_request.iid}" }

  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("/projects/#{project.id}/merge_requests/#{merge_request.iid}/draft_notes", user)

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

    it "returns only draft notes authored by the current user" do
      get api("/projects/#{project.id}/merge_requests/#{merge_request.iid}/draft_notes", 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(
          "/projects/#{project.id}/merge_requests/#{merge_request.iid}/draft_notes/#{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(
            "/projects/#{project.id}/merge_requests/#{merge_request.iid}/draft_notes/#{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(
            "/projects/#{project.id}/merge_requests/#{merge_request.iid}/draft_notes/#{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(
          "/projects/#{project.id}/merge_requests/#{merge_request.iid}/draft_notes/#{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(
          "/projects/#{project.id}/merge_requests/#{merge_request.iid}/draft_notes/#{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(
          "/projects/#{project.id}/merge_requests/#{merge_request.iid}/draft_notes/#{draft_note_by_random_user.id}",
          user
        )

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

  describe "Publishing a draft note" do
    let(:publish_draft_note) do
      put api(
        "#{api_stub}/draft_notes/#{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(
          "#{api_stub}/draft_notes/#{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(
          "#{api_stub}/draft_notes/#{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
end