diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-11-15 00:08:29 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-11-15 00:08:29 +0000 |
commit | 38b948a54e00841b51c446beb0adf079af60c963 (patch) | |
tree | 2e9dfe54e2d0ec444223f773dc283b1c639baae9 /spec/models/integrations | |
parent | 7f35b02e86cd3d2e8b4a81c5c3a8483ff6973c5a (diff) | |
download | gitlab-ce-38b948a54e00841b51c446beb0adf079af60c963.tar.gz |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/models/integrations')
-rw-r--r-- | spec/models/integrations/chat_message/pipeline_message_spec.rb | 41 | ||||
-rw-r--r-- | spec/models/integrations/packagist_spec.rb | 83 |
2 files changed, 87 insertions, 37 deletions
diff --git a/spec/models/integrations/chat_message/pipeline_message_spec.rb b/spec/models/integrations/chat_message/pipeline_message_spec.rb index a63cc0b6d83..f3388853b37 100644 --- a/spec/models/integrations/chat_message/pipeline_message_spec.rb +++ b/spec/models/integrations/chat_message/pipeline_message_spec.rb @@ -44,13 +44,18 @@ RSpec.describe Integrations::ChatMessage::PipelineMessage do before do test_commit = double("A test commit", committer: args[:user], title: "A test commit message") - test_project = double("A test project", commit_by: test_commit, name: args[:project][:name], web_url: args[:project][:web_url]) + test_project = build(:project, name: args[:project][:name]) + + allow(test_project).to receive(:commit_by).and_return(test_commit) + allow(test_project).to receive(:web_url).and_return(args[:project][:web_url]) allow(test_project).to receive(:avatar_url).with(no_args).and_return("/avatar") allow(test_project).to receive(:avatar_url).with(only_path: false).and_return(args[:project][:avatar_url]) allow(Project).to receive(:find) { test_project } - test_pipeline = double("A test pipeline", - has_yaml_errors?: has_yaml_errors, yaml_errors: "yaml error description here") + test_pipeline = build(:ci_empty_pipeline, name: 'Build pipeline') + + allow(test_pipeline).to receive(:has_yaml_errors?).and_return(has_yaml_errors) + allow(test_pipeline).to receive(:yaml_errors).and_return("yaml error description here") allow(Ci::Pipeline).to receive(:find) { test_pipeline } allow(Gitlab::UrlBuilder).to receive(:build).with(test_commit).and_return("http://example.com/commit") @@ -69,6 +74,24 @@ RSpec.describe Integrations::ChatMessage::PipelineMessage do ) end + it 'returns pipeline name' do + name_field = subject.attachments.first[:fields].find { |a| a[:title] == 'Pipeline name' } + + expect(name_field[:value]).to eq('Build pipeline') + end + + context 'when pipeline_name feature flag is disabled' do + before do + stub_feature_flags(pipeline_name: false) + end + + it 'does not return pipeline name' do + name_field = subject.attachments.first[:fields].find { |a| a[:title] == 'Pipeline name' } + + expect(name_field).to be nil + end + end + context "when the pipeline failed" do before do args[:object_attributes][:status] = 'failed' @@ -204,8 +227,8 @@ RSpec.describe Integrations::ChatMessage::PipelineMessage do expect(subject.attachments.first[:title_link]).to eq("http://example.gitlab.com/-/pipelines/123") end - it "returns two attachment fields" do - expect(subject.attachments.first[:fields].count).to eq(2) + it "returns three attachment fields" do + expect(subject.attachments.first[:fields].count).to eq(3) end it "returns the commit message as the attachment's second field property" do @@ -232,8 +255,8 @@ RSpec.describe Integrations::ChatMessage::PipelineMessage do ] end - it "returns four attachment fields" do - expect(subject.attachments.first[:fields].count).to eq(4) + it "returns five attachment fields" do + expect(subject.attachments.first[:fields].count).to eq(5) end it "returns the stage name and link to the 'Failed jobs' tab on the pipeline's page as the attachment's third field property" do @@ -337,8 +360,8 @@ RSpec.describe Integrations::ChatMessage::PipelineMessage do context "when the CI config file contains a YAML error" do let(:has_yaml_errors) { true } - it "returns three attachment fields" do - expect(subject.attachments.first[:fields].count).to eq(3) + it "returns four attachment fields" do + expect(subject.attachments.first[:fields].count).to eq(4) end it "returns the YAML error deatils as the attachment's third field property" do diff --git a/spec/models/integrations/packagist_spec.rb b/spec/models/integrations/packagist_spec.rb index ef86f0565b6..e00de0f7418 100644 --- a/spec/models/integrations/packagist_spec.rb +++ b/spec/models/integrations/packagist_spec.rb @@ -3,49 +3,76 @@ require 'spec_helper' RSpec.describe Integrations::Packagist do - let(:packagist_params) do - { - active: true, - project: project, - properties: { - username: packagist_username, - token: packagist_token, - server: packagist_server - } - } - end - - let(:packagist_hook_url) do - "#{packagist_server}/api/update-package?username=#{packagist_username}&apiToken=#{packagist_token}" - end - - let(:packagist_token) { 'verySecret' } - let(:packagist_username) { 'theUser' } - let(:packagist_server) { 'https://packagist.example.com' } - let_it_be(:project) { create(:project) } - it_behaves_like Integrations::HasWebHook do - let(:integration) { described_class.new(packagist_params) } - let(:hook_url) { "#{packagist_server}/api/update-package?username={username}&apiToken={token}" } + let_it_be(:project) { create(:project) } + + let(:integration) { build(:packagist_integration, project: project) } + let(:hook_url) { "#{integration.server}/api/update-package?username={username}&apiToken={token}" } end it_behaves_like Integrations::ResetSecretFields do - let(:integration) { described_class.new(packagist_params) } + let(:integration) { build(:packagist_integration) } end describe '#execute' do - let(:user) { create(:user) } - let(:push_sample_data) { Gitlab::DataBuilder::Push.build_sample(project, user) } - let(:packagist_integration) { described_class.create!(packagist_params) } + let(:project) { build(:project) } + let(:integration) { build(:packagist_integration, project: project) } + + let(:packagist_hook_url) do + "#{integration.server}/api/update-package?username=#{integration.username}&apiToken=#{integration.token}" + end before do stub_request(:post, packagist_hook_url) end it 'calls Packagist API' do - packagist_integration.execute(push_sample_data) + user = create(:user) + push_sample_data = Gitlab::DataBuilder::Push.build_sample(project, user) + integration.execute(push_sample_data) expect(a_request(:post, packagist_hook_url)).to have_been_made.once end end + + describe '#test' do + let(:integration) { build(:packagist_integration) } + let(:test_data) { { foo: 'bar' } } + + subject(:result) { integration.test(test_data) } + + context 'when test request executes without errors' do + before do + allow(integration).to receive(:execute).with(test_data).and_return( + ServiceResponse.success(message: 'success message', payload: { http_status: http_status }) + ) + end + + context 'when response is a 200' do + let(:http_status) { 200 } + + it 'return failure result' do + is_expected.to eq(success: false, result: 'success message') + end + end + + context 'when response is a 202' do + let(:http_status) { 202 } + + it 'return success result' do + is_expected.to eq(success: true, result: 'success message') + end + end + end + + context 'when test request executes with errors' do + before do + allow(integration).to receive(:execute).with(test_data).and_raise(StandardError, 'error message') + end + + it 'return failure result' do + is_expected.to eq(success: false, result: 'error message') + end + end + end end |