diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-03-18 20:02:30 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-03-18 20:02:30 +0000 |
commit | 41fe97390ceddf945f3d967b8fdb3de4c66b7dea (patch) | |
tree | 9c8d89a8624828992f06d892cd2f43818ff5dcc8 /spec/models/integrations | |
parent | 0804d2dc31052fb45a1efecedc8e06ce9bc32862 (diff) | |
download | gitlab-ce-41fe97390ceddf945f3d967b8fdb3de4c66b7dea.tar.gz |
Add latest changes from gitlab-org/gitlab@14-9-stable-eev14.9.0-rc42
Diffstat (limited to 'spec/models/integrations')
-rw-r--r-- | spec/models/integrations/base_issue_tracker_spec.rb | 22 | ||||
-rw-r--r-- | spec/models/integrations/field_spec.rb | 118 | ||||
-rw-r--r-- | spec/models/integrations/harbor_spec.rb | 133 | ||||
-rw-r--r-- | spec/models/integrations/jira_spec.rb | 26 | ||||
-rw-r--r-- | spec/models/integrations/slack_spec.rb | 2 |
5 files changed, 296 insertions, 5 deletions
diff --git a/spec/models/integrations/base_issue_tracker_spec.rb b/spec/models/integrations/base_issue_tracker_spec.rb index 25e27e96a84..37f7d99717c 100644 --- a/spec/models/integrations/base_issue_tracker_spec.rb +++ b/spec/models/integrations/base_issue_tracker_spec.rb @@ -3,12 +3,12 @@ require 'spec_helper' RSpec.describe Integrations::BaseIssueTracker do - describe 'Validations' do - let(:project) { create :project } + let(:integration) { Integrations::Redmine.new(project: project, active: true, issue_tracker_data: build(:issue_tracker_data)) } - describe 'only one issue tracker per project' do - let(:integration) { Integrations::Redmine.new(project: project, active: true, issue_tracker_data: build(:issue_tracker_data)) } + let_it_be_with_refind(:project) { create :project } + describe 'Validations' do + describe 'only one issue tracker per project' do before do create(:custom_issue_tracker_integration, project: project) end @@ -31,4 +31,18 @@ RSpec.describe Integrations::BaseIssueTracker do end end end + + describe '#activate_disabled_reason' do + subject { integration.activate_disabled_reason } + + context 'when there is an existing issue tracker integration' do + let_it_be(:custom_tracker) { create(:custom_issue_tracker_integration, project: project) } + + it { is_expected.to eq(trackers: [custom_tracker]) } + end + + context 'when there is no existing issue tracker integration' do + it { is_expected.to be(nil) } + end + end end diff --git a/spec/models/integrations/field_spec.rb b/spec/models/integrations/field_spec.rb new file mode 100644 index 00000000000..0d660c4a3ab --- /dev/null +++ b/spec/models/integrations/field_spec.rb @@ -0,0 +1,118 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe ::Integrations::Field do + subject(:field) { described_class.new(**attrs) } + + let(:attrs) { { name: nil } } + + describe '#name' do + before do + attrs[:name] = :foo + end + + it 'is stringified' do + expect(field.name).to eq 'foo' + expect(field[:name]).to eq 'foo' + end + + context 'when not set' do + before do + attrs.delete(:name) + end + + it 'complains' do + expect { field }.to raise_error(ArgumentError) + end + end + end + + described_class::ATTRIBUTES.each do |name| + describe "##{name}" do + it "responds to #{name}" do + expect(field).to be_respond_to(name) + end + + context 'when not set' do + before do + attrs.delete(name) + end + + let(:have_correct_default) do + case name + when :api_only + be false + when :type + eq 'text' + else + be_nil + end + end + + it 'has the correct default' do + expect(field[name]).to have_correct_default + expect(field.send(name)).to have_correct_default + end + end + + context 'when set to a static value' do + before do + attrs[name] = :known + end + + it 'is known' do + expect(field[name]).to eq(:known) + expect(field.send(name)).to eq(:known) + end + end + + context 'when set to a dynamic value' do + before do + attrs[name] = -> { Time.current } + end + + it 'is computed' do + start = Time.current + + travel_to(start + 1.minute) do + expect(field[name]).to be_after(start) + expect(field.send(name)).to be_after(start) + end + end + end + end + end + + describe '#sensitive' do + context 'when empty' do + it { is_expected.not_to be_sensitive } + end + + context 'when a password field' do + before do + attrs[:type] = 'password' + end + + it { is_expected.to be_sensitive } + end + + %w[token api_token api_key secret_key secret_sauce password passphrase].each do |name| + context "when named #{name}" do + before do + attrs[:name] = name + end + + it { is_expected.to be_sensitive } + end + end + + context "when named url" do + before do + attrs[:name] = :url + end + + it { is_expected.not_to be_sensitive } + end + end +end diff --git a/spec/models/integrations/harbor_spec.rb b/spec/models/integrations/harbor_spec.rb new file mode 100644 index 00000000000..4a6eb27d63a --- /dev/null +++ b/spec/models/integrations/harbor_spec.rb @@ -0,0 +1,133 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Integrations::Harbor do + let(:url) { 'https://demo.goharbor.io' } + let(:project_name) { 'testproject' } + let(:username) { 'harborusername' } + let(:password) { 'harborpassword' } + let(:harbor_integration) { create(:harbor_integration) } + + describe "masked password" do + subject { build(:harbor_integration) } + + it { is_expected.not_to allow_value('hello').for(:password) } + it { is_expected.not_to allow_value('hello world').for(:password) } + it { is_expected.not_to allow_value('hello$VARIABLEworld').for(:password) } + it { is_expected.not_to allow_value('hello\rworld').for(:password) } + it { is_expected.to allow_value('helloworld').for(:password) } + end + + describe '#fields' do + it 'returns custom fields' do + expect(harbor_integration.fields.pluck(:name)).to eq(%w[url project_name username password]) + end + end + + describe '#test' do + let(:test_response) { "pong" } + + before do + allow_next_instance_of(Gitlab::Harbor::Client) do |client| + allow(client).to receive(:ping).and_return(test_response) + end + end + + it 'gets response from Gitlab::Harbor::Client#ping' do + expect(harbor_integration.test).to eq(test_response) + end + end + + describe '#help' do + it 'renders prompt information' do + expect(harbor_integration.help).not_to be_empty + end + end + + describe '.to_param' do + it 'returns the name of the integration' do + expect(described_class.to_param).to eq('harbor') + end + end + + context 'ci variables' do + it 'returns vars when harbor_integration is activated' do + ci_vars = [ + { key: 'HARBOR_URL', value: url }, + { key: 'HARBOR_PROJECT', value: project_name }, + { key: 'HARBOR_USERNAME', value: username }, + { key: 'HARBOR_PASSWORD', value: password, public: false, masked: true } + ] + + expect(harbor_integration.ci_variables).to match_array(ci_vars) + end + + it 'returns [] when harbor_integration is inactive' do + harbor_integration.update!(active: false) + expect(harbor_integration.ci_variables).to match_array([]) + end + end + + describe 'before_validation :reset_username_and_password' do + context 'when username/password was previously set' do + it 'resets username and password if url changed' do + harbor_integration.url = 'https://anotherharbor.com' + harbor_integration.valid? + + expect(harbor_integration.password).to be_nil + expect(harbor_integration.username).to be_nil + end + + it 'does not reset password if username changed' do + harbor_integration.username = 'newusername' + harbor_integration.valid? + + expect(harbor_integration.password).to eq('harborpassword') + end + + it 'does not reset username if password changed' do + harbor_integration.password = 'newpassword' + harbor_integration.valid? + + expect(harbor_integration.username).to eq('harborusername') + end + + it "does not reset password if new url is set together with password, even if it's the same password" do + harbor_integration.url = 'https://anotherharbor.com' + harbor_integration.password = 'harborpassword' + harbor_integration.valid? + + expect(harbor_integration.password).to eq('harborpassword') + expect(harbor_integration.username).to be_nil + expect(harbor_integration.url).to eq('https://anotherharbor.com') + end + + it "does not reset username if new url is set together with username, even if it's the same username" do + harbor_integration.url = 'https://anotherharbor.com' + harbor_integration.username = 'harborusername' + harbor_integration.valid? + + expect(harbor_integration.password).to be_nil + expect(harbor_integration.username).to eq('harborusername') + expect(harbor_integration.url).to eq('https://anotherharbor.com') + end + end + + it 'saves password if new url is set together with password when no password was previously set' do + harbor_integration.password = nil + harbor_integration.username = nil + + harbor_integration.url = 'https://anotherharbor.com' + harbor_integration.password = 'newpassword' + harbor_integration.username = 'newusername' + harbor_integration.save! + + expect(harbor_integration).to have_attributes( + url: 'https://anotherharbor.com', + password: 'newpassword', + username: 'newusername' + ) + end + end +end diff --git a/spec/models/integrations/jira_spec.rb b/spec/models/integrations/jira_spec.rb index 6ce84c28044..08656bfe543 100644 --- a/spec/models/integrations/jira_spec.rb +++ b/spec/models/integrations/jira_spec.rb @@ -109,6 +109,32 @@ RSpec.describe Integrations::Jira do end end + describe '#sections' do + let(:integration) { create(:jira_integration) } + + subject(:sections) { integration.sections.map { |s| s[:type] } } + + context 'when project_level? is true' do + before do + allow(integration).to receive(:project_level?).and_return(true) + end + + it 'includes SECTION_TYPE_JIRA_ISSUES' do + expect(sections).to include(described_class::SECTION_TYPE_JIRA_ISSUES) + end + end + + context 'when project_level? is false' do + before do + allow(integration).to receive(:project_level?).and_return(false) + end + + it 'does not include SECTION_TYPE_JIRA_ISSUES' do + expect(sections).not_to include(described_class::SECTION_TYPE_JIRA_ISSUES) + end + end + end + describe '.reference_pattern' do using RSpec::Parameterized::TableSyntax diff --git a/spec/models/integrations/slack_spec.rb b/spec/models/integrations/slack_spec.rb index 4661d9c8291..9f69f4f51f8 100644 --- a/spec/models/integrations/slack_spec.rb +++ b/spec/models/integrations/slack_spec.rb @@ -7,7 +7,7 @@ RSpec.describe Integrations::Slack do describe '#execute' do before do - stub_request(:post, "https://slack.service.url/") + stub_request(:post, slack_integration.webhook) end let_it_be(:slack_integration) { create(:integrations_slack, branches_to_be_notified: 'all') } |