CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/spec/models/mdm/workspace_spec.rb
Views: 1904
1
RSpec.describe Mdm::Workspace, type: :model do
2
subject(:workspace) do
3
Mdm::Workspace.new
4
end
5
6
context 'validations' do
7
context 'boundary' do
8
let(:boundary) do
9
nil
10
end
11
12
let(:error) do
13
'must be a valid IP range'
14
end
15
16
context 'when the workspace is limited to a network' do
17
before(:example) do
18
workspace.boundary = boundary
19
workspace.limit_to_network = true
20
workspace.valid?
21
end
22
23
it 'should validate using #boundary_must_be_ip_range' do
24
expect(workspace).to receive(:boundary_must_be_ip_range).and_return(false)
25
26
workspace.valid?
27
end
28
29
context 'with valid IP' do
30
let(:boundary) do
31
'192.168.0.1'
32
end
33
34
it 'should not record an error' do
35
expect(workspace.errors[:boundary]).not_to include(error)
36
end
37
end
38
39
context 'with valid range' do
40
let(:boundary) do
41
'192.168.0.1/24'
42
end
43
44
it 'should not record an error' do
45
expect(workspace.errors[:boundary]).not_to include(error)
46
end
47
end
48
49
context 'with invalid IP or range' do
50
let(:boundary) do
51
'192.168'
52
end
53
54
it 'should record error that boundary must be a valid IP range' do
55
expect(workspace).not_to be_valid
56
expect(workspace.errors[:boundary]).to include(error)
57
end
58
end
59
end
60
61
context 'when the workspace is not network limited' do
62
before(:example) do
63
workspace.boundary = boundary
64
workspace.valid?
65
end
66
67
it 'should not care about the value of the boundary' do
68
expect(workspace.errors[:boundary]).not_to include(error)
69
end
70
end
71
end
72
end
73
74
context 'methods' do
75
context '#valid_ip_or_range?' do
76
let(:ip_or_range) do
77
nil
78
end
79
80
subject(:valid_ip_or_range?) { workspace.send(:valid_ip_or_range?, ip_or_range) }
81
82
context 'with exception from Rex::Socket::RangeWalker' do
83
before(:example) do
84
allow(Rex::Socket::RangeWalker).to receive(:new).with(ip_or_range).and_raise(StandardError)
85
end
86
87
it { expect { valid_ip_or_range? }.to raise_error(StandardError) }
88
end
89
90
context 'without exception from Rex::Socket::RangeWalker' do
91
context 'with valid IP' do
92
let(:ip_or_range) do
93
'192.168.0.1'
94
end
95
96
it { expect(valid_ip_or_range?).to be_truthy }
97
end
98
end
99
end
100
end
101
102
103
it_should_behave_like 'Mdm::Workspace::Boundary'
104
end
105
106