| 1 |
"""Notification object test module""" |
| 2 |
|
| 3 |
import unittest |
| 4 |
from unittest.mock import patch |
| 5 |
import uuid |
| 6 |
import json |
| 7 |
|
| 8 |
from rachiopy import Notification |
| 9 |
from tests.constants import BASE_API_URL, AUTHTOKEN, RESPONSE200, RESPONSE204 |
| 10 |
|
| 11 |
|
| 12 |
class TestNotificationMethods(unittest.TestCase): |
| 13 |
"""Class containing the Notification object test cases.""" |
| 14 |
|
| 15 |
def setUp(self): |
| 16 |
self.notification = Notification(AUTHTOKEN) |
| 17 |
|
| 18 |
def test_init(self): |
| 19 |
"""Test if the constructor works as expected.""" |
| 20 |
self.assertEqual(self.notification.authtoken, AUTHTOKEN) |
| 21 |
|
| 22 |
@patch("requests.Session.request") |
| 23 |
def test_get_webhook_eventtype(self, mock): |
| 24 |
"""Test if the get webhook eventtype method works as expected.""" |
| 25 |
mock.return_value = RESPONSE200 |
| 26 |
|
| 27 |
self.notification.get_webhook_event_type() |
| 28 |
|
| 29 |
args, kwargs = mock.call_args |
| 30 |
|
| 31 |
|
| 32 |
self.assertEqual( |
| 33 |
args[1], f"{BASE_API_URL}/notification/webhook_event_type" |
| 34 |
) |
| 35 |
self.assertEqual(args[0], "GET") |
| 36 |
self.assertEqual(kwargs["data"], None) |
| 37 |
|
| 38 |
@patch("requests.Session.request") |
| 39 |
def test_get_device_webhook(self, mock): |
| 40 |
"""Test if the get device webhook method works as expected.""" |
| 41 |
mock.return_value = RESPONSE200 |
| 42 |
|
| 43 |
deviceid = str(uuid.uuid4()) |
| 44 |
|
| 45 |
self.notification.get_device_webhook(deviceid) |
| 46 |
|
| 47 |
args, kwargs = mock.call_args |
| 48 |
|
| 49 |
|
| 50 |
self.assertEqual( |
| 51 |
args[1], f"{BASE_API_URL}/notification/{deviceid}/webhook" |
| 52 |
) |
| 53 |
self.assertEqual(args[0], "GET") |
| 54 |
self.assertEqual(kwargs["data"], None) |
| 55 |
|
| 56 |
@patch("requests.Session.request") |
| 57 |
def test_add(self, mock): |
| 58 |
"""Test if the add method works as expected.""" |
| 59 |
mock.return_value = RESPONSE200 |
| 60 |
|
| 61 |
deviceid = str(uuid.uuid4()) |
| 62 |
externalid = "Test ID" |
| 63 |
url = "https://www.mydomain.com/another_webhook_new_url" |
| 64 |
eventtypes = [{"id": "1"}, {"id": "2"}] |
| 65 |
|
| 66 |
self.notification.add(deviceid, externalid, url, eventtypes) |
| 67 |
|
| 68 |
args, kwargs = mock.call_args |
| 69 |
|
| 70 |
|
| 71 |
self.assertEqual(args[1], f"{BASE_API_URL}/notification/webhook") |
| 72 |
self.assertEqual(args[0], "POST") |
| 73 |
self.assertEqual( |
| 74 |
kwargs["data"], |
| 75 |
json.dumps( |
| 76 |
{ |
| 77 |
"device": {"id": deviceid}, |
| 78 |
"externalId": externalid, |
| 79 |
"url": url, |
| 80 |
"eventTypes": eventtypes, |
| 81 |
} |
| 82 |
), |
| 83 |
) |
| 84 |
|
| 85 |
@patch("requests.Session.request") |
| 86 |
def test_update(self, mock): |
| 87 |
"""Test if the update method works as expected.""" |
| 88 |
mock.return_value = RESPONSE200 |
| 89 |
|
| 90 |
hookid = str(uuid.uuid4()) |
| 91 |
externalid = "Test ID" |
| 92 |
url = "https://www.mydomain.com/another_webhook_new_url" |
| 93 |
eventtypes = [{"id": "1"}, {"id": "2"}] |
| 94 |
|
| 95 |
self.notification.update(hookid, externalid, url, eventtypes) |
| 96 |
|
| 97 |
args, kwargs = mock.call_args |
| 98 |
|
| 99 |
|
| 100 |
self.assertEqual(args[1], f"{BASE_API_URL}/notification/webhook") |
| 101 |
self.assertEqual(args[0], "PUT") |
| 102 |
self.assertEqual( |
| 103 |
kwargs["data"], |
| 104 |
json.dumps( |
| 105 |
{ |
| 106 |
"id": hookid, |
| 107 |
"externalId": externalid, |
| 108 |
"url": url, |
| 109 |
"eventTypes": eventtypes, |
| 110 |
} |
| 111 |
), |
| 112 |
) |
| 113 |
|
| 114 |
@patch("requests.Session.request") |
| 115 |
def test_delete(self, mock): |
| 116 |
"""Test if the delete method works as expected.""" |
| 117 |
mock.return_value = RESPONSE204 |
| 118 |
|
| 119 |
hookid = str(uuid.uuid4()) |
| 120 |
|
| 121 |
self.notification.delete(hookid) |
| 122 |
|
| 123 |
args, kwargs = mock.call_args |
| 124 |
|
| 125 |
|
| 126 |
self.assertEqual( |
| 127 |
args[1], f"{BASE_API_URL}/notification/webhook/{hookid}" |
| 128 |
) |
| 129 |
self.assertEqual(args[0], "DELETE") |
| 130 |
self.assertEqual(kwargs["data"], None) |
| 131 |
|
| 132 |
@patch("requests.Session.request") |
| 133 |
def test_get(self, mock): |
| 134 |
"""Test if the get method works as expected.""" |
| 135 |
mock.return_value = RESPONSE200 |
| 136 |
|
| 137 |
hookid = str(uuid.uuid4()) |
| 138 |
|
| 139 |
self.notification.get(hookid) |
| 140 |
|
| 141 |
args, kwargs = mock.call_args |
| 142 |
|
| 143 |
|
| 144 |
self.assertEqual( |
| 145 |
args[1], f"{BASE_API_URL}/notification/webhook/{hookid}" |
| 146 |
) |
| 147 |
self.assertEqual(args[0], "GET") |
| 148 |
self.assertEqual(kwargs["data"], None) |