merge_request_spec.rb 60.7 KB
Newer Older
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
1 2
require 'spec_helper'

Douwe Maan's avatar
Douwe Maan committed
3
describe MergeRequest, models: true do
4 5
  include RepoHelpers

6 7
  subject { create(:merge_request) }

8
  describe 'associations' do
9 10
    it { is_expected.to belong_to(:target_project).class_name('Project') }
    it { is_expected.to belong_to(:source_project).class_name('Project') }
11
    it { is_expected.to belong_to(:merge_user).class_name("User") }
12
    it { is_expected.to have_many(:merge_request_diffs).dependent(:destroy) }
Valery Sizov's avatar
Valery Sizov committed
13
    it { is_expected.to have_many(:approver_groups).dependent(:destroy) }
14 15
  end

16 17 18 19 20 21 22 23
  describe 'modules' do
    subject { described_class }

    it { is_expected.to include_module(InternalId) }
    it { is_expected.to include_module(Issuable) }
    it { is_expected.to include_module(Referable) }
    it { is_expected.to include_module(Sortable) }
    it { is_expected.to include_module(Taskable) }
24 25
  end

26 27 28 29 30
  describe "act_as_paranoid" do
    it { is_expected.to have_db_column(:deleted_at) }
    it { is_expected.to have_db_index(:deleted_at) }
  end

31
  describe 'validation' do
32 33
    it { is_expected.to validate_presence_of(:target_branch) }
    it { is_expected.to validate_presence_of(:source_branch) }
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

    context "Validation of merge user with Merge When Build succeeds" do
      it "allows user to be nil when the feature is disabled" do
        expect(subject).to be_valid
      end

      it "is invalid without merge user" do
        subject.merge_when_build_succeeds = true
        expect(subject).not_to be_valid
      end

      it "is valid with merge user" do
        subject.merge_when_build_succeeds = true
        subject.merge_user = build(:user)

        expect(subject).to be_valid
      end
    end
52 53
  end

54
  describe 'respond to' do
55 56 57
    it { is_expected.to respond_to(:unchecked?) }
    it { is_expected.to respond_to(:can_be_merged?) }
    it { is_expected.to respond_to(:cannot_be_merged?) }
58 59
    it { is_expected.to respond_to(:merge_params) }
    it { is_expected.to respond_to(:merge_when_build_succeeds) }
60
  end
Andrey Kumanyaev's avatar
Andrey Kumanyaev committed
61

62 63 64 65 66 67
  describe '.in_projects' do
    it 'returns the merge requests for a set of projects' do
      expect(described_class.in_projects(Project.all)).to eq([subject])
    end
  end

68
  describe '#target_branch_sha' do
69
    let(:project) { create(:project) }
70

71
    subject { create(:merge_request, source_project: project, target_project: project) }
72

73
    context 'when the target branch does not exist' do
74 75 76
      before do
        project.repository.raw_repository.delete_branch(subject.target_branch)
      end
77 78

      it 'returns nil' do
79
        expect(subject.target_branch_sha).to be_nil
80 81
      end
    end
82 83 84 85 86 87

    it 'returns memoized value' do
      subject.target_branch_sha = '8ffb3c15a5475e59ae909384297fede4badcb4c7'

      expect(subject.target_branch_sha).to eq '8ffb3c15a5475e59ae909384297fede4badcb4c7'
    end
88 89
  end

90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
  describe '#cache_merge_request_closes_issues!' do
    before do
      subject.project.team << [subject.author, :developer]
      subject.target_branch = subject.project.default_branch
    end

    it 'caches closed issues' do
      issue  = create :issue, project: subject.project
      commit = double('commit1', safe_message: "Fixes #{issue.to_reference}")
      allow(subject).to receive(:commits).and_return([commit])

      expect { subject.cache_merge_request_closes_issues! }.to change(subject.merge_requests_closing_issues, :count).by(1)
    end

    it 'does not cache issues from external trackers' do
      subject.project.update_attribute(:has_external_issue_tracker, true)
      issue  = ExternalIssue.new('JIRA-123', subject.project)
      commit = double('commit1', safe_message: "Fixes #{issue.to_reference}")
      allow(subject).to receive(:commits).and_return([commit])

      expect { subject.cache_merge_request_closes_issues! }.not_to change(subject.merge_requests_closing_issues, :count)
    end
  end

114
  describe '#source_branch_sha' do
115 116 117 118 119
    let(:last_branch_commit) { subject.source_project.repository.commit(subject.source_branch) }

    context 'with diffs' do
      subject { create(:merge_request, :with_diffs) }
      it 'returns the sha of the source branch last commit' do
120
        expect(subject.source_branch_sha).to eq(last_branch_commit.sha)
121 122 123
      end
    end

124 125 126
    context 'without diffs' do
      subject { create(:merge_request, :without_diffs) }
      it 'returns the sha of the source branch last commit' do
127
        expect(subject.source_branch_sha).to eq(last_branch_commit.sha)
128 129 130
      end
    end

131 132 133
    context 'when the merge request is being created' do
      subject { build(:merge_request, source_branch: nil, compare_commits: []) }
      it 'returns nil' do
134
        expect(subject.source_branch_sha).to be_nil
135 136
      end
    end
137 138 139 140 141 142

    it 'returns memoized value' do
      subject.source_branch_sha = '2e5d3239642f9161dcbbc4b70a211a68e5e45e2b'

      expect(subject.source_branch_sha).to eq '2e5d3239642f9161dcbbc4b70a211a68e5e45e2b'
    end
143 144
  end

145 146 147 148 149 150 151 152 153
  describe '#to_reference' do
    it 'returns a String reference to the object' do
      expect(subject.to_reference).to eq "!#{subject.iid}"
    end

    it 'supports a cross-project reference' do
      cross = double('project')
      expect(subject.to_reference(cross)).to eq "#{subject.source_project.to_reference}!#{subject.iid}"
    end
154
  end
155

156
  describe '#raw_diffs' do
157 158 159 160 161 162 163
    let(:merge_request) { build(:merge_request) }
    let(:options) { { paths: ['a/b', 'b/a', 'c/*'] } }

    context 'when there are MR diffs' do
      it 'delegates to the MR diffs' do
        merge_request.merge_request_diff = MergeRequestDiff.new

164
        expect(merge_request.merge_request_diff).to receive(:raw_diffs).with(options)
165

166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
        merge_request.raw_diffs(options)
      end
    end

    context 'when there are no MR diffs' do
      it 'delegates to the compare object' do
        merge_request.compare = double(:compare)

        expect(merge_request.compare).to receive(:raw_diffs).with(options)

        merge_request.raw_diffs(options)
      end
    end
  end

  describe '#diffs' do
    let(:merge_request) { build(:merge_request) }
    let(:options) { { paths: ['a/b', 'b/a', 'c/*'] } }

    context 'when there are MR diffs' do
      it 'delegates to the MR diffs' do
187
        merge_request.save
188

189
        expect(merge_request.merge_request_diff).to receive(:raw_diffs).with(hash_including(options))
190

191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
        merge_request.diffs(options)
      end
    end

    context 'when there are no MR diffs' do
      it 'delegates to the compare object' do
        merge_request.compare = double(:compare)

        expect(merge_request.compare).to receive(:diffs).with(options)

        merge_request.diffs(options)
      end
    end
  end

206
  describe "#mr_and_commit_notes" do
207
    let!(:merge_request) { create(:merge_request) }
208 209

    before do
210
      allow(merge_request).to receive(:commits) { [merge_request.source_project.repository.commit] }
211 212
      create(:note_on_commit, commit_id: merge_request.commits.first.id,
                              project: merge_request.project)
213
      create(:note, noteable: merge_request, project: merge_request.project)
214 215
    end

216
    it "includes notes for commits" do
217 218
      expect(merge_request.commits).not_to be_empty
      expect(merge_request.mr_and_commit_notes.count).to eq(2)
219
    end
220

221
    it "includes notes for commits from target project as well" do
222 223 224
      create(:note_on_commit, commit_id: merge_request.commits.first.id,
                              project: merge_request.target_project)

