|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'spec_helper' |
| 4 | + |
| 5 | +describe 'stdlib::rewrap_sensitive_data' do |
| 6 | + it { is_expected.not_to be_nil } |
| 7 | + |
| 8 | + context 'when called with data containing no sensitive elements' do |
| 9 | + it { is_expected.to run.with_params({}).and_return({}) } |
| 10 | + it { is_expected.to run.with_params([]).and_return([]) } |
| 11 | + it { is_expected.to run.with_params('a_string').and_return('a_string') } |
| 12 | + it { is_expected.to run.with_params(42).and_return(42) } |
| 13 | + it { is_expected.to run.with_params(true).and_return(true) } |
| 14 | + it { is_expected.to run.with_params(false).and_return(false) } |
| 15 | + |
| 16 | + it { is_expected.to run.with_params({ 'foo' => 'bar' }).and_return({ 'foo' => 'bar' }) } |
| 17 | + end |
| 18 | + |
| 19 | + context 'when called with a hash containing a sensitive string' do |
| 20 | + it 'unwraps the sensitive string and returns a sensitive hash' do |
| 21 | + is_expected.to run.with_params( |
| 22 | + { |
| 23 | + 'username' => 'my_user', |
| 24 | + 'password' => sensitive('hunter2') |
| 25 | + }, |
| 26 | + ).and_return(sensitive( |
| 27 | + { |
| 28 | + 'username' => 'my_user', |
| 29 | + 'password' => 'hunter2' |
| 30 | + }, |
| 31 | + )) |
| 32 | + end |
| 33 | + end |
| 34 | + |
| 35 | + context 'when called with data containing lots of sensitive elements (including nested in arrays, and sensitive hashes etc)' do |
| 36 | + it 'recursively unwraps everything and marks the whole result as sensitive' do |
| 37 | + is_expected.to run.with_params( |
| 38 | + { |
| 39 | + 'a' => sensitive('bar'), |
| 40 | + 'b' => [ |
| 41 | + 1, |
| 42 | + 2, |
| 43 | + :undef, |
| 44 | + true, |
| 45 | + false, |
| 46 | + { |
| 47 | + 'password' => sensitive('secret'), |
| 48 | + 'weird_example' => sensitive({ 'foo' => sensitive(42) }) # A sensitive hash containing a sensitive Int as the value to a hash contained in an array which is the value of a hash key... |
| 49 | + }, |
| 50 | + ], |
| 51 | + 'c' => :undef, |
| 52 | + 'd' => [], |
| 53 | + 'e' => true, |
| 54 | + 'f' => false, |
| 55 | + }, |
| 56 | + ).and_return(sensitive( |
| 57 | + { |
| 58 | + 'a' => 'bar', |
| 59 | + 'b' => [ |
| 60 | + 1, |
| 61 | + 2, |
| 62 | + :undef, |
| 63 | + true, |
| 64 | + false, |
| 65 | + { |
| 66 | + 'password' => 'secret', |
| 67 | + 'weird_example' => { 'foo' => 42 } |
| 68 | + }, |
| 69 | + ], |
| 70 | + 'c' => :undef, |
| 71 | + 'd' => [], |
| 72 | + 'e' => true, |
| 73 | + 'f' => false, |
| 74 | + }, |
| 75 | + )) |
| 76 | + end |
| 77 | + end |
| 78 | + |
| 79 | + context 'when a hash _key_ is sensitive' do |
| 80 | + it 'unwraps the key' do |
| 81 | + is_expected.to run.with_params( |
| 82 | + { |
| 83 | + sensitive('key') => 'value', |
| 84 | + }, |
| 85 | + ).and_return(sensitive( |
| 86 | + { |
| 87 | + 'key' => 'value', |
| 88 | + }, |
| 89 | + )) |
| 90 | + end |
| 91 | + end |
| 92 | + |
| 93 | + context 'when called with a block' do |
| 94 | + context 'that upcases hash values' do |
| 95 | + it do |
| 96 | + is_expected.to run |
| 97 | + .with_params({ 'secret' => sensitive('hunter2') }) |
| 98 | + .with_lambda { |data| data.transform_values { |value| value.upcase } } |
| 99 | + .and_return(sensitive({ 'secret' => 'HUNTER2' })) |
| 100 | + end |
| 101 | + end |
| 102 | + context 'that converts data to yaml' do |
| 103 | + it do |
| 104 | + is_expected.to run |
| 105 | + .with_params({ 'secret' => sensitive('hunter2') }) |
| 106 | + .with_lambda { |data| data.to_yaml } |
| 107 | + .and_return(sensitive("---\nsecret: hunter2\n")) |
| 108 | + end |
| 109 | + end |
| 110 | + end |
| 111 | +end |
0 commit comments