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 = yaml.safe_load(open( |
14
|
|
|
'packs/cloudflare/tests/fixture/blank.yaml').read()) |
15
|
|
|
MOCK_CONFIG_NO_SCHEMA = yaml.safe_load(open( |
16
|
|
|
'packs/cloudflare/tests/fixture/no_schema.yaml').read()) |
17
|
|
|
MOCK_CONFIG_FULL = yaml.safe_load(open( |
18
|
|
|
'packs/cloudflare/tests/fixture/full.yaml').read()) |
19
|
|
|
|
20
|
|
|
MOCK_DATA_INVALID_JSON = "{'dd': doo}" |
21
|
|
|
MOCK_DATA_SUCCESS = open( |
22
|
|
|
'packs/cloudflare/tests/fixture/success.json').read() |
23
|
|
|
MOCK_DATA_FAIL = open( |
24
|
|
|
'packs/cloudflare/tests/fixture/fail.json').read() |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
class GetIPsTestCase(BaseActionTestCase): |
28
|
|
|
action_cls = GetIPs |
29
|
|
|
|
30
|
|
|
def test_run_no_config(self): |
31
|
|
|
self.assertRaises(ValueError, GetIPs, MOCK_CONFIG_BLANK) |
32
|
|
|
|
33
|
|
|
def test_run_is_instance(self): |
34
|
|
|
action = self.get_action_instance(MOCK_CONFIG_FULL) |
35
|
|
|
|
36
|
|
|
self.assertIsInstance(action, GetIPs) |
37
|
|
|
|
38
|
|
|
def test_run_status_no_schema(self): |
39
|
|
|
action = self.get_action_instance(MOCK_CONFIG_NO_SCHEMA) |
40
|
|
|
|
41
|
|
|
self.assertRaises(requests.exceptions.MissingSchema, |
42
|
|
|
action.run) |
43
|
|
|
|
44
|
|
|
def test_run_status_404(self): |
45
|
|
|
action = self.get_action_instance(MOCK_CONFIG_FULL) |
46
|
|
|
|
47
|
|
|
adapter = requests_mock.Adapter() |
48
|
|
|
action.session.mount('mock', adapter) |
49
|
|
|
|
50
|
|
|
adapter.register_uri('GET', |
51
|
|
|
"mock://api.cloudflare.com/client/v4/ips", |
52
|
|
|
status_code=404) |
53
|
|
|
|
54
|
|
|
self.assertRaises(ValueError, |
55
|
|
|
action.run) |
56
|
|
|
|
57
|
|
|
def test_run_invalid_json(self): |
58
|
|
|
action = self.get_action_instance(MOCK_CONFIG_FULL) |
59
|
|
|
|
60
|
|
|
adapter = requests_mock.Adapter() |
61
|
|
|
action.session.mount('mock', adapter) |
62
|
|
|
|
63
|
|
|
adapter.register_uri('GET', |
64
|
|
|
"mock://api.cloudflare.com/client/v4/ips", |
65
|
|
|
text=MOCK_DATA_INVALID_JSON) |
66
|
|
|
|
67
|
|
|
self.assertRaises(ValueError, |
68
|
|
|
action.run) |
69
|
|
|
|
70
|
|
|
def test_run_success_true(self): |
71
|
|
|
expected = {'ipv4_cidrs': [u'199.27.128.0/21'], |
72
|
|
|
'ipv6_cidrs': [u'2400:cb00::/32'], |
73
|
|
|
'messages': []} |
74
|
|
|
|
75
|
|
|
action = self.get_action_instance(MOCK_CONFIG_FULL) |
76
|
|
|
|
77
|
|
|
adapter = requests_mock.Adapter() |
78
|
|
|
action.session.mount('mock', adapter) |
79
|
|
|
|
80
|
|
|
adapter.register_uri('GET', |
81
|
|
|
"mock://api.cloudflare.com/client/v4/ips", |
82
|
|
|
text=MOCK_DATA_SUCCESS) |
83
|
|
|
|
84
|
|
|
result = action.run() |
85
|
|
|
self.assertEqual(result, expected) |
86
|
|
|
|
87
|
|
|
def test_run_success_false(self): |
88
|
|
|
action = self.get_action_instance(MOCK_CONFIG_FULL) |
89
|
|
|
|
90
|
|
|
adapter = requests_mock.Adapter() |
91
|
|
|
action.session.mount('mock', adapter) |
92
|
|
|
|
93
|
|
|
adapter.register_uri('GET', |
94
|
|
|
"mock://api.cloudflare.com/client/v4/ips", |
95
|
|
|
text=MOCK_DATA_FAIL) |
96
|
|
|
self.assertRaises(Exception, |
97
|
|
|
action.run) |
98
|
|
|
|