Skip to content

Commit

Permalink
Replaced exception with the warnings and removed related failing spec…
Browse files Browse the repository at this point in the history
…s(used earlier for raising issue) (#367)

* Replaced exception with the warnings and removed related failing specs
* Added regeex for matching whitespace
* Added additional specs for validating vault data with tab or new line

Signed-off-by: sanga17 <[email protected]>
  • Loading branch information
sanga1794 authored Sep 1, 2021
1 parent 7cb395b commit 51fff69
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
4 changes: 2 additions & 2 deletions lib/chef/knife/mixin/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def validate_json(json)
next unless printable?(value.to_s)

msg = "Value '#{value}' of key '#{key}' contains non-printable characters. Check that backslashes are escaped with another backslash (e.g. C:\\\\Windows) in double-quoted strings."
raise ChefVault::Exceptions::InvalidValue, msg
ChefVault::Log.warn(msg)
end
end
end
Expand All @@ -69,7 +69,7 @@ def validate_json(json)
# returns true if string is free of non-printable characters (escape sequences)
# this returns false for whitespace escape sequences as well, e.g. \n\t
def printable?(string)
/[^[:print:]]/.match(string)
/[^[:print:]]|[[:space:]]/.match(string)
end
end
end
Expand Down
13 changes: 8 additions & 5 deletions spec/chef/helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,24 @@
RSpec.describe ChefVault::Mixin::Helper do
include ChefVault::Mixin::Helper

let(:json) { { "username": "root" } }
let(:json_data) { '{"username": "root", "password": "abcabc"}' }
let(:json_data_control_char) { '{"username": "root", "password": "abc\abc"}' }
let(:buggy_json_data) { '{"username": "root", "password": "abc\abc"' }

describe "#validate_json" do
it "Raises InvalidValue Exception when invalid data provided" do
expect { validate_json(buggy_json_data) }.to raise_error(ChefVault::Exceptions::InvalidValue)
end

it "Raises InvalidValue Exception when value consist of control characters" do
expect { validate_json(json_data_control_char) }.to raise_error(ChefVault::Exceptions::InvalidValue)
end

it "Not to raise error if valid data provided" do
expect { validate_json(json_data) }.to_not raise_error
end

it "not to raise error if data consist of tab/new line OR space" do
%w{abc\tabc abc\nabc}.each do |pass|
json_data_with_slash = json.merge("password": pass)
expect { validate_json(json_data_with_slash.to_s) }.to_not raise_error
end
end
end
end

0 comments on commit 51fff69

Please sign in to comment.