44from functools import cache
55from hashlib import sha256
66from os .path import join
7- from os import getenv
87from time import time
98import pytest_asyncio
109import pytest
1514from util import _test_attributes , CURRENT_DIR
1615
1716
17+ MOCK_SECRET = 'testsecret1234'
1818MOCK_TRACE = 'trace'
1919WebhooksFixture = tuple [topgg .Webhooks , test_utils .TestClient ]
2020WebhooksSignatureFixture = Callable [[str ], tuple [str , str ]]
2121
2222
23- @pytest .fixture
24- def webhook_secret () -> str :
25- secret = getenv ('TOPGG_WEBHOOK_SECRET' )
26-
27- assert secret is not None , 'Missing TOPGG_WEBHOOK_SECRET environment variable.'
28- return secret
29-
30-
3123@pytest_asyncio .fixture
32- async def webhooks (webhook_secret : str ) -> AsyncGenerator [WebhooksFixture , None ]:
24+ async def webhooks () -> AsyncGenerator [WebhooksFixture , None ]:
3325 app = test_utils .TestClient (test_utils .TestServer (web .Application ()))
34- webhooks = topgg .Webhooks ('/webhook' , webhook_secret , app = app )
26+ webhooks = topgg .Webhooks ('/webhook' , MOCK_SECRET , app = app )
3527
3628 for payload_type in topgg .PayloadType :
3729
@@ -47,12 +39,12 @@ async def handler(payload: topgg.Payload, trace: str) -> web.Response:
4739
4840
4941@pytest .fixture
50- def webhook_signature (webhook_secret : str ) -> WebhooksSignatureFixture :
42+ def webhook_signature () -> WebhooksSignatureFixture :
5143 t = str (int (time ()))
5244
5345 def generate_webhook_signature (body : str ) -> tuple [str , str ]:
5446 return t , hmac .new (
55- webhook_secret .encode ('utf-8' ),
47+ MOCK_SECRET .encode ('utf-8' ),
5648 f'{ t } .{ body } ' .encode ('utf-8' ),
5749 sha256 ,
5850 ).hexdigest ()
@@ -63,7 +55,6 @@ def generate_webhook_signature(body: str) -> tuple[str, str]:
6355@pytest .mark .asyncio
6456async def test_Webhooks_error_handling_works (
6557 webhooks : WebhooksFixture ,
66- webhook_secret : str ,
6758 webhook_signature : WebhooksSignatureFixture ,
6859) -> None :
6960 wh , client = webhooks
@@ -76,7 +67,10 @@ async def test_Webhooks_error_handling_works(
7667 topgg .Webhooks (None , None )
7768
7869 with pytest .raises (TypeError , match = '^The specified port must be an integer.$' ):
79- topgg .Webhooks ('foo' , webhook_secret , port = '' )
70+ topgg .Webhooks ('foo' , MOCK_SECRET , port = '' )
71+
72+ with pytest .raises (TypeError , match = '^The specified timeout must be a float.$' ):
73+ topgg .Webhooks ('foo' , MOCK_SECRET , timeout = 1 )
8074
8175 with pytest .raises (
8276 ValueError , match = r'^The specified secret, route, and/or host must not be empty.$'
@@ -140,6 +134,15 @@ def test():
140134 assert response .status == 403
141135 assert (await response .json ()).get ('error' ) == 'Invalid signature'
142136
137+ response = await client .post (
138+ '/webhook' ,
139+ data = '' ,
140+ headers = {'Content-Length' : '2' , 'Content-Type' : 'application/json' },
141+ )
142+
143+ assert response .status == 400
144+ assert (await response .json ()).get ('error' ) == 'Malformed request'
145+
143146
144147@cache
145148async def start_webhooks (webhooks : topgg .Webhooks ):
0 commit comments