From 311b0269b4eb9839fa63f80c8d7a58f32b8138a0 Mon Sep 17 00:00:00 2001 From: GitLab Bot Date: Thu, 18 Nov 2021 13:16:36 +0000 Subject: Add latest changes from gitlab-org/gitlab@14-5-stable-ee --- spec/models/integrations/jira_spec.rb | 148 ++++++++++++++++++----- spec/models/integrations/pipelines_email_spec.rb | 38 +++++- spec/models/integrations/shimo_spec.rb | 41 +++++++ spec/models/integrations/zentao_spec.rb | 6 + 4 files changed, 199 insertions(+), 34 deletions(-) create mode 100644 spec/models/integrations/shimo_spec.rb (limited to 'spec/models/integrations') diff --git a/spec/models/integrations/jira_spec.rb b/spec/models/integrations/jira_spec.rb index 0321b151633..1d81668f97d 100644 --- a/spec/models/integrations/jira_spec.rb +++ b/spec/models/integrations/jira_spec.rb @@ -495,6 +495,18 @@ RSpec.describe Integrations::Jira do end end + describe '#client' do + it 'uses the default GitLab::HTTP timeouts' do + timeouts = Gitlab::HTTP::DEFAULT_TIMEOUT_OPTIONS + stub_request(:get, 'http://jira.example.com/foo') + + expect(Gitlab::HTTP).to receive(:httparty_perform_request) + .with(Net::HTTP::Get, '/foo', hash_including(timeouts)).and_call_original + + jira_integration.client.get('/foo') + end + end + describe '#find_issue' do let(:issue_key) { 'JIRA-123' } let(:issue_url) { "#{url}/rest/api/2/issue/#{issue_key}" } @@ -503,7 +515,7 @@ RSpec.describe Integrations::Jira do stub_request(:get, issue_url).with(basic_auth: [username, password]) end - it 'call the Jira API to get the issue' do + it 'calls the Jira API to get the issue' do jira_integration.find_issue(issue_key) expect(WebMock).to have_requested(:get, issue_url) @@ -845,10 +857,14 @@ RSpec.describe Integrations::Jira do let_it_be(:user) { build_stubbed(:user) } let(:jira_issue) { ExternalIssue.new('JIRA-123', project) } + let(:success_message) { 'SUCCESS: Successfully posted to http://jira.example.com.' } + let(:favicon_path) { "http://localhost/assets/#{find_asset('favicon.png').digest_path}" } subject { jira_integration.create_cross_reference_note(jira_issue, resource, user) } - shared_examples 'creates a comment on Jira' do + shared_examples 'handles cross-references' do + let(:resource_name) { jira_integration.send(:noteable_name, resource) } + let(:resource_url) { jira_integration.send(:build_entity_url, resource_name, resource.to_param) } let(:issue_url) { "#{url}/rest/api/2/issue/JIRA-123" } let(:comment_url) { "#{issue_url}/comment" } let(:remote_link_url) { "#{issue_url}/remotelink" } @@ -860,12 +876,77 @@ RSpec.describe Integrations::Jira do stub_request(:post, remote_link_url).with(basic_auth: [username, password]) end - it 'creates a comment on Jira' do - subject + context 'when enabled' do + before do + allow(jira_integration).to receive(:can_cross_reference?) { true } + end - expect(WebMock).to have_requested(:post, comment_url).with( - body: /mentioned this issue in/ - ).once + it 'creates a comment and remote link' do + expect(subject).to eq(success_message) + expect(WebMock).to have_requested(:post, comment_url).with(body: comment_body).once + expect(WebMock).to have_requested(:post, remote_link_url).with( + body: hash_including( + GlobalID: 'GitLab', + relationship: 'mentioned on', + object: { + url: resource_url, + title: "#{resource.model_name.human} - #{resource.title}", + icon: { title: 'GitLab', url16x16: favicon_path }, + status: { resolved: false } + } + ) + ).once + end + + context 'when comment already exists' do + before do + allow(jira_integration).to receive(:comment_exists?) { true } + end + + it 'does not create a comment or remote link' do + expect(subject).to be_nil + expect(WebMock).not_to have_requested(:post, comment_url) + expect(WebMock).not_to have_requested(:post, remote_link_url) + end + end + + context 'when remote link already exists' do + let(:link) { double(object: { 'url' => resource_url }) } + + before do + allow(jira_integration).to receive(:find_remote_link).and_return(link) + end + + it 'updates the remote link but does not create a comment' do + expect(link).to receive(:save!) + expect(subject).to eq(success_message) + expect(WebMock).not_to have_requested(:post, comment_url) + end + end + end + + context 'when disabled' do + before do + allow(jira_integration).to receive(:can_cross_reference?) { false } + end + + it 'does not create a comment or remote link' do + expect(subject).to eq("Events for #{resource_name.pluralize.humanize(capitalize: false)} are disabled.") + expect(WebMock).not_to have_requested(:post, comment_url) + expect(WebMock).not_to have_requested(:post, remote_link_url) + end + end + + context 'with jira_use_first_ref_by_oid feature flag disabled' do + before do + stub_feature_flags(jira_use_first_ref_by_oid: false) + end + + it 'creates a comment and remote link on Jira' do + expect(subject).to eq(success_message) + expect(WebMock).to have_requested(:post, comment_url).with(body: comment_body).once + expect(WebMock).to have_requested(:post, remote_link_url).once + end end it 'tracks usage' do @@ -877,39 +958,38 @@ RSpec.describe Integrations::Jira do end end - context 'when resource is a commit' do - let(:resource) { project.commit('master') } - - context 'when disabled' do - before do - allow_next_instance_of(described_class) do |instance| - allow(instance).to receive(:commit_events) { false } - end - end - - it { is_expected.to eq('Events for commits are disabled.') } + context 'for commits' do + it_behaves_like 'handles cross-references' do + let(:resource) { project.commit('master') } + let(:comment_body) { /mentioned this issue in \[a commit\|.* on branch \[master\|/ } end + end - context 'when enabled' do - it_behaves_like 'creates a comment on Jira' + context 'for issues' do + it_behaves_like 'handles cross-references' do + let(:resource) { build_stubbed(:issue, project: project) } + let(:comment_body) { /mentioned this issue in \[a issue\|/ } end end - context 'when resource is a merge request' do - let(:resource) { build_stubbed(:merge_request, source_project: project) } - - context 'when disabled' do - before do - allow_next_instance_of(described_class) do |instance| - allow(instance).to receive(:merge_requests_events) { false } - end - end + context 'for merge requests' do + it_behaves_like 'handles cross-references' do + let(:resource) { build_stubbed(:merge_request, source_project: project) } + let(:comment_body) { /mentioned this issue in \[a merge request\|.* on branch \[master\|/ } + end + end - it { is_expected.to eq('Events for merge requests are disabled.') } + context 'for notes' do + it_behaves_like 'handles cross-references' do + let(:resource) { build_stubbed(:note, project: project) } + let(:comment_body) { /mentioned this issue in \[a note\|/ } end + end - context 'when enabled' do - it_behaves_like 'creates a comment on Jira' + context 'for snippets' do + it_behaves_like 'handles cross-references' do + let(:resource) { build_stubbed(:snippet, project: project) } + let(:comment_body) { /mentioned this issue in \[a snippet\|/ } end end end @@ -946,7 +1026,9 @@ RSpec.describe Integrations::Jira do expect(jira_integration).to receive(:log_error).with( 'Error sending message', client_url: 'http://jira.example.com', - error: error_message + 'exception.class' => anything, + 'exception.message' => error_message, + 'exception.backtrace' => anything ) expect(jira_integration.test(nil)).to eq(success: false, result: error_message) diff --git a/spec/models/integrations/pipelines_email_spec.rb b/spec/models/integrations/pipelines_email_spec.rb index afd9d71ebc4..d70f104b965 100644 --- a/spec/models/integrations/pipelines_email_spec.rb +++ b/spec/models/integrations/pipelines_email_spec.rb @@ -35,6 +35,42 @@ RSpec.describe Integrations::PipelinesEmail, :mailer do it { is_expected.not_to validate_presence_of(:recipients) } end + + describe 'validates number of recipients' do + before do + stub_const("#{described_class}::RECIPIENTS_LIMIT", 2) + end + + subject(:integration) { described_class.new(project: project, recipients: recipients, active: true) } + + context 'valid number of recipients' do + let(:recipients) { 'foo@bar.com, , ' } + + it 'does not count empty emails' do + is_expected.to be_valid + end + end + + context 'invalid number of recipients' do + let(:recipients) { 'foo@bar.com bar@foo.com bob@gitlab.com' } + + it { is_expected.not_to be_valid } + + it 'adds an error message' do + integration.valid? + + expect(integration.errors).to contain_exactly('Recipients can\'t exceed 2') + end + + context 'when integration is not active' do + before do + integration.active = false + end + + it { is_expected.to be_valid } + end + end + end end shared_examples 'sending email' do |branches_to_be_notified: nil| @@ -50,7 +86,7 @@ RSpec.describe Integrations::PipelinesEmail, :mailer do it 'sends email' do emails = receivers.map { |r| double(notification_email_or_default: r) } - should_only_email(*emails, kind: :bcc) + should_only_email(*emails) end end diff --git a/spec/models/integrations/shimo_spec.rb b/spec/models/integrations/shimo_spec.rb new file mode 100644 index 00000000000..25df8d2b249 --- /dev/null +++ b/spec/models/integrations/shimo_spec.rb @@ -0,0 +1,41 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe ::Integrations::Shimo do + describe '#fields' do + let(:shimo_integration) { create(:shimo_integration) } + + it 'returns custom fields' do + expect(shimo_integration.fields.pluck(:name)).to eq(%w[external_wiki_url]) + end + end + + describe '#create' do + let(:project) { create(:project, :repository) } + let(:external_wiki_url) { 'https://shimo.example.com/desktop' } + let(:params) { { active: true, project: project, external_wiki_url: external_wiki_url } } + + context 'with valid params' do + it 'creates the Shimo integration' do + shimo = described_class.create!(params) + + expect(shimo.valid?).to be true + expect(shimo.render?).to be true + expect(shimo.external_wiki_url).to eq(external_wiki_url) + end + end + + context 'with invalid params' do + it 'cannot create the Shimo integration without external_wiki_url' do + params['external_wiki_url'] = nil + expect { described_class.create!(params) }.to raise_error(ActiveRecord::RecordInvalid) + end + + it 'cannot create the Shimo integration with invalid external_wiki_url' do + params['external_wiki_url'] = 'Fake Invalid URL' + expect { described_class.create!(params) }.to raise_error(ActiveRecord::RecordInvalid) + end + end + end +end diff --git a/spec/models/integrations/zentao_spec.rb b/spec/models/integrations/zentao_spec.rb index a1503ecc092..2b0532c7930 100644 --- a/spec/models/integrations/zentao_spec.rb +++ b/spec/models/integrations/zentao_spec.rb @@ -50,4 +50,10 @@ RSpec.describe Integrations::Zentao do expect(zentao_integration.test).to eq(test_response) end end + + describe '#help' do + it 'renders prompt information' do + expect(zentao_integration.help).not_to be_empty + end + end end -- cgit v1.2.1