225 226 227
      expect(merge_request.commits).not_to be_empty
      expect(merge_request.mr_and_commit_notes.count).to eq(3)
    end
228
  end
229 230 231

  describe '#is_being_reassigned?' do
    it 'returns true if the merge_request assignee has changed' do
232
      subject.assignee = create(:user)
233
      expect(subject.is_being_reassigned?).to be_truthy
234 235
    end
    it 'returns false if the merge request assignee has not changed' do
236
      expect(subject.is_being_reassigned?).to be_falsey
237 238
    end
  end
239 240 241

  describe '#for_fork?' do
    it 'returns true if the merge request is for a fork' do
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
242 243
      subject.source_project = create(:project, namespace: create(:group))
      subject.target_project = create(:project, namespace: create(:group))
244

245
      expect(subject.for_fork?).to be_truthy
246
    end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
247

248
    it 'returns false if is not for a fork' do
249
      expect(subject.for_fork?).to be_falsey
250 251 252
    end
  end

253 254 255
  describe 'detection of issues to be closed' do
    let(:issue0) { create :issue, project: subject.project }
    let(:issue1) { create :issue, project: subject.project }
256 257 258 259

    let(:commit0) { double('commit0', safe_message: "Fixes #{issue0.to_reference}") }
    let(:commit1) { double('commit1', safe_message: "Fixes #{issue0.to_reference}") }
    let(:commit2) { double('commit2', safe_message: "Fixes #{issue1.to_reference}") }
260 261

    before do
262
      subject.project.team << [subject.author, :developer]
263
      allow(subject).to receive(:commits).and_return([commit0, commit1, commit2])
264 265 266
    end

    it 'accesses the set of issues that will be closed on acceptance' do
267 268
      allow(subject.project).to receive(:default_branch).
        and_return(subject.target_branch)
269

270 271 272
      closed = subject.closes_issues

      expect(closed).to include(issue0, issue1)
273 274 275
    end

    it 'only lists issues as to be closed if it targets the default branch' do
276
      allow(subject.project).to receive(:default_branch).and_return('master')
277 278
      subject.target_branch = 'something-else'

279
      expect(subject.closes_issues).to be_empty
280
    end
281

282 283
    it 'detects issues mentioned in the description' do
      issue2 = create(:issue, project: subject.project)
284
      subject.description = "Closes #{issue2.to_reference}"
285 286
      allow(subject.project).to receive(:default_branch).
        and_return(subject.target_branch)
287

288
      expect(subject.closes_issues).to include(issue2)
289
    end
290 291
  end

292
  describe "#work_in_progress?" do
293 294 295
    ['WIP ', 'WIP:', 'WIP: ', '[WIP]', '[WIP] ', ' [WIP] WIP [WIP] WIP: WIP '].each do |wip_prefix|
      it "detects the '#{wip_prefix}' prefix" do
        subject.title = "#{wip_prefix}#{subject.title}"
296
        expect(subject.work_in_progress?).to eq true
297
      end
298 299
    end

300 301
    it "doesn't detect WIP for words starting with WIP" do
      subject.title = "Wipwap #{subject.title}"
302
      expect(subject.work_in_progress?).to eq false
303 304
    end

305 306
    it "doesn't detect WIP for words containing with WIP" do
      subject.title = "WupWipwap #{subject.title}"
307
      expect(subject.work_in_progress?).to eq false
308 309
    end

310
    it "doesn't detect WIP by default" do
311
      expect(subject.work_in_progress?).to eq false
312 313 314
    end
  end

Rémy Coutable's avatar
Rémy Coutable committed
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
  describe "#wipless_title" do
    ['WIP ', 'WIP:', 'WIP: ', '[WIP]', '[WIP] ', ' [WIP] WIP [WIP] WIP: WIP '].each do |wip_prefix|
      it "removes the '#{wip_prefix}' prefix" do
        wipless_title = subject.title
        subject.title = "#{wip_prefix}#{subject.title}"

        expect(subject.wipless_title).to eq wipless_title
      end

      it "is satisfies the #work_in_progress? method" do
        subject.title = "#{wip_prefix}#{subject.title}"
        subject.title = subject.wipless_title

        expect(subject.work_in_progress?).to eq false
      end
    end
  end

  describe "#wip_title" do
    it "adds the WIP: prefix to the title" do
      wip_title = "WIP: #{subject.title}"

      expect(subject.wip_title).to eq wip_title
    end

    it "does not add the WIP: prefix multiple times" do
      wip_title = "WIP: #{subject.title}"
      subject.title = subject.wip_title
      subject.title = subject.wip_title

      expect(subject.wip_title).to eq wip_title
    end

    it "is satisfies the #work_in_progress? method" do
      subject.title = subject.wip_title

      expect(subject.work_in_progress?).to eq true
    end
  end

355
  describe "#approvers_left" do
356 357 358 359 360
    let(:merge_request) {create :merge_request}

    it "returns correct value" do
      user = create(:user)
      user1 = create(:user)
Valery Sizov's avatar
Valery Sizov committed
361 362
      merge_request.approvers.create(user_id: user.id)
      merge_request.approvers.create(user_id: user1.id)
363 364 365 366
      merge_request.approvals.create(user_id: user1.id)

      expect(merge_request.approvers_left).to eq [user]
    end
Valery Sizov's avatar
Valery Sizov committed
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391

    it "returns correct value when there is a group approver" do
      user = create(:user)
      user1 = create(:user)
      user2 = create(:user)
      group = create(:group)

      group.add_developer(user2)
      merge_request.approver_groups.create(group: group)
      merge_request.approvers.create(user_id: user.id)
      merge_request.approvers.create(user_id: user1.id)
      merge_request.approvals.create(user_id: user1.id)

      expect(merge_request.approvers_left).to match_array [user, user2]
    end

    it "returns correct value when there is only a group approver" do
      user = create(:user)
      group = create(:group)
      group.add_developer(user)

      merge_request.approver_groups.create(group: group)

      expect(merge_request.approvers_left).to eq [user]
    end
392 393
  end

394
  describe "#number_of_potential_approvers" do
395 396 397 398 399 400 401
    let(:project) { create(:empty_project) }
    let(:author) { create(:user) }
    let(:merge_request) { create(:merge_request, source_project: project, author: author) }

    it "includes approvers set on the MR" do
      expect do
        create(:approver, user: create(:user), target: merge_request)
402
      end.to change { merge_request.number_of_potential_approvers }.by(1)
403 404
    end

Valery Sizov's avatar
Valery Sizov committed
405 406 407 408 409 410 411 412
    it "includes approvers from group" do
      group = create(:group_with_members)

      expect do
        create(:approver_group, group: group, target: merge_request)
      end.to change { merge_request.number_of_potential_approvers }.by(1)
    end

413 414 415 416 417 418
    it "includes project members with developer access and up" do
      expect do
        project.team << [create(:user), :guest]
        project.team << [create(:user), :reporter]
        project.team << [create(:user), :developer]
        project.team << [create(:user), :master]
419
      end.to change { merge_request.number_of_potential_approvers }.by(2)
420 421 422 423 424 425 426
    end

    it "excludes users who have already approved the MR" do
      expect do
        approver = create(:user)
        create(:approver, user: approver, target: merge_request)
        create(:approval, user: approver, merge_request: merge_request)
427
      end.not_to change { merge_request.number_of_potential_approvers }
428 429 430 431 432 433
    end

    it "excludes the MR author" do
      expect do
        create(:approver, user: create(:user), target: merge_request)
        create(:approver, user: author, target: merge_request)
434
      end.to change { merge_request.number_of_potential_approvers }.by(1)
435 436
    end

437 438 439 440 441 442 443 444 445
    it "excludes blocked users" do
      developer = create(:user)
      blocked_developer = create(:user).tap { |u| u.block! }
      project.team << [developer, :developer]
      project.team << [blocked_developer, :developer]

      expect(merge_request.number_of_potential_approvers).to eq(1)
    end

