merge_requests_spec.rb 31.2 KB
Newer Older
1 2
require "spec_helper"

3
describe API::MergeRequests, api: true  do
4
  include ApiHelpers
5 6 7
  let(:base_time)   { Time.now }
  let(:user)        { create(:user) }
  let(:admin)       { create(:user, :admin) }
8
  let(:non_member)  { create(:user) }
9 10 11 12
  let!(:project)    { create(:project, :public, :repository, creator: user, namespace: user.namespace) }
  let!(:merge_request) { create(:merge_request, :simple, author: user, assignee: user, source_project: project, title: "Test", created_at: base_time) }
  let!(:merge_request_closed) { create(:merge_request, state: "closed", author: user, assignee: user, source_project: project, title: "Closed test", created_at: base_time + 1.second) }
  let!(:merge_request_merged) { create(:merge_request, state: "merged", author: user, assignee: user, source_project: project, title: "Merged test", created_at: base_time + 2.seconds, merge_commit_sha: '9999999999999999999999999999999999999999') }
13
  let(:milestone)   { create(:milestone, title: '1.0.0', project: project) }
14 15

  before do
16
    project.team << [user, :reporter]
17
  end
18 19 20

  describe "GET /projects/:id/merge_requests" do
    context "when unauthenticated" do
21
      it "returns authentication error" do
Hiroyuki Sato's avatar
Hiroyuki Sato committed
22
        get api("/projects/#{project.id}/merge_requests")
23
        expect(response).to have_http_status(401)
24 25 26 27
      end
    end

    context "when authenticated" do
28
      it "returns an array of all merge_requests" do
Hiroyuki Sato's avatar
Hiroyuki Sato committed
29
        get api("/projects/#{project.id}/merge_requests", user)
30

31
        expect(response).to have_http_status(200)
32
        expect(response).to include_pagination_headers
33 34 35
        expect(json_response).to be_an Array
        expect(json_response.length).to eq(3)
        expect(json_response.last['title']).to eq(merge_request.title)
36
        expect(json_response.last).to have_key('web_url')
37 38 39 40 41 42 43
        expect(json_response.last['sha']).to eq(merge_request.diff_head_sha)
        expect(json_response.last['merge_commit_sha']).to be_nil
        expect(json_response.last['merge_commit_sha']).to eq(merge_request.merge_commit_sha)
        expect(json_response.first['title']).to eq(merge_request_merged.title)
        expect(json_response.first['sha']).to eq(merge_request_merged.diff_head_sha)
        expect(json_response.first['merge_commit_sha']).not_to be_nil
        expect(json_response.first['merge_commit_sha']).to eq(merge_request_merged.merge_commit_sha)
44
      end
45

46
      it "returns an array of all merge_requests" do
47
        get api("/projects/#{project.id}/merge_requests?state", user)
48

49
        expect(response).to have_http_status(200)
50
        expect(response).to include_pagination_headers
51 52 53
        expect(json_response).to be_an Array
        expect(json_response.length).to eq(3)
        expect(json_response.last['title']).to eq(merge_request.title)
54
      end
55

56
      it "returns an array of open merge_requests" do
57
        get api("/projects/#{project.id}/merge_requests?state=opened", user)
58

59
        expect(response).to have_http_status(200)
60
        expect(response).to include_pagination_headers
61 62 63
        expect(json_response).to be_an Array
        expect(json_response.length).to eq(1)
        expect(json_response.last['title']).to eq(merge_request.title)
64
      end
65

66
      it "returns an array of closed merge_requests" do
67
        get api("/projects/#{project.id}/merge_requests?state=closed", user)
68

69
        expect(response).to have_http_status(200)
70
        expect(response).to include_pagination_headers
71
        expect(json_response).to be_an Array
72 73
        expect(json_response.length).to eq(1)
        expect(json_response.first['title']).to eq(merge_request_closed.title)
74
      end
75

76
      it "returns an array of merged merge_requests" do
77
        get api("/projects/#{project.id}/merge_requests?state=merged", user)
78

79
        expect(response).to have_http_status(200)
80
        expect(response).to include_pagination_headers
81 82 83
        expect(json_response).to be_an Array
        expect(json_response.length).to eq(1)
        expect(json_response.first['title']).to eq(merge_request_merged.title)
