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

Jeroen van Baarsen's avatar
Jeroen van Baarsen committed
3
describe API::API, api: true  do
4
  include ApiHelpers
5
  let(:user) { create(:user) }
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
6
  let!(:project) {create(:project, creator_id: user.id, namespace: user.namespace) }
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
7
  let!(:merge_request) { create(:merge_request, :simple, author: user, assignee: user, source_project: project, target_project: project, title: "Test") }
8 9
  let!(:merge_request_closed) { create(:merge_request, state: "closed", author: user, assignee: user, source_project: project, target_project: project, title: "Closed test") }
  let!(:merge_request_merged) { create(:merge_request, state: "merged", author: user, assignee: user, source_project: project, target_project: project, title: "Merged test") }
10
  let!(:note) { create(:note_on_merge_request, author: user, project: project, noteable: merge_request, note: "a comment on a MR") }
11
  let!(:note2) { create(:note_on_merge_request, author: user, project: project, noteable: merge_request, note: "another comment on a MR") }
12 13

  before do
14
    project.team << [user, :reporters]
15
  end
16 17 18 19

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

    context "when authenticated" do
26
      it "should return an array of all merge_requests" do
Hiroyuki Sato's avatar
Hiroyuki Sato committed
27
        get api("/projects/#{project.id}/merge_requests", user)
28 29 30 31
        expect(response.status).to eq(200)
        expect(json_response).to be_an Array
        expect(json_response.length).to eq(3)
        expect(json_response.last['title']).to eq(merge_request.title)
32
      end
33

34 35
      it "should return an array of all merge_requests" do
        get api("/projects/#{project.id}/merge_requests?state", user)
36 37 38 39
        expect(response.status).to eq(200)
        expect(json_response).to be_an Array
        expect(json_response.length).to eq(3)
        expect(json_response.last['title']).to eq(merge_request.title)
40
      end
41

42 43
      it "should return an array of open merge_requests" do
        get api("/projects/#{project.id}/merge_requests?state=opened", user)
44 45 46 47
        expect(response.status).to eq(200)
        expect(json_response).to be_an Array
        expect(json_response.length).to eq(1)
        expect(json_response.last['title']).to eq(merge_request.title)
48
      end
49

50 51
      it "should return an array of closed merge_requests" do
        get api("/projects/#{project.id}/merge_requests?state=closed", user)
52 53
        expect(response.status).to eq(200)
        expect(json_response).to be_an Array
54 55
        expect(json_response.length).to eq(1)
        expect(json_response.first['title']).to eq(merge_request_closed.title)
56
      end
57

58 59
      it "should return an array of merged merge_requests" do
        get api("/projects/#{project.id}/merge_requests?state=merged", user)
60 61 62 63
        expect(response.status).to eq(200)
        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)
64
      end
65 66 67 68 69 70 71 72 73

      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

        it "should return an array of merge_requests in ascending order" do
          get api("/projects/#{project.id}/merge_requests?sort=asc", user)
74 75 76 77 78
          expect(response.status).to eq(200)
          expect(json_response).to be_an Array
          expect(json_response.length).to eq(3)
          expect(json_response.last['id']).to eq(@mr_earlier.id)
          expect(json_response.first['id']).to eq(@mr_later.id)
79
        end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
80

81 82
        it "should return an array of merge_requests in descending order" do
          get api("/projects/#{project.id}/merge_requests?sort=desc", user)
83 84 85 86 87
          expect(response.status).to eq(200)
          expect(json_response).to be_an Array
          expect(json_response.length).to eq(3)
          expect(json_response.first['id']).to eq(@mr_later.id)
          expect(json_response.last['id']).to eq(@mr_earlier.id)
88
        end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
89

90 91
        it "should return an array of merge_requests ordered by updated_at" do
          get api("/projects/#{project.id}/merge_requests?order_by=updated_at", user)
92 93 94 95 96
          expect(response.status).to eq(200)
          expect(json_response).to be_an Array
          expect(json_response.length).to eq(3)
          expect(json_response.last['id']).to eq(@mr_earlier.id)
          expect(json_response.first['id']).to eq(@mr_later.id)
97
        end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
98

99 100
        it "should return an array of merge_requests ordered by created_at" do
          get api("/projects/#{project.id}/merge_requests?sort=created_at", user)
101 102 103 104 105
          expect(response.status).to eq(200)
          expect(json_response).to be_an Array
          expect(json_response.length).to eq(3)
          expect(json_response.last['id']).to eq(@mr_earlier.id)
          expect(json_response.first['id']).to eq(@mr_later.id)
106
        end