446 447 448 449 450 451 452 453 454 455
    context "when the project is part of a group" do
      let(:group) { create(:group) }
      before { project.update_attributes(group: group) }

      it "includes group members with developer access and up" do
        expect do
          group.add_guest(create(:user))
          group.add_reporter(create(:user))
          group.add_developer(create(:user))
          group.add_master(create(:user))
456 457
          blocked_developer = create(:user).tap { |u| u.block! }
          group.add_developer(blocked_developer)
458
        end.to change { merge_request.number_of_potential_approvers }.by(2)
459 460 461 462
      end
    end
  end

Valery Sizov's avatar
Valery Sizov committed
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
  describe "#overall_approver_groups" do
    it 'returns a merge request group approver' do
      project = create :empty_project
      create :approver_group, target: project

      merge_request = create :merge_request, target_project: project, source_project: project
      approver_group2 = create :approver_group, target: merge_request

      expect(merge_request.overall_approver_groups).to eq([approver_group2])
    end

    it 'returns a project group approver' do
      project = create :empty_project
      approver_group1 = create :approver_group, target: project

      merge_request = create :merge_request, target_project: project, source_project: project

      expect(merge_request.overall_approver_groups).to eq([approver_group1])
    end

    it 'returns a merge request approver if there is no project group approver' do
      project = create :empty_project

      merge_request = create :merge_request, target_project: project, source_project: project
      approver_group1 = create :approver_group, target: merge_request

      expect(merge_request.overall_approver_groups).to eq([approver_group1])
    end
  end

  describe '#all_approvers_including_groups' do
    it 'returns correct set of users' do
      user = create :user
      user1 = create :user
      user2 = create :user
      create :user

      project = create :empty_project
      group = create :group
      group.add_master user
      create :approver_group, target: project, group: group

      merge_request = create :merge_request, target_project: project, source_project: project
      group1 = create :group
      group1.add_master user1
      create :approver_group, target: merge_request, group: group1

      create(:approver, user: user2, target: merge_request)

      expect(merge_request.all_approvers_including_groups).to match_array([user1, user2])
    end
  end

  describe '#approver_group_ids=' do
    it 'create approver_groups' do
      group = create :group
      group1 = create :group

      merge_request = create :merge_request

      merge_request.approver_group_ids = "#{group.id}, #{group1.id}"
      merge_request.save!

      expect(merge_request.approver_groups.map(&:group)).to match_array([group, group1])
    end
  end

530
  describe "#approvals_required" do
531 532
    let(:merge_request) { build(:merge_request) }
    before { merge_request.target_project.update_attributes(approvals_before_merge: 3) }
533

534 535
    context "when the MR has approvals_before_merge set" do
      before { merge_request.update_attributes(approvals_before_merge: 1) }
536

537 538 539 540 541 542 543 544 545
      it "uses the approvals_before_merge from the MR" do
        expect(merge_request.approvals_required).to eq(1)
      end
    end

    context "when the MR doesn't have approvals_before_merge set" do
      it "takes approvals_before_merge from the target project" do
        expect(merge_request.approvals_required).to eq(3)
      end
546 547 548
    end
  end

Zeger-Jan van de Weg's avatar
Zeger-Jan van de Weg committed
549
  describe '#can_remove_source_branch?' do
550 551
    let(:user) { create(:user) }
    let(:user2) { create(:user) }
552 553 554 555

    before do
      subject.source_project.team << [user, :master]

556 557 558 559
      subject.source_branch = "feature"
      subject.target_branch = "master"
      subject.save!
    end
560

561 562
    it "can't be removed when its a protected branch" do
      allow(subject.source_project).to receive(:protected_branch?).and_return(true)
563 564 565
      expect(subject.can_remove_source_branch?(user)).to be_falsey
    end

566
    it "can't remove a root ref" do
567 568
      subject.source_branch = "master"
      subject.target_branch = "feature"
569 570 571 572

      expect(subject.can_remove_source_branch?(user)).to be_falsey
    end

573 574 575 576
    it "is unable to remove the source branch for a project the user cannot push to" do
      expect(subject.can_remove_source_branch?(user2)).to be_falsey
    end

577
    it "can be removed if the last commit is the head of the source branch" do
578
      allow(subject).to receive(:source_branch_head).and_return(subject.diff_head_commit)
579

580
      expect(subject.can_remove_source_branch?(user)).to be_truthy
581
    end
582 583

    it "cannot be removed if the last commit is not also the head of the source branch" do
584 585
      subject.source_branch = "lfs"

586 587
      expect(subject.can_remove_source_branch?(user)).to be_falsey
    end
588 589
  end

590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
  describe '#merge_commit_message' do
    it 'includes merge information as the title' do
      request = build(:merge_request, source_branch: 'source', target_branch: 'target')

      expect(request.merge_commit_message)
        .to match("Merge branch 'source' into 'target'\n\n")
    end

    it 'includes its title in the body' do
      request = build(:merge_request, title: 'Remove all technical debt')

      expect(request.merge_commit_message)
        .to match("Remove all technical debt\n\n")
    end

    it 'includes its description in the body' do
      request = build(:merge_request, description: 'By removing all code')

      expect(request.merge_commit_message)
        .to match("By removing all code\n\n")
    end

    it 'includes its reference in the body' do
      request = build_stubbed(:merge_request)

      expect(request.merge_commit_message)
        .to match("See merge request #{request.to_reference}")
    end

    it 'excludes multiple linebreak runs when description is blank' do
      request = build(:merge_request, title: 'Title', description: nil)

      expect(request.merge_commit_message).not_to match("Title\n\n\n\n")
    end
  end

626
  describe "#reset_merge_when_build_succeeds" do
627 628 629 630
    let(:merge_if_green) do
      create :merge_request, merge_when_build_succeeds: true, merge_user: create(:user),
                             merge_params: { "should_remove_source_branch" => "1", "commit_message" => "msg" }
    end
631

632 633 634 635 636
    it "sets the item to false" do
      merge_if_green.reset_merge_when_build_succeeds
      merge_if_green.reload

      expect(merge_if_green.merge_when_build_succeeds).to be_falsey
637 638
      expect(merge_if_green.merge_params["should_remove_source_branch"]).to be_nil
      expect(merge_if_green.merge_params["commit_message"]).to be_nil
639 640 641
    end
  end

642
  describe "#hook_attrs" do
643 644 645 646 647 648 649 650 651 652 653
    let(:attrs_hash) { subject.hook_attrs.to_h }

    [:source, :target].each do |key|
      describe "#{key} key" do
        include_examples 'project hook data', project_key: key do
          let(:data)    { attrs_hash }
          let(:project) { subject.send("#{key}_project") }
        end
      end
    end

654
    it "has all the required keys" do
655 656 657 658
      expect(attrs_hash).to include(:source)
      expect(attrs_hash).to include(:target)
      expect(attrs_hash).to include(:last_commit)
      expect(attrs_hash).to include(:work_in_progress)
659 660 661
    end
  end

662 663 664 665
  describe '#diverged_commits_count' do
    let(:project)      { create(:project) }
    let(:fork_project) { create(:project, forked_from_project: project) }

666
    context 'when the target branch does not exist anymore' do
667 668 669 670 671 672
      subject { create(:merge_request, source_project: project, target_project: project) }

      before do
        project.repository.raw_repository.delete_branch(subject.target_branch)
        subject.reload
      end
673 674 675 676 677 678 679 680 681 682

      it 'does not crash' do
        expect{ subject.diverged_commits_count }.not_to raise_error
      end

      it 'returns 0' do
        expect(subject.diverged_commits_count).to eq(0)
      end
    end

683 684 685 686
    context 'diverged on same repository' do
      subject(:merge_request_with_divergence) { create(:merge_request, :diverged, source_project: project, target_project: project) }

      it 'counts commits that are on target branch but not on source branch' do
687
        expect(subject.diverged_commits_count).to eq(29)
688 689 690 691 692 693 694
      end
    end

    context 'diverged on fork' do
      subject(:merge_request_fork_with_divergence) { create(:merge_request, :diverged, source_project: fork_project, target_project: project) }

      it 'counts commits that are on target branch but not on source branch' do