84
      end
85

86 87 88 89 90 91 92 93 94 95
      it 'returns merge_request by "iids" array' do
        get api("/projects/#{project.id}/merge_requests", user), iids: [merge_request.iid, merge_request_closed.iid]

        expect(response).to have_http_status(200)
        expect(json_response).to be_an Array
        expect(json_response.length).to eq(2)
        expect(json_response.first['title']).to eq merge_request_closed.title
        expect(json_response.first['id']).to eq merge_request_closed.id
      end

96 97 98 99 100 101
      context "with ordering" do
        before do
          @mr_later = mr_with_later_created_and_updated_at_time
          @mr_earlier = mr_with_earlier_created_and_updated_at_time
        end

102
        it "returns an array of merge_requests in ascending order" do
103
          get api("/projects/#{project.id}/merge_requests?sort=asc", user)
104

105
          expect(response).to have_http_status(200)
106
          expect(response).to include_pagination_headers
107 108
          expect(json_response).to be_an Array
          expect(json_response.length).to eq(3)
109 110
          response_dates = json_response.map{ |merge_request| merge_request['created_at'] }
          expect(response_dates).to eq(response_dates.sort)
111
        end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
112

113
        it "returns an array of merge_requests in descending order" do
114
          get api("/projects/#{project.id}/merge_requests?sort=desc", user)
115

116
          expect(response).to have_http_status(200)
117
          expect(response).to include_pagination_headers
118 119
          expect(json_response).to be_an Array
          expect(json_response.length).to eq(3)
120 121
          response_dates = json_response.map{ |merge_request| merge_request['created_at'] }
          expect(response_dates).to eq(response_dates.sort.reverse)
122
        end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
123

124
        it "returns an array of merge_requests ordered by updated_at" do
125
          get api("/projects/#{project.id}/merge_requests?order_by=updated_at", user)
126

127
          expect(response).to have_http_status(200)
128
          expect(response).to include_pagination_headers
129 130
          expect(json_response).to be_an Array
          expect(json_response.length).to eq(3)
131 132
          response_dates = json_response.map{ |merge_request| merge_request['updated_at'] }
          expect(response_dates).to eq(response_dates.sort.reverse)
133
        end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
134

135
        it "returns an array of merge_requests ordered by created_at" do
136
          get api("/projects/#{project.id}/merge_requests?order_by=created_at&sort=asc", user)
137

138
          expect(response).to have_http_status(200)
139
          expect(response).to include_pagination_headers
140 141
          expect(json_response).to be_an Array
          expect(json_response.length).to eq(3)
142 143
          response_dates = json_response.map{ |merge_request| merge_request['created_at'] }
          expect(response_dates).to eq(response_dates.sort)
144
        end
145
      end
146 147 148
    end
  end

149
  describe "GET /projects/:id/merge_requests/:merge_request_id" do
150 151 152
    it 'exposes known attributes' do
      get api("/projects/#{project.id}/merge_requests/#{merge_request.id}", user)

153
      expect(response).to have_http_status(200)
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
      expect(json_response['id']).to eq(merge_request.id)
      expect(json_response['iid']).to eq(merge_request.iid)
      expect(json_response['project_id']).to eq(merge_request.project.id)
      expect(json_response['title']).to eq(merge_request.title)
      expect(json_response['description']).to eq(merge_request.description)
      expect(json_response['state']).to eq(merge_request.state)
      expect(json_response['created_at']).to be_present
      expect(json_response['updated_at']).to be_present
      expect(json_response['labels']).to eq(merge_request.label_names)
      expect(json_response['milestone']).to be_nil
      expect(json_response['assignee']).to be_a Hash
      expect(json_response['author']).to be_a Hash
      expect(json_response['target_branch']).to eq(merge_request.target_branch)
      expect(json_response['source_branch']).to eq(merge_request.source_branch)
      expect(json_response['upvotes']).to eq(0)
      expect(json_response['downvotes']).to eq(0)
      expect(json_response['source_project_id']).to eq(merge_request.source_project.id)
      expect(json_response['target_project_id']).to eq(merge_request.target_project.id)
      expect(json_response['work_in_progress']).to be_falsy
173
      expect(json_response['merge_when_pipeline_succeeds']).to be_falsy
