jira_service_spec.rb 11.2 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

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 61 62 63 64 65 66 67 68 69 70
  describe '#can_test?' do
    let(:jira_service) { described_class.new }

    it 'returns false if username is blank' do
      allow(jira_service).to receive_messages(
        url: 'http://jira.example.com',
        username: '',
        password: '12345678'
      )

      expect(jira_service.can_test?).to be_falsy
    end

    it 'returns false if password is blank' do
      allow(jira_service).to receive_messages(
        url: 'http://jira.example.com',
        username: 'tester',
        password: ''
      )

      expect(jira_service.can_test?).to be_falsy
    end

    it 'returns true if password and username are present' do
      jira_service = described_class.new
      allow(jira_service).to receive_messages(
        url: 'http://jira.example.com',
        username: 'tester',
        password: '12345678'
      )

      expect(jira_service.can_test?).to be_truthy
    end
  end

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

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")

F
Felipe Artur 已提交
101 102
      @jira_service.save

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

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

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

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

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

      # 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" },
            status: { resolved: true, icon: { url16x16: "http://www.openwebgraphics.com/resources/data/1768/16x16_apply.png", title: "Closed" } }
          }
        )
      ).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)

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

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

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

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

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

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

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

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

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

187 188 189 190 191 192 193 194 195 196
  describe '#test_settings' do
    let(:jira_service) do
      described_class.new(
        url: 'http://jira.example.com',
        username: 'gitlab_jira_username',
        password: 'gitlab_jira_password',
        project_key: 'GitLabProject'
      )
    end
    let(:project_url) { 'http://gitlab_jira_username:gitlab_jira_password@jira.example.com/rest/api/2/project/GitLabProject' }
197

198 199 200 201 202 203 204 205
    before do
      WebMock.stub_request(:get, project_url)
    end

    it 'tries to get JIRA project' do
      jira_service.test_settings

      expect(WebMock).to have_requested(:get, project_url)
206
    end
D
Drew Blessing 已提交
207 208 209 210 211 212 213
  end

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

    context "when a password was previously set" do
      before do
214
        @jira_service = JiraService.create!(
D
Drew Blessing 已提交
215 216
          project: create(:project),
          properties: {
F
Felipe Artur 已提交
217
            url: 'http://jira.example.com/rest/api/2',
D
Drew Blessing 已提交
218 219 220 221 222 223 224
            username: 'mic',
            password: "password"
          }
        )
      end

      it "reset password if url changed" do
F
Felipe Artur 已提交
225
        @jira_service.url = 'http://jira_edited.example.com/rest/api/2'
D
Drew Blessing 已提交
226 227 228 229 230 231 232 233 234 235 236
        @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 已提交
237
        @jira_service.url = 'http://jira_edited.example.com/rest/api/2'
D
Drew Blessing 已提交
238 239 240
        @jira_service.password = 'password'
        @jira_service.save
        expect(@jira_service.password).to eq("password")
F
Felipe Artur 已提交
241
        expect(@jira_service.url).to eq("http://jira_edited.example.com/rest/api/2")
D
Drew Blessing 已提交
242 243
      end

244
      it "resets password if url changed, even if setter called multiple times" do
F
Felipe Artur 已提交
245 246
        @jira_service.url = 'http://jira1.example.com/rest/api/2'
        @jira_service.url = 'http://jira1.example.com/rest/api/2'
D
Drew Blessing 已提交
247 248 249 250 251 252 253 254 255 256
        @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 已提交
257
            url: 'http://jira.example.com/rest/api/2',
D
Drew Blessing 已提交
258 259 260 261 262 263
            username: 'mic'
          }
        )
      end

      it "saves password if new url is set together with password" do
F
Felipe Artur 已提交
264
        @jira_service.url = 'http://jira_edited.example.com/rest/api/2'
D
Drew Blessing 已提交
265 266 267
        @jira_service.password = 'password'
        @jira_service.save
        expect(@jira_service.password).to eq("password")
F
Felipe Artur 已提交
268
        expect(@jira_service.url).to eq("http://jira_edited.example.com/rest/api/2")
D
Drew Blessing 已提交
269 270 271 272
      end
    end
  end

273 274 275 276 277 278
  describe "Validations" do
    context "active" do
      before do
        subject.active = true
      end

F
Felipe Artur 已提交
279
      it { is_expected.to validate_presence_of :url }
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
    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

295
      it 'is initialized' do
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
        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

311
      it "is correct" do
312 313 314 315 316 317 318 319 320 321 322
        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 已提交
323 324 325
        settings = {
          "jira" => {
            "title" => "Jira",
F
Felipe Artur 已提交
326
            "url" => "http://jira.sample/projects/project_a"
327 328
          }
        }
329
        allow(Gitlab.config).to receive(:issues_tracker).and_return(settings)
330 331 332 333 334 335 336
        @service = project.create_jira_service(active: true)
      end

      after do
        @service.destroy!
      end

337
      it 'is prepopulated with the settings' do
F
Felipe Artur 已提交
338 339
        expect(@service.properties["title"]).to eq('Jira')
        expect(@service.properties["url"]).to eq('http://jira.sample/projects/project_a')
340 341 342 343
      end
    end
  end
end