jira_service_spec.rb 7.3 KB
Newer Older
1 2
require 'spec_helper'

D
Douwe Maan 已提交
3
describe JiraService, models: true do
4 5
  include Gitlab::Routing.url_helpers

6
  describe "Associations" do
7 8
    it { is_expected.to belong_to :project }
    it { is_expected.to have_one :service_hook }
9 10
  end

11 12 13 14
  describe 'Validations' do
    context 'when service is active' do
      before { subject.active = true }

F
Felipe Artur 已提交
15 16 17
      it { is_expected.to validate_presence_of(:url) }
      it { is_expected.to validate_presence_of(:project_key) }
      it_behaves_like 'issue tracker service URL attribute', :url
18 19 20 21 22
    end

    context 'when service is inactive' do
      before { subject.active = false }

F
Felipe Artur 已提交
23
      it { is_expected.not_to validate_presence_of(:url) }
24 25 26
    end
  end

27 28 29 30 31 32 33 34 35
  describe '#reference_pattern' do
    it_behaves_like 'allows project key on reference pattern'

    it 'does not allow # on the code' do
      expect(subject.reference_pattern.match('#123')).to be_nil
      expect(subject.reference_pattern.match('1#23#12')).to be_nil
    end
  end

D
Drew Blessing 已提交
36 37 38 39 40 41 42 43 44 45 46
  describe "Execute" do
    let(:user)    { create(:user) }
    let(:project) { create(:project) }
    let(:merge_request) { create(:merge_request) }

    before do
      @jira_service = JiraService.new
      allow(@jira_service).to receive_messages(
        project_id: project.id,
        project: project,
        service_hook: true,
F
Felipe Artur 已提交
47
        url: 'http://jira.example.com',
D
Drew Blessing 已提交
48 49 50
        username: 'gitlab_jira_username',
        password: 'gitlab_jira_password'
      )
F
Felipe Artur 已提交
51 52 53 54 55

      @jira_service.save

      project_url = 'http://gitlab_jira_username:gitlab_jira_password@jira.example.com/rest/api/2/issue/JIRA-123'
      @transitions_url = 'http://gitlab_jira_username:gitlab_jira_password@jira.example.com/rest/api/2/issue/JIRA-123/transitions'
D
Drew Blessing 已提交
56 57
      @comment_url = 'http://gitlab_jira_username:gitlab_jira_password@jira.example.com/rest/api/2/issue/JIRA-123/comment'

F
Felipe Artur 已提交
58 59
      WebMock.stub_request(:get, project_url)
      WebMock.stub_request(:post, @transitions_url)
D
Drew Blessing 已提交
60 61 62
      WebMock.stub_request(:post, @comment_url)
    end

63
    it "calls JIRA API" do
F
Felipe Artur 已提交
64 65
      @jira_service.execute(merge_request, ExternalIssue.new("JIRA-123", project))

D
Drew Blessing 已提交
66 67 68 69 70
      expect(WebMock).to have_requested(:post, @comment_url).with(
        body: /Issue solved with/
      ).once
    end

71 72 73 74 75 76 77 78 79 80 81 82
    it "references the GitLab commit/merge request" do
      @jira_service.execute(merge_request, ExternalIssue.new("JIRA-123", project))

      expect(WebMock).to have_requested(:post, @comment_url).with(
        body: /#{Gitlab.config.gitlab.url}\/#{project.path_with_namespace}\/commit\/#{merge_request.diff_head_sha}/
      ).once
    end

    it "references the GitLab commit/merge request (relative URL)" do
      stub_config_setting(relative_url_root: '/gitlab')
      stub_config_setting(url: Settings.send(:build_gitlab_url))

83 84 85
      allow(JiraService).to receive(:default_url_options) do
        { script_name: '/gitlab' }
      end
86 87 88 89 90 91 92 93

      @jira_service.execute(merge_request, ExternalIssue.new("JIRA-123", project))

      expect(WebMock).to have_requested(:post, @comment_url).with(
        body: /#{Gitlab.config.gitlab.url}\/#{project.path_with_namespace}\/commit\/#{merge_request.diff_head_sha}/
      ).once
    end

D
Drew Blessing 已提交
94 95
    it "calls the api with jira_issue_transition_id" do
      @jira_service.jira_issue_transition_id = 'this-is-a-custom-id'
F
Felipe Artur 已提交
96 97 98
      @jira_service.execute(merge_request, ExternalIssue.new("JIRA-123", project))

      expect(WebMock).to have_requested(:post, @transitions_url).with(
D
Drew Blessing 已提交
99 100 101 102 103 104 105 106 107 108
        body: /this-is-a-custom-id/
      ).once
    end
  end

  describe "Stored password invalidation" do
    let(:project) { create(:project) }

    context "when a password was previously set" do
      before do