174
      expect(json_response['merge_status']).to eq('can_be_merged')
175 176
      expect(json_response['should_close_merge_request']).to be_falsy
      expect(json_response['force_close_merge_request']).to be_falsy
177 178
    end

179
    it "returns merge_request" do
180
      get api("/projects/#{project.id}/merge_requests/#{merge_request.id}", user)
181
      expect(response).to have_http_status(200)
182 183
      expect(json_response['title']).to eq(merge_request.title)
      expect(json_response['iid']).to eq(merge_request.iid)
184
      expect(json_response['work_in_progress']).to eq(false)
185
      expect(json_response['merge_status']).to eq('can_be_merged')
186 187
      expect(json_response['should_close_merge_request']).to be_falsy
      expect(json_response['force_close_merge_request']).to be_falsy
188
    end
189

190
    it "returns a 404 error if merge_request_id not found" do
191
      get api("/projects/#{project.id}/merge_requests/999", user)
192
      expect(response).to have_http_status(404)
193
    end
194 195 196 197

    context 'Work in Progress' do
      let!(:merge_request_wip) { create(:merge_request, author: user, assignee: user, source_project: project, target_project: project, title: "WIP: Test", created_at: base_time + 1.second) }

198
      it "returns merge_request" do
199
        get api("/projects/#{project.id}/merge_requests/#{merge_request_wip.id}", user)
200
        expect(response).to have_http_status(200)
201 202 203
        expect(json_response['work_in_progress']).to eq(true)
      end
    end
204 205
  end

206
  describe 'GET /projects/:id/merge_requests/:merge_request_id/commits' do
Valery Sizov's avatar
Valery Sizov committed
207 208 209 210 211
    it 'returns a 200 when merge request is valid' do
      get api("/projects/#{project.id}/merge_requests/#{merge_request.id}/commits", user)
      commit = merge_request.commits.first

      expect(response.status).to eq 200
212 213
      expect(response).to include_pagination_headers
      expect(json_response).to be_an Array
Valery Sizov's avatar
Valery Sizov committed
214 215 216
      expect(json_response.size).to eq(merge_request.commits.size)
      expect(json_response.first['id']).to eq(commit.id)
      expect(json_response.first['title']).to eq(commit.title)
217 218 219
    end

    it 'returns a 404 when merge_request_id not found' do
220
      get api("/projects/#{project.id}/merge_requests/999/commits", user)
221
      expect(response).to have_http_status(404)
222 223 224
    end
  end

225
  describe 'GET /projects/:id/merge_requests/:merge_request_id/changes' do
226
    it 'returns the change information of the merge_request' do
227
      get api("/projects/#{project.id}/merge_requests/#{merge_request.id}/changes", user)
228

229 230 231 232 233
      expect(response.status).to eq 200
      expect(json_response['changes'].size).to eq(merge_request.diffs.size)
    end

    it 'returns a 404 when merge_request_id not found' do
234
      get api("/projects/#{project.id}/merge_requests/999/changes", user)
235
      expect(response).to have_http_status(404)
236 237 238
    end
  end

239
  describe "POST /projects/:id/merge_requests" do
240
    context 'between branches projects' do
241
      it "returns merge_request" do
Hiroyuki Sato's avatar
Hiroyuki Sato committed
242
        post api("/projects/#{project.id}/merge_requests", user),
243
             title: 'Test merge_request',
244
             source_branch: 'feature_conflict',
245 246
             target_branch: 'master',
             author: user,
247
             labels: 'label, label2',
248 249 250
             milestone_id: milestone.id,
             remove_source_branch: true

251
        expect(response).to have_http_status(201)
252
        expect(json_response['title']).to eq('Test merge_request')
Douwe Maan's avatar
Douwe Maan committed
253
        expect(json_response['labels']).to eq(%w(label label2))
254
        expect(json_response['milestone']['id']).to eq(milestone.id)
255
        expect(json_response['force_remove_source_branch']).to be_truthy
256
      end
257

258
      it "returns 422 when source_branch equals target_branch" do
Hiroyuki Sato's avatar
Hiroyuki Sato committed
259
        post api("/projects/#{project.id}/merge_requests", user),
