Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMaster3558 committed Oct 19, 2022
1 parent dfb66b3 commit 2271561
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
3 changes: 1 addition & 2 deletions aiointeractions/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ def __init__(
app.add_routes([web.post(route, self.interactions_handler)])
self.app: web.Application = app

self.success_code = 204 if success_response is None else 200
self.success_response = success_response
self.forbidden_response = forbidden_response

Expand Down Expand Up @@ -128,7 +127,7 @@ async def interactions_handler(self, request: web.Request) -> web.Response:
self.client._connection.parse_interaction_create(data)
await get_latest_task(tasks)

return web.Response(status=self.success_code, body=self.success_response)
return web.Response(status=200, body=self.success_response)

async def start(self, token: str, **kwargs: Any) -> None:
"""
Expand Down
5 changes: 5 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,8 @@ skip-string-normalization = true
[tool.pyright]
pythonVersion = "3.8"
typeCheckingMode = "basic"


[tool.pytest.ini_options]
pythonpath = ["."]
filterwarnings = ["ignore::DeprecationWarning"]
53 changes: 53 additions & 0 deletions tests/test_interactions_handler.py
Original file line number Diff line number Diff line change
@@ -1 +1,54 @@
from typing import Any, Mapping

import pytest

import aiointeractions
import discord


class MockRequest:
def __init__(
self,
headers: Mapping[str, Any],
data: Any
):
self.headers = headers
self._data = data

async def text(self) -> str:
return str(self._data)


class MockApp(aiointeractions.InteractionsApp):
verification: str = '{}{"type": 1}'

def _verify_request(self, headers: Mapping[str, Any], body: str) -> bool:
return f'{headers}{body}' == self.verification


intents = discord.Intents.none()
client = discord.Client(intents=intents)
tree = discord.app_commands.CommandTree(client)

app = MockApp(client)


@pytest.mark.asyncio
async def test_no_verification() -> None:
request = MockRequest({}, '')
response = await app.interactions_handler(request) # type: ignore
assert response.status == 401


@pytest.mark.asyncio
async def test_invalid_verification() -> None:
request = MockRequest({}, 'badtext')
response = await app.interactions_handler(request) # type: ignore
assert response.status == 401


@pytest.mark.asyncio
async def test_valid_verification() -> None:
request = MockRequest({}, '{"type": 1}')
response = await app.interactions_handler(request) # type: ignore
assert response.status == 200

0 comments on commit 2271561

Please sign in to comment.