Skip to content

Commit b6fc162

Browse files
committed
[feat,refactor,fix]: remove env requirement, fix widgeet tests, add timeout tests
1 parent 8895aa6 commit b6fc162

3 files changed

Lines changed: 32 additions & 32 deletions

File tree

tests/test_client.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from multidict import CIMultiDict
22
from datetime import datetime
3-
from os import getenv, path
3+
from os import path
44
import sys
55

66
sys.path.insert(0, path.join(path.dirname(path.realpath(__file__)), '..'))
@@ -17,16 +17,14 @@
1717
from util import _test_attributes, RequestMock
1818

1919

20+
MOCK_TOKEN = '.eyJfdCI6IiIsImlkIjoiMzY0ODA2MDI5ODc2NTU1Nzc2In0=.'
21+
22+
2023
@pytest_asyncio.fixture
2124
async def client(
2225
monkeypatch: pytest.MonkeyPatch,
2326
) -> AsyncGenerator[topgg.Client, None]:
24-
token = getenv('TOPGG_TOKEN')
25-
26-
if TYPE_CHECKING:
27-
assert token is not None, 'Missing Top.gg API token'
28-
29-
client = topgg.Client(token)
27+
client = topgg.Client(MOCK_TOKEN)
3028

3129
monkeypatch.setattr(topgg.Ratelimiter, '_calls', deque([time()]))
3230

@@ -50,12 +48,7 @@ async def test_Client_basic_error_handling_works() -> None:
5048
pass
5149

5250
with pytest.raises(topgg.Error, match='^Client session is already closed.$'):
53-
token = getenv('TOPGG_TOKEN')
54-
55-
if TYPE_CHECKING:
56-
assert token is not None, 'Missing Top.gg API token'
57-
58-
test_client = topgg.Client(token)
51+
test_client = topgg.Client(MOCK_TOKEN)
5952

6053
await test_client.close()
6154
await test_client.get_self()

tests/test_webhooks.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from functools import cache
55
from hashlib import sha256
66
from os.path import join
7-
from os import getenv
87
from time import time
98
import pytest_asyncio
109
import pytest
@@ -15,23 +14,16 @@
1514
from util import _test_attributes, CURRENT_DIR
1615

1716

17+
MOCK_SECRET = 'testsecret1234'
1818
MOCK_TRACE = 'trace'
1919
WebhooksFixture = tuple[topgg.Webhooks, test_utils.TestClient]
2020
WebhooksSignatureFixture = 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
6456
async 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
145148
async def start_webhooks(webhooks: topgg.Webhooks):

tests/test_widget.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,20 @@
66
@pytest.mark.parametrize('function_name', ('large', 'votes', 'owner', 'social'))
77
@pytest.mark.parametrize('platform', iter(Platform))
88
@pytest.mark.parametrize('project_type', iter(ProjectType))
9-
def test_Widget_works(function_name: str, platform: Platform, project_type: ProjectType):
9+
def test_Widget_works(
10+
function_name: str, platform: Platform, project_type: ProjectType
11+
):
1012
function = getattr(Widget, function_name)
1113

12-
url = function(project_type, 123456)
14+
url = function(platform, project_type, 123456)
1315
path = 'large' if function_name == 'large' else f'small/{function_name}'
1416

15-
assert url == f'{BASE_URL}/widgets/{path}/{platform.value}/{project_type.value}/123456'
17+
assert (
18+
url == f'{BASE_URL}/widgets/{path}/{platform.value}/{project_type.value}/123456'
19+
)
1620

1721
with pytest.raises(
1822
TypeError,
1923
match=r'^The specified platform, project type, and/or project ID\'s type is invalid.$',
2024
):
21-
function(project_type, None)
25+
function(platform, project_type, None)

0 commit comments

Comments
 (0)