260
        title: "Test merge_request", source_branch: "master", target_branch: "master", author: user
261
        expect(response).to have_http_status(422)
262
      end
263

264
      it "returns 400 when source_branch is missing" do
Hiroyuki Sato's avatar
Hiroyuki Sato committed
265
        post api("/projects/#{project.id}/merge_requests", user),
266
        title: "Test merge_request", target_branch: "master", author: user
267
        expect(response).to have_http_status(400)
268
      end
269

270
      it "returns 400 when target_branch is missing" do
Hiroyuki Sato's avatar
Hiroyuki Sato committed
271
        post api("/projects/#{project.id}/merge_requests", user),
272
        title: "Test merge_request", source_branch: "markdown", author: user
273
        expect(response).to have_http_status(400)
274 275
      end

276
      it "returns 400 when title is missing" do
Hiroyuki Sato's avatar
Hiroyuki Sato committed
277
        post api("/projects/#{project.id}/merge_requests", user),
278
        target_branch: 'master', source_branch: 'markdown'
279
        expect(response).to have_http_status(400)
280
      end
281

282
      it 'allows special label names' do
283 284
        post api("/projects/#{project.id}/merge_requests", user),
             title: 'Test merge_request',
285
             source_branch: 'markdown',
286 287
             target_branch: 'master',
             author: user,
288 289 290 291 292 293 294
             labels: 'label, label?, label&foo, ?, &'
        expect(response.status).to eq(201)
        expect(json_response['labels']).to include 'label'
        expect(json_response['labels']).to include 'label?'
        expect(json_response['labels']).to include 'label&foo'
        expect(json_response['labels']).to include '?'
        expect(json_response['labels']).to include '&'
295
      end
296 297 298 299 300

      context 'with existing MR' do
        before do
          post api("/projects/#{project.id}/merge_requests", user),
               title: 'Test merge_request',
301
               source_branch: 'feature_conflict',
302 303 304 305 306
               target_branch: 'master',
               author: user
          @mr = MergeRequest.all.last
        end

307
        it 'returns 409 when MR already exists for source/target' do
308 309 310
          expect do
            post api("/projects/#{project.id}/merge_requests", user),
                 title: 'New test merge_request',
311
                 source_branch: 'feature_conflict',
312 313 314
                 target_branch: 'master',
                 author: user
          end.to change { MergeRequest.count }.by(0)
315
          expect(response).to have_http_status(409)
316 317
        end
      end
318
    end
319

320
    context 'forked projects' do
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
321
      let!(:user2) { create(:user) }
322 323
      let!(:fork_project) { create(:empty_project, forked_from_project: project,  namespace: user2.namespace, creator_id: user2.id) }
      let!(:unrelated_project) { create(:empty_project,  namespace: create(:user).namespace, creator_id: user2.id) }
324 325

      before :each do |each|
326
        fork_project.team << [user2, :reporter]
327 328
      end

329
      it "returns merge_request" do
Hiroyuki Sato's avatar
Hiroyuki Sato committed
330
        post api("/projects/#{fork_project.id}/merge_requests", user2),
331 332
          title: 'Test merge_request', source_branch: "feature_conflict", target_branch: "master",
          author: user2, target_project_id: project.id, description: 'Test description for Test merge_request'
333
        expect(response).to have_http_status(201)
334 335
        expect(json_response['title']).to eq('Test merge_request')
        expect(json_response['description']).to eq('Test description for Test merge_request')
336 337
      end

338
      it "does not return 422 when source_branch equals target_branch" do
339 340 341
        expect(project.id).not_to eq(fork_project.id)
        expect(fork_project.forked?).to be_truthy
        expect(fork_project.forked_from_project).to eq(project)
Hiroyuki Sato's avatar
Hiroyuki Sato committed
342
        post api("/projects/#{fork_project.id}/merge_requests", user2),
343
        title: 'Test merge_request', source_branch: "master", target_branch: "master", author: user2, target_project_id: project.id
344
        expect(response).to have_http_status(201)
345
        expect(json_response['title']).to eq('Test merge_request')
346 347
      end

348
      it "returns 400 when source_branch is missing" do
Hiroyuki Sato's avatar
Hiroyuki Sato committed
349
        post api("/projects/#{fork_project.id}/merge_requests", user2),
