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

3
describe JiraService do
4
  include Gitlab::Routing
5

6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
  describe '#options' do
    let(:service) do
      described_class.new(
        project: build_stubbed(:project),
        active: true,
        username: 'username',
        password: 'test',
        jira_issue_transition_id: 24,
        url: 'http://jira.test.com/path/'
      )
    end

    it 'sets the URL properly' do
      # jira-ruby gem parses the URI and handles trailing slashes
      # fine: https://github.com/sumoheavy/jira-ruby/blob/v1.4.1/lib/jira/http_client.rb#L59
      expect(service.options[:site]).to eq('http://jira.test.com/')
    end

    it 'leaves out trailing slashes in context' do
      expect(service.options[:context_path]).to eq('/path')
    end
  end

29
  describe "Associations" do
30 31
    it { is_expected.to belong_to :project }
    it { is_expected.to have_one :service_hook }
32 33
  end

34 35
  describe 'Validations' do
    context 'when service is active' do
36 37 38
      before do
        subject.active = true
      end
39

F
Felipe Artur 已提交
40 41
      it { is_expected.to validate_presence_of(:url) }
      it_behaves_like 'issue tracker service URL attribute', :url
42 43 44
    end

    context 'when service is inactive' do
45 46 47
      before do
        subject.active = false
      end
48

F
Felipe Artur 已提交
49
      it { is_expected.not_to validate_presence_of(:url) }
50 51
      it { is_expected.not_to validate_presence_of(:username) }
      it { is_expected.not_to validate_presence_of(:password) }
52
    end
J
Jarka Kadlecova 已提交
53 54 55 56

    context 'validating urls' do
      let(:service) do
        described_class.new(
57
          project: create(:project),
J
Jarka Kadlecova 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
          active: true,
          username: 'username',
          password: 'test',
          jira_issue_transition_id: 24,
          url: 'http://jira.test.com'
        )
      end

      it 'is valid when all fields have required values' do
        expect(service).to be_valid
      end

      it 'is not valid when url is not a valid url' do
        service.url = 'not valid'

        expect(service).not_to be_valid
      end

      it 'is not valid when api url is not a valid url' do
        service.api_url = 'not valid'

        expect(service).not_to be_valid
      end

82 83 84 85 86 87 88 89 90 91 92 93
      it 'is not valid when username is missing' do
        service.username = nil

        expect(service).not_to be_valid
      end

      it 'is not valid when password is missing' do
        service.password = nil

        expect(service).not_to be_valid
      end

J
Jarka Kadlecova 已提交
94 95 96 97 98 99
      it 'is valid when api url is a valid url' do
        service.api_url = 'http://jira.test.com/api'

        expect(service).to be_valid
      end
    end
100 101
  end

102
  describe '.reference_pattern' do
103 104 105
    it_behaves_like 'allows project key on reference pattern'

    it 'does not allow # on the code' do
106 107
      expect(described_class.reference_pattern.match('#123')).to be_nil
      expect(described_class.reference_pattern.match('1#23#12')).to be_nil
108 109 110
    end
  end

111
  describe '#close_issue' do
J
Jarka Kadlecova 已提交
112
    let(:custom_base_url) { 'http://custom_url' }
D
Drew Blessing 已提交
113
    let(:user)    { create(:user) }
114
    let(:project) { create(:project) }
D
Drew Blessing 已提交
115 116 117
    let(:merge_request) { create(:merge_request) }

    before do
118
      @jira_service = described_class.new
D
Drew Blessing 已提交
119 120 121 122
      allow(@jira_service).to receive_messages(
        project_id: project.id,
        project: project,
        service_hook: true,
F
Felipe Artur 已提交
123
        url: 'http://jira.example.com',
D
Drew Blessing 已提交
124
        username: 'gitlab_jira_username',
125
        password: 'gitlab_jira_password',
126
        jira_issue_transition_id: "custom-id"
D
Drew Blessing 已提交
127
      )
F
Felipe Artur 已提交
128

129 130 131 132 133 134 135 136 137 138
      # These stubs are needed to test JiraService#close_issue.
      # We close the issue then do another request to API to check if it got closed.
      # Here is stubbed the API return with a closed and an opened issues.
      open_issue   = JIRA::Resource::Issue.new(@jira_service.client, attrs: { "id" => "JIRA-123" })
      closed_issue = open_issue.dup
      allow(open_issue).to receive(:resolution).and_return(false)
      allow(closed_issue).to receive(:resolution).and_return(true)
      allow(JIRA::Resource::Issue).to receive(:find).and_return(open_issue, closed_issue)

      allow_any_instance_of(JIRA::Resource::Issue).to receive(:key).and_return("JIRA-123")
