Completed
Pull Request — master (#523)
by
unknown
02:53
created

GetIPsTestCase.test_run_status_404()   A

Complexity

Conditions 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 12
rs 9.4285
1
import yaml
2
import requests_mock
3
from mock import patch
4
5
from st2tests.base import BaseActionTestCase
6
7
from get_ips import GetIPsAction
8
9
__all__ = [
10
    'GetIPsActionTestCase'
11
]
12
13
MOCK_CONFIG_BLANK = yaml.safe_load(open(
14
    'packs/cloudflare/tests/fixture/blank.yaml').read())
15
MOCK_CONFIG_FULL = yaml.safe_load(open(
16
    'packs/cloudflare/tests/fixture/full.yaml').read())
17
18
MOCK_DATA_INVALID_JSON = "{'dd': doo}"
19
MOCK_DATA_SUCCESS = open(
20
    'packs/cloudflare/tests/fixture/success.json').read()
21
MOCK_DATA_FAIL = open(
22
    'packs/cloudflare/tests/fixture/fail.json').read()
23
24
25
class GetIPsActionTestCase(BaseActionTestCase):
26
    action_cls = GetIPsAction
27
28
    def test_run_no_config(self):
29
        self.assertRaises(ValueError, self.action_cls, MOCK_CONFIG_BLANK)
30
31
    def test_run_is_instance(self):
32
        action = self.get_action_instance(MOCK_CONFIG_FULL)
33
34
        self.assertIsInstance(action, self.action_cls)
35
        self.assertEqual(action.api_key, "API-Key")
36
        self.assertEqual(action.API_HOST, "https://api.cloudflare.com")
37
38
    @patch('get_ips.GetIPsAction.API_HOST', "mock://api.cloudflare.com")
39
    def test_run_status_404(self):
40
        action = self.get_action_instance(MOCK_CONFIG_FULL)
41
42
        adapter = requests_mock.Adapter()
43
        action.session.mount('mock', adapter)
44
45
        adapter.register_uri('GET',
46
                             "mock://api.cloudflare.com/client/v4/ips",
47
                             status_code=404)
48
49
        self.assertRaises(ValueError,
50
                          action.run)
51
52
    @patch('get_ips.GetIPsAction.API_HOST', "mock://api.cloudflare.com")
53
    def test_run_invalid_json(self):
54
        action = self.get_action_instance(MOCK_CONFIG_FULL)
55
56
        adapter = requests_mock.Adapter()
57
        action.session.mount('mock', adapter)
58
59
        adapter.register_uri('GET',
60
                             "mock://api.cloudflare.com/client/v4/ips",
61
                             text=MOCK_DATA_INVALID_JSON)
62
63
        self.assertRaises(ValueError,
64
                          action.run)
65
66
    @patch('get_ips.GetIPsAction.API_HOST', "mock://api.cloudflare.com")
67
    def test_run_success_true(self):
68
        expected = {'ipv4_cidrs': [u'199.27.128.0/21'],
69
                    'ipv6_cidrs': [u'2400:cb00::/32'],
70
                    'messages': []}
71
72
        action = self.get_action_instance(MOCK_CONFIG_FULL)
73
74
        adapter = requests_mock.Adapter()
75
        action.session.mount('mock', adapter)
76
77
        adapter.register_uri('GET',
78
                             "mock://api.cloudflare.com/client/v4/ips",
79
                             text=MOCK_DATA_SUCCESS)
80
81
        result = action.run()
82
        self.assertEqual(result, expected)
83
84
    @patch('get_ips.GetIPsAction.API_HOST', "mock://api.cloudflare.com")
85
    def test_run_success_false(self):
86
        action = self.get_action_instance(MOCK_CONFIG_FULL)
87
88
        adapter = requests_mock.Adapter()
89
        action.session.mount('mock', adapter)
90
91
        adapter.register_uri('GET',
92
                             "mock://api.cloudflare.com/client/v4/ips",
93
                             text=MOCK_DATA_FAIL)
94
        self.assertRaises(Exception,
95
                          action.run)
96