350
        title: 'Test merge_request', target_branch: "master", author: user2, target_project_id: project.id
351
        expect(response).to have_http_status(400)
352 353
      end

354
      it "returns 400 when target_branch is missing" do
Hiroyuki Sato's avatar
Hiroyuki Sato committed
355
        post api("/projects/#{fork_project.id}/merge_requests", user2),
356
        title: 'Test merge_request', target_branch: "master", author: user2, target_project_id: project.id
357
        expect(response).to have_http_status(400)
358 359
      end

360
      it "returns 400 when title is missing" do
Hiroyuki Sato's avatar
Hiroyuki Sato committed
361
        post api("/projects/#{fork_project.id}/merge_requests", user2),
362
        target_branch: 'master', source_branch: 'markdown', author: user2, target_project_id: project.id
363
        expect(response).to have_http_status(400)
364 365
      end

366
      context 'when target_branch is specified' do
367
        it 'returns 422 if not a forked project' do
368 369 370
          post api("/projects/#{project.id}/merge_requests", user),
               title: 'Test merge_request',
               target_branch: 'master',
371
               source_branch: 'markdown',
372 373
               author: user,
               target_project_id: fork_project.id
374
          expect(response).to have_http_status(422)
375
        end
Izaak Alpert's avatar
Izaak Alpert committed
376

377
        it 'returns 422 if targeting a different fork' do
378 379 380
          post api("/projects/#{fork_project.id}/merge_requests", user2),
               title: 'Test merge_request',
               target_branch: 'master',
381
               source_branch: 'markdown',
382 383
               author: user2,
               target_project_id: unrelated_project.id
384
          expect(response).to have_http_status(422)
385
        end
Izaak Alpert's avatar
Izaak Alpert committed
386 387
      end

388
      it "returns 201 when target_branch is specified and for the same project" do
Hiroyuki Sato's avatar
Hiroyuki Sato committed
389
        post api("/projects/#{fork_project.id}/merge_requests", user2),
390
        title: 'Test merge_request', target_branch: 'master', source_branch: 'markdown', author: user2, target_project_id: fork_project.id
391
        expect(response).to have_http_status(201)
Izaak Alpert's avatar
Izaak Alpert committed
392
      end
393
    end
394 395
  end

396 397 398
  describe "DELETE /projects/:id/merge_requests/:merge_request_id" do
    context "when the user is developer" do
      let(:developer) { create(:user) }
399

400 401 402
      before do
        project.team << [developer, :developer]
      end
403

404 405
      it "denies the deletion of the merge request" do
        delete api("/projects/#{project.id}/merge_requests/#{merge_request.id}", developer)
406
        expect(response).to have_http_status(403)
407
      end
408
    end
409

410 411 412 413
    context "when the user is project owner" do
      it "destroys the merge request owners can destroy" do
        delete api("/projects/#{project.id}/merge_requests/#{merge_request.id}", user)

414
        expect(response).to have_http_status(204)
415
      end
416
    end
417 418
  end

419
  describe "PUT /projects/:id/merge_requests/:merge_request_id/merge" do
420
    let(:pipeline) { create(:ci_pipeline_without_jobs) }
421

422
    it "returns merge_request in case of success" do
423
      put api("/projects/#{project.id}/merge_requests/#{merge_request.id}/merge", user)
424

425
      expect(response).to have_http_status(200)
426 427
    end

428
    it "returns 406 if branch can't be merged" do
429 430
      allow_any_instance_of(MergeRequest).
        to receive(:can_be_merged?).and_return(false)
431

432
      put api("/projects/#{project.id}/merge_requests/#{merge_request.id}/merge", user)
433

434
      expect(response).to have_http_status(406)
435
      expect(json_response['message']).to eq('Branch cannot be merged')
436
    end
437

438
    it "returns 405 if merge_request is not open" do
439
      merge_request.close
440
      put api("/projects/#{project.id}/merge_requests/#{merge_request.id}/merge", user)
441
      expect(response).to have_http_status(405)
442
      expect(json_response['message']).to eq('405 Method Not Allowed')
443 444
    end

445
    it "returns 405 if merge_request is a work in progress" do
446
      merge_request.update_attribute(:title, "WIP: #{merge_request.title}")