109
        @jira_service = JiraService.create!(
D
Drew Blessing 已提交
110 111
          project: create(:project),
          properties: {
F
Felipe Artur 已提交
112
            url: 'http://jira.example.com/rest/api/2',
D
Drew Blessing 已提交
113 114 115 116 117 118 119
            username: 'mic',
            password: "password"
          }
        )
      end

      it "reset password if url changed" do
F
Felipe Artur 已提交
120
        @jira_service.url = 'http://jira_edited.example.com/rest/api/2'
D
Drew Blessing 已提交
121 122 123 124 125 126 127 128 129 130 131
        @jira_service.save
        expect(@jira_service.password).to be_nil
      end

      it "does not reset password if username changed" do
        @jira_service.username = "some_name"
        @jira_service.save
        expect(@jira_service.password).to eq("password")
      end

      it "does not reset password if new url is set together with password, even if it's the same password" do
F
Felipe Artur 已提交
132
        @jira_service.url = 'http://jira_edited.example.com/rest/api/2'
D
Drew Blessing 已提交
133 134 135
        @jira_service.password = 'password'
        @jira_service.save
        expect(@jira_service.password).to eq("password")
F
Felipe Artur 已提交
136
        expect(@jira_service.url).to eq("http://jira_edited.example.com/rest/api/2")
D
Drew Blessing 已提交
137 138
      end

139
      it "resets password if url changed, even if setter called multiple times" do
F
Felipe Artur 已提交
140 141
        @jira_service.url = 'http://jira1.example.com/rest/api/2'
        @jira_service.url = 'http://jira1.example.com/rest/api/2'
D
Drew Blessing 已提交
142 143 144 145 146 147 148 149 150 151
        @jira_service.save
        expect(@jira_service.password).to be_nil
      end
    end

    context "when no password was previously set" do
      before do
        @jira_service = JiraService.create(
          project: create(:project),
          properties: {
F
Felipe Artur 已提交
152
            url: 'http://jira.example.com/rest/api/2',
D
Drew Blessing 已提交
153 154 155 156 157 158
            username: 'mic'
          }
        )
      end

      it "saves password if new url is set together with password" do
F
Felipe Artur 已提交
159
        @jira_service.url = 'http://jira_edited.example.com/rest/api/2'
D
Drew Blessing 已提交
160 161 162
        @jira_service.password = 'password'
        @jira_service.save
        expect(@jira_service.password).to eq("password")
F
Felipe Artur 已提交
163
        expect(@jira_service.url).to eq("http://jira_edited.example.com/rest/api/2")
D
Drew Blessing 已提交
164 165 166 167
      end
    end
  end

168 169 170 171 172 173
  describe "Validations" do
    context "active" do
      before do
        subject.active = true
      end

F
Felipe Artur 已提交
174
      it { is_expected.to validate_presence_of :url }
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
    end
  end

  describe 'description and title' do
    let(:project) { create(:project) }

    context 'when it is not set' do
      before do
        @service = project.create_jira_service(active: true)
      end

      after do
        @service.destroy!
      end

190
      it 'is initialized' do
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
        expect(@service.title).to eq('JIRA')
        expect(@service.description).to eq("Jira issue tracker")
      end
    end

    context 'when it is set' do
      before do
        properties = { 'title' => 'Jira One', 'description' => 'Jira One issue tracker' }
        @service = project.create_jira_service(active: true, properties: properties)
      end

      after do
        @service.destroy!
      end

206
      it "is correct" do
207 208 209 210 211 212 213 214 215 216 217
        expect(@service.title).to eq('Jira One')
        expect(@service.description).to eq('Jira One issue tracker')
      end
    end
  end

  describe 'project and issue urls' do
    let(:project) { create(:project) }

    context 'when gitlab.yml was initialized' do
      before do
D
Drew Blessing 已提交
218 219 220
        settings = {
          "jira" => {
            "title" => "Jira",
F
Felipe Artur 已提交
221
            "url" => "http://jira.sample/projects/project_a"
222 223
          }
        }
224
        allow(Gitlab.config).to receive(:issues_tracker).and_return(settings)
225 226 227 228 229 230 231
        @service = project.create_jira_service(active: true)
      end

      after do
        @service.destroy!
      end

232
      it 'is prepopulated with the settings' do
F
Felipe Artur 已提交
233 234
        expect(@service.properties["title"]).to eq('Jira')
        expect(@service.properties["url"]).to eq('http://jira.sample/projects/project_a')
235 236 237 238
      end
    end
  end
end