-
Notifications
You must be signed in to change notification settings - Fork 11
/
tests.py
63 lines (51 loc) · 1.76 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import sys
import unittest
from functools import partial
try:
from urllib2 import HTTPError
except ImportError:
from urllib.error import HTTPError
try:
from mock import patch, Mock, call
except ImportError:
from unittest.mock import patch, Mock, call
from cfnresponse import send
if sys.version_info >= (3, 0):
open_director_patch = partial(patch, 'urllib.request.OpenerDirector.open')
else:
open_director_patch = partial(patch, 'urllib2.OpenerDirector.open')
class TestCfnResponse(unittest.TestCase):
def _event(self):
return {
'StackId': 'stack_id',
'RequestId': 'request_id',
'LogicalResourceId': 'logical_resource_id',
'ResponseURL': 'http://localhost/response'
}
def _context(self):
return Mock(log_stream_name='log_stream_name')
@open_director_patch()
def test_cfn_send_success(self, open_mock):
open_mock.return_value = Mock()
open_mock.return_value.msg = 'OK'
open_mock.return_value.getcode.return_value = 200
response = send(
event=self._event(),
context=Mock(log_stream_name='log_stream_name'),
response_status='response_status',
response_data='response_data',
)
self.assertTrue(response)
@open_director_patch()
def test_cfn_send_error(self, open_mock):
class MockHTTPError(HTTPError):
def __init__(self, code=503):
self.code = code
open_mock.side_effect = MockHTTPError
response = send(
event=self._event(),
context=Mock(log_stream_name='log_stream_name'),
response_status='response_status',
response_data='response_data',
)
self.assertFalse(response)