1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Ci::HidableVariable, feature_category: :secrets_management do
using RSpec::Parameterized::TableSyntax
shared_examples 'HiddenVariableValidations' do |variable_class, association|
let(:variable_key) { 'TESTKEY1' }
let(:variable_value) { 'TESTVAL1' }
let(:create_variable) do
variable_class.new(association => send(association),
masked: pending_masked,
hidden: pending_hidden,
key: variable_key, value: variable_value).save!
end
describe '#validate_masked_and_hidden_on_create' do
subject(:validate_create) { create_variable }
context 'when masked and hidden attribute are allowed' do
where(:pending_masked, :pending_hidden) do
true | true
false | false
true | false
end
with_them do
it 'passes the validation' do
expect do
validate_create
end.not_to raise_error
end
end
end
context 'when masked and hidden attribute are not allowed' do
where(:pending_masked, :pending_hidden) do
false | true
end
with_them do
it 'raises an error' do
expect do
validate_create
end.to raise_error(ActiveRecord::RecordInvalid,
'Validation failed: Masked should be true when variable is hidden')
end
end
end
end
describe '#validate_masked_and_hidden_on_update' do
subject(:validate_update) do
test_variable = variable_class.create!(association => send(association),
masked: stored_masked,
hidden: stored_hidden,
key: variable_key, value: variable_value
)
test_variable.update!(masked: pending_masked, hidden: pending_hidden)
end
context 'when update is allowed' do
where(:stored_masked, :stored_hidden, :pending_masked,
:pending_hidden) do
true | false | true | false
true | false | false | false
true | true | true | true
false | false | true | false
end
with_them do
it 'passed the validation' do
expect do
validate_update
end.not_to raise_error
end
end
end
context 'when update is not allowed' do
where(:stored_masked, :stored_hidden, :pending_masked,
:pending_hidden) do
true | true | true | false
true | true | false | false
true | true | false | true
false | false | true | true
end
with_them do
it 'does not pass the validation' do
expected_error_message =
if stored_hidden == pending_hidden && stored_masked != pending_masked
'Updating masked attribute is not allowed on updates for hidden variables.'
else
'Updating hidden attribute is not allowed on updates.'
end
expect do
validate_update
end.to raise_error(ActiveRecord::RecordInvalid,
"Validation failed: #{expected_error_message}")
end
end
end
end
end
let_it_be(:project) { create(:project) }
let_it_be(:group) { create(:group) }
context 'with Ci::Variable' do
it_behaves_like 'HiddenVariableValidations', Ci::Variable, :project
end
context 'with Ci::GroupVariable' do
it_behaves_like 'HiddenVariableValidations', Ci::GroupVariable, :group
end
end
|