139
      allow(JIRA::Resource::Remotelink).to receive(:all).and_return([])
140

F
Felipe Artur 已提交
141 142
      @jira_service.save

143 144 145 146 147 148 149 150 151
      project_issues_url = 'http://jira.example.com/rest/api/2/issue/JIRA-123'
      @transitions_url   = 'http://jira.example.com/rest/api/2/issue/JIRA-123/transitions'
      @comment_url       = 'http://jira.example.com/rest/api/2/issue/JIRA-123/comment'
      @remote_link_url   = 'http://jira.example.com/rest/api/2/issue/JIRA-123/remotelink'

      WebMock.stub_request(:get, project_issues_url).with(basic_auth: %w(gitlab_jira_username gitlab_jira_password))
      WebMock.stub_request(:post, @transitions_url).with(basic_auth: %w(gitlab_jira_username gitlab_jira_password))
      WebMock.stub_request(:post, @comment_url).with(basic_auth: %w(gitlab_jira_username gitlab_jira_password))
      WebMock.stub_request(:post, @remote_link_url).with(basic_auth: %w(gitlab_jira_username gitlab_jira_password))
D
Drew Blessing 已提交
152 153
    end

154
    it "calls JIRA API" do
155
      @jira_service.close_issue(merge_request, ExternalIssue.new("JIRA-123", project))
F
Felipe Artur 已提交
156

D
Drew Blessing 已提交
157 158 159 160 161
      expect(WebMock).to have_requested(:post, @comment_url).with(
        body: /Issue solved with/
      ).once
    end

162 163 164
    # Check https://developer.atlassian.com/jiradev/jira-platform/guides/other/guide-jira-remote-issue-links/fields-in-remote-issue-links
    # for more information
    it "creates Remote Link reference in JIRA for comment" do
165
      @jira_service.close_issue(merge_request, ExternalIssue.new("JIRA-123", project))
166 167 168 169 170 171 172 173 174

      # Creates comment
      expect(WebMock).to have_requested(:post, @comment_url)

      # Creates Remote Link in JIRA issue fields
      expect(WebMock).to have_requested(:post, @remote_link_url).with(
        body: hash_including(
          GlobalID: "GitLab",
          object: {
175
            url: "#{Gitlab.config.gitlab.url}/#{project.full_path}/commit/#{merge_request.diff_head_sha}",
176 177
            title: "GitLab: Solved by commit #{merge_request.diff_head_sha}.",
            icon: { title: "GitLab", url16x16: "https://gitlab.com/favicon.ico" },
F
Felipe Artur 已提交
178
            status: { resolved: true }
179 180 181 182 183 184 185 186
          }
        )
      ).once
    end

    it "does not send comment or remote links to issues already closed" do
      allow_any_instance_of(JIRA::Resource::Issue).to receive(:resolution).and_return(true)

187
      @jira_service.close_issue(merge_request, ExternalIssue.new("JIRA-123", project))
188 189 190 191 192

      expect(WebMock).not_to have_requested(:post, @comment_url)
      expect(WebMock).not_to have_requested(:post, @remote_link_url)
    end

193 194 195 196 197 198 199 200 201
    it "does not send comment or remote links to issues with unknown resolution" do
      allow_any_instance_of(JIRA::Resource::Issue).to receive(:respond_to?).with(:resolution).and_return(false)

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

      expect(WebMock).not_to have_requested(:post, @comment_url)
      expect(WebMock).not_to have_requested(:post, @remote_link_url)
    end

202
    it "references the GitLab commit/merge request" do
J
Jarka Kadlecova 已提交
203
      stub_config_setting(base_url: custom_base_url)
204

205
      @jira_service.close_issue(merge_request, ExternalIssue.new("JIRA-123", project))
J
Jarka Kadlecova 已提交
206

207
      expect(WebMock).to have_requested(:post, @comment_url).with(
208
        body: /#{custom_base_url}\/#{project.full_path}\/commit\/#{merge_request.diff_head_sha}/
209 210 211 212 213 214 215
      ).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))

