File: test_notification.py

package info (click to toggle)
rachiopy 1.1.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 328 kB
  • sloc: python: 1,053; makefile: 14; sh: 5
file content (148 lines) | stat: -rw-r--r-- 4,721 bytes parent folder | download | duplicates (2)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
"""Notification object test module"""

import unittest
from unittest.mock import patch
import uuid
import json

from rachiopy import Notification
from tests.constants import BASE_API_URL, AUTHTOKEN, RESPONSE200, RESPONSE204


class TestNotificationMethods(unittest.TestCase):
    """Class containing the Notification object test cases."""

    def setUp(self):
        self.notification = Notification(AUTHTOKEN)

    def test_init(self):
        """Test if the constructor works as expected."""
        self.assertEqual(self.notification.authtoken, AUTHTOKEN)

    @patch("requests.Session.request")
    def test_get_webhook_eventtype(self, mock):
        """Test if the get webhook eventtype method works as expected."""
        mock.return_value = RESPONSE200

        self.notification.get_webhook_event_type()

        args, kwargs = mock.call_args

        # Check that the mock function is called with the rights args.
        self.assertEqual(
            args[1], f"{BASE_API_URL}/notification/webhook_event_type"
        )
        self.assertEqual(args[0], "GET")
        self.assertEqual(kwargs["data"], None)

    @patch("requests.Session.request")
    def test_get_device_webhook(self, mock):
        """Test if the get device webhook method works as expected."""
        mock.return_value = RESPONSE200

        deviceid = str(uuid.uuid4())

        self.notification.get_device_webhook(deviceid)

        args, kwargs = mock.call_args

        # Check that the mock function is called with the rights args.
        self.assertEqual(
            args[1], f"{BASE_API_URL}/notification/{deviceid}/webhook"
        )
        self.assertEqual(args[0], "GET")
        self.assertEqual(kwargs["data"], None)

    @patch("requests.Session.request")
    def test_add(self, mock):
        """Test if the add method works as expected."""
        mock.return_value = RESPONSE200

        deviceid = str(uuid.uuid4())
        externalid = "Test ID"
        url = "https://www.mydomain.com/another_webhook_new_url"
        eventtypes = [{"id": "1"}, {"id": "2"}]

        self.notification.add(deviceid, externalid, url, eventtypes)

        args, kwargs = mock.call_args

        # Check that the mock function is called with the rights args.
        self.assertEqual(args[1], f"{BASE_API_URL}/notification/webhook")
        self.assertEqual(args[0], "POST")
        self.assertEqual(
            kwargs["data"],
            json.dumps(
                {
                    "device": {"id": deviceid},
                    "externalId": externalid,
                    "url": url,
                    "eventTypes": eventtypes,
                }
            ),
        )

    @patch("requests.Session.request")
    def test_update(self, mock):
        """Test if the update method works as expected."""
        mock.return_value = RESPONSE200

        hookid = str(uuid.uuid4())
        externalid = "Test ID"
        url = "https://www.mydomain.com/another_webhook_new_url"
        eventtypes = [{"id": "1"}, {"id": "2"}]

        self.notification.update(hookid, externalid, url, eventtypes)

        args, kwargs = mock.call_args

        # Check that the mock function is called with the rights args.
        self.assertEqual(args[1], f"{BASE_API_URL}/notification/webhook")
        self.assertEqual(args[0], "PUT")
        self.assertEqual(
            kwargs["data"],
            json.dumps(
                {
                    "id": hookid,
                    "externalId": externalid,
                    "url": url,
                    "eventTypes": eventtypes,
                }
            ),
        )

    @patch("requests.Session.request")
    def test_delete(self, mock):
        """Test if the delete method works as expected."""
        mock.return_value = RESPONSE204

        hookid = str(uuid.uuid4())

        self.notification.delete(hookid)

        args, kwargs = mock.call_args

        # Check that the mock function is called with the rights args.
        self.assertEqual(
            args[1], f"{BASE_API_URL}/notification/webhook/{hookid}"
        )
        self.assertEqual(args[0], "DELETE")
        self.assertEqual(kwargs["data"], None)

    @patch("requests.Session.request")
    def test_get(self, mock):
        """Test if the get method works as expected."""
        mock.return_value = RESPONSE200

        hookid = str(uuid.uuid4())

        self.notification.get(hookid)

        args, kwargs = mock.call_args

        # Check that the mock function is called with the rights args.
        self.assertEqual(
            args[1], f"{BASE_API_URL}/notification/webhook/{hookid}"
        )
        self.assertEqual(args[0], "GET")
        self.assertEqual(kwargs["data"], None)