447
      put api("/projects/#{project.id}/merge_requests/#{merge_request.id}/merge", user)
448
      expect(response).to have_http_status(405)
449 450 451
      expect(json_response['message']).to eq('405 Method Not Allowed')
    end

452
    it 'returns 405 if the build failed for a merge request that requires success' do
453
      allow_any_instance_of(MergeRequest).to receive(:mergeable_ci_state?).and_return(false)
454 455

      put api("/projects/#{project.id}/merge_requests/#{merge_request.id}/merge", user)
456

457
      expect(response).to have_http_status(405)
458 459 460
      expect(json_response['message']).to eq('405 Method Not Allowed')
    end

461
    it "returns 401 if user has no permissions to merge" do
462 463
      user2 = create(:user)
      project.team << [user2, :reporter]
464
      put api("/projects/#{project.id}/merge_requests/#{merge_request.id}/merge", user2)
465
      expect(response).to have_http_status(401)
466
      expect(json_response['message']).to eq('401 Unauthorized')
467
    end
468

469
    it "returns 409 if the SHA parameter doesn't match" do
470
      put api("/projects/#{project.id}/merge_requests/#{merge_request.id}/merge", user), sha: merge_request.diff_head_sha.reverse
471

472
      expect(response).to have_http_status(409)
473 474 475 476
      expect(json_response['message']).to start_with('SHA does not match HEAD of source branch')
    end

    it "succeeds if the SHA parameter matches" do
477
      put api("/projects/#{project.id}/merge_requests/#{merge_request.id}/merge", user), sha: merge_request.diff_head_sha
478

479
      expect(response).to have_http_status(200)
480 481
    end

482
    it "enables merge when pipeline succeeds if the pipeline is active" do
483
      allow_any_instance_of(MergeRequest).to receive(:head_pipeline).and_return(pipeline)
484
      allow(pipeline).to receive(:active?).and_return(true)
485

486
      put api("/projects/#{project.id}/merge_requests/#{merge_request.id}/merge", user), merge_when_pipeline_succeeds: true
487

488
      expect(response).to have_http_status(200)
489
      expect(json_response['title']).to eq('Test')
490
      expect(json_response['merge_when_pipeline_succeeds']).to eq(true)
491
    end
492 493
  end

494
  describe "PUT /projects/:id/merge_requests/:merge_request_id" do
495 496 497 498 499 500 501 502 503
    context "to close a MR" do
      it "returns merge_request" do
        put api("/projects/#{project.id}/merge_requests/#{merge_request.id}", user), state_event: "close"

        expect(response).to have_http_status(200)
        expect(json_response['state']).to eq('closed')
      end
    end

504
    it "updates title and returns merge_request" do
505
      put api("/projects/#{project.id}/merge_requests/#{merge_request.id}", user), title: "New title"
506
      expect(response).to have_http_status(200)
507
      expect(json_response['title']).to eq('New title')
508
    end
509

510
    it "updates description and returns merge_request" do
511
      put api("/projects/#{project.id}/merge_requests/#{merge_request.id}", user), description: "New description"
512
      expect(response).to have_http_status(200)
513
      expect(json_response['description']).to eq('New description')
514 515
    end

516 517
    it "updates milestone_id and returns merge_request" do
      put api("/projects/#{project.id}/merge_requests/#{merge_request.id}", user), milestone_id: milestone.id
518
      expect(response).to have_http_status(200)
519 520 521
      expect(json_response['milestone']['id']).to eq(milestone.id)
    end

522
    it "returns merge_request with renamed target_branch" do
523
      put api("/projects/#{project.id}/merge_requests/#{merge_request.id}", user), target_branch: "wiki"
524
      expect(response).to have_http_status(200)
525
      expect(json_response['target_branch']).to eq('wiki')
526
    end
527

528 529 530 531 532 533 534
    it "returns merge_request that removes the source branch" do
      put api("/projects/#{project.id}/merge_requests/#{merge_request.id}", user), remove_source_branch: true

      expect(response).to have_http_status(200)
      expect(json_response['force_remove_source_branch']).to be_truthy
    end

535
    it 'allows special label names' do
536 537 538 539
      put api("/projects/#{project.id}/merge_requests/#{merge_request.id}", user),
        title: 'new issue',
        labels: 'label, label?, label&foo, ?, &'

