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

NagiosHandlerTestCase.test_get_auth_token()   A

Complexity

Conditions 1

Size

Total Lines 9

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 9
rs 9.6666
1
import json
2
3
import mock
4
import requests
5
import responses
6
import unittest2
7
8
import st2service_handler as nagios_handler
9
10
11
__all__ = [
12
    'NagiosHandlerTestCase'
13
]
14
15
16
class FakeResponse(object):
17
18
    def __init__(self, test, status_code, reason):
19
        self.text = test
20
        self.status_code = status_code
21
        self.reason = reason
22
23
    def json(self):
24
        return json.loads(self.text)
25
26
    def raise_for_status(self):
27
        raise Exception(self.reason)
28
29
30
class NagiosHandlerTestCase(unittest2.TestCase):
31
32
    def test_st2_headers_token_auth(self):
33
        nagios_handler.IS_API_KEY_AUTH = False
34
        self.assertEqual(nagios_handler.IS_API_KEY_AUTH, False,
35
                         'API auth should be off for this test.')
36
        nagios_handler.ST2_AUTH_TOKEN = 'dummy-token'
37
        nagios_handler.ST2_API_KEY = 'dummy-api-key'
38
        headers = nagios_handler._get_st2_request_headers()
39
        self.assertTrue('X-Auth-Token' in headers)
40
        self.assertTrue('St2-Api-Key' not in headers)
41
        self.assertEqual(headers['X-Auth-Token'], 'dummy-token')
42
43
    def test_st2_headers_apikey_auth(self):
44
        nagios_handler.IS_API_KEY_AUTH = True
45
        self.assertEqual(nagios_handler.IS_API_KEY_AUTH, True,
46
                         'API auth should be on for this test.')
47
        nagios_handler.ST2_AUTH_TOKEN = 'dummy-token'
48
        nagios_handler.ST2_API_KEY = 'dummy-api-key'
49
        headers = nagios_handler._get_st2_request_headers()
50
        self.assertTrue('X-Auth-Token' not in headers)
51
        self.assertTrue('St2-Api-Key' in headers)
52
        self.assertEqual(headers['St2-Api-Key'], 'dummy-api-key')
53
54
    def test_get_st2_auth_url(self):
55
        nagios_handler.ST2_AUTH_BASE_URL = 'https://localhost/auth/v1/'
56
        self.assertEqual(nagios_handler._get_auth_url(),
57
                         'https://localhost/auth/v1/tokens')
58
59
    def test_get_st2_triggers_base_url(self):
60
        nagios_handler.ST2_API_BASE_URL = 'https://localhost/api/v1/'
61
        self.assertEqual(nagios_handler._get_st2_triggers_base_url(),
62
                         'https://localhost/api/v1/triggertypes')
63
64
    def test_get_st2_triggers_url(self):
65
        nagios_handler.ST2_API_BASE_URL = 'https://localhost/api/v1/'
66
        self.assertEqual(nagios_handler._get_st2_triggers_url(),
67
                         'https://localhost/api/v1/triggertypes/nagios.service_state_change')
68
69
    def test_get_st2_webhooks_url(self):
70
        nagios_handler.ST2_API_BASE_URL = 'https://localhost/api/v1/'
71
        self.assertEqual(nagios_handler._get_st2_webhooks_url(),
72
                         'https://localhost/api/v1/webhooks/st2')
73
74
    def test_from_arg_to_payload(self):
75
        nagios_args = ['foo', 'bar']
76
        with self.assertRaises(SystemExit) as cm:
77
            nagios_handler._from_arg_to_payload(nagios_args)
78
        self.assertTrue(cm.exception.code > 0)
79
80
    @responses.activate
81
    def test_get_auth_token(self):
82
        nagios_handler.ST2_AUTH_BASE_URL = 'https://localhost/auth/v1/'
83
        responses.add(
84
            responses.POST, 'https://localhost/auth/v1/tokens',
85
            json={'token': 'your_auth_token'}, status=202
86
        )
87
        token = nagios_handler._get_auth_token()
88
        self.assertEqual(token, 'your_auth_token')
89
90
    @mock.patch('st2service_handler._create_trigger_type')
91
    @responses.activate
92
    def test_get_trigger_type_trigger_exists(self, mock_method):
93
        nagios_handler.ST2_API_BASE_URL = 'https://localhost/api/v1/'
94
        responses.add(
95
            responses.GET, 'https://localhost/api/v1/triggertypes/nagios.service_state_change',
96
            json={'type': 'nagios.service_state_change'}, status=200
97
        )
98
        nagios_handler._register_trigger_with_st2()
99
        self.assertFalse(mock_method.called)
100
101
    @mock.patch('st2service_handler._create_trigger_type')
102
    @responses.activate
103
    def test_trigger_creation_trigger_not_exists(self, mock_method):
104
        nagios_handler.ST2_API_BASE_URL = 'https://localhost/api/v1/'
105
        responses.add(
106
            responses.GET, 'https://localhost/api/v1/triggertypes/nagios.service_state_change',
107
            json={}, status=404
108
        )
109
        nagios_handler._register_trigger_with_st2()
110
        self.assertTrue(mock_method.called)
111
112
    @mock.patch.object(requests, 'post', mock.MagicMock(
113
        return_value=FakeResponse(json.dumps({}), status_code=200, reason='blah')))
114
    def test_create_trigger_type(self):
115
        nagios_handler.ST2_API_BASE_URL = 'https://localhost/api/v1/'
116
        nagios_handler._create_trigger_type()
117
        requests.post.assert_called_once_with('https://localhost/api/v1/triggertypes',
118
            data='{"description": "Trigger type for nagios event handler.", ' +
119
                 '"name": "service_state_change", "pack": "nagios"}',
120
            headers={'Content-Type': 'application/json; charset=utf-8'}, verify=False)
121
122
    @mock.patch.object(requests, 'post', mock.MagicMock(
123
        return_value=FakeResponse(json.dumps({}), status_code=200, reason='blah')))
124
    def test_ssl_verify_on(self):
125
        nagios_handler.ST2_API_BASE_URL = 'https://localhost/api/v1/'
126
        nagios_handler.ST2_SSL_VERIFY = True
127
        nagios_handler._create_trigger_type()
128
        requests.post.assert_called_with('https://localhost/api/v1/triggertypes',
129
            data='{"description": "Trigger type for nagios event handler.", ' +
130
                 '"name": "service_state_change", "pack": "nagios"}',
131
            headers={'Content-Type': 'application/json; charset=utf-8'}, verify=True
132
        )
133
134
    def test_post_event_to_st2_bad_api_url(self):
135
        nagios_handler.ST2_API_BASE_URL = 'https://localhost/api'
136
        trigger_payload = {'foo': 'bar'}
137
        with self.assertRaises(SystemExit) as cm:
138
            nagios_handler._post_event_to_st2(json.dumps(trigger_payload))
139
        self.assertTrue(cm.exception.code > 0)
140