BigW Consortium Gitlab

merge_requests_helper_spec.rb 5.92 KB
Newer Older
1 2 3
require 'spec_helper'

describe MergeRequestsHelper do
4
  describe 'ci_build_details_path' do
5
    let(:project) { create(:empty_project) }
6 7
    let(:merge_request) { MergeRequest.new }
    let(:ci_service) { CiService.new }
8
    let(:last_commit) { Ci::Pipeline.new({}) }
9 10 11 12 13 14 15 16 17 18 19

    before do
      allow(merge_request).to receive(:source_project).and_return(project)
      allow(merge_request).to receive(:last_commit).and_return(last_commit)
      allow(project).to receive(:ci_service).and_return(ci_service)
      allow(last_commit).to receive(:sha).and_return('12d65c')
    end

    it 'does not include api credentials in a link' do
      allow(ci_service).
        to receive(:build_page).and_return("http://secretuser:secretpass@jenkins.example.com:8888/job/test1/scm/bySHA1/12d65c")
20
      expect(helper.ci_build_details_path(merge_request)).not_to match("secret")
21 22 23
    end
  end

Drew Blessing committed
24
  describe '#issues_sentence' do
25 26 27 28 29 30
    subject { issues_sentence(issues) }
    let(:issues) do
      [build(:issue, iid: 1), build(:issue, iid: 2), build(:issue, iid: 3)]
    end

    it { is_expected.to eq('#1, #2, and #3') }
Drew Blessing committed
31 32

    context 'for JIRA issues' do
33
      let(:project) { create(:empty_project) }
Drew Blessing committed
34 35
      let(:issues) do
        [
36 37 38
          ExternalIssue.new('JIRA-123', project),
          ExternalIssue.new('JIRA-456', project),
          ExternalIssue.new('FOOBAR-7890', project)
Drew Blessing committed
39 40 41 42 43
        ]
      end

      it { is_expected.to eq('FOOBAR-7890, JIRA-123, and JIRA-456') }
    end
44 45
  end

Drew Blessing committed
46 47
  describe '#format_mr_branch_names' do
    describe 'within the same project' do
48 49 50 51 52 53
      let(:merge_request) { create(:merge_request) }
      subject { format_mr_branch_names(merge_request) }

      it { is_expected.to eq([merge_request.source_branch, merge_request.target_branch]) }
    end

Drew Blessing committed
54
    describe 'within different projects' do
55 56
      let(:project) { create(:empty_project) }
      let(:fork_project) { create(:empty_project, forked_from_project: project) }
57 58 59 60 61 62 63 64
      let(:merge_request) { create(:merge_request, source_project: fork_project, target_project: project) }
      subject { format_mr_branch_names(merge_request) }
      let(:source_title) { "#{fork_project.path_with_namespace}:#{merge_request.source_branch}" }
      let(:target_title) { "#{project.path_with_namespace}:#{merge_request.target_branch}" }

      it { is_expected.to eq([source_title, target_title]) }
    end
  end
Jarka Kadlecova committed
65

Jarka Kadlecova committed
66 67 68 69 70
  describe '#mr_widget_refresh_url' do
    let(:guest)         { create(:user) }
    let(:project)       { create(:project, :public) }
    let(:project_fork)  { Projects::ForkService.new(project, guest).execute }
    let(:merge_request) { create(:merge_request, source_project: project_fork, target_project: project) }
Jarka Kadlecova committed
71 72 73 74 75 76 77 78

    it 'returns correct url for MR' do
      expected_url = "#{project.path_with_namespace}/merge_requests/#{merge_request.iid}/merge_widget_refresh"

      expect(mr_widget_refresh_url(merge_request)).to end_with(expected_url)
    end

    it 'returns empty string for nil' do
Jarka Kadlecova committed
79
      expect(mr_widget_refresh_url(nil)).to eq('')
Jarka Kadlecova committed
80 81
    end
  end
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163

  describe '#mr_closes_issues' do
    let(:user_1) { create(:user) }
    let(:user_2) { create(:user) }

    let(:project_1) { create(:project, :private, creator: user_1, namespace: user_1.namespace) }
    let(:project_2) { create(:project, :private, creator: user_2, namespace: user_2.namespace) }

    let(:issue_1) { create(:issue, project: project_1) }
    let(:issue_2) { create(:issue, project: project_2) }

    let(:merge_request) { create(:merge_request, source_project: project_1, target_project: project_1,) }

    let(:merge_request) do
      create(:merge_request,
             source_project: project_1, target_project: project_1,
             description: "Fixes #{issue_1.to_reference} Fixes #{issue_2.to_reference(project_1)}")
    end

    before do
      project_1.team << [user_2, :developer]
      project_2.team << [user_2, :developer]
      allow(merge_request.project).to receive(:default_branch).and_return(merge_request.target_branch)
      @merge_request = merge_request
    end

    context 'user without access to another private project' do
      let(:current_user) { user_1 }

      it 'cannot see that project\'s issue that will be closed on acceptance' do
        expect(mr_closes_issues).to contain_exactly(issue_1)
      end
    end

    context 'user with access to another private project' do
      let(:current_user) { user_2 }

      it 'can see that project\'s issue that will be closed on acceptance' do
        expect(mr_closes_issues).to contain_exactly(issue_1, issue_2)
      end
    end
  end

  describe '#mr_issues_mentioned_but_not_closing' do
    let(:user_1) { create(:user) }
    let(:user_2) { create(:user) }

    let(:project_1) { create(:project, :private, creator: user_1, namespace: user_1.namespace) }
    let(:project_2) { create(:project, :private, creator: user_2, namespace: user_2.namespace) }

    let(:issue_1) { create(:issue, project: project_1) }
    let(:issue_2) { create(:issue, project: project_2) }

    let(:merge_request) do
      create(:merge_request,
             source_project: project_1, target_project: project_1,
             description: "#{issue_1.to_reference} #{issue_2.to_reference(project_1)}")
    end

    before do
      project_1.team << [user_2, :developer]
      project_2.team << [user_2, :developer]
      allow(merge_request.project).to receive(:default_branch).and_return(merge_request.target_branch)
      @merge_request = merge_request
    end

    context 'user without access to another private project' do
      let(:current_user) { user_1 }

      it 'cannot see that project\'s issue that will be closed on acceptance' do
        expect(mr_issues_mentioned_but_not_closing).to contain_exactly(issue_1)
      end
    end

    context 'user with access to another private project' do
      let(:current_user) { user_2 }

      it 'can see that project\'s issue that will be closed on acceptance' do
        expect(mr_issues_mentioned_but_not_closing).to contain_exactly(issue_1, issue_2)
      end
    end
  end
164
end