540 541 542 543 544 545
      expect(response.status).to eq(200)
      expect(json_response['labels']).to include 'label'
      expect(json_response['labels']).to include 'label?'
      expect(json_response['labels']).to include 'label&foo'
      expect(json_response['labels']).to include '?'
      expect(json_response['labels']).to include '&'
546
    end
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562

    it 'does not update state when title is empty' do
      put api("/projects/#{project.id}/merge_requests/#{merge_request.id}", user), state_event: 'close', title: nil

      merge_request.reload
      expect(response).to have_http_status(400)
      expect(merge_request.state).to eq('opened')
    end

    it 'does not update state when target_branch is empty' do
      put api("/projects/#{project.id}/merge_requests/#{merge_request.id}", user), state_event: 'close', target_branch: nil

      merge_request.reload
      expect(response).to have_http_status(400)
      expect(merge_request.state).to eq('opened')
    end
563 564
  end

565
  describe "POST /projects/:id/merge_requests/:merge_request_id/comments" do
566
    it "returns comment" do
567 568
      original_count = merge_request.notes.size

569
      post api("/projects/#{project.id}/merge_requests/#{merge_request.id}/comments", user), note: "My comment"
570

571
      expect(response).to have_http_status(201)
572
      expect(json_response['note']).to eq('My comment')
573 574
      expect(json_response['author']['name']).to eq(user.name)
      expect(json_response['author']['username']).to eq(user.username)
575
      expect(merge_request.reload.notes.size).to eq(original_count + 1)
576
    end
577

578
    it "returns 400 if note is missing" do
579
      post api("/projects/#{project.id}/merge_requests/#{merge_request.id}/comments", user)
580
      expect(response).to have_http_status(400)
581
    end
582

583
    it "returns 404 if note is attached to non existent merge request" do
584
      post api("/projects/#{project.id}/merge_requests/404/comments", user),
585
        note: 'My comment'
586
      expect(response).to have_http_status(404)
587
    end
588
  end
589

590
  describe "GET :id/merge_requests/:merge_request_id/comments" do
591 592 593
    let!(:note)  { create(:note_on_merge_request, author: user, project: project, noteable: merge_request, note: "a comment on a MR") }
    let!(:note2) { create(:note_on_merge_request, author: user, project: project, noteable: merge_request, note: "another comment on a MR") }

594
    it "returns merge_request comments ordered by created_at" do
595
      get api("/projects/#{project.id}/merge_requests/#{merge_request.id}/comments", user)
596

597
      expect(response).to have_http_status(200)
598
      expect(response).to include_pagination_headers
599
      expect(json_response).to be_an Array
600
      expect(json_response.length).to eq(2)
601 602
      expect(json_response.first['note']).to eq("a comment on a MR")
      expect(json_response.first['author']['id']).to eq(user.id)
603
      expect(json_response.last['note']).to eq("another comment on a MR")
604 605
    end

606
    it "returns a 404 error if merge_request_id not found" do
607
      get api("/projects/#{project.id}/merge_requests/999/comments", user)
608
      expect(response).to have_http_status(404)
609 610
    end
  end
611

612 613 614 615 616 617
  describe 'GET :id/merge_requests/:merge_request_id/closes_issues' do
    it 'returns the issue that will be closed on merge' do
      issue = create(:issue, project: project)
      mr = merge_request.tap do |mr|
        mr.update_attribute(:description, "Closes #{issue.to_reference(mr.project)}")
      end
618

619
      get api("/projects/#{project.id}/merge_requests/#{mr.id}/closes_issues", user)
620

621
      expect(response).to have_http_status(200)
622
      expect(response).to include_pagination_headers
623 624 625 626 627
      expect(json_response).to be_an Array
      expect(json_response.length).to eq(1)
      expect(json_response.first['id']).to eq(issue.id)
    end

628
    it 'returns an empty array when there are no issues to be closed' do
629
      get api("/projects/#{project.id}/merge_requests/#{merge_request.id}/closes_issues", user)
630

631
      expect(response).to have_http_status(200)
632
      expect(response).to include_pagination_headers
633 634 635
      expect(json_response).to be_an Array
      expect(json_response.length).to eq(0)
    end
