discussions_controller_spec.rb 5.78 KB
Newer Older
1
2
# frozen_string_literal: true

3
4
5
require 'spec_helper'

describe Projects::DiscussionsController do
6
7
8
  let(:user) { create(:user) }
  let(:merge_request) { create(:merge_request) }
  let(:project) { merge_request.source_project }
9
  let(:note) { create(:discussion_note_on_merge_request, noteable: merge_request, project: project) }
Douwe Maan's avatar
Douwe Maan committed
10
11
12
13
14
15
16
17
18
19
  let(:discussion) { note.discussion }

  let(:request_params) do
    {
      namespace_id: project.namespace,
      project_id: project,
      merge_request_id: merge_request,
      id: note.discussion_id
    }
  end
20

21
22
23
24
25
26
27
  describe 'GET show' do
    before do
      sign_in user
    end

    context 'when user is not authorized to read the MR' do
      it 'returns 404' do
blackst0ne's avatar
blackst0ne committed
28
        get :show, params: request_params, session: { format: :json }
29
30
31
32
33
34
35
36
37
38
39

        expect(response).to have_gitlab_http_status(404)
      end
    end

    context 'when user is authorized to read the MR' do
      before do
        project.add_reporter(user)
      end

      it 'returns status 200' do
blackst0ne's avatar
blackst0ne committed
40
        get :show, params: request_params, session: { format: :json }
41
42
43
44
45
46
47

        expect(response).to have_gitlab_http_status(200)
      end

      it 'returns status 404 if MR does not exists' do
        merge_request.destroy!

blackst0ne's avatar
blackst0ne committed
48
        get :show, params: request_params, session: { format: :json }
49
50
51
52
53
54
55
56
57
58
59
60

        expect(response).to have_gitlab_http_status(404)
      end
    end

    context 'when user is authorized but note is LegacyDiffNote' do
      before do
        project.add_developer(user)
        note.update!(type: 'LegacyDiffNote')
      end

      it 'returns status 200' do
blackst0ne's avatar
blackst0ne committed
61
        get :show, params: request_params, session: { format: :json }
62
63
64
65
66
67

        expect(response).to have_gitlab_http_status(200)
      end
    end
  end

68
69
70
71
72
73
74
  describe 'POST resolve' do
    before do
      sign_in user
    end

    context "when the user is not authorized to resolve the discussion" do
      it "returns status 404" do
blackst0ne's avatar
blackst0ne committed
75
        post :resolve, params: request_params
76

77
        expect(response).to have_gitlab_http_status(404)
78
79
80
81
82
      end
    end

    context "when the user is authorized to resolve the discussion" do
      before do
83
        project.add_developer(user)
84
85
86
87
88
89
90
91
      end

      context "when the discussion is not resolvable" do
        before do
          note.update(system: true)
        end

        it "returns status 404" do
blackst0ne's avatar
blackst0ne committed
92
          post :resolve, params: request_params
93

94
          expect(response).to have_gitlab_http_status(404)
95
96
97
98
99
        end
      end

      context "when the discussion is resolvable" do
        it "resolves the discussion" do
blackst0ne's avatar
blackst0ne committed
100
          post :resolve, params: request_params
101

Douwe Maan's avatar
Douwe Maan committed
102
103
          expect(note.reload.discussion.resolved?).to be true
          expect(note.reload.discussion.resolved_by).to eq(user)
104
105
        end

Douwe Maan's avatar
Douwe Maan committed
106
        it "sends notifications if all discussions are resolved" do
107
108
109
          expect_next_instance_of(MergeRequests::ResolvedDiscussionNotificationService) do |instance|
            expect(instance).to receive(:execute).with(merge_request)
          end
110

blackst0ne's avatar
blackst0ne committed
111
          post :resolve, params: request_params
112
113
114
        end

        it "returns the name of the resolving user" do
blackst0ne's avatar
blackst0ne committed
115
          post :resolve, params: request_params
116

117
          expect(json_response['resolved_by']['name']).to eq(user.name)
118
119
120
        end

        it "returns status 200" do
blackst0ne's avatar
blackst0ne committed
121
          post :resolve, params: request_params
122

123
          expect(response).to have_gitlab_http_status(200)
124
        end
125

Felipe Artur's avatar
Felipe Artur committed
126
        it "renders discussion with serializer" do
127
128
129
130
          expect_next_instance_of(DiscussionSerializer) do |instance|
            expect(instance).to receive(:represent)
              .with(instance_of(Discussion), { context: instance_of(described_class), render_truncated_diff_lines: true })
          end
131

blackst0ne's avatar
blackst0ne committed
132
          post :resolve, params: request_params
Felipe Artur's avatar
Felipe Artur committed
133
        end
134

Felipe Artur's avatar
Felipe Artur committed
135
136
137
138
139
        context 'diff discussion' do
          let(:note) { create(:diff_note_on_merge_request, noteable: merge_request, project: project) }
          let(:discussion) { note.discussion }

          it "returns truncated diff lines" do
blackst0ne's avatar
blackst0ne committed
140
            post :resolve, params: request_params
Felipe Artur's avatar
Felipe Artur committed
141

142
            expect(json_response['truncated_diff_lines']).to be_present
143
144
          end
        end
145
146
147
148
149
150
151
      end
    end
  end

  describe 'DELETE unresolve' do
    before do
      sign_in user
Douwe Maan's avatar
Douwe Maan committed
152
153

      note.discussion.resolve!(user)
154
155
156
157
    end

    context "when the user is not authorized to resolve the discussion" do
      it "returns status 404" do
blackst0ne's avatar
blackst0ne committed
158
        delete :unresolve, params: request_params
159

160
        expect(response).to have_gitlab_http_status(404)
161
162
163
164
165
      end
    end

    context "when the user is authorized to resolve the discussion" do
      before do
166
        project.add_developer(user)
167
168
169
170
171
172
173
174
      end

      context "when the discussion is not resolvable" do
        before do
          note.update(system: true)
        end

        it "returns status 404" do
blackst0ne's avatar
blackst0ne committed
175
          delete :unresolve, params: request_params
176

177
          expect(response).to have_gitlab_http_status(404)
178
179
180
181
182
        end
      end

      context "when the discussion is resolvable" do
        it "unresolves the discussion" do
blackst0ne's avatar
blackst0ne committed
183
          delete :unresolve, params: request_params
184

Douwe Maan's avatar
Douwe Maan committed
185
          expect(note.reload.discussion.resolved?).to be false
186
187
188
        end

        it "returns status 200" do
blackst0ne's avatar
blackst0ne committed
189
          delete :unresolve, params: request_params
190

191
          expect(response).to have_gitlab_http_status(200)
192
        end
193
194
195

        context "when vue_mr_discussions cookie is present" do
          before do
196
            cookies[:vue_mr_discussions] = 'true'
197
198
199
          end

          it "renders discussion with serializer" do
200
201
202
203
            expect_next_instance_of(DiscussionSerializer) do |instance|
              expect(instance).to receive(:represent)
                .with(instance_of(Discussion), { context: instance_of(described_class), render_truncated_diff_lines: true })
            end
204

blackst0ne's avatar
blackst0ne committed
205
            delete :unresolve, params: request_params
206
207
          end
        end
208
209
210
211
      end
    end
  end
end