695
        expect(subject.diverged_commits_count).to eq(29)
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726
      end
    end

    context 'rebased on fork' do
      subject(:merge_request_rebased) { create(:merge_request, :rebased, source_project: fork_project, target_project: project) }

      it 'counts commits that are on target branch but not on source branch' do
        expect(subject.diverged_commits_count).to eq(0)
      end
    end

    describe 'caching' do
      before(:example) do
        allow(Rails).to receive(:cache).and_return(ActiveSupport::Cache::MemoryStore.new)
      end

      it 'caches the output' do
        expect(subject).to receive(:compute_diverged_commits_count).
          once.
          and_return(2)

        subject.diverged_commits_count
        subject.diverged_commits_count
      end

      it 'invalidates the cache when the source sha changes' do
        expect(subject).to receive(:compute_diverged_commits_count).
          twice.
          and_return(2)

        subject.diverged_commits_count
727
        allow(subject).to receive(:source_branch_sha).and_return('123abc')
728 729 730 731 732 733 734 735 736
        subject.diverged_commits_count
      end

      it 'invalidates the cache when the target sha changes' do
        expect(subject).to receive(:compute_diverged_commits_count).
          twice.
          and_return(2)

        subject.diverged_commits_count
737
        allow(subject).to receive(:target_branch_sha).and_return('123abc')
738 739 740
        subject.diverged_commits_count
      end
    end
Valery Sizov's avatar
Valery Sizov committed
741 742
  end

743
  it_behaves_like 'an editable mentionable' do
744
    subject { create(:merge_request, :simple) }
745

746 747
    let(:backref_text) { "merge request #{subject.to_reference}" }
    let(:set_mentionable_text) { ->(txt){ subject.description = txt } }
748
  end
Vinnie Okada's avatar
Vinnie Okada committed
749 750

  it_behaves_like 'a Taskable' do
751
    subject { create :merge_request, :simple }
Vinnie Okada's avatar
Vinnie Okada committed
752
  end
753

754
  describe '#rebase_in_progress?' do
755
    it 'returns true' do
756 757
      allow(File).to receive(:exist?).and_return(true)
      allow(File).to receive(:new).and_return(double(:file, mtime: Time.now))
758

759 760 761
      expect(subject.rebase_in_progress?).to be_truthy
    end

762
    it 'returns false' do
763
      allow(File).to receive(:exist?).with(subject.rebase_dir_path).and_return(false)
764

765 766 767
      expect(subject.rebase_in_progress?).to be_falsey
    end

768
    it 'returns false if temporary file exists by is expired' do
769 770
      allow(File).to receive(:exist?).and_return(true)
      allow(File).to receive(:new).and_return(double(:file, mtime: Time.now - 2.hours))
771 772 773 774 775 776 777 778 779 780

      expect(subject.rebase_in_progress?).to be_falsey
    end

    it 'returns false if source_project is removed' do
      allow(subject).to receive(:source_project).and_return(nil)
      allow(File).to receive(:exist?).and_return(true)
      allow(File).to receive(:new).and_return(double(:file, mtime: Time.now))

      expect(File).not_to have_received(:exist?)
781 782 783 784
      expect(subject.rebase_in_progress?).to be_falsey
    end
  end

785 786 787 788 789 790
  describe '#commits_sha' do
    let(:commit0) { double('commit0', sha: 'sha1') }
    let(:commit1) { double('commit1', sha: 'sha2') }
    let(:commit2) { double('commit2', sha: 'sha3') }

    before do
791
      allow(subject.merge_request_diff).to receive(:commits).and_return([commit0, commit1, commit2])
792 793 794 795 796 797 798
    end

    it 'returns sha of commits' do
      expect(subject.commits_sha).to contain_exactly('sha1', 'sha2', 'sha3')
    end
  end

799
  describe '#pipeline' do
800
    describe 'when the source project exists' do
801
      it 'returns the latest pipeline' do
802
        pipeline = double(:ci_pipeline, ref: 'master')
803

804
        allow(subject).to receive(:diff_head_sha).and_return('123abc')
805

Lin Jen-Shin's avatar
Lin Jen-Shin committed
806 807
        expect(subject.source_project).to receive(:pipeline_for).
          with('master', '123abc').
808
          and_return(pipeline)
809

810
        expect(subject.pipeline).to eq(pipeline)
811 812 813 814 815 816 817
      end
    end

    describe 'when the source project does not exist' do
      it 'returns nil' do
        allow(subject).to receive(:source_project).and_return(nil)

818
        expect(subject.pipeline).to be_nil
819 820 821
      end
    end
  end
Yorick Peterse's avatar
Yorick Peterse committed
822

823
  describe '#all_pipelines' do
824 825 826 827 828 829 830 831 832 833 834 835 836
    shared_examples 'returning pipelines with proper ordering' do
      let!(:all_pipelines) do
        subject.all_commits_sha.map do |sha|
          create(:ci_empty_pipeline,
                 project: subject.source_project,
                 sha: sha,
                 ref: subject.source_branch)
        end
      end

      it 'returns all pipelines' do
        expect(subject.all_pipelines).not_to be_empty
        expect(subject.all_pipelines).to eq(all_pipelines.reverse)
837
      end
838 839
    end

840 841 842 843 844 845
    context 'with single merge_request_diffs' do
      it_behaves_like 'returning pipelines with proper ordering'
    end

    context 'with multiple irrelevant merge_request_diffs' do
      before do
846
        subject.update(target_branch: 'v1.0.0')
847 848 849 850 851 852 853 854 855 856 857 858 859
      end

      it_behaves_like 'returning pipelines with proper ordering'
    end

    context 'with unsaved merge request' do
      subject { build(:merge_request) }

      let!(:pipeline) do
        create(:ci_empty_pipeline,
               project: subject.project,
               sha: subject.diff_head_sha,
               ref: subject.source_branch)
860
      end
861 862 863 864 865 866 867 868

      it 'returns pipelines from diff_head_sha' do
        expect(subject.all_pipelines).to contain_exactly(pipeline)
      end
    end
  end

  describe '#all_commits_sha' do
869 870 871 872
    context 'when merge request is persisted' do
      let(:all_commits_sha) do
        subject.merge_request_diffs.flat_map(&:commits).map(&:sha).uniq
      end
873

874 875 876 877 878
      shared_examples 'returning all SHA' do
        it 'returns all SHA from all merge_request_diffs' do
          expect(subject.merge_request_diffs.size).to eq(2)
          expect(subject.all_commits_sha).to eq(all_commits_sha)
        end
879 880
      end

881 882 883 884 885 886
      context 'with a completely different branch' do
        before do
          subject.update(target_branch: 'v1.0.0')
        end

        it_behaves_like 'returning all SHA'
887 888
      end

889 890 891 892 893 894 895 896
      context 'with a branch having no difference' do
        before do
          subject.update(target_branch: 'v1.1.0')
          subject.reload # make sure commits were not cached
        end

        it_behaves_like 'returning all SHA'
      end
897 898
    end

899 900 901 902 903 904 905 906 907 908 909
    context 'when merge request is not persisted' do
      context 'when compare commits are set in the service' do
        let(:commit) { spy('commit') }

        subject do
          build(:merge_request, compare_commits: [commit, commit])
        end

        it 'returns commits from compare commits temporary data' do
          expect(subject.all_commits_sha).to eq [commit, commit]
        end
910 911
      end

912 913 914 915 916 917 918
      context 'when compare commits are not set in the service' do
        subject { build(:merge_request) }

        it 'returns array with diff head sha element only' do
          expect(subject.all_commits_sha).to eq [subject.diff_head_sha]
        end
      end
919 920 921
    end
  end

Yorick Peterse's avatar
Yorick Peterse committed
922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944
  describe '#participants' do
    let(:project) { create(:project, :public) }

    let(:mr) do
      create(:merge_request, source_project: project, target_project: project)
    end

    let!(:note1) do
      create(:note_on_merge_request, noteable: mr, project: project, note: 'a')
    end

    let!(:note2) do
      create(:note_on_merge_request, noteable: mr, project: project, note: 'b')
    end

    it 'includes the merge request author' do
      expect(mr.participants).to include(mr.author)
    end

    it 'includes the authors of the notes' do
      expect(mr.participants).to include(note1.author, note2.author)
    end
  end
