jira_service_spec.rb 12.4 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
    end
J
Jarka Kadlecova 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

    context 'validating urls' do
      let(:service) do
        described_class.new(
          project: create(:empty_project),
          active: true,
          username: 'username',
          password: 'test',
          project_key: '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

      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
61 62
  end

63 64 65 66 67 68 69 70 71
  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

72
  describe '#close_issue' do
J
Jarka Kadlecova 已提交
73
    let(:custom_base_url) { 'http://custom_url' }
D
Drew Blessing 已提交
74
    let(:user)    { create(:user) }
75
    let(:project) { create(:empty_project) }
D
Drew Blessing 已提交
76 77 78 79 80 81 82 83
    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 已提交
84
        url: 'http://jira.example.com',
D
Drew Blessing 已提交
85
        username: 'gitlab_jira_username',
86
        password: 'gitlab_jira_password',
87 88
        project_key: 'GitLabProject',
        jira_issue_transition_id: "custom-id"
D
Drew Blessing 已提交
89
      )
F
Felipe Artur 已提交
90

91 92 93 94 95 96 97 98 99 100
      # 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")
101
      allow(JIRA::Resource::Remotelink).to receive(:all).and_return([])
102

F
Felipe Artur 已提交
103 104
      @jira_service.save

105 106 107
      project_issues_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'
      @comment_url       = 'http://gitlab_jira_username:gitlab_jira_password@jira.example.com/rest/api/2/issue/JIRA-123/comment'
108
      @remote_link_url   = 'http://gitlab_jira_username:gitlab_jira_password@jira.example.com/rest/api/2/issue/JIRA-123/remotelink'
D
Drew Blessing 已提交
109

110
      WebMock.stub_request(:get, project_issues_url)
F
Felipe Artur 已提交
111
      WebMock.stub_request(:post, @transitions_url)
D
Drew Blessing 已提交
112
      WebMock.stub_request(:post, @comment_url)
113
      WebMock.stub_request(:post, @remote_link_url)
D
Drew Blessing 已提交
114 115
    end

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

D
Drew Blessing 已提交
119 120 121 122 123
      expect(WebMock).to have_requested(:post, @comment_url).with(
        body: /Issue solved with/
      ).once
    end

124 125 126
    # 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
127
      @jira_service.close_issue(merge_request, ExternalIssue.new("JIRA-123", project))
128 129 130 131 132 133 134 135 136 137 138 139

      # 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: {
            url: "#{Gitlab.config.gitlab.url}/#{project.path_with_namespace}/commit/#{merge_request.diff_head_sha}",
            title: "GitLab: Solved by commit #{merge_request.diff_head_sha}.",
            icon: { title: "GitLab", url16x16: "https://gitlab.com/favicon.ico" },
F
Felipe Artur 已提交
140
            status: { resolved: true }
141 142 143 144 145 146 147 148
          }
        )
      ).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)

149
      @jira_service.close_issue(merge_request, ExternalIssue.new("JIRA-123", project))
150 151 152 153 154

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

155
    it "references the GitLab commit/merge request" do
J
Jarka Kadlecova 已提交
156
      stub_config_setting(base_url: custom_base_url)
157

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

160
      expect(WebMock).to have_requested(:post, @comment_url).with(
J
Jarka Kadlecova 已提交
161
        body: /#{custom_base_url}\/#{project.path_with_namespace}\/commit\/#{merge_request.diff_head_sha}/
162 163 164 165 166 167 168
      ).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))

169 170 171
      allow(JiraService).to receive(:default_url_options) do
        { script_name: '/gitlab' }
      end
172

173
      @jira_service.close_issue(merge_request, ExternalIssue.new("JIRA-123", project))
174 175 176 177 178 179

      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 已提交
180
    it "calls the api with jira_issue_transition_id" do
181
      @jira_service.close_issue(merge_request, ExternalIssue.new("JIRA-123", project))
F
Felipe Artur 已提交
182 183

      expect(WebMock).to have_requested(:post, @transitions_url).with(
184
        body: /custom-id/
D
Drew Blessing 已提交
185 186
      ).once
    end
187
  end
188

189 190 191
  describe '#test_settings' do
    let(:jira_service) do
      described_class.new(
J
Jarka Kadlecova 已提交
192
        project: create(:project),
193
        url: 'http://jira.example.com',
J
Jarka Kadlecova 已提交
194 195
        username: 'jira_username',
        password: 'jira_password',
196 197 198
        project_key: 'GitLabProject'
      )
    end
