Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a test for Email ✨ #25

Merged
merged 3 commits into from
Sep 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions AuthX/core/jwt.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@
import jwt

from AuthX.core.config import JWT_ALGORITHM
from AuthX.database.redis import RedisBackend


class JWTBackend:
def __init__(
self,
cache_backend: RedisBackend,
private_key: Optional[bytes],
public_key: bytes,
access_expiration: int,
refresh_expiration: int,
) -> None:
self._cache = cache_backend

self._private_key = private_key
self._public_key = public_key
self._access_expiration = access_expiration
self._refresh_expiration = refresh_expiration

Expand All @@ -42,7 +44,9 @@ async def _user_in_logout(self, id: int, iat: datetime) -> bool:
async def decode_token(self, token: str, leeway: int = 0) -> Optional[dict]:
if token:
try:
payload = jwt.decode(token, leeway=leeway, algorithms=JWT_ALGORITHM,)
payload = jwt.decode(
token, self._public_key, leeway=leeway, algorithms=JWT_ALGORITHM,
)
id = payload.get("id")
iat = datetime.utcfromtimestamp(int(payload.get("iat")))
checks = await asyncio.gather(
Expand Down Expand Up @@ -71,10 +75,10 @@ def _create_token(

payload.update({"iat": iat, "exp": exp, "type": token_type})

return jwt.encode(payload, algorithm=JWT_ALGORITHM, key=None).decode()
return jwt.encode(payload, self._private_key, algorithm=JWT_ALGORITHM).decode()

def create_access_token(self, payload: dict) -> str:
return self._create_token(payload, self._access_expiration, "access")
return self._create_token(payload, "access", self._access_expiration)

def create_refresh_token(self, payload: dict) -> str:
return self._create_token(payload, "refresh", self._refresh_expiration)
Expand Down
3 changes: 0 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ astroid
async-timeout
atomicwrites
attrs
autoflake
bcrypt
black
certifi
cffi
charset-normalizer
Expand All @@ -19,7 +17,6 @@ cryptography
dnspython
email-validator
fastapi
flake8
h11
httpcore
httpx
Expand Down
14 changes: 14 additions & 0 deletions tests/core/test_core_email.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from unittest import mock

import pytest

from AuthX.core.email import EmailClient


@pytest.mark.asyncio
@pytest.mark.parametrize("language", ["en"])
@mock.patch("aiosmtplib.send", mock.AsyncMock(return_value=None))
async def test_email_client(language: str):
email_client = EmailClient(None, None, None, None, language, "", "", "")
await email_client.send_confirmation_email("", "")
await email_client.send_forgot_password_email("", "")