945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961

  describe 'cached counts' do
    it 'updates when assignees change' do
      user1 = create(:user)
      user2 = create(:user)
      mr = create(:merge_request, assignee: user1)

      expect(user1.assigned_open_merge_request_count).to eq(1)
      expect(user2.assigned_open_merge_request_count).to eq(0)

      mr.assignee = user2
      mr.save

      expect(user1.assigned_open_merge_request_count).to eq(0)
      expect(user2.assigned_open_merge_request_count).to eq(1)
    end
  end
962 963 964 965 966 967 968 969 970

  describe '#check_if_can_be_merged' do
    let(:project) { create(:project, only_allow_merge_if_build_succeeds: true) }

    subject { create(:merge_request, source_project: project, merge_status: :unchecked) }

    context 'when it is not broken and has no conflicts' do
      it 'is marked as mergeable' do
        allow(subject).to receive(:broken?) { false }
971
        allow(project.repository).to receive(:can_be_merged?).and_return(true)
972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987

        expect { subject.check_if_can_be_merged }.to change { subject.merge_status }.to('can_be_merged')
      end
    end

    context 'when broken' do
      before { allow(subject).to receive(:broken?) { true } }

      it 'becomes unmergeable' do
        expect { subject.check_if_can_be_merged }.to change { subject.merge_status }.to('cannot_be_merged')
      end
    end

    context 'when it has conflicts' do
      before do
        allow(subject).to receive(:broken?) { false }
988
        allow(project.repository).to receive(:can_be_merged?).and_return(false)
989 990 991 992 993 994 995 996 997
      end

      it 'becomes unmergeable' do
        expect { subject.check_if_can_be_merged }.to change { subject.merge_status }.to('cannot_be_merged')
      end
    end
  end

  describe '#mergeable?' do
998 999 1000 1001
    let(:project) { create(:project) }

    subject { create(:merge_request, source_project: project) }

1002 1003
    it 'returns false if #mergeable_state? is false' do
      expect(subject).to receive(:mergeable_state?) { false }
1004

1005
      expect(subject.mergeable?).to be_falsey
1006 1007
    end

1008
    it 'return true if #mergeable_state? is true and the MR #can_be_merged? is true' do
1009 1010
      allow(subject).to receive(:mergeable_state?) { true }
      expect(subject).to receive(:check_if_can_be_merged)
1011
      expect(subject).to receive(:can_be_merged?) { true }
1012 1013 1014 1015 1016 1017 1018

      expect(subject.mergeable?).to be_truthy
    end
  end

  describe '#mergeable_state?' do
    let(:project) { create(:project) }
1019 1020 1021

    subject { create(:merge_request, source_project: project) }

1022
    it 'checks if merge request can be merged' do
1023
      allow(subject).to receive(:mergeable_ci_state?) { true }
1024 1025 1026 1027 1028 1029 1030 1031 1032
      expect(subject).to receive(:check_if_can_be_merged)

      subject.mergeable?
    end

    context 'when not open' do
      before { subject.close }

      it 'returns false' do
1033
        expect(subject.mergeable_state?).to be_falsey
1034 1035 1036 1037 1038 1039 1040
      end
    end

    context 'when working in progress' do
      before { subject.title = 'WIP MR' }

      it 'returns false' do
1041
        expect(subject.mergeable_state?).to be_falsey
1042 1043 1044 1045 1046 1047 1048
      end
    end

    context 'when broken' do
      before { allow(subject).to receive(:broken?) { true } }

      it 'returns false' do
1049
        expect(subject.mergeable_state?).to be_falsey
1050 1051 1052 1053
      end
    end

    context 'when failed' do
1054
      context 'when #mergeable_ci_state? is false' do
1055
        before do
1056
          allow(subject).to receive(:mergeable_ci_state?) { false }
1057 1058 1059 1060
        end

        it 'returns false' do
          expect(subject.mergeable_state?).to be_falsey
1061 1062
        end
      end
1063

1064
      context 'when #mergeable_discussions_state? is false' do
1065 1066 1067 1068 1069 1070 1071 1072
        before do
          allow(subject).to receive(:mergeable_discussions_state?) { false }
        end

        it 'returns false' do
          expect(subject.mergeable_state?).to be_falsey
        end
      end
1073 1074 1075
    end
  end

1076
  describe '#mergeable_ci_state?' do
1077
    let(:project) { create(:empty_project, only_allow_merge_if_build_succeeds: true) }
1078
    let(:pipeline) { create(:ci_empty_pipeline) }
1079 1080 1081

    subject { build(:merge_request, target_project: project) }

1082
    context 'when it is only allowed to merge when build is green' do
1083
      context 'and a failed pipeline is associated' do
1084
        before do
1085
          pipeline.update(status: 'failed')
1086
          allow(subject).to receive(:pipeline) { pipeline }
1087
        end
1088

1089
        it { expect(subject.mergeable_ci_state?).to be_falsey }
1090 1091
      end

1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
      context 'and a successful pipeline is associated' do
        before do
          pipeline.update(status: 'success')
          allow(subject).to receive(:pipeline) { pipeline }
        end

        it { expect(subject.mergeable_ci_state?).to be_truthy }
      end

      context 'and a skipped pipeline is associated' do
        before do
          pipeline.update(status: 'skipped')
          allow(subject).to receive(:pipeline) { pipeline }
        end

        it { expect(subject.mergeable_ci_state?).to be_truthy }
      end

1110
      context 'when no pipeline is associated' do
1111
        before do
1112
          allow(subject).to receive(:pipeline) { nil }
1113 1114 1115
        end

        it { expect(subject.mergeable_ci_state?).to be_truthy }
1116 1117 1118
      end
    end

1119
    context 'when merges are not restricted to green builds' do
1120 1121
      subject { build(:merge_request, target_project: build(:empty_project, only_allow_merge_if_build_succeeds: false)) }

1122
      context 'and a failed pipeline is associated' do
1123
        before do
1124 1125
          pipeline.statuses << create(:commit_status, status: 'failed', project: project)
          allow(subject).to receive(:pipeline) { pipeline }
1126 1127 1128 1129 1130
        end

        it { expect(subject.mergeable_ci_state?).to be_truthy }
      end

1131
      context 'when no pipeline is associated' do
1132
        before do
1133
          allow(subject).to receive(:pipeline) { nil }
1134 1135 1136
        end

        it { expect(subject.mergeable_ci_state?).to be_truthy }
1137 1138 1139
      end
    end
  end
1140

1141
  describe '#mergeable_discussions_state?' do
1142
    let(:merge_request) { create(:merge_request_with_diff_notes, source_project: project) }
1143

1144 1145
    context 'when project.only_allow_merge_if_all_discussions_are_resolved == true' do
      let(:project) { create(:project, only_allow_merge_if_all_discussions_are_resolved: true) }
1146

1147
      context 'with all discussions resolved' do
1148
        before do
1149
          merge_request.discussions.each { |d| d.resolve!(merge_request.author) }
1150 1151 1152
        end

        it 'returns true' do
1153
          expect(merge_request.mergeable_discussions_state?).to be_truthy
1154 1155 1156
        end
      end

1157
      context 'with unresolved discussions' do
1158
        before do
1159
          merge_request.discussions.each(&:unresolve!)
1160 1161 1162
        end

        it 'returns false' do
1163
          expect(merge_request.mergeable_discussions_state?).to be_falsey
1164 1165
        end
      end
1166 1167 1168 1169 1170 1171 1172 1173 1174 1175

      context 'with no discussions' do
        before do
          merge_request.notes.destroy_all
        end

        it 'returns true' do
          expect(merge_request.mergeable_discussions_state?).to be_truthy
        end
      end
1176 1177
    end

1178 1179
    context 'when project.only_allow_merge_if_all_discussions_are_resolved == false' do
      let(:project) { create(:project, only_allow_merge_if_all_discussions_are_resolved: false) }
1180

