Debian Codegraph @

tests/test_ekey.py

raw symbols SCIP
1 import aiohttp
2 import pytest
3 from aioresponses import aioresponses
4
5 from ekey_bionyxpy import AbstractAuth
6 from ekey_bionyxpy import BionyxAPI
7 from ekey_bionyxpy import System
8 from ekey_bionyxpy import SystemResponse
9 from ekey_bionyxpy import Webhook
10 from ekey_bionyxpy import WebhookData
11 from ekey_bionyxpy import WebhookRename
12 from ekey_bionyxpy import WebhookResponse
13
14
15 system_template: SystemResponse = {
16 "systemName": "TESTSYSTEM",
17 "systemId": "946da01f-9abd-4d9d-80c7-02af85c822a8",
18 "ownSystem": True,
19 "functionWebhookQuotas": {"free": 5, "used": 0},
20 }
21 base_url = "http://test.example.com"
22
23 webhook_template: WebhookResponse = {
24 "functionWebhookId": "946da01f-9abd-4d9d-80c7-02af85c822a8",
25 "integrationName": "Third Party",
26 "locationName": "A simple string containing 0 to 128 word, space and punctuation characters.",
27 "functionName": "A simple string containing 0 to 50 word, space and punctuation characters.",
28 "expiresAt": "2022-05-16T04:11:28.0000000+00:00",
29 "modificationState": None,
30 }
31
32 webhook_data_template: WebhookData = {
33 "integrationName": "Third Party",
34 "locationName": "A simple string containing 0 to 128 word, space and punctuation characters.",
35 "functionName": "A simple string containing 0 to 50 word, space and punctuation characters.",
36 "definition": {
37 "method": "Post",
38 "url": "https://www.rfc-editor.org/rfc/rfc3986.html",
39 "body": {"contentType": "application/json", "content": "string"},
40 "securityLevel": "AllowHttp",
41 "authentication": {
42 "apiAuthenticationType": "None",
43 },
44 },
45 }
46
47 webhook_patch_data: WebhookRename = {"functionName": "A simple string", "locationName": "0 to 50 word"}
48
49 system_id = system_template["systemId"]
50 webhook_id = webhook_template["functionWebhookId"]
51
52
53 class Auth(AbstractAuth):
54 def __init__(self, websession: aiohttp.ClientSession, host: str, token: str):
55 super().__init__(websession, host)
56 self.token = token
57
58 async def async_get_access_token(self) -> str:
59 return self.token
60
61
62 @pytest.mark.asyncio
63 async def test_request_with_headers():
64 session = aiohttp.ClientSession()
65 auth = Auth(session, base_url, "not needed")
66 with aioresponses() as m:
67 m.get(f"{base_url}")
68 resp = await auth.request("GET", "", headers={"test": "test"})
69 resp.raise_for_status()
70 m.assert_called_once_with(
71 f"{base_url}",
72 method="get",
73 headers={"authorization": "Bearer not needed", "test": "test"},
74 )
75 await session.close()
76
77
78 @pytest.mark.asyncio
79 async def test_system(): # noqa: RUF029
80 session = aiohttp.ClientSession()
81 auth = Auth(session, base_url, "not needed")
82 system = System(system_template, auth)
83 assert system.system_id == system_template["systemId"]
84 assert system.system_name == system_template["systemName"]
85 assert system.own_system == system_template["ownSystem"]
86 assert system.function_webhook_quotas == system_template["functionWebhookQuotas"]
87
88
89 @pytest.mark.asyncio
90 async def test_get_systems():
91 session = aiohttp.ClientSession()
92 auth = Auth(session, base_url, "not needed")
93 api = BionyxAPI(auth)
94 with aioresponses() as m:
95 m.get(f"{base_url}/systems", payload=[system_template])
96 systems = await api.get_systems()
97 comp_system = System(system_template, auth)
98 assert comp_system.__dict__ == systems[0].__dict__
99 m.assert_called_once()
100 await session.close()
101
102
103 @pytest.mark.asyncio
104 async def test_unauthorized():
105 session = aiohttp.ClientSession()
106 auth = Auth(session, base_url, "not needed")
107 api = BionyxAPI(auth)
108 with aioresponses() as m:
109 m.get(f"{base_url}/systems", status=401)
110 with pytest.raises(aiohttp.ClientResponseError, match="401"):
111 await api.get_systems()
112 m.assert_called_once()
113 await session.close()
114
115
116 @pytest.mark.asyncio
117 async def test_webhook(): # noqa: RUF029
118 session = aiohttp.ClientSession()
119 auth = Auth(session, base_url, "not needed")
120 webhook = Webhook(webhook_template, system_id, auth)
121 assert webhook.webhook_id == webhook_template["functionWebhookId"]
122 assert webhook.webhook_function_name == webhook_template["functionName"]
123 assert webhook.webhook_location_name == webhook_template["locationName"]
124 assert webhook.expires_at == webhook_template["expiresAt"]
125 assert webhook.modification_state == webhook_template["modificationState"]
126
127
128 @pytest.mark.asyncio
129 async def test_get_webhooks():
130 session = aiohttp.ClientSession()
131 auth = Auth(session, base_url, "not needed")
132 with aioresponses() as m:
133 m.get(f"{base_url}/systems/{system_id}/function-webhooks", payload=[webhook_template])
134 webhooks = await (System(system_template, auth)).get_webhooks()
135 comp_webhook = Webhook(webhook_template, system_id, auth)
136 assert comp_webhook.__dict__ == webhooks[0].__dict__
137 m.assert_called_once()
138 await session.close()
139
140
141 @pytest.mark.asyncio
142 async def test_get_webhook():
143 session = aiohttp.ClientSession()
144 auth = Auth(session, base_url, "not needed")
145 with aioresponses() as m:
146 m.get(
147 f"{base_url}/systems/{system_id}/function-webhooks/{webhook_id}",
148 payload=webhook_template,
149 )
150 webhook = await (System(system_template, auth)).get_webhook(webhook_id)
151 comp_webhook = Webhook(webhook_template, system_id, auth)
152 assert comp_webhook.__dict__ == webhook.__dict__
153 m.assert_called_once()
154 await session.close()
155
156
157 @pytest.mark.asyncio
158 async def test_add_webhook():
159 session = aiohttp.ClientSession()
160 auth = Auth(session, base_url, "not needed")
161 with aioresponses() as m:
162 m.post(
163 f"{base_url}/systems/{system_id}/function-webhooks",
164 payload=webhook_template,
165 )
166 webhook = await (System(system_template, auth)).add_webhook(webhook_data_template)
167 comp_webhook = Webhook(webhook_template, system_id, auth)
168 assert comp_webhook.__dict__ == webhook.__dict__
169 m.assert_called_once_with(
170 f"{base_url}/systems/{system_id}/function-webhooks",
171 method="post",
172 json=webhook_data_template,
173 headers={"authorization": "Bearer not needed"},
174 )
175 await session.close()
176
177
178 @pytest.mark.asyncio
179 async def test_webhook_get_update():
180 session = aiohttp.ClientSession()
181 auth = Auth(session, base_url, "not needed")
182 with aioresponses() as m:
183 m.get(
184 f"{base_url}/systems/{system_id}/function-webhooks/{webhook_id}",
185 payload=webhook_template,
186 )
187 webhook = Webhook(webhook_template, system_id, auth)
188 webhook._webhook_function_name = "Test"
189 assert webhook._webhook_function_name == "Test"
190 await webhook.get_update()
191 comp_webhook = Webhook(webhook_template, system_id, auth)
192 assert comp_webhook.__dict__ == webhook.__dict__
193 m.assert_called_once()
194 await session.close()
195
196
197 @pytest.mark.asyncio
198 async def test_webhook_delete():
199 session = aiohttp.ClientSession()
200 auth = Auth(session, base_url, "not needed")
201
202 with aioresponses() as m:
203 m.delete(f"{base_url}/systems/{system_id}/function-webhooks/{webhook_id}")
204 webhook = Webhook(webhook_template, system_id, auth)
205 await webhook.delete()
206 assert webhook._modification_state == "DeleteRequested"
207 m.assert_called_once_with(
208 f"{base_url}/systems/{system_id}/function-webhooks/{webhook_id}",
209 method="delete",
210 headers={"authorization": "Bearer not needed"},
211 )
212 await session.close()
213
214
215 @pytest.mark.asyncio
216 async def test_webhook_update():
217 session = aiohttp.ClientSession()
218 auth = Auth(session, base_url, "not needed")
219 with aioresponses() as m:
220 m.put(f"{base_url}/systems/{system_id}/function-webhooks/{webhook_id}")
221 webhook = Webhook(webhook_template, system_id, auth)
222 await webhook.update(webhook_data_template)
223 assert webhook._modification_state == "UpdateRequested"
224 m.assert_called_once_with(
225 f"{base_url}/systems/{system_id}/function-webhooks/{webhook_id}",
226 method="put",
227 json=webhook_data_template,
228 headers={"authorization": "Bearer not needed"},
229 )
230 await session.close()
231
232
233 @pytest.mark.asyncio
234 async def test_webhook_patch():
235 session = aiohttp.ClientSession()
236 auth = Auth(session, base_url, "not needed")
237 with aioresponses() as m:
238 m.patch(f"{base_url}/systems/{system_id}/function-webhooks/{webhook_id}/name")
239 webhook = Webhook(webhook_template, system_id, auth)
240 await webhook.update_name(webhook_patch_data)
241 m.assert_called_once_with(
242 f"{base_url}/systems/{system_id}/function-webhooks/{webhook_id}/name",
243 method="patch",
244 json=webhook_patch_data,
245 headers={"authorization": "Bearer not needed"},
246 )
247 await session.close()