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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
# frozen_string_literal: true
module QA
RSpec.describe QA::Support::Helpers::Masker do
let(:secrets) { [:secret_content, 'secret'] }
let(:content) do
{
numeric_content: 1,
secret_content: 'a-private-token',
public_content: 'gitlab',
array_content: ['secret', :foo],
private: 'hide me',
hash_content: {
secret: 'a-private-key',
public: 'gitlab',
array: ['secret', :bar],
hash: { foo: 'secret' }
}
}
end
subject { described_class.new(by_key: secrets).mask(content) }
shared_examples 'masks secrets' do
it 'masks secrets' do
expect(subject).to match(a_hash_including(expected))
end
end
describe '.mask' do
it 'instantiates an object and calls the mask instance method' do
instance = instance_double('QA::Support::Helpers::Masker')
expect(described_class).to receive(:new)
.with(by_key: secrets, by_value: nil, mask: nil)
.and_return(instance)
expect(instance).to receive(:mask).with(content)
described_class.mask(content, by_key: secrets, by_value: nil, mask: nil)
end
end
describe '#initialize' do
it 'requires by_key or by_key' do
expect { described_class.new }.to raise_error(ArgumentError, /Please specify `by_key` or `by_value`/)
end
end
describe '#mask' do
context 'when content is blank' do
let(:content) { [] }
it 'returns content' do
expect(subject).to match([])
end
end
context 'when masking by key' do
subject { described_class.new(by_key: secrets).mask(content) }
let(:secrets) { [:secret_content, 'secret'] }
it 'masks secrets' do
expect(subject).to match(a_hash_including({
secret_content: '****',
hash_content: {
secret: '****',
public: 'gitlab',
array: ['secret', :bar],
hash: { foo: 'secret' }
}
}))
end
it 'does not mask by value' do
expect(subject).to match(a_hash_including({
array_content: ['secret', :foo],
hash_content: {
secret: '****',
public: 'gitlab',
array: ['secret', :bar],
hash: { foo: 'secret' }
}
}))
end
context 'with values that are not strings' do
let(:secrets) { [:numeric_content] }
let(:expected) { { numeric_content: '****' } }
include_examples 'masks secrets'
end
context 'when by_key is not an array' do
let(:secrets) { :secret_content }
let(:expected) { { secret_content: '****' } }
include_examples 'masks secrets'
end
end
context 'when masking by value' do
shared_examples 'does not mask' do
it 'does not mask' do
expect(subject).to eq(content)
end
end
subject { described_class.new(by_value: secrets).mask(content) }
let(:secrets) { [:private, 'secret'] }
it 'masks secrets' do
expect(subject).to match(a_hash_including({
secret_content: 'a-****-token',
array_content: ['****', :foo],
private: 'hide me',
hash_content: {
secret: 'a-****-key',
public: 'gitlab',
array: ['****', :bar],
hash: { foo: '****' }
}
}))
end
it 'does not mask by key' do
expect(subject).to match(a_hash_including(private: 'hide me'))
expect(subject.fetch(:hash_content)).to match(a_hash_including(secret: 'a-****-key'))
end
context 'when content is an Array' do
let(:content) { %w[secret not-secret] }
it 'masks secrets' do
expect(subject).to match_array(%w[**** not-****])
end
end
context 'when content is a String' do
let(:content) { 'secret' }
let(:expected) { '****' }
it 'masks secret values' do
expect(subject).to eq(expected)
end
end
context 'when content is an Integer' do
let(:content) { 1 }
include_examples 'does not mask'
end
context 'when content is a Float' do
let(:content) { 1.0 }
include_examples 'does not mask'
end
context 'when content is a Boolean' do
let(:content) { false }
include_examples 'does not mask'
end
end
end
end
end
|