1181
      context 'with unresolved discussions' do
1182
        before do
1183
          merge_request.discussions.each(&:unresolve!)
1184 1185 1186
        end

        it 'returns true' do
1187
          expect(merge_request.mergeable_discussions_state?).to be_truthy
1188 1189 1190 1191 1192 1193
        end
      end
    end
  end

  describe "#environments" do
Z.J. van de Weg's avatar
Z.J. van de Weg committed
1194 1195 1196
    let(:project)       { create(:project) }
    let(:merge_request) { create(:merge_request, source_project: project) }

1197 1198
    context 'with multiple environments' do
      let(:environments) { create_list(:environment, 3, project: project) }
1199

1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243
      before do
        create(:deployment, environment: environments.first, ref: 'master', sha: project.commit('master').id)
        create(:deployment, environment: environments.second, ref: 'feature', sha: project.commit('feature').id)
      end

      it 'selects deployed environments' do
        expect(merge_request.environments).to contain_exactly(environments.first)
      end
    end

    context 'with environments on source project' do
      let(:source_project) do
        create(:project) do |fork_project|
          fork_project.create_forked_project_link(forked_to_project_id: fork_project.id, forked_from_project_id: project.id)
        end
      end

      let(:merge_request) do
        create(:merge_request,
               source_project: source_project, source_branch: 'feature',
               target_project: project)
      end

      let(:source_environment) { create(:environment, project: source_project) }

      before do
        create(:deployment, environment: source_environment, ref: 'feature', sha: merge_request.diff_head_sha)
      end

      it 'selects deployed environments' do
        expect(merge_request.environments).to contain_exactly(source_environment)
      end

      context 'with environments on target project' do
        let(:target_environment) { create(:environment, project: project) }

        before do
          create(:deployment, environment: target_environment, tag: true, sha: merge_request.diff_head_sha)
        end

        it 'selects deployed environments' do
          expect(merge_request.environments).to contain_exactly(source_environment, target_environment)
        end
      end
1244 1245 1246 1247 1248 1249 1250 1251 1252 1253
    end

    context 'without a diff_head_commit' do
      before do
        expect(merge_request).to receive(:diff_head_commit).and_return(nil)
      end

      it 'returns an empty array' do
        expect(merge_request.environments).to be_empty
      end
Z.J. van de Weg's avatar
Z.J. van de Weg committed
1254 1255 1256
    end
  end

1257 1258 1259 1260 1261
  describe "#reload_diff" do
    let(:note) { create(:diff_note_on_merge_request, project: subject.project, noteable: subject) }

    let(:commit) { subject.project.commit(sample_commit.id) }

1262
    it "does not change existing merge request diff" do
1263
      expect(subject.merge_request_diff).not_to receive(:save_git_content)
1264 1265 1266
      subject.reload_diff
    end

1267 1268 1269 1270
    it "creates new merge request diff" do
      expect { subject.reload_diff }.to change { subject.merge_request_diffs.count }.by(1)
    end

1271 1272 1273 1274 1275 1276
    it "executs diff cache service" do
      expect_any_instance_of(MergeRequests::MergeRequestDiffCacheService).to receive(:execute).with(subject)

      subject.reload_diff
    end

1277 1278 1279 1280
    it "updates diff note positions" do
      old_diff_refs = subject.diff_refs

      # Update merge_request_diff so that #diff_refs will return commit.diff_refs
1281 1282 1283 1284 1285 1286
      allow(subject).to receive(:create_merge_request_diff) do
        subject.merge_request_diffs.create(
          base_commit_sha: commit.parent_id,
          start_commit_sha: commit.parent_id,
          head_commit_sha: commit.sha
        )
1287 1288

        subject.merge_request_diff(true)
1289 1290 1291 1292 1293 1294 1295 1296 1297 1298
      end

      expect(Notes::DiffPositionUpdateService).to receive(:new).with(
        subject.project,
        nil,
        old_diff_refs: old_diff_refs,
        new_diff_refs: commit.diff_refs,
        paths: note.position.paths
      ).and_call_original

1299
      expect_any_instance_of(Notes::DiffPositionUpdateService).to receive(:execute).with(note)
1300 1301 1302 1303 1304
      expect_any_instance_of(DiffNote).to receive(:save).once

      subject.reload_diff
    end
  end
1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315

  describe 'approvals' do
    let(:project) { create(:empty_project) }
    let(:merge_request) { create(:merge_request, source_project: project, author: author) }
    let(:author) { create(:user) }
    let(:approver) { create(:user) }

    context 'on a project with only one member' do
      context 'when there is one approver' do
        before { project.update_attributes(approvals_before_merge: 1) }

1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328
        context 'when that approver is the MR author' do
          before do
            project.team << [author, :developer]
            create(:approver, user: author, target: merge_request)
          end

          it 'does not require approval for the merge request' do
            expect(merge_request.approvals_left).to eq(0)
          end

          it 'does not allow the approver to approve the MR' do
            expect(merge_request.can_approve?(author)).to be_falsey
          end
1329 1330 1331 1332

          it 'does not allow a logged-out user to approve the MR' do
            expect(merge_request.can_approve?(nil)).to be_falsey
          end
1333 1334
        end

1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347
        context 'when that approver is not the MR author' do
          before do
            project.team << [approver, :developer]
            create(:approver, user: approver, target: merge_request)
          end

          it 'requires one approval' do
            expect(merge_request.approvals_left).to eq(1)
          end

          it 'allows the approver to approve the MR' do
            expect(merge_request.can_approve?(approver)).to be_truthy
          end
1348 1349 1350 1351

          it 'does not allow a logged-out user to approve the MR' do
            expect(merge_request.can_approve?(nil)).to be_falsey
          end
1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389
        end
      end
    end

    context 'on a project with several members' do
      let(:approver_2) { create(:user) }
      let(:developer) { create(:user) }
      let(:reporter) { create(:user) }
      let(:stranger) { create(:user) }

      before do
        project.team << [author, :developer]
        project.team << [approver, :developer]
        project.team << [approver_2, :developer]
        project.team << [developer, :developer]
        project.team << [reporter, :reporter]
      end

      context 'when there is one approver required' do
        before { project.update_attributes(approvals_before_merge: 1) }

        context 'when that approver is the MR author' do
          before { create(:approver, user: author, target: merge_request) }

          it 'requires one approval' do
            expect(merge_request.approvals_left).to eq(1)
          end

          it 'does not allow the author to approve the MR' do
            expect(merge_request.can_approve?(author)).to be_falsey
          end

          it 'allows any other project member with write access to approve the MR' do
            expect(merge_request.can_approve?(developer)).to be_truthy

            expect(merge_request.can_approve?(reporter)).to be_falsey
            expect(merge_request.can_approve?(stranger)).to be_falsey
          end
1390 1391 1392 1393

          it 'does not allow a logged-out user to approve the MR' do
            expect(merge_request.can_approve?(nil)).to be_falsey
          end
1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409
        end

        context 'when that approver is not the MR author' do
          before { create(:approver, user: approver, target: merge_request) }

          it 'requires one approval' do
            expect(merge_request.approvals_left).to eq(1)
          end

          it 'only allows the approver to approve the MR' do
            expect(merge_request.can_approve?(approver)).to be_truthy

            expect(merge_request.can_approve?(author)).to be_falsey
            expect(merge_request.can_approve?(developer)).to be_falsey
            expect(merge_request.can_approve?(reporter)).to be_falsey
            expect(merge_request.can_approve?(stranger)).to be_falsey
1410
            expect(merge_request.can_approve?(nil)).to be_falsey
1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436
          end
        end
      end

      context 'when there are multiple approvers required' do
        before { project.update_attributes(approvals_before_merge: 3) }

        context 'when one of those approvers is the MR author' do
          before do
            create(:approver, user: author, target: merge_request)
            create(:approver, user: approver, target: merge_request)
            create(:approver, user: approver_2, target: merge_request)
          end

          it 'requires the original number of approvals' do
            expect(merge_request.approvals_left).to eq(3)
          end

          it 'does not allow the author to approve the MR' do
            expect(merge_request.can_approve?(author)).to be_falsey
          end

          it 'allows any other other approver to approve the MR' do
            expect(merge_request.can_approve?(approver)).to be_truthy
          end

