diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2023-05-17 16:05:49 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2023-05-17 16:05:49 +0000 |
commit | 43a25d93ebdabea52f99b05e15b06250cd8f07d7 (patch) | |
tree | dceebdc68925362117480a5d672bcff122fb625b /spec/services/clusters/agents/authorizations/ci_access/filter_service_spec.rb | |
parent | 20c84b99005abd1c82101dfeff264ac50d2df211 (diff) | |
download | gitlab-ce-0f94cf6ca9d272d8e0fda4a7a597866cf3dc1fc0.tar.gz |
Add latest changes from gitlab-org/gitlab@16-0-stable-eev16.0.0-rc4216-0-stable
Diffstat (limited to 'spec/services/clusters/agents/authorizations/ci_access/filter_service_spec.rb')
-rw-r--r-- | spec/services/clusters/agents/authorizations/ci_access/filter_service_spec.rb | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/spec/services/clusters/agents/authorizations/ci_access/filter_service_spec.rb b/spec/services/clusters/agents/authorizations/ci_access/filter_service_spec.rb new file mode 100644 index 00000000000..45443cfd887 --- /dev/null +++ b/spec/services/clusters/agents/authorizations/ci_access/filter_service_spec.rb @@ -0,0 +1,100 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Clusters::Agents::Authorizations::CiAccess::FilterService, feature_category: :continuous_integration do + describe '#execute' do + let_it_be(:group) { create(:group) } + let_it_be(:project) { create(:project, group: group) } + + let(:agent_authorizations_without_env) do + [ + build(:agent_ci_access_project_authorization, project: project, agent: build(:cluster_agent, project: project)), + build(:agent_ci_access_group_authorization, group: group, agent: build(:cluster_agent, project: project)), + ::Clusters::Agents::Authorizations::CiAccess::ImplicitAuthorization.new(agent: build(:cluster_agent, project: project)) + ] + end + + let(:filter_params) { {} } + + subject(:execute_filter) { described_class.new(agent_authorizations, filter_params).execute } + + context 'when there are no filters' do + let(:agent_authorizations) { agent_authorizations_without_env } + + it 'returns the authorizations as is' do + expect(execute_filter).to eq agent_authorizations + end + end + + context 'when filtering by environment' do + let(:agent_authorizations_with_env) do + [ + build( + :agent_ci_access_project_authorization, + project: project, + agent: build(:cluster_agent, project: project), + environments: ['staging', 'review/*', 'production'] + ), + build( + :agent_ci_access_group_authorization, + group: group, + agent: build(:cluster_agent, project: project), + environments: ['staging', 'review/*', 'production'] + ) + ] + end + + let(:agent_authorizations_with_different_env) do + [ + build( + :agent_ci_access_project_authorization, + project: project, + agent: build(:cluster_agent, project: project), + environments: ['staging'] + ), + build( + :agent_ci_access_group_authorization, + group: group, + agent: build(:cluster_agent, project: project), + environments: ['staging'] + ) + ] + end + + let(:agent_authorizations) do + ( + agent_authorizations_without_env + + agent_authorizations_with_env + + agent_authorizations_with_different_env + ) + end + + let(:filter_params) { { environment: 'production' } } + + it 'returns the authorizations with the given environment AND authorizations without any environment' do + expected_authorizations = agent_authorizations_with_env + agent_authorizations_without_env + + expect(execute_filter).to match_array expected_authorizations + end + + context 'when environment filter has a wildcard' do + let(:filter_params) { { environment: 'review/123' } } + + it 'returns the authorizations with matching environments AND authorizations without any environment' do + expected_authorizations = agent_authorizations_with_env + agent_authorizations_without_env + + expect(execute_filter).to match_array expected_authorizations + end + end + + context 'when environment filter is nil' do + let(:filter_params) { { environment: nil } } + + it 'returns the authorizations without any environment' do + expect(execute_filter).to match_array agent_authorizations_without_env + end + end + end + end +end |