107
      end
108 109 110 111 112
    end
  end

  describe "GET /projects/:id/merge_request/:merge_request_id" do
    it "should return merge_request" do
Hiroyuki Sato's avatar
Hiroyuki Sato committed
113
      get api("/projects/#{project.id}/merge_request/#{merge_request.id}", user)
114 115 116
      expect(response.status).to eq(200)
      expect(json_response['title']).to eq(merge_request.title)
      expect(json_response['iid']).to eq(merge_request.iid)
117
    end
118

119 120 121
    it 'should return merge_request by iid' do
      url = "/projects/#{project.id}/merge_requests?iid=#{merge_request.iid}"
      get api(url, user)
122 123 124
      expect(response.status).to eq 200
      expect(json_response.first['title']).to eq merge_request.title
      expect(json_response.first['id']).to eq merge_request.id
125 126
    end

127
    it "should return a 404 error if merge_request_id not found" do
Hiroyuki Sato's avatar
Hiroyuki Sato committed
128
      get api("/projects/#{project.id}/merge_request/999", user)
129
      expect(response.status).to eq(404)
130
    end
131 132
  end

133 134 135 136 137 138 139 140 141 142 143 144 145
  describe 'GET /projects/:id/merge_request/:merge_request_id/changes' do
    it 'should return the change information of the merge_request' do
      get api("/projects/#{project.id}/merge_request/#{merge_request.id}/changes", user)
      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
      get api("/projects/#{project.id}/merge_request/999/changes", user)
      expect(response.status).to eq(404)
    end
  end

146
  describe "POST /projects/:id/merge_requests" do
147 148
    context 'between branches projects' do
      it "should return merge_request" do
Hiroyuki Sato's avatar
Hiroyuki Sato committed
149
        post api("/projects/#{project.id}/merge_requests", user),
150 151 152 153 154
             title: 'Test merge_request',
             source_branch: 'stable',
             target_branch: 'master',
             author: user,
             labels: 'label, label2'
155 156 157
        expect(response.status).to eq(201)
        expect(json_response['title']).to eq('Test merge_request')
        expect(json_response['labels']).to eq(['label', 'label2'])
158
      end
159

160
      it "should return 422 when source_branch equals target_branch" do
Hiroyuki Sato's avatar
Hiroyuki Sato committed
161
        post api("/projects/#{project.id}/merge_requests", user),
162
        title: "Test merge_request", source_branch: "master", target_branch: "master", author: user
163
        expect(response.status).to eq(422)
164
      end
165

166
      it "should return 400 when source_branch is missing" do
Hiroyuki Sato's avatar
Hiroyuki Sato committed
167
        post api("/projects/#{project.id}/merge_requests", user),
168
        title: "Test merge_request", target_branch: "master", author: user
169
        expect(response.status).to eq(400)
170
      end
171

172
      it "should return 400 when target_branch is missing" do
Hiroyuki Sato's avatar
Hiroyuki Sato committed
173
        post api("/projects/#{project.id}/merge_requests", user),
174
        title: "Test merge_request", source_branch: "stable", author: user
175
        expect(response.status).to eq(400)
176 177 178
      end

      it "should return 400 when title is missing" do
Hiroyuki Sato's avatar
Hiroyuki Sato committed
179
        post api("/projects/#{project.id}/merge_requests", user),
180
        target_branch: 'master', source_branch: 'stable'
181
        expect(response.status).to eq(400)
182
      end
183

Robert Schilling's avatar
Robert Schilling committed
184
      it 'should return 400 on invalid label names' do
185 186 187 188 189 190
        post api("/projects/#{project.id}/merge_requests", user),
             title: 'Test merge_request',
             source_branch: 'stable',
             target_branch: 'master',
             author: user,
             labels: 'label, ?'
191 192
        expect(response.status).to eq(400)
        expect(json_response['message']['labels']['?']['title']).to eq(
Robert Schilling's avatar
Robert Schilling committed
193
          ['is invalid']
194
        )
195
      end
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214

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

        it 'should return 409 when MR already exists for source/target' do
          expect do
            post api("/projects/#{project.id}/merge_requests", user),
                 title: 'New test merge_request',
                 source_branch: 'stable',
                 target_branch: 'master',
                 author: user
          end.to change { MergeRequest.count }.by(0)
215
          expect(response.status).to eq(409)
216 217
        end
      end
218
    end
219

220
    context 'forked projects' do
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
221 222 223
      let!(:user2) { create(:user) }
      let!(:fork_project) { create(:project, forked_from_project: project,  namespace: user2.namespace, creator_id: user2.id) }
      let!(:unrelated_project) { create(:project,  namespace: create(:user).namespace, creator_id: user2.id) }