199

J
Jarka Kadlecova 已提交
200 201 202
    def test_settings(api_url)
      project_url = "http://jira_username:jira_password@#{api_url}/rest/api/2/project/GitLabProject"

203 204 205
      WebMock.stub_request(:get, project_url)

      jira_service.test_settings
J
Jarka Kadlecova 已提交
206 207 208 209 210
    end

    it 'tries to get JIRA project with URL when API URL not set' do
      test_settings('jira.example.com')
    end
211

J
Jarka Kadlecova 已提交
212 213 214
    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')
215
    end
D
Drew Blessing 已提交
216 217 218
  end

  describe "Stored password invalidation" do
219
    let(:project) { create(:empty_project) }
D
Drew Blessing 已提交
220 221 222

    context "when a password was previously set" do
      before do
223
        @jira_service = JiraService.create!(
224
          project: project,
D
Drew Blessing 已提交
225
          properties: {
J
Jarka Kadlecova 已提交
226
            url: 'http://jira.example.com/web',
D
Drew Blessing 已提交
227 228 229 230 231 232
            username: 'mic',
            password: "password"
          }
        )
      end

J
Jarka Kadlecova 已提交
233 234 235 236 237 238 239 240 241 242 243 244 245 246
      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 已提交
247 248
      end

J
Jarka Kadlecova 已提交
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
      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 已提交
280
        @jira_service.save
J
Jarka Kadlecova 已提交
281 282

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

J
Jarka Kadlecova 已提交
285
      it 'does not reset password if new url is set together with password, even if it\'s the same password' do
F
Felipe Artur 已提交
286
        @jira_service.url = 'http://jira_edited.example.com/rest/api/2'
D
Drew Blessing 已提交
287 288
        @jira_service.password = 'password'
        @jira_service.save
J
Jarka Kadlecova 已提交
289 290 291

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

J
Jarka Kadlecova 已提交
294
      it 'resets password if url changed, even if setter called multiple times' do
F
Felipe Artur 已提交
295 296
        @jira_service.url = 'http://jira1.example.com/rest/api/2'
        @jira_service.url = 'http://jira1.example.com/rest/api/2'
D
Drew Blessing 已提交
297 298 299 300 301
        @jira_service.save
        expect(@jira_service.password).to be_nil
      end
    end

J
Jarka Kadlecova 已提交
302
    context 'when no password was previously set' do
D
Drew Blessing 已提交
303 304
      before do
        @jira_service = JiraService.create(
305
          project: project,
D
Drew Blessing 已提交
306
          properties: {
F
Felipe Artur 已提交
307
            url: 'http://jira.example.com/rest/api/2',
D
Drew Blessing 已提交
308 309 310 311 312
            username: 'mic'
          }
        )
      end

J
Jarka Kadlecova 已提交
313
      it 'saves password if new url is set together with password' do
F
Felipe Artur 已提交
314
        @jira_service.url = 'http://jira_edited.example.com/rest/api/2'
D
Drew Blessing 已提交
315 316
        @jira_service.password = 'password'
        @jira_service.save
J
Jarka Kadlecova 已提交
317 318
        expect(@jira_service.password).to eq('password')
        expect(@jira_service.url).to eq('http://jira_edited.example.com/rest/api/2')
D
Drew Blessing 已提交
319 320 321 322
      end
    end
  end

323
  describe 'description and title' do
324
    let(:project) { create(:empty_project) }
325 326 327 328 329 330 331 332 333 334

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

      after do
        @service.destroy!
      end

335
      it 'is initialized' do
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
        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

351
      it "is correct" do
352 353 354 355 356 357 358
        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
359
    let(:project) { create(:empty_project) }
360 361 362

    context 'when gitlab.yml was initialized' do
      before do
D
Drew Blessing 已提交
363
        settings = {
J
Jarka Kadlecova 已提交
364 365 366 367
          'jira' => {
            'title' => 'Jira',
            'url' => 'http://jira.sample/projects/project_a',
            'api_url' => 'http://jira.sample/api'
368 369
          }
        }
370
        allow(Gitlab.config).to receive(:issues_tracker).and_return(settings)
371 372 373 374 375 376 377
        @service = project.create_jira_service(active: true)
      end

      after do
        @service.destroy!
      end

378
      it 'is prepopulated with the settings' do
J
Jarka Kadlecova 已提交
379 380 381
        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')
382 383 384 385
      end
    end
  end
end