/
test_user_notification.py
60 lines (48 loc) · 1.7 KB
/
test_user_notification.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
"""Tests for the 'user_notification' module."""
from unittest import mock
from user_notification import UserNotification
import os
def mock_response(flag):
"""Mock the call to the notification service."""
class MockResponse:
"""Mock response object."""
def __init__(self, json_data, status_code, raise_status):
"""Create a mock json response."""
self.json_data = json_data
self.status_code = status_code
self.raise_status = raise_status
def json(self):
"""Get the mock json response."""
return self.json_data
def raise_for_status(self):
"""Get the mock json response."""
return self.raise_status
# return the URL to check whether we are calling the correct service.
resp = {
"access_token": "f98d1366-738e-4c14-a3ff-594f359e131c",
"status": {
"message": "",
"code": 200,
"attributes": {}
},
"result": {
"data": [
"a"
],
"meta": {}
}
}
if flag == "false":
return MockResponse(resp, 400, "status_raised")
elif flag == "true":
return MockResponse(resp, 202, "")
@mock.patch("requests.post")
@mock.patch.dict(os.environ, {'NOTIFICATION_SERVICE_HOST': 'http://somehost'})
def test_send_notification(mocker):
"""Test the function init_auth_sa_token."""
mocker.return_value = mock_response("true")
notifications = [{"a": "b"}]
out = UserNotification.send_notification(notifications, "f98d1366-738e-4c14-a3ff-594f359e131c")
assert "success" == out['status']
if __name__ == '__main__':
test_send_notification()