1437 1438 1439 1440
          it 'does not allow a logged-out user to approve the MR' do
            expect(merge_request.can_approve?(nil)).to be_falsey
          end

1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464
          context 'when all of the valid approvers have approved the MR' do
            before do
              create(:approval, user: approver, merge_request: merge_request)
              create(:approval, user: approver_2, merge_request: merge_request)
            end

            it 'requires the original number of approvals' do
              expect(merge_request.approvals_left).to eq(1)
            end

            it 'does not allow the author to approve the MR' do
              expect(merge_request.can_approve?(author)).to be_falsey
            end

            it 'does not allow the approvers to approve the MR again' do
              expect(merge_request.can_approve?(approver)).to be_falsey
              expect(merge_request.can_approve?(approver_2)).to be_falsey
            end

            it 'allows any other project member with write access to approve the MR' do
              expect(merge_request.can_approve?(developer)).to be_truthy

              expect(merge_request.can_approve?(reporter)).to be_falsey
              expect(merge_request.can_approve?(stranger)).to be_falsey
1465
              expect(merge_request.can_approve?(nil)).to be_falsey
1466 1467
            end
          end
1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483

          context 'when more than the number of approvers have approved the MR' do
            before do
              create(:approval, user: approver, merge_request: merge_request)
              create(:approval, user: approver_2, merge_request: merge_request)
              create(:approval, user: developer, merge_request: merge_request)
            end

            it 'marks the MR as approved' do
              expect(merge_request).to be_approved
            end

            it 'clamps the approvals left at zero' do
              expect(merge_request.approvals_left).to eq(0)
            end
          end
1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504
        end

        context 'when the approvers do not contain the MR author' do
          before do
            create(:approver, user: developer, target: merge_request)
            create(:approver, user: approver, target: merge_request)
            create(:approver, user: approver_2, target: merge_request)
          end

          it 'requires the original number of approvals' do
            expect(merge_request.approvals_left).to eq(3)
          end

          it 'only allows the approvers to approve the MR' do
            expect(merge_request.can_approve?(developer)).to be_truthy
            expect(merge_request.can_approve?(approver)).to be_truthy
            expect(merge_request.can_approve?(approver_2)).to be_truthy

            expect(merge_request.can_approve?(author)).to be_falsey
            expect(merge_request.can_approve?(reporter)).to be_falsey
            expect(merge_request.can_approve?(stranger)).to be_falsey
1505
            expect(merge_request.can_approve?(nil)).to be_falsey
1506 1507 1508 1509 1510
          end
        end
      end
    end
  end
1511

1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524
  describe '#branch_merge_base_commit' do
    context 'source and target branch exist' do
      it { expect(subject.branch_merge_base_commit.sha).to eq('ae73cb07c9eeaf35924a10f713b364d32b2dd34f') }
      it { expect(subject.branch_merge_base_commit).to be_a(Commit) }
    end

    context 'when the target branch does not exist' do
      before do
        subject.project.repository.raw_repository.delete_branch(subject.target_branch)
      end

      it 'returns nil' do
        expect(subject.branch_merge_base_commit).to be_nil
1525 1526 1527 1528
      end
    end
  end

1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551
  describe "#diff_sha_refs" do
    context "with diffs" do
      subject { create(:merge_request, :with_diffs) }

      it "does not touch the repository" do
        subject # Instantiate the object

        expect_any_instance_of(Repository).not_to receive(:commit)

        subject.diff_sha_refs
      end

      it "returns expected diff_refs" do
        expected_diff_refs = Gitlab::Diff::DiffRefs.new(
          base_sha:  subject.merge_request_diff.base_commit_sha,
          start_sha: subject.merge_request_diff.start_commit_sha,
          head_sha:  subject.merge_request_diff.head_commit_sha
        )

        expect(subject.diff_sha_refs).to eq(expected_diff_refs)
      end
    end
  end
1552 1553 1554 1555 1556 1557 1558

  context "discussion status" do
    let(:first_discussion) { Discussion.new([create(:diff_note_on_merge_request)]) }
    let(:second_discussion) { Discussion.new([create(:diff_note_on_merge_request)]) }
    let(:third_discussion) { Discussion.new([create(:diff_note_on_merge_request)]) }

    before do
1559
      allow(subject).to receive(:diff_discussions).and_return([first_discussion, second_discussion, third_discussion])
1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641
    end

    describe "#discussions_resolvable?" do
      context "when all discussions are unresolvable" do
        before do
          allow(first_discussion).to receive(:resolvable?).and_return(false)
          allow(second_discussion).to receive(:resolvable?).and_return(false)
          allow(third_discussion).to receive(:resolvable?).and_return(false)
        end

        it "returns false" do
          expect(subject.discussions_resolvable?).to be false
        end
      end

      context "when some discussions are unresolvable and some discussions are resolvable" do
        before do
          allow(first_discussion).to receive(:resolvable?).and_return(true)
          allow(second_discussion).to receive(:resolvable?).and_return(false)
          allow(third_discussion).to receive(:resolvable?).and_return(true)
        end

        it "returns true" do
          expect(subject.discussions_resolvable?).to be true
        end
      end

      context "when all discussions are resolvable" do
        before do
          allow(first_discussion).to receive(:resolvable?).and_return(true)
          allow(second_discussion).to receive(:resolvable?).and_return(true)
          allow(third_discussion).to receive(:resolvable?).and_return(true)
        end

        it "returns true" do
          expect(subject.discussions_resolvable?).to be true
        end
      end
    end

    describe "#discussions_resolved?" do
      context "when discussions are not resolvable" do
        before do
          allow(subject).to receive(:discussions_resolvable?).and_return(false)
        end

        it "returns false" do
          expect(subject.discussions_resolved?).to be false
        end
      end

      context "when discussions are resolvable" do
        before do
          allow(subject).to receive(:discussions_resolvable?).and_return(true)

          allow(first_discussion).to receive(:resolvable?).and_return(true)
          allow(second_discussion).to receive(:resolvable?).and_return(false)
          allow(third_discussion).to receive(:resolvable?).and_return(true)
        end

        context "when all resolvable discussions are resolved" do
          before do
            allow(first_discussion).to receive(:resolved?).and_return(true)
            allow(third_discussion).to receive(:resolved?).and_return(true)
          end

          it "returns true" do
            expect(subject.discussions_resolved?).to be true
          end
        end

        context "when some resolvable discussions are not resolved" do
          before do
            allow(first_discussion).to receive(:resolved?).and_return(true)
            allow(third_discussion).to receive(:resolved?).and_return(false)
          end

          it "returns false" do
            expect(subject.discussions_resolved?).to be false
          end
        end
      end
1642
    end
1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686

    describe "#discussions_to_be_resolved?" do
      context "when discussions are not resolvable" do
        before do
          allow(subject).to receive(:discussions_resolvable?).and_return(false)
        end

        it "returns false" do
          expect(subject.discussions_to_be_resolved?).to be false
        end
      end

      context "when discussions are resolvable" do
        before do
          allow(subject).to receive(:discussions_resolvable?).and_return(true)

          allow(first_discussion).to receive(:resolvable?).and_return(true)
          allow(second_discussion).to receive(:resolvable?).and_return(false)
          allow(third_discussion).to receive(:resolvable?).and_return(true)
        end

        context "when all resolvable discussions are resolved" do
          before do
            allow(first_discussion).to receive(:resolved?).and_return(true)
            allow(third_discussion).to receive(:resolved?).and_return(true)
          end

          it "returns false" do
            expect(subject.discussions_to_be_resolved?).to be false
          end
        end

        context "when some resolvable discussions are not resolved" do
          before do
            allow(first_discussion).to receive(:resolved?).and_return(true)
            allow(third_discussion).to receive(:resolved?).and_return(false)
          end

          it "returns true" do
            expect(subject.discussions_to_be_resolved?).to be true
          end
        end
      end
    end
1687
  end
1688