636 637 638 639 640 641 642 643 644

    it 'handles external issues' do
      jira_project = create(:jira_project, :public, name: 'JIR_EXT1')
      issue = ExternalIssue.new("#{jira_project.name}-123", jira_project)
      merge_request = create(:merge_request, :simple, author: user, assignee: user, source_project: jira_project)
      merge_request.update_attribute(:description, "Closes #{issue.to_reference(jira_project)}")

      get api("/projects/#{jira_project.id}/merge_requests/#{merge_request.id}/closes_issues", user)

645
      expect(response).to have_http_status(200)
646
      expect(response).to include_pagination_headers
647 648 649 650 651
      expect(json_response).to be_an Array
      expect(json_response.length).to eq(1)
      expect(json_response.first['title']).to eq(issue.title)
      expect(json_response.first['id']).to eq(issue.id)
    end
652 653 654 655 656 657 658 659 660 661 662

    it 'returns 403 if the user has no access to the merge request' do
      project = create(:empty_project, :private)
      merge_request = create(:merge_request, :simple, source_project: project)
      guest = create(:user)
      project.team << [guest, :guest]

      get api("/projects/#{project.id}/merge_requests/#{merge_request.id}/closes_issues", guest)

      expect(response).to have_http_status(403)
    end
663 664
  end

665
  describe 'POST :id/merge_requests/:merge_request_id/subscribe' do
666
    it 'subscribes to a merge request' do
667
      post api("/projects/#{project.id}/merge_requests/#{merge_request.id}/subscribe", admin)
668

669
      expect(response).to have_http_status(201)
670 671 672 673
      expect(json_response['subscribed']).to eq(true)
    end

    it 'returns 304 if already subscribed' do
674
      post api("/projects/#{project.id}/merge_requests/#{merge_request.id}/subscribe", user)
675

676
      expect(response).to have_http_status(304)
677
    end
678 679

    it 'returns 404 if the merge request is not found' do
680
      post api("/projects/#{project.id}/merge_requests/123/subscribe", user)
681

682
      expect(response).to have_http_status(404)
683
    end
684 685 686 687 688

    it 'returns 403 if user has no access to read code' do
      guest = create(:user)
      project.team << [guest, :guest]

689
      post api("/projects/#{project.id}/merge_requests/#{merge_request.id}/subscribe", guest)
690 691 692

      expect(response).to have_http_status(403)
    end
693 694
  end

695
  describe 'POST :id/merge_requests/:merge_request_id/unsubscribe' do
696
    it 'unsubscribes from a merge request' do
697
      post api("/projects/#{project.id}/merge_requests/#{merge_request.id}/unsubscribe", user)
698

699
      expect(response).to have_http_status(201)
700 701 702 703
      expect(json_response['subscribed']).to eq(false)
    end

    it 'returns 304 if not subscribed' do
704
      post api("/projects/#{project.id}/merge_requests/#{merge_request.id}/unsubscribe", admin)
705

706
      expect(response).to have_http_status(304)
707
    end
708 709

    it 'returns 404 if the merge request is not found' do
710
      post api("/projects/#{project.id}/merge_requests/123/unsubscribe", user)
711

712
      expect(response).to have_http_status(404)
713
    end
714 715 716 717 718

    it 'returns 403 if user has no access to read code' do
      guest = create(:user)
      project.team << [guest, :guest]

719
      post api("/projects/#{project.id}/merge_requests/#{merge_request.id}/unsubscribe", guest)
720 721 722

      expect(response).to have_http_status(403)
    end
723 724
  end

725 726 727 728 729 730
  describe 'Time tracking' do
    let(:issuable) { merge_request }

    include_examples 'time tracking endpoints', 'merge_request'
  end

731 732 733 734 735 736 737 738 739 740 741 742 743 744 745
  def mr_with_later_created_and_updated_at_time
    merge_request
    merge_request.created_at += 1.hour
    merge_request.updated_at += 30.minutes
    merge_request.save
    merge_request
  end

  def mr_with_earlier_created_and_updated_at_time
    merge_request_closed
    merge_request_closed.created_at -= 1.hour
    merge_request_closed.updated_at -= 30.minutes
    merge_request_closed.save
    merge_request_closed
  end
746
end