Commit 2125d648 authored by Felipe Artur's avatar Felipe Artur Committed by Sean McGivern

Add JIRA service specs to commit object

parent 638209e1
...@@ -27,13 +27,13 @@ describe JiraService do ...@@ -27,13 +27,13 @@ describe JiraService do
end end
end end
describe "Associations" do describe 'Associations' do
it { is_expected.to belong_to :project } it { is_expected.to belong_to :project }
it { is_expected.to have_one :service_hook } it { is_expected.to have_one :service_hook }
it { is_expected.to allow_value(nil).for(:jira_issue_transition_id) } it { is_expected.to allow_value(nil).for(:jira_issue_transition_id) }
it { is_expected.to allow_value("1,2,3").for(:jira_issue_transition_id) } it { is_expected.to allow_value('1,2,3').for(:jira_issue_transition_id) }
it { is_expected.to allow_value("1;2;3").for(:jira_issue_transition_id) } it { is_expected.to allow_value('1;2;3').for(:jira_issue_transition_id) }
it { is_expected.not_to allow_value("a,b,cd").for(:jira_issue_transition_id) } it { is_expected.not_to allow_value('a,b,cd').for(:jira_issue_transition_id) }
end end
describe 'Validations' do describe 'Validations' do
...@@ -116,9 +116,9 @@ describe JiraService do ...@@ -116,9 +116,9 @@ describe JiraService do
describe '#close_issue' do describe '#close_issue' do
let(:custom_base_url) { 'http://custom_url' } let(:custom_base_url) { 'http://custom_url' }
let(:user) { create(:user) } let(:user) { create(:user) }
let(:project) { create(:project) } let(:project) { create(:project, :repository) }
let(:merge_request) { create(:merge_request) }
shared_examples 'close_issue' do
before do before do
@jira_service = described_class.new @jira_service = described_class.new
allow(@jira_service).to receive_messages( allow(@jira_service).to receive_messages(
...@@ -128,19 +128,19 @@ describe JiraService do ...@@ -128,19 +128,19 @@ describe JiraService do
url: 'http://jira.example.com', url: 'http://jira.example.com',
username: 'gitlab_jira_username', username: 'gitlab_jira_username',
password: 'gitlab_jira_password', password: 'gitlab_jira_password',
jira_issue_transition_id: "999" jira_issue_transition_id: '999'
) )
# These stubs are needed to test JiraService#close_issue. # 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. # 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. # 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" }) open_issue = JIRA::Resource::Issue.new(@jira_service.client, attrs: { 'id' => 'JIRA-123' })
closed_issue = open_issue.dup closed_issue = open_issue.dup
allow(open_issue).to receive(:resolution).and_return(false) allow(open_issue).to receive(:resolution).and_return(false)
allow(closed_issue).to receive(:resolution).and_return(true) allow(closed_issue).to receive(:resolution).and_return(true)
allow(JIRA::Resource::Issue).to receive(:find).and_return(open_issue, closed_issue) 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") allow_any_instance_of(JIRA::Resource::Issue).to receive(:key).and_return('JIRA-123')
allow(JIRA::Resource::Remotelink).to receive(:all).and_return([]) allow(JIRA::Resource::Remotelink).to receive(:all).and_return([])
@jira_service.save @jira_service.save
...@@ -156,8 +156,8 @@ describe JiraService do ...@@ -156,8 +156,8 @@ describe JiraService do
WebMock.stub_request(:post, @remote_link_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))
end end
it "calls JIRA API" do it 'calls JIRA API' do
@jira_service.close_issue(merge_request, ExternalIssue.new("JIRA-123", project)) @jira_service.close_issue(resource, ExternalIssue.new('JIRA-123', project))
expect(WebMock).to have_requested(:post, @comment_url).with( expect(WebMock).to have_requested(:post, @comment_url).with(
body: /Issue solved with/ body: /Issue solved with/
...@@ -166,8 +166,8 @@ describe JiraService do ...@@ -166,8 +166,8 @@ describe JiraService do
# Check https://developer.atlassian.com/jiradev/jira-platform/guides/other/guide-jira-remote-issue-links/fields-in-remote-issue-links # Check https://developer.atlassian.com/jiradev/jira-platform/guides/other/guide-jira-remote-issue-links/fields-in-remote-issue-links
# for more information # for more information
it "creates Remote Link reference in JIRA for comment" do it 'creates Remote Link reference in JIRA for comment' do
@jira_service.close_issue(merge_request, ExternalIssue.new("JIRA-123", project)) @jira_service.close_issue(resource, ExternalIssue.new('JIRA-123', project))
favicon_path = "http://localhost/assets/#{find_asset('favicon.png').digest_path}" favicon_path = "http://localhost/assets/#{find_asset('favicon.png').digest_path}"
...@@ -176,46 +176,46 @@ describe JiraService do ...@@ -176,46 +176,46 @@ describe JiraService do
# Creates Remote Link in JIRA issue fields # Creates Remote Link in JIRA issue fields
expect(WebMock).to have_requested(:post, @remote_link_url).with( expect(WebMock).to have_requested(:post, @remote_link_url).with(
body: hash_including( body: hash_including(
GlobalID: "GitLab", GlobalID: 'GitLab',
object: { object: {
url: "#{Gitlab.config.gitlab.url}/#{project.full_path}/commit/#{merge_request.diff_head_sha}", url: "#{Gitlab.config.gitlab.url}/#{project.full_path}/commit/#{commit_id}",
title: "GitLab: Solved by commit #{merge_request.diff_head_sha}.", title: "GitLab: Solved by commit #{commit_id}.",
icon: { title: "GitLab", url16x16: favicon_path }, icon: { title: 'GitLab', url16x16: favicon_path },
status: { resolved: true } status: { resolved: true }
} }
) )
).once ).once
end end
it "does not send comment or remote links to issues already closed" do 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) allow_any_instance_of(JIRA::Resource::Issue).to receive(:resolution).and_return(true)
@jira_service.close_issue(merge_request, ExternalIssue.new("JIRA-123", project)) @jira_service.close_issue(resource, ExternalIssue.new('JIRA-123', project))
expect(WebMock).not_to have_requested(:post, @comment_url) expect(WebMock).not_to have_requested(:post, @comment_url)
expect(WebMock).not_to have_requested(:post, @remote_link_url) expect(WebMock).not_to have_requested(:post, @remote_link_url)
end end
it "does not send comment or remote links to issues with unknown resolution" do 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) 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)) @jira_service.close_issue(resource, ExternalIssue.new('JIRA-123', project))
expect(WebMock).not_to have_requested(:post, @comment_url) expect(WebMock).not_to have_requested(:post, @comment_url)
expect(WebMock).not_to have_requested(:post, @remote_link_url) expect(WebMock).not_to have_requested(:post, @remote_link_url)
end end
it "references the GitLab commit/merge request" do it 'references the GitLab commit' do
stub_config_setting(base_url: custom_base_url) stub_config_setting(base_url: custom_base_url)
@jira_service.close_issue(merge_request, ExternalIssue.new("JIRA-123", project)) @jira_service.close_issue(resource, ExternalIssue.new('JIRA-123', project))
expect(WebMock).to have_requested(:post, @comment_url).with( expect(WebMock).to have_requested(:post, @comment_url).with(
body: %r{#{custom_base_url}/#{project.full_path}/commit/#{merge_request.diff_head_sha}} body: %r{#{custom_base_url}/#{project.full_path}/commit/#{commit_id}}
).once ).once
end end
it "references the GitLab commit/merge request (relative URL)" do it 'references the GitLab commit' do
stub_config_setting(relative_url_root: '/gitlab') stub_config_setting(relative_url_root: '/gitlab')
stub_config_setting(url: Settings.send(:build_gitlab_url)) stub_config_setting(url: Settings.send(:build_gitlab_url))
...@@ -223,36 +223,35 @@ describe JiraService do ...@@ -223,36 +223,35 @@ describe JiraService do
{ script_name: '/gitlab' } { script_name: '/gitlab' }
end end
@jira_service.close_issue(merge_request, ExternalIssue.new("JIRA-123", project)) @jira_service.close_issue(resource, ExternalIssue.new('JIRA-123', project))
expect(WebMock).to have_requested(:post, @comment_url).with( expect(WebMock).to have_requested(:post, @comment_url).with(
body: %r{#{Gitlab.config.gitlab.url}/#{project.full_path}/commit/#{merge_request.diff_head_sha}} body: %r{#{Gitlab.config.gitlab.url}/#{project.full_path}/commit/#{commit_id}}
).once ).once
end end
context '#close_issue' do it 'logs exception when transition id is not valid' do
it "logs exception when transition id is not valid" do
allow(Rails.logger).to receive(:info) allow(Rails.logger).to receive(:info)
WebMock.stub_request(:post, @transitions_url).with(basic_auth: %w(gitlab_jira_username gitlab_jira_password)).and_raise("Bad Request") WebMock.stub_request(:post, @transitions_url).with(basic_auth: %w(gitlab_jira_username gitlab_jira_password)).and_raise('Bad Request')
@jira_service.close_issue(merge_request, ExternalIssue.new("JIRA-123", project)) @jira_service.close_issue(resource, ExternalIssue.new('JIRA-123', project))
expect(Rails.logger).to have_received(:info).with("JiraService Issue Transition failed message ERROR: http://jira.example.com - Bad Request") expect(Rails.logger).to have_received(:info).with('JiraService Issue Transition failed message ERROR: http://jira.example.com - Bad Request')
end end
it "calls the api with jira_issue_transition_id" do it 'calls the api with jira_issue_transition_id' do
@jira_service.close_issue(merge_request, ExternalIssue.new("JIRA-123", project)) @jira_service.close_issue(resource, ExternalIssue.new('JIRA-123', project))
expect(WebMock).to have_requested(:post, @transitions_url).with( expect(WebMock).to have_requested(:post, @transitions_url).with(
body: /999/ body: /999/
).once ).once
end end
context "when have multiple transition ids" do context 'when have multiple transition ids' do
it "calls the api with transition ids separated by comma" do it 'calls the api with transition ids separated by comma' do
allow(@jira_service).to receive_messages(jira_issue_transition_id: "1,2,3") allow(@jira_service).to receive_messages(jira_issue_transition_id: '1,2,3')
@jira_service.close_issue(merge_request, ExternalIssue.new("JIRA-123", project)) @jira_service.close_issue(resource, ExternalIssue.new('JIRA-123', project))
1.upto(3) do |transition_id| 1.upto(3) do |transition_id|
expect(WebMock).to have_requested(:post, @transitions_url).with( expect(WebMock).to have_requested(:post, @transitions_url).with(
...@@ -261,10 +260,10 @@ describe JiraService do ...@@ -261,10 +260,10 @@ describe JiraService do
end end
end end
it "calls the api with transition ids separated by semicolon" do it 'calls the api with transition ids separated by semicolon' do
allow(@jira_service).to receive_messages(jira_issue_transition_id: "1;2;3") allow(@jira_service).to receive_messages(jira_issue_transition_id: '1;2;3')
@jira_service.close_issue(merge_request, ExternalIssue.new("JIRA-123", project)) @jira_service.close_issue(resource, ExternalIssue.new('JIRA-123', project))
1.upto(3) do |transition_id| 1.upto(3) do |transition_id|
expect(WebMock).to have_requested(:post, @transitions_url).with( expect(WebMock).to have_requested(:post, @transitions_url).with(
...@@ -274,6 +273,20 @@ describe JiraService do ...@@ -274,6 +273,20 @@ describe JiraService do
end end
end end
end end
context 'when resource is a merge request' do
let(:resource) { create(:merge_request) }
let(:commit_id) { resource.diff_head_sha }
it_behaves_like 'close_issue'
end
context 'when resource is a commit' do
let(:resource) { project.commit('master') }
let(:commit_id) { resource.id }
it_behaves_like 'close_issue'
end
end end
describe '#test_settings' do describe '#test_settings' do
...@@ -321,17 +334,17 @@ describe JiraService do ...@@ -321,17 +334,17 @@ describe JiraService do
end end
end end
describe "Stored password invalidation" do describe 'Stored password invalidation' do
let(:project) { create(:project) } let(:project) { create(:project) }
context "when a password was previously set" do context 'when a password was previously set' do
before do before do
@jira_service = described_class.create!( @jira_service = described_class.create!(
project: project, project: project,
properties: { properties: {
url: 'http://jira.example.com/web', url: 'http://jira.example.com/web',
username: 'mic', username: 'mic',
password: "password" password: 'password'
} }
) )
end end
...@@ -370,10 +383,10 @@ describe JiraService do ...@@ -370,10 +383,10 @@ describe JiraService do
@jira_service.url = 'http://jira_edited.example.com/rweb' @jira_service.url = 'http://jira_edited.example.com/rweb'
@jira_service.save @jira_service.save
expect(@jira_service.password).to eq("password") expect(@jira_service.password).to eq('password')
end end
it 'reset password if api url set to ""' do it 'reset password if api url set to empty' do
@jira_service.api_url = '' @jira_service.api_url = ''
@jira_service.save @jira_service.save
...@@ -440,7 +453,7 @@ describe JiraService do ...@@ -440,7 +453,7 @@ describe JiraService do
it 'is initialized' do it 'is initialized' do
expect(@service.title).to eq('JIRA') expect(@service.title).to eq('JIRA')
expect(@service.description).to eq("Jira issue tracker") expect(@service.description).to eq('Jira issue tracker')
end end
end end
...@@ -454,7 +467,7 @@ describe JiraService do ...@@ -454,7 +467,7 @@ describe JiraService do
@service.destroy! @service.destroy!
end end
it "is correct" do it 'is correct' do
expect(@service.title).to eq('Jira One') expect(@service.title).to eq('Jira One')
expect(@service.description).to eq('Jira One issue tracker') expect(@service.description).to eq('Jira One issue tracker')
end end
...@@ -476,7 +489,7 @@ describe JiraService do ...@@ -476,7 +489,7 @@ describe JiraService do
it 'is initialized' do it 'is initialized' do
expect(@service.options[:use_cookies]).to eq(true) expect(@service.options[:use_cookies]).to eq(true)
expect(@service.options[:additional_cookies]).to eq(["OBBasicAuth=fromDialog"]) expect(@service.options[:additional_cookies]).to eq(['OBBasicAuth=fromDialog'])
end end
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment