forked from top-gg-community/python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_webhook.py
More file actions
80 lines (64 loc) · 2.14 KB
/
test_webhook.py
File metadata and controls
80 lines (64 loc) · 2.14 KB
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
import typing as t
import aiohttp
import mock
import pytest
from topgg import WebhookManager, WebhookType
from topgg.errors import TopGGException
auth = "youshallnotpass"
@pytest.fixture
def webhook_manager() -> WebhookManager:
return (
WebhookManager()
.endpoint()
.type(WebhookType.BOT)
.auth(auth)
.route("/dbl")
.callback(print)
.add_to_manager()
.endpoint()
.type(WebhookType.GUILD)
.auth(auth)
.route("/dsl")
.callback(print)
.add_to_manager()
)
def test_WebhookManager_routes(webhook_manager: WebhookManager) -> None:
assert len(webhook_manager.app.router.routes()) == 2
@pytest.mark.asyncio
@pytest.mark.parametrize(
"headers, result, state",
[({"authorization": auth}, 200, True), ({}, 401, False)],
)
async def test_WebhookManager_validates_auth(
webhook_manager: WebhookManager, headers: t.Dict[str, str], result: int, state: bool
) -> None:
await webhook_manager.start(5000)
try:
for path in ("dbl", "dsl"):
async with aiohttp.request(
"POST", f"http://localhost:5000/{path}", headers=headers, json={}
) as r:
assert r.status == result
finally:
await webhook_manager.close()
assert not webhook_manager.is_running
def test_WebhookEndpoint_callback_unset(webhook_manager: WebhookManager):
with pytest.raises(
TopGGException,
match="endpoint missing callback.",
):
webhook_manager.endpoint().add_to_manager()
def test_WebhookEndpoint_route_unset(webhook_manager: WebhookManager):
with pytest.raises(
TopGGException,
match="endpoint missing type.",
):
webhook_manager.endpoint().callback(mock.Mock()).add_to_manager()
def test_WebhookEndpoint_type_unset(webhook_manager: WebhookManager):
with pytest.raises(
TopGGException,
match="endpoint missing route.",
):
webhook_manager.endpoint().callback(mock.Mock()).type(
WebhookType.BOT
).add_to_manager()