Skip to content

Commit

Permalink
Move shared example from pro into framework
Browse files Browse the repository at this point in the history
MS-1361
  • Loading branch information
farias-r7 committed Apr 7, 2016
1 parent f5415c8 commit 8f3f2f7
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 0 deletions.
10 changes: 10 additions & 0 deletions app/concerns/mdm/workspace/boundary_range.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ def boundary_must_be_ip_range
end
end

# Returns an array of addresses ranges
#
# @return [Array<String>]

This comment has been minimized.

Copy link
@fja43

fja43 Jan 19, 2020

spec/models/mdm/workspace_spec.rb

def addresses
(boundary || "").split("\n")
end

private

# Returns whether `string` is a valid IP address or IP address range.
Expand All @@ -68,5 +75,8 @@ def valid_ip_or_range?(string)
range = Rex::Socket::RangeWalker.new(string)
range && range.ranges && range.ranges.any?
end

end


end
33 changes: 33 additions & 0 deletions spec/models/mdm/workspace_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,37 @@
end
end
end

context 'methods' do
context '#valid_ip_or_range?' do
let(:ip_or_range) do
nil
end

subject do
-> {workspace.send(:valid_ip_or_range?, ip_or_range)}
end

context 'with exception from Rex::Socket::RangeWalker' do
before(:example) do
allow(Rex::Socket::RangeWalker).to receive(:new).with(ip_or_range).and_raise(StandardError)
end

it { is_expected.to raise_error(StandardError) }
end

context 'without exception from Rex::Socket::RangeWalker' do
context 'with valid IP' do
let(:ip_or_range) do
'192.168.0.1'
end

it { is_expected.to be_truthy }
end
end
end
end


it_should_behave_like 'Mdm::Workspace::Boundary'
end
134 changes: 134 additions & 0 deletions spec/support/shared/examples/mdm/workspace/boundary.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
RSpec.shared_examples_for 'Mdm::Workspace::Boundary' do
context 'methods' do
let(:boundary) do
nil
end

before(:example) do
workspace.boundary = boundary
end

context '#addresses' do
subject(:addresses) do
workspace.addresses
end

context 'with boundary' do
let(:boundary) do
expected_addresses.join("\n")
end

let(:expected_addresses) do
[
'10,10,10,10',
'192.168.0.1'
]
end

it 'should return addresses split on newline' do
expect(addresses).to eq(expected_addresses)
end
end

context 'without boundary' do
let(:boundary) do
nil
end

it 'should return an empty Array' do
expect(addresses).to eq([])
end
end
end

context '#boundary_must_be_ip_range' do
let(:error) do
'must be a valid IP range'
end

context 'with boundary' do
let(:boundary) do
'192.168.0.1'
end

it 'should split boundary' do
expect(Shellwords).to receive(:split).with(boundary).and_call_original

workspace.valid?
end

context 'with error from Shellwords.split' do
before(:example) do
allow(Shellwords).to receive(:split).with(boundary).and_raise(ArgumentError)
end

it 'should not raise error' do
expect {
workspace.valid?
}.to_not raise_error
end

it 'should not record an error' do
workspace.valid?

expect(workspace.errors[:boundary]).not_to include(error)
end
end

context 'with empty' do
let(:boundary) do
''
end

it 'should not record an error' do
workspace.valid?

expect(workspace.errors[:boundary]).not_to include(error)
end
end

context 'without empty' do
let(:ranges) do
[
'10.10.10.10',
'192.168.0.1'
]
end

let(:boundary) do
ranges.join(' ')
end

it 'should validate each range' do
ranges.each do |range|
expect(workspace).to receive(:valid_ip_or_range?).with(range).and_call_original

This comment has been minimized.

Copy link
@bojoh007

bojoh007 Jun 9, 2019

end

workspace.valid?
end

context 'with invalid range' do
let(:ranges) do
[
'192.168'
]
end

it 'should record error', :skip => 'https://www.pivotaltracker.com/story/show/43171927' do
expect(workspace).not_to be_valid
expect(workspace.errors[:boundary]).to include(error)
end
end
end
end

context 'without boundary' do
it 'should not record error' do
workspace.valid?

expect(workspace.errors[:boundary]).not_to include(error)
end
end
end
end
end

0 comments on commit 8f3f2f7

Please sign in to comment.