debian-mirror-gitlab/spec/services/clusters/integrations/create_service_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

93 lines
2.6 KiB
Ruby
Raw Permalink Normal View History

2021-04-29 21:17:54 +05:30
# frozen_string_literal: true
require 'spec_helper'
2023-06-20 00:43:36 +05:30
RSpec.describe Clusters::Integrations::CreateService, '#execute', feature_category: :deployment_management do
2021-04-29 21:17:54 +05:30
let_it_be(:project) { create(:project) }
let_it_be_with_reload(:cluster) { create(:cluster, :provided_by_gcp, projects: [project]) }
let(:service) do
2022-03-02 08:16:31 +05:30
described_class.new(container: project, cluster: cluster, current_user: project.first_owner, params: params)
2021-04-29 21:17:54 +05:30
end
2021-06-08 01:23:25 +05:30
shared_examples_for 'a cluster integration' do |application_type|
let(:integration) { cluster.public_send("integration_#{application_type}") }
2021-04-29 21:17:54 +05:30
2021-06-08 01:23:25 +05:30
context 'when enabled param is true' do
let(:params) do
{ application_type: application_type, enabled: true }
end
2021-04-29 21:17:54 +05:30
2021-06-08 01:23:25 +05:30
it 'creates a new enabled integration' do
expect(service.execute).to be_success
2021-04-29 21:17:54 +05:30
2021-06-08 01:23:25 +05:30
expect(integration).to be_present
expect(integration).to be_persisted
expect(integration).to be_enabled
end
2021-04-29 21:17:54 +05:30
end
2021-06-08 01:23:25 +05:30
context 'when enabled param is false' do
let(:params) do
{ application_type: application_type, enabled: false }
end
2021-04-29 21:17:54 +05:30
2021-06-08 01:23:25 +05:30
it 'creates a new disabled integration' do
expect(service.execute).to be_success
2021-04-29 21:17:54 +05:30
2021-06-08 01:23:25 +05:30
expect(integration).to be_present
expect(integration).to be_persisted
expect(integration).not_to be_enabled
end
2021-04-29 21:17:54 +05:30
end
2021-06-08 01:23:25 +05:30
context 'when integration already exists' do
before do
create(:"clusters_integrations_#{application_type}", cluster: cluster, enabled: false)
end
2021-04-29 21:17:54 +05:30
let(:params) do
2021-06-08 01:23:25 +05:30
{ application_type: application_type, enabled: true }
2021-04-29 21:17:54 +05:30
end
2021-06-08 01:23:25 +05:30
it 'updates the integration' do
expect(integration).not_to be_enabled
2021-04-29 21:17:54 +05:30
expect(service.execute).to be_success
2021-06-08 01:23:25 +05:30
expect(integration.reload).to be_enabled
2021-04-29 21:17:54 +05:30
end
end
end
2021-06-08 01:23:25 +05:30
it_behaves_like 'a cluster integration', 'prometheus'
context 'when application_type is invalid' do
2021-04-29 21:17:54 +05:30
let(:params) do
{ application_type: 'something_else', enabled: true }
end
it 'errors' do
2022-08-27 11:52:29 +05:30
expect { service.execute }.to raise_error(ArgumentError)
2021-04-29 21:17:54 +05:30
end
end
2021-06-08 01:23:25 +05:30
context 'when user is unauthorized' do
let(:params) do
{ application_type: 'prometheus', enabled: true }
end
let(:service) do
unauthorized_user = create(:user)
described_class.new(container: project, cluster: cluster, current_user: unauthorized_user, params: params)
end
it 'returns error and does not create a new integration record' do
expect(service.execute).to be_error
expect(cluster.integration_prometheus).to be_nil
end
end
2021-04-29 21:17:54 +05:30
end