Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let VRT::Map#find_node accept nil values #29

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/vrt/map.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
44 changes: 44 additions & 0 deletions spec/vrt/map_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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' }
Expand Down