diff --git a/spec/models/ci/bridge_spec.rb b/spec/models/ci/bridge_spec.rb index ae8c5aea8580bf41ea469e0df5a081be284b0226..bc3a5ccddb2f4c607833e250c01fb8d7a8246224 100644 --- a/spec/models/ci/bridge_spec.rb +++ b/spec/models/ci/bridge_spec.rb @@ -552,261 +552,264 @@ ) end end - end - - describe '#variables' do - it 'returns bridge scoped variables and pipeline persisted variables' do - expect(bridge.variables.to_hash) - .to eq(bridge.scoped_variables.concat(bridge.pipeline.persisted_variables).to_hash) - end - end - - describe '#pipeline_variables' do - it 'returns the pipeline variables' do - expect(bridge.pipeline_variables).to eq(bridge.pipeline.variables) - end - end - - describe '#pipeline_schedule_variables' do - context 'when pipeline is on a schedule' do - let(:pipeline_schedule) { create(:ci_pipeline_schedule, project: project) } - let(:pipeline) { create(:ci_pipeline, pipeline_schedule: pipeline_schedule) } - - it 'returns the pipeline schedule variables' do - create(:ci_pipeline_schedule_variable, key: 'FOO', value: 'foo', pipeline_schedule: pipeline.pipeline_schedule) - - pipeline_schedule_variables = bridge.reload.pipeline_schedule_variables - expect(pipeline_schedule_variables).to match_array([have_attributes({ key: 'FOO', value: 'foo' })]) - end - end - - context 'when pipeline is not on a schedule' do - it 'returns empty array' do - expect(bridge.pipeline_schedule_variables).to eq([]) - end - end - end - describe '#forward_yaml_variables?' do - using RSpec::Parameterized::TableSyntax - - where(:forward, :result) do - true | true - false | false - nil | true - end - - with_them do + describe 'variables expansion' do let(:options) do { trigger: { project: 'my/project', branch: 'master', - forward: { yaml_variables: forward }.compact + forward: { yaml_variables: true, + pipeline_variables: true }.compact } } end - let(:bridge) { build(:ci_bridge, options: options) } + let(:yaml_variables) do + [ + { + key: 'EXPANDED_PROJECT_VAR6', + value: 'project value6 $PROJECT_PROTECTED_VAR' + }, + { + key: 'EXPANDED_GROUP_VAR6', + value: 'group value6 $GROUP_PROTECTED_VAR' + }, - it { expect(bridge.forward_yaml_variables?).to eq(result) } - end - end + { + key: 'VAR7', + value: 'value7 $VAR1', + raw: true + } + ] + end - describe 'variables expansion' do - let(:options) do - { - trigger: { - project: 'my/project', - branch: 'master', - forward: { yaml_variables: true, - pipeline_variables: true }.compact - } - } - end + let_it_be(:downstream_creator_user) { create(:user) } + let_it_be(:bridge_creator_user) { create(:user) } - let(:yaml_variables) do - [ - { - key: 'EXPANDED_PROJECT_VAR6', - value: 'project value6 $PROJECT_PROTECTED_VAR' - }, - { - key: 'EXPANDED_GROUP_VAR6', - value: 'group value6 $GROUP_PROTECTED_VAR' - }, + let_it_be(:bridge_group) { create(:group) } + let_it_be(:downstream_group) { create(:group) } + let_it_be(:downstream_project) { create(:project, creator: downstream_creator_user, group: downstream_group) } + let_it_be(:project) do + create(:project, :repository, :in_group, creator: bridge_creator_user, group: bridge_group) + end - { - key: 'VAR7', - value: 'value7 $VAR1', - raw: true - } - ] - end + let(:bridge) { build(:ci_bridge, :playable, pipeline: pipeline, downstream: downstream_project) } + let!(:pipeline) { create(:ci_pipeline, project: project) } - let_it_be(:downstream_creator_user) { create(:user) } - let_it_be(:bridge_creator_user) { create(:user) } - - let_it_be(:bridge_group) { create(:group) } - let_it_be(:downstream_group) { create(:group) } - let_it_be(:downstream_project) { create(:project, creator: downstream_creator_user, group: downstream_group) } - let_it_be(:project) { create(:project, :repository, :in_group, creator: bridge_creator_user, group: bridge_group) } - let(:bridge) { build(:ci_bridge, :playable, pipeline: pipeline, downstream: downstream_project) } - let!(:pipeline) { create(:ci_pipeline, project: project) } - - let!(:ci_variable) do - create(:ci_variable, - project: project, - key: 'PROJECT_PROTECTED_VAR', - value: 'this is a secret', - protected: is_variable_protected?) - end + let!(:ci_variable) do + create(:ci_variable, + project: project, + key: 'PROJECT_PROTECTED_VAR', + value: 'this is a secret', + protected: is_variable_protected?) + end - let!(:ci_group_variable) do - create(:ci_group_variable, - group: bridge_group, - key: 'GROUP_PROTECTED_VAR', - value: 'this is a secret', - protected: is_variable_protected?) - end + let!(:ci_group_variable) do + create(:ci_group_variable, + group: bridge_group, + key: 'GROUP_PROTECTED_VAR', + value: 'this is a secret', + protected: is_variable_protected?) + end - before do - bridge.yaml_variables = yaml_variables - allow(bridge.project).to receive(:protected_for?).and_return(true) - end + before do + bridge.yaml_variables = yaml_variables + allow(bridge.project).to receive(:protected_for?).and_return(true) + end - shared_examples 'expands variables from a project downstream' do - it do - vars = bridge.downstream_variables - expect(vars).to include({ key: 'EXPANDED_PROJECT_VAR6', value: 'project value6 this is a secret' }) + shared_examples 'expands variables from a project downstream' do + it do + vars = bridge.downstream_variables + expect(vars).to include({ key: 'EXPANDED_PROJECT_VAR6', value: 'project value6 this is a secret' }) + end end - end - shared_examples 'expands variables from a group downstream' do - it do - vars = bridge.downstream_variables - expect(vars).to include({ key: 'EXPANDED_GROUP_VAR6', value: 'group value6 this is a secret' }) + shared_examples 'expands variables from a group downstream' do + it do + vars = bridge.downstream_variables + expect(vars).to include({ key: 'EXPANDED_GROUP_VAR6', value: 'group value6 this is a secret' }) + end end - end - shared_examples 'expands project and group variables downstream' do - it_behaves_like 'expands variables from a project downstream' + shared_examples 'expands project and group variables downstream' do + it_behaves_like 'expands variables from a project downstream' - it_behaves_like 'expands variables from a group downstream' - end + it_behaves_like 'expands variables from a group downstream' + end - shared_examples 'does not expand variables from a project downstream' do - it do - vars = bridge.downstream_variables - expect(vars).not_to include({ key: 'EXPANDED_PROJECT_VAR6', value: 'project value6 this is a secret' }) + shared_examples 'does not expand variables from a project downstream' do + it do + vars = bridge.downstream_variables + expect(vars).not_to include({ key: 'EXPANDED_PROJECT_VAR6', value: 'project value6 this is a secret' }) + end end - end - shared_examples 'does not expand variables from a group downstream' do - it do - vars = bridge.downstream_variables - expect(vars).not_to include({ key: 'EXPANDED_GROUP_VAR6', value: 'group value6 this is a secret' }) + shared_examples 'does not expand variables from a group downstream' do + it do + vars = bridge.downstream_variables + expect(vars).not_to include({ key: 'EXPANDED_GROUP_VAR6', value: 'group value6 this is a secret' }) + end + end + + shared_examples 'feature flag is disabled' do + before do + stub_feature_flags(exclude_protected_variables_from_multi_project_pipeline_triggers: false) + end + + it_behaves_like 'expands project and group variables downstream' end - end - shared_examples 'feature flag is disabled' do - before do - stub_feature_flags(exclude_protected_variables_from_multi_project_pipeline_triggers: false) + shared_examples 'does not expand project and group variables downstream' do + it_behaves_like 'does not expand variables from a project downstream' + + it_behaves_like 'does not expand variables from a group downstream' end - it_behaves_like 'expands project and group variables downstream' - end + context 'when they are protected' do + let!(:is_variable_protected?) { true } - shared_examples 'does not expand project and group variables downstream' do - it_behaves_like 'does not expand variables from a project downstream' + context 'and downstream project group is different from bridge group' do + it_behaves_like 'does not expand project and group variables downstream' - it_behaves_like 'does not expand variables from a group downstream' - end + it_behaves_like 'feature flag is disabled' + end - context 'when they are protected' do - let!(:is_variable_protected?) { true } + context 'and there is no downstream project' do + let(:downstream_project) { nil } - context 'and downstream project group is different from bridge group' do - it_behaves_like 'does not expand project and group variables downstream' + it_behaves_like 'expands project and group variables downstream' - it_behaves_like 'feature flag is disabled' - end + it_behaves_like 'feature flag is disabled' + end - context 'and there is no downstream project' do - let(:downstream_project) { nil } + context 'and downstream project equals bridge project' do + let(:downstream_project) { project } - it_behaves_like 'expands project and group variables downstream' + it_behaves_like 'expands project and group variables downstream' - it_behaves_like 'feature flag is disabled' - end + it_behaves_like 'feature flag is disabled' + end - context 'and downstream project equals bridge project' do - let(:downstream_project) { project } + context 'and downstream project group is equal to bridge project group' do + let_it_be(:downstream_project) { create(:project, creator: downstream_creator_user, group: bridge_group) } - it_behaves_like 'expands project and group variables downstream' + it_behaves_like 'expands variables from a group downstream' - it_behaves_like 'feature flag is disabled' - end + it_behaves_like 'does not expand variables from a project downstream' - context 'and downstream project group is equal to bridge project group' do - let_it_be(:downstream_project) { create(:project, creator: downstream_creator_user, group: bridge_group) } + it_behaves_like 'feature flag is disabled' + end - it_behaves_like 'expands variables from a group downstream' + context 'and downstream project has no group' do + let_it_be(:downstream_project) { create(:project, creator: downstream_creator_user) } - it_behaves_like 'does not expand variables from a project downstream' + it_behaves_like 'does not expand project and group variables downstream' - it_behaves_like 'feature flag is disabled' + it_behaves_like 'feature flag is disabled' + end end - context 'and downstream project has no group' do - let_it_be(:downstream_project) { create(:project, creator: downstream_creator_user) } + context 'when they are not protected' do + let!(:is_variable_protected?) { false } - it_behaves_like 'does not expand project and group variables downstream' + context 'and downstream project group is different from bridge group' do + it_behaves_like 'expands project and group variables downstream' - it_behaves_like 'feature flag is disabled' - end - end + it_behaves_like 'feature flag is disabled' + end - context 'when they are not protected' do - let!(:is_variable_protected?) { false } + context 'and there is no downstream project' do + let(:downstream_project) { nil } - context 'and downstream project group is different from bridge group' do - it_behaves_like 'expands project and group variables downstream' + it_behaves_like 'expands project and group variables downstream' - it_behaves_like 'feature flag is disabled' - end + it_behaves_like 'feature flag is disabled' + end - context 'and there is no downstream project' do - let(:downstream_project) { nil } + context 'and downstream project equals bridge project' do + let(:downstream_project) { project } - it_behaves_like 'expands project and group variables downstream' + it_behaves_like 'expands project and group variables downstream' - it_behaves_like 'feature flag is disabled' - end + it_behaves_like 'feature flag is disabled' + end - context 'and downstream project equals bridge project' do - let(:downstream_project) { project } + context 'and downstream project group is equal to bridge project group' do + let_it_be(:downstream_project) { create(:project, creator: downstream_creator_user, group: bridge_group) } - it_behaves_like 'expands project and group variables downstream' + it_behaves_like 'expands project and group variables downstream' - it_behaves_like 'feature flag is disabled' + it_behaves_like 'feature flag is disabled' + end + + context 'and downstream project has no group' do + let_it_be(:downstream_project) { create(:project, creator: downstream_creator_user) } + + it_behaves_like 'expands project and group variables downstream' + + it_behaves_like 'feature flag is disabled' + end end + end + end + + describe '#variables' do + it 'returns bridge scoped variables and pipeline persisted variables' do + expect(bridge.variables.to_hash) + .to eq(bridge.scoped_variables.concat(bridge.pipeline.persisted_variables).to_hash) + end + end - context 'and downstream project group is equal to bridge project group' do - let_it_be(:downstream_project) { create(:project, creator: downstream_creator_user, group: bridge_group) } + describe '#pipeline_variables' do + it 'returns the pipeline variables' do + expect(bridge.pipeline_variables).to eq(bridge.pipeline.variables) + end + end - it_behaves_like 'expands project and group variables downstream' + describe '#pipeline_schedule_variables' do + context 'when pipeline is on a schedule' do + let(:pipeline_schedule) { create(:ci_pipeline_schedule, project: project) } + let(:pipeline) { create(:ci_pipeline, pipeline_schedule: pipeline_schedule) } - it_behaves_like 'feature flag is disabled' + it 'returns the pipeline schedule variables' do + create(:ci_pipeline_schedule_variable, key: 'FOO', value: 'foo', pipeline_schedule: pipeline.pipeline_schedule) + + pipeline_schedule_variables = bridge.reload.pipeline_schedule_variables + expect(pipeline_schedule_variables).to match_array([have_attributes({ key: 'FOO', value: 'foo' })]) + end + end + + context 'when pipeline is not on a schedule' do + it 'returns empty array' do + expect(bridge.pipeline_schedule_variables).to eq([]) end + end + end - context 'and downstream project has no group' do - let_it_be(:downstream_project) { create(:project, creator: downstream_creator_user) } + describe '#forward_yaml_variables?' do + using RSpec::Parameterized::TableSyntax - it_behaves_like 'expands project and group variables downstream' + where(:forward, :result) do + true | true + false | false + nil | true + end - it_behaves_like 'feature flag is disabled' + with_them do + let(:options) do + { + trigger: { + project: 'my/project', + branch: 'master', + forward: { yaml_variables: forward }.compact + } + } end + + let(:bridge) { build(:ci_bridge, options: options) } + + it { expect(bridge.forward_yaml_variables?).to eq(result) } end end