Browse Source

Move shared example from pro into framework

MS-1361
Fernando Arias 3 years ago
parent
commit
8f3f2f74b4
No account linked to committer's email address

+ 10
- 0
app/concerns/mdm/workspace/boundary_range.rb View File

@@ -58,6 +58,13 @@ module Mdm::Workspace::BoundaryRange
58 58
       end
59 59
     end
60 60
 
61
+    # Returns an array of addresses ranges
62
+    #
63
+    # @return [Array<String>]
64
+    def addresses
65
+      (boundary || "").split("\n")
66
+    end
67
+
61 68
     private
62 69
 
63 70
     # Returns whether `string` is a valid IP address or IP address range.
@@ -68,5 +75,8 @@ module Mdm::Workspace::BoundaryRange
68 75
       range = Rex::Socket::RangeWalker.new(string)
69 76
       range && range.ranges && range.ranges.any?
70 77
     end
78
+
71 79
   end
80
+
81
+
72 82
 end

+ 33
- 0
spec/models/mdm/workspace_spec.rb View File

@@ -70,4 +70,37 @@ RSpec.describe Mdm::Workspace, type: :model do
70 70
       end
71 71
     end
72 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 do
81
+        -> {workspace.send(:valid_ip_or_range?, ip_or_range)}
82
+      end
83
+
84
+      context 'with exception from Rex::Socket::RangeWalker' do
85
+        before(:example) do
86
+          allow(Rex::Socket::RangeWalker).to receive(:new).with(ip_or_range).and_raise(StandardError)
87
+        end
88
+
89
+        it { is_expected.to raise_error(StandardError) }
90
+      end
91
+
92
+      context 'without exception from Rex::Socket::RangeWalker' do
93
+        context 'with valid IP' do
94
+          let(:ip_or_range) do
95
+            '192.168.0.1'
96
+          end
97
+
98
+          it { is_expected.to be_truthy }
99
+        end
100
+      end
101
+    end
102
+  end
103
+
104
+
105
+  it_should_behave_like 'Mdm::Workspace::Boundary'
73 106
 end

+ 134
- 0
spec/support/shared/examples/mdm/workspace/boundary.rb View File

@@ -0,0 +1,134 @@
1
+RSpec.shared_examples_for 'Mdm::Workspace::Boundary' do
2
+  context 'methods' do
3
+    let(:boundary) do
4
+      nil
5
+    end
6
+
7
+    before(:example) do
8
+      workspace.boundary = boundary
9
+    end
10
+
11
+    context '#addresses' do
12
+      subject(:addresses) do
13
+        workspace.addresses
14
+      end
15
+
16
+      context 'with boundary' do
17
+        let(:boundary) do
18
+          expected_addresses.join("\n")
19
+        end
20
+
21
+        let(:expected_addresses) do
22
+          [
23
+            '10,10,10,10',
24
+            '192.168.0.1'
25
+          ]
26
+        end
27
+
28
+        it 'should return addresses split on newline' do
29
+          expect(addresses).to eq(expected_addresses)
30
+        end
31
+      end
32
+
33
+      context 'without boundary' do
34
+        let(:boundary) do
35
+          nil
36
+        end
37
+
38
+        it 'should return an empty Array' do
39
+          expect(addresses).to eq([])
40
+        end
41
+      end
42
+    end
43
+
44
+    context '#boundary_must_be_ip_range' do
45
+      let(:error) do
46
+        'must be a valid IP range'
47
+      end
48
+
49
+      context 'with boundary' do
50
+        let(:boundary) do
51
+          '192.168.0.1'
52
+        end
53
+
54
+        it 'should split boundary' do
55
+          expect(Shellwords).to receive(:split).with(boundary).and_call_original
56
+
57
+          workspace.valid?
58
+        end
59
+
60
+        context 'with error from Shellwords.split' do
61
+          before(:example) do
62
+            allow(Shellwords).to receive(:split).with(boundary).and_raise(ArgumentError)
63
+          end
64
+
65
+          it 'should not raise error' do
66
+            expect {
67
+              workspace.valid?
68
+            }.to_not raise_error
69
+          end
70
+
71
+          it 'should not record an error' do
72
+            workspace.valid?
73
+
74
+            expect(workspace.errors[:boundary]).not_to include(error)
75
+          end
76
+        end
77
+
78
+        context 'with empty' do
79
+          let(:boundary) do
80
+            ''
81
+          end
82
+
83
+          it 'should not record an error' do
84
+            workspace.valid?
85
+
86
+            expect(workspace.errors[:boundary]).not_to include(error)
87
+          end
88
+        end
89
+
90
+        context 'without empty' do
91
+          let(:ranges) do
92
+            [
93
+              '10.10.10.10',
94
+              '192.168.0.1'
95
+            ]
96
+          end
97
+
98
+          let(:boundary) do
99
+            ranges.join(' ')
100
+          end
101
+
102
+          it 'should validate each range' do
103
+            ranges.each do |range|
104
+              expect(workspace).to receive(:valid_ip_or_range?).with(range).and_call_original
105
+            end
106
+
107
+            workspace.valid?
108
+          end
109
+
110
+          context 'with invalid range' do
111
+            let(:ranges) do
112
+              [
113
+                '192.168'
114
+              ]
115
+            end
116
+
117
+            it 'should record error', :skip => 'https://www.pivotaltracker.com/story/show/43171927' do
118
+              expect(workspace).not_to be_valid
119
+              expect(workspace.errors[:boundary]).to include(error)
120
+            end
121
+          end
122
+        end
123
+      end
124
+
125
+      context 'without boundary' do
126
+        it 'should not record error' do
127
+          workspace.valid?
128
+
129
+          expect(workspace.errors[:boundary]).not_to include(error)
130
+        end
131
+      end
132
+    end
133
+  end
134
+end

Loading…
Cancel
Save