Commit d476e636 authored by Markus Koller's avatar Markus Koller

Add spec for failing Jira issue transitions

parent 126e0158
......@@ -472,7 +472,7 @@ RSpec.describe JiraService do
shared_examples 'close_issue' do
let(:issue_key) { 'JIRA-123' }
let(:issue_url) { "http://jira.example.com/rest/api/2/issue/#{issue_key}" }
let(:issue_url) { "#{url}/rest/api/2/issue/#{issue_key}" }
let(:transitions_url) { "#{issue_url}/transitions" }
let(:comment_url) { "#{issue_url}/comment" }
let(:remote_link_url) { "#{issue_url}/remotelink" }
......@@ -487,24 +487,16 @@ RSpec.describe JiraService do
end
subject(:close_issue) do
@jira_service.close_issue(resource, ExternalIssue.new(issue_key, project))
jira_service.close_issue(resource, ExternalIssue.new(issue_key, project))
end
before do
@jira_service = described_class.new
allow(@jira_service).to receive_messages(
project_id: project.id,
project: project,
url: 'http://jira.example.com',
username: 'gitlab_jira_username',
password: 'gitlab_jira_password',
jira_issue_transition_id: '999'
)
allow(jira_service).to receive_messages(jira_issue_transition_id: '999')
# 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: issue_fields.deep_stringify_keys)
open_issue = JIRA::Resource::Issue.new(jira_service.client, attrs: issue_fields.deep_stringify_keys)
closed_issue = open_issue.dup
allow(open_issue).to receive(:resolution).and_return(false)
allow(closed_issue).to receive(:resolution).and_return(true)
......@@ -513,18 +505,16 @@ RSpec.describe JiraService do
allow_any_instance_of(JIRA::Resource::Issue).to receive(:key).and_return(issue_key)
allow(JIRA::Resource::Remotelink).to receive(:all).and_return([])
@jira_service.save!
WebMock.stub_request(:get, issue_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))
WebMock.stub_request(:get, issue_url).with(basic_auth: %w(jira-username jira-password))
WebMock.stub_request(:post, transitions_url).with(basic_auth: %w(jira-username jira-password))
WebMock.stub_request(:post, comment_url).with(basic_auth: %w(jira-username jira-password))
WebMock.stub_request(:post, remote_link_url).with(basic_auth: %w(jira-username jira-password))
end
let(:external_issue) { ExternalIssue.new('JIRA-123', project) }
def close_issue
@jira_service.close_issue(resource, external_issue, current_user)
jira_service.close_issue(resource, external_issue, current_user)
end
it 'calls Jira API' do
......@@ -575,7 +565,7 @@ RSpec.describe JiraService do
context 'when "comment_on_event_enabled" is set to false' do
it 'creates Remote Link reference but does not create comment' do
allow(@jira_service).to receive_messages(comment_on_event_enabled: false)
allow(jira_service).to receive_messages(comment_on_event_enabled: false)
close_issue
expect(WebMock).not_to have_requested(:post, comment_url)
......@@ -648,12 +638,12 @@ RSpec.describe JiraService do
end
it 'logs exception when transition id is not valid' do
allow(@jira_service).to receive(:log_error)
WebMock.stub_request(:post, transitions_url).with(basic_auth: %w(gitlab_jira_username gitlab_jira_password)).and_raise("Bad Request")
allow(jira_service).to receive(:log_error)
WebMock.stub_request(:post, transitions_url).with(basic_auth: %w(jira-username jira-password)).and_raise("Bad Request")
close_issue
expect(@jira_service).to have_received(:log_error).with(
expect(jira_service).to have_received(:log_error).with(
"Issue transition failed",
error: hash_including(
exception_class: 'StandardError',
......@@ -682,7 +672,7 @@ RSpec.describe JiraService do
end
before do
allow(@jira_service).to receive_messages(jira_issue_transition_id: '')
allow(jira_service).to receive_messages(jira_issue_transition_id: '')
close_issue
end
......@@ -715,9 +705,11 @@ RSpec.describe JiraService do
end
context 'when using multiple transition ids' 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')
before do
allow(jira_service).to receive_messages(jira_issue_transition_id: '1,2,3')
end
it 'calls the api with transition ids separated by comma' do
close_issue
1.upto(3) do |transition_id|
......@@ -725,10 +717,12 @@ RSpec.describe JiraService do
body: /"id":"#{transition_id}"/
).once
end
expect(WebMock).to have_requested(:post, comment_url)
end
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')
close_issue
......@@ -737,6 +731,32 @@ RSpec.describe JiraService do
body: /"id":"#{transition_id}"/
).once
end
expect(WebMock).to have_requested(:post, comment_url)
end
context 'when a transition fails' do
before do
WebMock.stub_request(:post, transitions_url).with(basic_auth: %w(jira-username jira-password)).to_return do |request|
{ status: request.body.include?('"id":"2"') ? 500 : 200 }
end
end
it 'stops the sequence' do
close_issue
1.upto(2) do |transition_id|
expect(WebMock).to have_requested(:post, transitions_url).with(
body: /"id":"#{transition_id}"/
)
end
expect(WebMock).not_to have_requested(:post, transitions_url).with(
body: /"id":"3"/
)
expect(WebMock).not_to have_requested(:post, comment_url)
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