216
      allow(described_class).to receive(:default_url_options) do
217 218
        { script_name: '/gitlab' }
      end
219

220
      @jira_service.close_issue(merge_request, ExternalIssue.new("JIRA-123", project))
221 222

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

D
Drew Blessing 已提交
227
    it "calls the api with jira_issue_transition_id" do
228
      @jira_service.close_issue(merge_request, ExternalIssue.new("JIRA-123", project))
F
Felipe Artur 已提交
229 230

      expect(WebMock).to have_requested(:post, @transitions_url).with(
231
        body: /custom-id/
D
Drew Blessing 已提交
232 233
      ).once
    end
234
  end
235

236 237 238
  describe '#test_settings' do
    let(:jira_service) do
      described_class.new(
239
        project: create(:project),
240
        url: 'http://jira.example.com',
J
Jarka Kadlecova 已提交
241
        username: 'jira_username',
242
        password: 'jira_password'
243 244
      )
    end
245

246 247
    def test_settings(api_url = nil)
      api_url ||= 'jira.example.com'
248
      test_url = "http://#{api_url}/rest/api/2/serverInfo"
J
Jarka Kadlecova 已提交
249

250
      WebMock.stub_request(:get, test_url).with(basic_auth: %w(jira_username jira_password)).to_return(body: { url: 'http://url' }.to_json )
251

252
      jira_service.test(nil)
J
Jarka Kadlecova 已提交
253 254
    end

255 256 257 258 259 260 261 262 263 264 265 266 267
    context 'when the test succeeds' do
      it 'tries to get JIRA project with URL when API URL not set' do
        test_settings('jira.example.com')
      end

      it 'returns correct result' do
        expect(test_settings).to eq( { success: true, result: { 'url' => 'http://url' } })
      end

      it 'tries to get JIRA project with API URL if set' do
        jira_service.update(api_url: 'http://jira.api.com')
        test_settings('jira.api.com')
      end
J
Jarka Kadlecova 已提交
268
    end
269

270 271 272 273 274 275 276 277
    context 'when the test fails' do
      it 'returns result with the error' do
        test_url = 'http://jira.example.com/rest/api/2/serverInfo'
        WebMock.stub_request(:get, test_url).with(basic_auth: %w(jira_username jira_password))
          .to_raise(JIRA::HTTPError.new(double(message: 'Some specific failure.')))

        expect(jira_service.test(nil)).to eq( { success: false, result: 'Some specific failure.' })
      end
278
    end
D
Drew Blessing 已提交
279 280 281
  end

  describe "Stored password invalidation" do
282
    let(:project) { create(:project) }
D
Drew Blessing 已提交
283 284 285

    context "when a password was previously set" do
      before do
286
        @jira_service = described_class.create!(
287
          project: project,
D
Drew Blessing 已提交
288
          properties: {
J
Jarka Kadlecova 已提交
289
            url: 'http://jira.example.com/web',
D
Drew Blessing 已提交
290 291 292 293 294 295
            username: 'mic',
            password: "password"
          }
        )
      end

J
Jarka Kadlecova 已提交
296 297 298 299 300 301 302 303 304 305 306 307 308 309
      context 'when only web url present' do
        it 'reset password if url changed' do
          @jira_service.url = 'http://jira_edited.example.com/rest/api/2'
          @jira_service.save

          expect(@jira_service.password).to be_nil
        end

        it 'reset password if url not changed but api url added' do
          @jira_service.api_url = 'http://jira_edited.example.com/rest/api/2'
          @jira_service.save

          expect(@jira_service.password).to be_nil
        end
D
Drew Blessing 已提交
310 311
      end

J
Jarka Kadlecova 已提交
312 313 314 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
      context 'when both web and api url present' do
        before do
          @jira_service.api_url = 'http://jira.example.com/rest/api/2'
          @jira_service.password = 'password'

          @jira_service.save
        end
        it 'reset password if api url changed' do
          @jira_service.api_url = 'http://jira_edited.example.com/rest/api/2'
          @jira_service.save

          expect(@jira_service.password).to be_nil
        end

        it 'does not reset password if url changed' do
          @jira_service.url = 'http://jira_edited.example.com/rweb'
          @jira_service.save

          expect(@jira_service.password).to eq("password")
        end

        it 'reset password if api url set to ""' do
          @jira_service.api_url = ''
          @jira_service.save

          expect(@jira_service.password).to be_nil
        end
      end

      it 'does not reset password if username changed' do
        @jira_service.username = 'some_name'
