1
|
|
|
import yaml |
2
|
|
|
import requests |
3
|
|
|
import requests_mock |
4
|
|
|
|
5
|
|
|
from st2tests.base import BaseActionTestCase |
6
|
|
|
|
7
|
|
|
from get_ips import GetIPs |
8
|
|
|
|
9
|
|
|
__all__ = [ |
10
|
|
|
'GetIPsTestCase' |
11
|
|
|
] |
12
|
|
|
|
13
|
|
|
MOCK_CONFIG_BLANK = "" |
14
|
|
|
|
15
|
|
|
MOCK_CONFIG_NO_SCHEMA = """ |
16
|
|
|
api_host: "api.cloudflare.com" |
17
|
|
|
""" |
18
|
|
|
|
19
|
|
|
MOCK_CONFIG_FULL = """ |
20
|
|
|
api_host: "mock://api.cloudflare.com" |
21
|
|
|
""" |
22
|
|
|
|
23
|
|
|
MOCK_DATA_INVALID_JSON = "{'dd': doo}" |
24
|
|
|
|
25
|
|
|
MOCK_DATA_SUCCESS = """ |
26
|
|
|
{ |
27
|
|
|
"success": true, |
28
|
|
|
"errors": [], |
29
|
|
|
"messages": [], |
30
|
|
|
"result": { |
31
|
|
|
"ipv4_cidrs": [ |
32
|
|
|
"199.27.128.0/21" |
33
|
|
|
], |
34
|
|
|
"ipv6_cidrs": [ |
35
|
|
|
"2400:cb00::/32" |
36
|
|
|
] |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
""" |
40
|
|
|
|
41
|
|
|
MOCK_DATA_FAIL = """ |
42
|
|
|
{ |
43
|
|
|
"success": false, |
44
|
|
|
"errors": ["An Error happened"], |
45
|
|
|
"messages": [], |
46
|
|
|
"result": {} |
47
|
|
|
} |
48
|
|
|
""" |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
class GetIPsTestCase(BaseActionTestCase): |
52
|
|
|
action_cls = GetIPs |
53
|
|
|
|
54
|
|
|
def test_run_no_config(self): |
55
|
|
|
config = yaml.safe_load(MOCK_CONFIG_BLANK) |
56
|
|
|
|
57
|
|
|
self.assertRaises(ValueError, GetIPs, config) |
58
|
|
|
|
59
|
|
|
def test_run_status_no_schema(self): |
60
|
|
|
config = yaml.safe_load(MOCK_CONFIG_NO_SCHEMA) |
61
|
|
|
|
62
|
|
|
action = self.get_action_instance(config) |
63
|
|
|
|
64
|
|
|
self.assertRaises(requests.exceptions.MissingSchema, |
65
|
|
|
action.run) |
66
|
|
|
|
67
|
|
|
def test_run_status_404(self): |
68
|
|
|
config = yaml.safe_load(MOCK_CONFIG_FULL) |
69
|
|
|
|
70
|
|
|
action = self.get_action_instance(config) |
71
|
|
|
|
72
|
|
|
adapter = requests_mock.Adapter() |
73
|
|
|
action.session.mount('mock', adapter) |
74
|
|
|
|
75
|
|
|
adapter.register_uri('GET', |
76
|
|
|
"mock://api.cloudflare.com/client/v4/ips", |
77
|
|
|
status_code=404) |
78
|
|
|
|
79
|
|
|
self.assertRaises(ValueError, |
80
|
|
|
action.run) |
81
|
|
|
|
82
|
|
|
def test_run_invalid_json(self): |
83
|
|
|
config = yaml.safe_load(MOCK_CONFIG_FULL) |
84
|
|
|
|
85
|
|
|
action = self.get_action_instance(config) |
86
|
|
|
|
87
|
|
|
adapter = requests_mock.Adapter() |
88
|
|
|
action.session.mount('mock', adapter) |
89
|
|
|
|
90
|
|
|
adapter.register_uri('GET', |
91
|
|
|
"mock://api.cloudflare.com/client/v4/ips", |
92
|
|
|
text=MOCK_DATA_INVALID_JSON) |
93
|
|
|
|
94
|
|
|
self.assertRaises(ValueError, |
95
|
|
|
action.run) |
96
|
|
|
|
97
|
|
|
def test_run_success_true(self): |
98
|
|
|
expected = {'ipv4_cidrs': [u'199.27.128.0/21'], |
99
|
|
|
'ipv6_cidrs': [u'2400:cb00::/32'], |
100
|
|
|
'messages': []} |
101
|
|
|
|
102
|
|
|
config = yaml.safe_load(MOCK_CONFIG_FULL) |
103
|
|
|
|
104
|
|
|
action = self.get_action_instance(config) |
105
|
|
|
|
106
|
|
|
adapter = requests_mock.Adapter() |
107
|
|
|
action.session.mount('mock', adapter) |
108
|
|
|
|
109
|
|
|
adapter.register_uri('GET', |
110
|
|
|
"mock://api.cloudflare.com/client/v4/ips", |
111
|
|
|
text=MOCK_DATA_SUCCESS) |
112
|
|
|
|
113
|
|
|
result = action.run() |
114
|
|
|
self.assertEqual(result, expected) |
115
|
|
|
|
116
|
|
|
def test_run_success_flase(self): |
117
|
|
|
config = yaml.safe_load(MOCK_CONFIG_FULL) |
118
|
|
|
|
119
|
|
|
action = self.get_action_instance(config) |
120
|
|
|
|
121
|
|
|
adapter = requests_mock.Adapter() |
122
|
|
|
action.session.mount('mock', adapter) |
123
|
|
|
|
124
|
|
|
adapter.register_uri('GET', |
125
|
|
|
"mock://api.cloudflare.com/client/v4/ips", |
126
|
|
|
text=MOCK_DATA_FAIL) |
127
|
|
|
self.assertRaises(Exception, |
128
|
|
|
action.run) |
129
|
|
|
|