Skip to content

Commit

Permalink
refactor(test): define a DEFAULT_USER use to be basic testing user
Browse files Browse the repository at this point in the history
  • Loading branch information
arasHi87 committed Mar 14, 2023
1 parent 3a4aec2 commit 0f401bd
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 22 deletions.
15 changes: 9 additions & 6 deletions api/tests/__init__.py
@@ -1,11 +1,19 @@
from dataclasses import dataclass
from typing import Any, Callable, Dict, Mapping, Optional

import schemas
from fastapi import FastAPI
from httpx import AsyncClient, Response
from starlette.datastructures import URLPath
from utils import create_access_token

DEFAULT_USER = schemas.UserWithoutPassword(
id=1,
name="default",
email="default@gmail.com",
password="default",
)


@dataclass
class RequestBody:
Expand All @@ -21,12 +29,7 @@ class ResponseBody:

class AssertRequest:
def __init__(self):
claims = {
"id": 1,
"name": "default",
"email": "default@gmail.com",
"exp": "123",
}
claims = {**DEFAULT_USER.dict(), "exp": 123}
self.header = {"Authorization": f"Bearer {create_access_token(claims=claims)}"}

async def __call__(
Expand Down
5 changes: 4 additions & 1 deletion api/tests/conftest.py
Expand Up @@ -11,6 +11,7 @@
from models.base import Base
from models.user import User
from repositories.user import UserRepository
from tests import DEFAULT_USER

user_repo = UserRepository(User)

Expand Down Expand Up @@ -45,7 +46,9 @@ async def init_table(request):
# insert default data for user
async with session() as conn:
user = schemas.UserCreate(
name="default", email="default@gmail.com", password="default"
name=DEFAULT_USER.name,
email=DEFAULT_USER.email,
password=DEFAULT_USER.password,
)
await user_repo.create(conn, user)
pass
14 changes: 7 additions & 7 deletions api/tests/test_auth.py
Expand Up @@ -5,7 +5,7 @@
from fastapi import FastAPI
from httpx import Response
from jose import jwt
from tests import RequestBody, ResponseBody, assert_request
from tests import DEFAULT_USER, RequestBody, ResponseBody, assert_request

""" Test user authentication endpoint
@router post /auth/
Expand All @@ -26,8 +26,8 @@ def _test_user_auth_success_assert(resp: Response, expected_resp: ResponseBody):
assert resp.status_code == expected_resp.status_code
assert resp.json()["token_type"] == expected_resp.body["token_type"]
assert payload["id"] == 1
assert payload["name"] == "default"
assert payload["email"] == "default@gmail.com"
assert payload["name"] == DEFAULT_USER.name
assert payload["email"] == DEFAULT_USER.email


async def test_user_auth_success(app: FastAPI):
Expand All @@ -44,8 +44,8 @@ async def test_user_auth_success(app: FastAPI):
assert_func=_test_user_auth_success_assert,
data={
"grant_type": "",
"username": "default@gmail.com",
"password": "default",
"username": DEFAULT_USER.email,
"password": DEFAULT_USER.password,
"scope": "",
"client_id": "",
"client_secret": "",
Expand All @@ -58,7 +58,7 @@ async def test_user_auth_success(app: FastAPI):
[
{
"grant_type": "",
"username": "default@gmail.com",
"username": DEFAULT_USER.email,
"password": "meow",
"scope": "",
"client_id": "",
Expand All @@ -67,7 +67,7 @@ async def test_user_auth_success(app: FastAPI):
{
"grant_type": "",
"username": "meow@gmail.com",
"password": "default",
"password": DEFAULT_USER.password,
"scope": "",
"client_id": "",
"client_secret": "",
Expand Down
24 changes: 16 additions & 8 deletions api/tests/test_user.py
@@ -1,6 +1,6 @@
from app import APP
from fastapi import FastAPI
from tests import RequestBody, ResponseBody, assert_request
from tests import DEFAULT_USER, RequestBody, ResponseBody, assert_request

""" Test create user endpoint
@router post /user/
Expand Down Expand Up @@ -30,7 +30,7 @@ async def test_create_user_success(app: FastAPI):
async def test_create_user_exists(app: FastAPI):
req = RequestBody(
url=app.url_path_for(name="user:create_user"),
body={"name": "default", "email": "default@gmail.com", "password": "default"},
body=DEFAULT_USER.__dict__,
)
resp = ResponseBody(status_code=400, body={"detail": "User already exists"})
await assert_request(app=APP, method="POST", req_body=req, resp_body=resp)
Expand All @@ -46,14 +46,17 @@ async def test_create_user_exists(app: FastAPI):

async def test_get_user_success(app: FastAPI):
req = RequestBody(url=app.url_path_for(name="user:get_user"), body={})
resp = ResponseBody(
status_code=200, body={"id": 1, "name": "default", "email": "default@gmail.com"}
)
resp = ResponseBody(status_code=200, body=DEFAULT_USER.dict())
await assert_request(app=APP, method="GET", req_body=req, resp_body=resp)


async def test_get_user_none_exists(app: FastAPI):
claims = {"id": 100, "name": "default", "email": "default@gmail.com", "exp": 123}
claims = {
"id": 100,
"name": DEFAULT_USER.name,
"email": DEFAULT_USER.email,
"exp": 123,
}
req = RequestBody(url=app.url_path_for(name="user:get_user"), body={})
resp = ResponseBody(status_code=404, body={"detail": "User not found"})
await assert_request(
Expand All @@ -75,7 +78,7 @@ async def test_update_user_success(app: FastAPI):
body={
"name": "test",
"email": "test@gmail.com",
"password": "default",
"password": DEFAULT_USER.password,
"new_password": "test",
},
)
Expand All @@ -86,7 +89,12 @@ async def test_update_user_success(app: FastAPI):


async def test_update_user_none_exists(app: FastAPI):
claims = {"id": 100, "name": "default", "email": "default@gmail.com", "exp": 123}
claims = {
"id": 100,
"name": DEFAULT_USER.name,
"email": DEFAULT_USER.email,
"exp": 123,
}
req = RequestBody(
url=app.url_path_for(name="user:update_user"),
body={
Expand Down

0 comments on commit 0f401bd

Please sign in to comment.