224 225 226 227 228 229

      before :each do |each|
        fork_project.team << [user2, :reporters]
      end

      it "should return merge_request" do
Hiroyuki Sato's avatar
Hiroyuki Sato committed
230
        post api("/projects/#{fork_project.id}/merge_requests", user2),
231
        title: 'Test merge_request', source_branch: "stable", target_branch: "master", author: user2, target_project_id: project.id, description: 'Test description for Test merge_request'
232 233 234
        expect(response.status).to eq(201)
        expect(json_response['title']).to eq('Test merge_request')
        expect(json_response['description']).to eq('Test description for Test merge_request')
235 236 237
      end

      it "should not return 422 when source_branch equals target_branch" do
238 239 240
        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
241
        post api("/projects/#{fork_project.id}/merge_requests", user2),
242
        title: 'Test merge_request', source_branch: "master", target_branch: "master", author: user2, target_project_id: project.id
243 244
        expect(response.status).to eq(201)
        expect(json_response['title']).to eq('Test merge_request')
245 246 247
      end

      it "should return 400 when source_branch is missing" do
Hiroyuki Sato's avatar
Hiroyuki Sato committed
248
        post api("/projects/#{fork_project.id}/merge_requests", user2),
249
        title: 'Test merge_request', target_branch: "master", author: user2, target_project_id: project.id
250
        expect(response.status).to eq(400)
251 252 253
      end

      it "should return 400 when target_branch is missing" do
Hiroyuki Sato's avatar
Hiroyuki Sato committed
254
        post api("/projects/#{fork_project.id}/merge_requests", user2),
255
        title: 'Test merge_request', target_branch: "master", author: user2, target_project_id: project.id
256
        expect(response.status).to eq(400)
257 258 259
      end

      it "should return 400 when title is missing" do
Hiroyuki Sato's avatar
Hiroyuki Sato committed
260
        post api("/projects/#{fork_project.id}/merge_requests", user2),
261
        target_branch: 'master', source_branch: 'stable', author: user2, target_project_id: project.id
262
        expect(response.status).to eq(400)
263 264
      end

265 266 267 268 269 270 271 272
      context 'when target_branch is specified' do
        it 'should return 422 if not a forked project' do
          post api("/projects/#{project.id}/merge_requests", user),
               title: 'Test merge_request',
               target_branch: 'master',
               source_branch: 'stable',
               author: user,
               target_project_id: fork_project.id
273
          expect(response.status).to eq(422)
274
        end
Izaak Alpert's avatar
Izaak Alpert committed
275

276 277 278 279 280 281 282
        it 'should return 422 if targeting a different fork' do
          post api("/projects/#{fork_project.id}/merge_requests", user2),
               title: 'Test merge_request',
               target_branch: 'master',
               source_branch: 'stable',
               author: user2,
               target_project_id: unrelated_project.id
283
          expect(response.status).to eq(422)
284
        end
Izaak Alpert's avatar
Izaak Alpert committed
285 286 287
      end

      it "should return 201 when target_branch is specified and for the same project" do
Hiroyuki Sato's avatar
Hiroyuki Sato committed
288
        post api("/projects/#{fork_project.id}/merge_requests", user2),
289
        title: 'Test merge_request', target_branch: 'master', source_branch: 'stable', author: user2, target_project_id: fork_project.id
290
        expect(response.status).to eq(201)
Izaak Alpert's avatar
Izaak Alpert committed
291
      end
292
    end
293 294
  end

295 296
  describe "PUT /projects/:id/merge_request/:merge_request_id to close MR" do
    it "should return merge_request" do
Hiroyuki Sato's avatar
Hiroyuki Sato committed
297
      put api("/projects/#{project.id}/merge_request/#{merge_request.id}", user), state_event: "close"
298 299
      expect(response.status).to eq(200)
      expect(json_response['state']).to eq('closed')
300 301 302
    end
  end

303 304
  describe "PUT /projects/:id/merge_request/:merge_request_id/merge" do
    it "should return merge_request in case of success" do
305 306 307
      allow_any_instance_of(MergeRequest).
        to receive_messages(can_be_merged?: true, automerge!: true)

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

310
      expect(response.status).to eq(200)
311 312 313
    end

    it "should return 405 if branch can't be merged" do
314 315 316
      allow_any_instance_of(MergeRequest).
        to receive(:can_be_merged?).and_return(false)

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

319 320
      expect(response.status).to eq(405)
      expect(json_response['message']).to eq('Branch cannot be merged')
