diff --git a/CHANGELOG.md b/CHANGELOG.md index daed38a..7ddedcc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p ### Changed - Fixed bug for mappings with multiple keys and a default (resolves: [#26](https://github.com/bugcrowd/vrt-ruby/issues/26)) +- `VRT::Map#find_node` returns `nil` when passed an invalid vrt_id (resolves: [#32](https://github.com/bugcrowd/vrt-ruby/issues/32)) ### Removed - Removed `Gemfile.lock` from source control diff --git a/lib/vrt/map.rb b/lib/vrt/map.rb index 0d338e6..4771ce4 100644 --- a/lib/vrt/map.rb +++ b/lib/vrt/map.rb @@ -18,6 +18,7 @@ def initialize(version = nil) end def find_node(string, max_depth: 'variant') + return nil unless valid_identifier?(string) @_found_nodes[string + max_depth] ||= walk_node_tree(string, max_depth: max_depth) end diff --git a/spec/vrt/map_spec.rb b/spec/vrt/map_spec.rb index e1f80ae..1f55858 100644 --- a/spec/vrt/map_spec.rb +++ b/spec/vrt/map_spec.rb @@ -51,6 +51,50 @@ end end + describe '#find_node' do + subject { sample_map.find_node(vrt_id) } + + context 'when vrt_id is nil' do + let(:vrt_id) { nil } + + it { is_expected.to be_nil } + end + + context 'when vrt_id is not a valid identifier' do + let(:vrt_id) { "I'm not valid" } + + it { is_expected.to be_nil } + end + + context 'when vrt_id is not a string' do + let(:vrt_id) { 55 } + + it { is_expected.to be_nil } + end + + context 'when vrt_id is a valid identifier' do + context 'vrt_id does not exist in version' do + let(:vrt_id) { 'cool_new_concept' } + + it { is_expected.to be_nil } + end + + context 'vrt_id exists in version' do + context 'vrt_id is category level' do + let(:vrt_id) { 'server_security_misconfiguration' } + + it { is_expected.to be_a(VRT::Node) } + end + + context 'vrt_id is a variant' do + let(:vrt_id) { 'server_security_misconfiguration.using_default_credentials.production_server' } + + it { is_expected.to be_a(VRT::Node) } + end + end + end + end + describe '#get_lineage' do context 'with a complex hierarchy' do let(:id) { 'server_security_misconfiguration.using_default_credentials.production_server' }