D
Drew Blessing 已提交
343
        @jira_service.save
J
Jarka Kadlecova 已提交
344 345

        expect(@jira_service.password).to eq('password')
D
Drew Blessing 已提交
346 347
      end

J
Jarka Kadlecova 已提交
348
      it 'does not reset password if new url is set together with password, even if it\'s the same password' do
F
Felipe Artur 已提交
349
        @jira_service.url = 'http://jira_edited.example.com/rest/api/2'
D
Drew Blessing 已提交
350 351
        @jira_service.password = 'password'
        @jira_service.save
J
Jarka Kadlecova 已提交
352 353 354

        expect(@jira_service.password).to eq('password')
        expect(@jira_service.url).to eq('http://jira_edited.example.com/rest/api/2')
D
Drew Blessing 已提交
355 356
      end

J
Jarka Kadlecova 已提交
357
      it 'resets password if url changed, even if setter called multiple times' do
F
Felipe Artur 已提交
358 359
        @jira_service.url = 'http://jira1.example.com/rest/api/2'
        @jira_service.url = 'http://jira1.example.com/rest/api/2'
D
Drew Blessing 已提交
360 361 362 363 364
        @jira_service.save
        expect(@jira_service.password).to be_nil
      end
    end

J
Jarka Kadlecova 已提交
365
    context 'when no password was previously set' do
D
Drew Blessing 已提交
366
      before do
367
        @jira_service = described_class.create(
368
          project: project,
D
Drew Blessing 已提交
369
          properties: {
F
Felipe Artur 已提交
370
            url: 'http://jira.example.com/rest/api/2',
D
Drew Blessing 已提交
371 372 373 374 375
            username: 'mic'
          }
        )
      end

J
Jarka Kadlecova 已提交
376
      it 'saves password if new url is set together with password' do
F
Felipe Artur 已提交
377
        @jira_service.url = 'http://jira_edited.example.com/rest/api/2'
D
Drew Blessing 已提交
378 379
        @jira_service.password = 'password'
        @jira_service.save
J
Jarka Kadlecova 已提交
380 381
        expect(@jira_service.password).to eq('password')
        expect(@jira_service.url).to eq('http://jira_edited.example.com/rest/api/2')
D
Drew Blessing 已提交
382 383 384 385
      end
    end
  end

386
  describe 'description and title' do
387
    let(:project) { create(:project) }
388 389 390 391 392 393 394 395 396 397

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

      after do
        @service.destroy!
      end

398
      it 'is initialized' do
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
        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

414
      it "is correct" do
415 416 417 418 419 420
        expect(@service.title).to eq('Jira One')
        expect(@service.description).to eq('Jira One issue tracker')
      end
    end
  end

421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
  describe 'additional cookies' do
    let(:project) { create(:project) }

    context 'provides additional cookies to allow basic auth with oracle webgate' do
      before do
        @service = project.create_jira_service(
          active: true, properties: { url: 'http://jira.com' })
      end

      after do
        @service.destroy!
      end

      it 'is initialized' do
        expect(@service.options[:use_cookies]).to eq(true)
        expect(@service.options[:additional_cookies]).to eq(["OBBasicAuth=fromDialog"])
      end
    end
  end

441
  describe 'project and issue urls' do
442
    let(:project) { create(:project) }
443 444 445

    context 'when gitlab.yml was initialized' do
      before do
D
Drew Blessing 已提交
446
        settings = {
J
Jarka Kadlecova 已提交
447 448 449 450
          'jira' => {
            'title' => 'Jira',
            'url' => 'http://jira.sample/projects/project_a',
            'api_url' => 'http://jira.sample/api'
451 452
          }
        }
453
        allow(Gitlab.config).to receive(:issues_tracker).and_return(settings)
454 455 456 457 458 459 460
        @service = project.create_jira_service(active: true)
      end

      after do
        @service.destroy!
      end

461
      it 'is prepopulated with the settings' do
J
Jarka Kadlecova 已提交
462 463 464
        expect(@service.properties['title']).to eq('Jira')
        expect(@service.properties['url']).to eq('http://jira.sample/projects/project_a')
        expect(@service.properties['api_url']).to eq('http://jira.sample/api')
465 466 467 468
      end
    end
  end
end