2015-01-27 01:08:27 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2015-12-09 04:50:51 -05:00
|
|
|
describe JiraService, models: true do
|
2016-11-03 10:05:31 -04:00
|
|
|
include Gitlab::Routing.url_helpers
|
|
|
|
|
2015-01-27 01:08:27 -05:00
|
|
|
describe "Associations" do
|
2015-02-12 13:17:35 -05:00
|
|
|
it { is_expected.to belong_to :project }
|
|
|
|
it { is_expected.to have_one :service_hook }
|
2015-01-27 01:08:27 -05:00
|
|
|
end
|
|
|
|
|
2016-04-21 11:13:14 -04:00
|
|
|
describe 'Validations' do
|
|
|
|
context 'when service is active' do
|
|
|
|
before { subject.active = true }
|
|
|
|
|
2016-09-29 17:11:32 -04:00
|
|
|
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
|
2016-04-21 11:13:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when service is inactive' do
|
|
|
|
before { subject.active = false }
|
|
|
|
|
2016-09-29 17:11:32 -04:00
|
|
|
it { is_expected.not_to validate_presence_of(:url) }
|
2016-04-21 11:13:14 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-10-06 18:05:27 -04:00
|
|
|
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
|
|
|
|
|
2016-10-27 17:20:47 -04:00
|
|
|
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
|
|
|
|
|
2016-12-01 03:45:06 -05:00
|
|
|
describe '#close_issue' do
|
2016-11-05 09:59:08 -04:00
|
|
|
let(:custom_base_url) { 'http://custom_url' }
|
2015-12-17 17:08:14 -05:00
|
|
|
let(:user) { create(:user) }
|
2017-01-26 17:44:58 -05:00
|
|
|
let(:project) { create(:empty_project) }
|
2015-12-17 17:08:14 -05:00
|
|
|
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,
|
2016-09-29 17:11:32 -04:00
|
|
|
url: 'http://jira.example.com',
|
2015-12-17 17:08:14 -05:00
|
|
|
username: 'gitlab_jira_username',
|
2016-10-27 17:20:47 -04:00
|
|
|
password: 'gitlab_jira_password',
|
2016-11-14 16:30:01 -05:00
|
|
|
project_key: 'GitLabProject',
|
|
|
|
jira_issue_transition_id: "custom-id"
|
2015-12-17 17:08:14 -05:00
|
|
|
)
|
2016-09-29 17:11:32 -04:00
|
|
|
|
2016-11-09 16:55:21 -05:00
|
|
|
# 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")
|
|
|
|
|
2016-09-29 17:11:32 -04:00
|
|
|
@jira_service.save
|
|
|
|
|
2016-10-27 17:20:47 -04:00
|
|
|
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'
|
2016-11-09 16:55:21 -05:00
|
|
|
@remote_link_url = 'http://gitlab_jira_username:gitlab_jira_password@jira.example.com/rest/api/2/issue/JIRA-123/remotelink'
|
2015-12-17 17:08:14 -05:00
|
|
|
|
2016-10-27 17:20:47 -04:00
|
|
|
WebMock.stub_request(:get, project_issues_url)
|
2016-09-29 17:11:32 -04:00
|
|
|
WebMock.stub_request(:post, @transitions_url)
|
2015-12-17 17:08:14 -05:00
|
|
|
WebMock.stub_request(:post, @comment_url)
|
2016-11-09 16:55:21 -05:00
|
|
|
WebMock.stub_request(:post, @remote_link_url)
|
2015-12-17 17:08:14 -05:00
|
|
|
end
|
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it "calls JIRA API" do
|
2016-12-01 03:45:06 -05:00
|
|
|
@jira_service.close_issue(merge_request, ExternalIssue.new("JIRA-123", project))
|
2016-09-29 17:11:32 -04:00
|
|
|
|
2015-12-17 17:08:14 -05:00
|
|
|
expect(WebMock).to have_requested(:post, @comment_url).with(
|
|
|
|
body: /Issue solved with/
|
|
|
|
).once
|
|
|
|
end
|
|
|
|
|
2016-11-09 16:55:21 -05:00
|
|
|
# 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
|
2016-12-01 03:45:06 -05:00
|
|
|
@jira_service.close_issue(merge_request, ExternalIssue.new("JIRA-123", project))
|
2016-11-09 16:55:21 -05:00
|
|
|
|
|
|
|
# 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)
|
|
|
|
|
2016-12-01 03:45:06 -05:00
|
|
|
@jira_service.close_issue(merge_request, ExternalIssue.new("JIRA-123", project))
|
2016-11-09 16:55:21 -05:00
|
|
|
|
|
|
|
expect(WebMock).not_to have_requested(:post, @comment_url)
|
|
|
|
expect(WebMock).not_to have_requested(:post, @remote_link_url)
|
|
|
|
end
|
|
|
|
|
2016-09-01 01:50:27 -04:00
|
|
|
it "references the GitLab commit/merge request" do
|
2016-11-05 09:59:08 -04:00
|
|
|
stub_config_setting(base_url: custom_base_url)
|
2016-09-01 01:50:27 -04:00
|
|
|
|
2016-12-01 03:45:06 -05:00
|
|
|
@jira_service.close_issue(merge_request, ExternalIssue.new("JIRA-123", project))
|
2016-11-12 12:22:39 -05:00
|
|
|
|
2016-09-01 01:50:27 -04:00
|
|
|
expect(WebMock).to have_requested(:post, @comment_url).with(
|
2016-11-05 09:59:08 -04:00
|
|
|
body: /#{custom_base_url}\/#{project.path_with_namespace}\/commit\/#{merge_request.diff_head_sha}/
|
2016-09-01 01:50:27 -04:00
|
|
|
).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))
|
|
|
|
|
2016-11-03 11:02:19 -04:00
|
|
|
allow(JiraService).to receive(:default_url_options) do
|
|
|
|
{ script_name: '/gitlab' }
|
|
|
|
end
|
2016-09-01 01:50:27 -04:00
|
|
|
|
2016-12-01 03:45:06 -05:00
|
|
|
@jira_service.close_issue(merge_request, ExternalIssue.new("JIRA-123", project))
|
2016-09-01 01:50:27 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2015-12-17 17:08:14 -05:00
|
|
|
it "calls the api with jira_issue_transition_id" do
|
2016-12-01 03:45:06 -05:00
|
|
|
@jira_service.close_issue(merge_request, ExternalIssue.new("JIRA-123", project))
|
2016-09-29 17:11:32 -04:00
|
|
|
|
|
|
|
expect(WebMock).to have_requested(:post, @transitions_url).with(
|
2016-11-14 16:30:01 -05:00
|
|
|
body: /custom-id/
|
2015-12-17 17:08:14 -05:00
|
|
|
).once
|
|
|
|
end
|
2016-12-01 03:45:06 -05:00
|
|
|
end
|
2016-10-27 17:20:47 -04:00
|
|
|
|
2016-12-01 03:45:06 -05:00
|
|
|
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' }
|
2016-10-27 17:20:47 -04:00
|
|
|
|
2016-12-01 03:45:06 -05:00
|
|
|
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)
|
2016-10-27 17:20:47 -04:00
|
|
|
end
|
2015-12-17 17:08:14 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "Stored password invalidation" do
|
2017-01-26 17:44:58 -05:00
|
|
|
let(:project) { create(:empty_project) }
|
2015-12-17 17:08:14 -05:00
|
|
|
|
|
|
|
context "when a password was previously set" do
|
|
|
|
before do
|
2016-04-21 11:13:14 -04:00
|
|
|
@jira_service = JiraService.create!(
|
2017-01-26 17:44:58 -05:00
|
|
|
project: project,
|
2015-12-17 17:08:14 -05:00
|
|
|
properties: {
|
2016-09-29 17:11:32 -04:00
|
|
|
url: 'http://jira.example.com/rest/api/2',
|
2015-12-17 17:08:14 -05:00
|
|
|
username: 'mic',
|
|
|
|
password: "password"
|
|
|
|
}
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "reset password if url changed" do
|
2016-09-29 17:11:32 -04:00
|
|
|
@jira_service.url = 'http://jira_edited.example.com/rest/api/2'
|
2015-12-17 17:08:14 -05:00
|
|
|
@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
|
2016-09-29 17:11:32 -04:00
|
|
|
@jira_service.url = 'http://jira_edited.example.com/rest/api/2'
|
2015-12-17 17:08:14 -05:00
|
|
|
@jira_service.password = 'password'
|
|
|
|
@jira_service.save
|
|
|
|
expect(@jira_service.password).to eq("password")
|
2016-09-29 17:11:32 -04:00
|
|
|
expect(@jira_service.url).to eq("http://jira_edited.example.com/rest/api/2")
|
2015-12-17 17:08:14 -05:00
|
|
|
end
|
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it "resets password if url changed, even if setter called multiple times" do
|
2016-09-29 17:11:32 -04:00
|
|
|
@jira_service.url = 'http://jira1.example.com/rest/api/2'
|
|
|
|
@jira_service.url = 'http://jira1.example.com/rest/api/2'
|
2015-12-17 17:08:14 -05:00
|
|
|
@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(
|
2017-01-26 17:44:58 -05:00
|
|
|
project: project,
|
2015-12-17 17:08:14 -05:00
|
|
|
properties: {
|
2016-09-29 17:11:32 -04:00
|
|
|
url: 'http://jira.example.com/rest/api/2',
|
2015-12-17 17:08:14 -05:00
|
|
|
username: 'mic'
|
|
|
|
}
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "saves password if new url is set together with password" do
|
2016-09-29 17:11:32 -04:00
|
|
|
@jira_service.url = 'http://jira_edited.example.com/rest/api/2'
|
2015-12-17 17:08:14 -05:00
|
|
|
@jira_service.password = 'password'
|
|
|
|
@jira_service.save
|
|
|
|
expect(@jira_service.password).to eq("password")
|
2016-09-29 17:11:32 -04:00
|
|
|
expect(@jira_service.url).to eq("http://jira_edited.example.com/rest/api/2")
|
2015-12-17 17:08:14 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-01-27 01:08:27 -05:00
|
|
|
describe "Validations" do
|
|
|
|
context "active" do
|
|
|
|
before do
|
|
|
|
subject.active = true
|
|
|
|
end
|
|
|
|
|
2016-09-29 17:11:32 -04:00
|
|
|
it { is_expected.to validate_presence_of :url }
|
2015-01-27 01:08:27 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'description and title' do
|
2017-01-26 17:44:58 -05:00
|
|
|
let(:project) { create(:empty_project) }
|
2015-01-27 01:08:27 -05:00
|
|
|
|
|
|
|
context 'when it is not set' do
|
|
|
|
before do
|
|
|
|
@service = project.create_jira_service(active: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
@service.destroy!
|
|
|
|
end
|
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it 'is initialized' do
|
2015-01-27 01:08:27 -05:00
|
|
|
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
|
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it "is correct" do
|
2015-01-27 01:08:27 -05:00
|
|
|
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
|
2017-01-26 17:44:58 -05:00
|
|
|
let(:project) { create(:empty_project) }
|
2015-01-27 01:08:27 -05:00
|
|
|
|
|
|
|
context 'when gitlab.yml was initialized' do
|
|
|
|
before do
|
2015-12-17 17:08:14 -05:00
|
|
|
settings = {
|
|
|
|
"jira" => {
|
|
|
|
"title" => "Jira",
|
2016-09-29 17:11:32 -04:00
|
|
|
"url" => "http://jira.sample/projects/project_a"
|
2015-01-27 01:08:27 -05:00
|
|
|
}
|
|
|
|
}
|
2015-02-12 13:17:35 -05:00
|
|
|
allow(Gitlab.config).to receive(:issues_tracker).and_return(settings)
|
2015-01-27 01:08:27 -05:00
|
|
|
@service = project.create_jira_service(active: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
@service.destroy!
|
|
|
|
end
|
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it 'is prepopulated with the settings' do
|
2016-09-29 17:11:32 -04:00
|
|
|
expect(@service.properties["title"]).to eq('Jira')
|
|
|
|
expect(@service.properties["url"]).to eq('http://jira.sample/projects/project_a')
|
2015-01-27 01:08:27 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|