Skip to content

Commit

Permalink
Merge pull request #42 from smortex/unwrap-in-errors
Browse files Browse the repository at this point in the history
Unwrap sensitive values in error messages
  • Loading branch information
jordanbreen28 authored Mar 8, 2024
2 parents 2ab0d16 + b9dde53 commit 6888827
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/rspec-puppet/matchers/parameter_matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ def matches?(resource)
actual = resource[@parameter]
expected = @value

actual = RSpec::Puppet::Sensitive.new(actual.unwrap) if actual.is_a?(Puppet::Pops::Types::PSensitiveType::Sensitive)

# Puppet flattens an array with a single value into just the value and
# this can cause confusion when testing as people expect when you put
# an array in, you'll get an array out.
Expand Down
5 changes: 5 additions & 0 deletions lib/rspec-puppet/sensitive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ def inspect
"Sensitive(#{@value.inspect})"
end

# @return the unwrapped value (needed to show diff)
def to_s
inspect
end

# Check for equality with another value.
# If compared to Puppet Sensitive type, it compares the wrapped values.

Expand Down
29 changes: 29 additions & 0 deletions spec/unit/matchers/parameter_matcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,34 @@
expect(subject.matches?(foo_parameter: nil)).to be(false)
end
end

context 'with sensitive("foo") expected' do
subject do
described_class.new(:foo_parameter, RSpec::Puppet::Sensitive.new('foo'), :should)
end

it 'matches sensitive("foo")' do
expect(subject.matches?(foo_parameter: RSpec::Puppet::Sensitive.new('foo'))).to be(true)
expect(subject.errors.size).to eq(0)
end

it 'does not match sensitive("bar")' do
expect(subject.matches?(foo_parameter: RSpec::Puppet::Sensitive.new('bar'))).to be(false)
expect(subject.errors.size).to eq(1)
expect(subject.errors[0].message).to eq('foo_parameter set to Sensitive("foo") but it is set to Sensitive("bar")')
end

it 'does not matches "foo"' do
expect(subject.matches?(foo_parameter: 'foo')).to be(false)
expect(subject.errors.size).to eq(1)
expect(subject.errors[0].message).to eq('foo_parameter set to Sensitive("foo") but it is set to "foo"')
end

it 'does not matches "Sensitive [value redacted]"' do
expect(subject.matches?(foo_parameter: 'Sensitive [value redacted]')).to be(false)
expect(subject.errors.size).to eq(1)
expect(subject.errors[0].message).to eq('foo_parameter set to Sensitive("foo") but it is set to "Sensitive [value redacted]"')
end
end
end
end

0 comments on commit 6888827

Please sign in to comment.