1689
  describe '#conflicts_can_be_resolved_in_ui?' do
1690 1691 1692 1693 1694 1695 1696 1697 1698 1699
    def create_merge_request(source_branch)
      create(:merge_request, source_branch: source_branch, target_branch: 'conflict-start') do |mr|
        mr.mark_as_unmergeable
      end
    end

    it 'returns a falsey value when the MR can be merged without conflicts' do
      merge_request = create_merge_request('master')
      merge_request.mark_as_mergeable

1700
      expect(merge_request.conflicts_can_be_resolved_in_ui?).to be_falsey
1701 1702
    end

1703 1704 1705 1706 1707 1708
    it 'returns a falsey value when the MR is marked as having conflicts, but has none' do
      merge_request = create_merge_request('master')

      expect(merge_request.conflicts_can_be_resolved_in_ui?).to be_falsey
    end

1709 1710 1711 1712 1713 1714 1715
    it 'returns a falsey value when the MR has a missing ref after a force push' do
      merge_request = create_merge_request('conflict-resolvable')
      allow(merge_request.conflicts).to receive(:merge_index).and_raise(Rugged::OdbError)

      expect(merge_request.conflicts_can_be_resolved_in_ui?).to be_falsey
    end

1716 1717 1718 1719
    it 'returns a falsey value when the MR does not support new diff notes' do
      merge_request = create_merge_request('conflict-resolvable')
      merge_request.merge_request_diff.update_attributes(start_commit_sha: nil)

1720
      expect(merge_request.conflicts_can_be_resolved_in_ui?).to be_falsey
1721 1722 1723 1724 1725
    end

    it 'returns a falsey value when the conflicts contain a large file' do
      merge_request = create_merge_request('conflict-too-large')

1726
      expect(merge_request.conflicts_can_be_resolved_in_ui?).to be_falsey
1727 1728 1729 1730 1731
    end

    it 'returns a falsey value when the conflicts contain a binary file' do
      merge_request = create_merge_request('conflict-binary-file')

1732
      expect(merge_request.conflicts_can_be_resolved_in_ui?).to be_falsey
1733 1734 1735 1736 1737
    end

    it 'returns a falsey value when the conflicts contain a file edited in one branch and deleted in another' do
      merge_request = create_merge_request('conflict-missing-side')

1738
      expect(merge_request.conflicts_can_be_resolved_in_ui?).to be_falsey
1739 1740 1741 1742 1743
    end

    it 'returns a truthy value when the conflicts are resolvable in the UI' do
      merge_request = create_merge_request('conflict-resolvable')

1744
      expect(merge_request.conflicts_can_be_resolved_in_ui?).to be_truthy
1745
    end
Sean McGivern's avatar
Sean McGivern committed
1746 1747 1748 1749 1750 1751

    it 'returns a truthy value when the conflicts have to be resolved in an editor' do
      merge_request = create_merge_request('conflict-contains-conflict-markers')

      expect(merge_request.conflicts_can_be_resolved_in_ui?).to be_truthy
    end
1752
  end
1753

1754
  describe "#source_project_missing?" do
1755 1756 1757 1758 1759
    let(:project)      { create(:project) }
    let(:fork_project) { create(:project, forked_from_project: project) }
    let(:user)         { create(:user) }
    let(:unlink_project) { Projects::UnlinkForkService.new(fork_project, user) }

Katarzyna Kobierska's avatar
Katarzyna Kobierska committed
1760
    context "when the fork exists" do
1761 1762 1763 1764 1765 1766
      let(:merge_request) do
        create(:merge_request,
          source_project: fork_project,
          target_project: project)
      end

1767
      it { expect(merge_request.source_project_missing?).to be_falsey }
1768 1769
    end

Katarzyna Kobierska's avatar
Katarzyna Kobierska committed
1770
    context "when the source project is the same as the target project" do
1771 1772
      let(:merge_request) { create(:merge_request, source_project: project) }

1773
      it { expect(merge_request.source_project_missing?).to be_falsey }
1774 1775
    end

Katarzyna Kobierska's avatar
Katarzyna Kobierska committed
1776
    context "when the fork does not exist" do
1777 1778 1779 1780 1781 1782
      let(:merge_request) do
        create(:merge_request,
          source_project: fork_project,
          target_project: project)
      end

Katarzyna Kobierska's avatar
Katarzyna Kobierska committed
1783
      it "returns true" do
1784 1785 1786
        unlink_project.execute
        merge_request.reload

1787
        expect(merge_request.source_project_missing?).to be_truthy
1788 1789 1790 1791 1792 1793 1794 1795 1796 1797
      end
    end
  end

  describe "#closed_without_fork?" do
    let(:project)      { create(:project) }
    let(:fork_project) { create(:project, forked_from_project: project) }
    let(:user)         { create(:user) }
    let(:unlink_project) { Projects::UnlinkForkService.new(fork_project, user) }

Katarzyna Kobierska's avatar
Katarzyna Kobierska committed
1798
    context "when the merge request is closed" do
1799 1800 1801 1802 1803 1804
      let(:closed_merge_request) do
        create(:closed_merge_request,
          source_project: fork_project,
          target_project: project)
      end

Katarzyna Kobierska's avatar
Katarzyna Kobierska committed
1805
      it "returns false if the fork exist" do
1806 1807 1808
        expect(closed_merge_request.closed_without_fork?).to be_falsey
      end

Katarzyna Kobierska's avatar
Katarzyna Kobierska committed
1809
      it "returns true if the fork does not exist" do
1810 1811 1812 1813 1814 1815
        unlink_project.execute
        closed_merge_request.reload

        expect(closed_merge_request.closed_without_fork?).to be_truthy
      end
    end
Katarzyna Kobierska's avatar
Katarzyna Kobierska committed
1816

Katarzyna Kobierska's avatar
Katarzyna Kobierska committed
1817
    context "when the merge request is open" do
Katarzyna Kobierska's avatar
Katarzyna Kobierska committed
1818 1819 1820 1821 1822 1823 1824 1825 1826 1827
      let(:open_merge_request) do
        create(:merge_request,
          source_project: fork_project,
          target_project: project)
      end

      it "returns false" do
        expect(open_merge_request.closed_without_fork?).to be_falsey
      end
    end
1828
  end
1829

1830
  describe '#reopenable?' do
1831 1832 1833
    context 'when the merge request is closed' do
      it 'returns true' do
        subject.close
Katarzyna Kobierska's avatar
Katarzyna Kobierska committed
1834

1835
        expect(subject.reopenable?).to be_truthy
1836 1837 1838 1839 1840
      end

      context 'forked project' do
        let(:project)      { create(:project) }
        let(:user)         { create(:user) }
1841
        let(:fork_project) { create(:project, forked_from_project: project, namespace: user.namespace) }
1842 1843

        let!(:merge_request) do
1844 1845 1846 1847 1848 1849 1850 1851
          create(:closed_merge_request,
            source_project: fork_project,
            target_project: project)
        end

        it 'returns false if unforked' do
          Projects::UnlinkForkService.new(fork_project, user).execute

1852
          expect(merge_request.reload.reopenable?).to be_falsey
1853 1854 1855
        end

        it 'returns false if the source project is deleted' do
1856
          Projects::DestroyService.new(fork_project, user).execute
1857

1858
          expect(merge_request.reload.reopenable?).to be_falsey
1859 1860
        end

Katarzyna Kobierska's avatar
Katarzyna Kobierska committed
1861
        it 'returns false if the merge request is merged' do
1862 1863
          merge_request.update_attributes(state: 'merged')

1864
          expect(merge_request.reload.reopenable?).to be_falsey
1865 1866
        end
      end
Katarzyna Kobierska's avatar
Katarzyna Kobierska committed
1867 1868
    end

Katarzyna Kobierska's avatar
Katarzyna Kobierska committed
1869
    context 'when the merge request is opened' do
1870
      it 'returns false' do
1871
        expect(subject.reopenable?).to be_falsey
1872
      end
Katarzyna Kobierska's avatar
Katarzyna Kobierska committed
1873 1874
    end
  end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
1875
end