321
    end
322 323 324 325

    it "should return 405 if merge_request is not open" do
      merge_request.close
      put api("/projects/#{project.id}/merge_request/#{merge_request.id}/merge", user)
326 327
      expect(response.status).to eq(405)
      expect(json_response['message']).to eq('405 Method Not Allowed')
328 329
    end

330 331 332 333 334 335 336
    it "should return 405 if merge_request is a work in progress" do
      merge_request.update_attribute(:title, "WIP: #{merge_request.title}")
      put api("/projects/#{project.id}/merge_request/#{merge_request.id}/merge", user)
      expect(response.status).to eq(405)
      expect(json_response['message']).to eq('405 Method Not Allowed')
    end

337 338 339 340
    it "should return 401 if user has no permissions to merge" do
      user2 = create(:user)
      project.team << [user2, :reporter]
      put api("/projects/#{project.id}/merge_request/#{merge_request.id}/merge", user2)
341 342
      expect(response.status).to eq(401)
      expect(json_response['message']).to eq('401 Unauthorized')
343
    end
344 345
  end

346 347
  describe "PUT /projects/:id/merge_request/:merge_request_id" do
    it "should return merge_request" do
Hiroyuki Sato's avatar
Hiroyuki Sato committed
348
      put api("/projects/#{project.id}/merge_request/#{merge_request.id}", user), title: "New title"
349 350
      expect(response.status).to eq(200)
      expect(json_response['title']).to eq('New title')
351
    end
352

353 354
    it "should return merge_request" do
      put api("/projects/#{project.id}/merge_request/#{merge_request.id}", user), description: "New description"
355 356
      expect(response.status).to eq(200)
      expect(json_response['description']).to eq('New description')
357 358
    end

359
    it "should return 400 when source_branch is specified" do
Hiroyuki Sato's avatar
Hiroyuki Sato committed
360
      put api("/projects/#{project.id}/merge_request/#{merge_request.id}", user),
361
      source_branch: "master", target_branch: "master"
362
      expect(response.status).to eq(400)
363 364 365
    end

    it "should return merge_request with renamed target_branch" do
Hiroyuki Sato's avatar
Hiroyuki Sato committed
366
      put api("/projects/#{project.id}/merge_request/#{merge_request.id}", user), target_branch: "wiki"
367 368
      expect(response.status).to eq(200)
      expect(json_response['target_branch']).to eq('wiki')
369
    end
370

Robert Schilling's avatar
Robert Schilling committed
371
    it 'should return 400 on invalid label names' do
372 373 374 375
      put api("/projects/#{project.id}/merge_request/#{merge_request.id}",
              user),
          title: 'new issue',
          labels: 'label, ?'
376 377
      expect(response.status).to eq(400)
      expect(json_response['message']['labels']['?']['title']).to eq(['is invalid'])
378
    end
379 380 381 382
  end

  describe "POST /projects/:id/merge_request/:merge_request_id/comments" do
    it "should return comment" do
Hiroyuki Sato's avatar
Hiroyuki Sato committed
383
      post api("/projects/#{project.id}/merge_request/#{merge_request.id}/comments", user), note: "My comment"
384 385
      expect(response.status).to eq(201)
      expect(json_response['note']).to eq('My comment')
386
    end
387 388

    it "should return 400 if note is missing" do
Hiroyuki Sato's avatar
Hiroyuki Sato committed
389
      post api("/projects/#{project.id}/merge_request/#{merge_request.id}/comments", user)
390
      expect(response.status).to eq(400)
391
    end
392 393

    it "should return 404 if note is attached to non existent merge request" do
394 395
      post api("/projects/#{project.id}/merge_request/404/comments", user),
           note: 'My comment'
396
      expect(response.status).to eq(404)
397
    end
398
  end
399 400

  describe "GET :id/merge_request/:merge_request_id/comments" do
401
    it "should return merge_request comments ordered by created_at" do
402
      get api("/projects/#{project.id}/merge_request/#{merge_request.id}/comments", user)
403 404
      expect(response.status).to eq(200)
      expect(json_response).to be_an Array
405
      expect(json_response.length).to eq(2)
406 407
      expect(json_response.first['note']).to eq("a comment on a MR")
      expect(json_response.first['author']['id']).to eq(user.id)
408
      expect(json_response.last['note']).to eq("another comment on a MR")
409 410 411 412
    end

    it "should return a 404 error if merge_request_id not found" do
      get api("/projects/#{project.id}/merge_request/999/comments", user)
413
      expect(response.status).to eq(404)
414 415
    end
  end
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431

  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
432
end