Fix some general testing, three tests are still failing, working on using sqlite as testing db
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
from datetime import datetime
|
||||
from typing import Annotated
|
||||
from typing import Annotated, List
|
||||
from fastapi.responses import JSONResponse
|
||||
from fastapi.security import OAuth2PasswordRequestForm
|
||||
from fastapi.routing import APIRouter
|
||||
@@ -31,7 +31,12 @@ async def login(form: Annotated[OAuth2PasswordRequestForm, Depends()]) -> JSONRe
|
||||
|
||||
Logs the user into our API, creates tokens and passes them back to User.
|
||||
"""
|
||||
user: User | None = await User.filter(email=form.username).first()
|
||||
user: User | None = await User.filter(
|
||||
Q(email=form.username) & Q(password=form.password)
|
||||
).first()
|
||||
|
||||
print(await User.all())
|
||||
print(form.username, form.password, user.__dict__ if user else None)
|
||||
|
||||
if user is None:
|
||||
raise HTTPException(status_code=401, detail=account_error)
|
||||
|
||||
@@ -1,63 +1,5 @@
|
||||
from dataclasses import dataclass
|
||||
from typing import List
|
||||
from modules.auth.utils import create_jwt_tokens
|
||||
from modules.organizations.models import Organization, OrganizationType
|
||||
from modules.users.models import ACL, Membership, User
|
||||
from modules.auth.models import Token
|
||||
import pytest
|
||||
from config import settings
|
||||
|
||||
crypt = settings.CRYPT
|
||||
|
||||
@dataclass
|
||||
class user_creation_return_type:
|
||||
user: User
|
||||
organization: Organization
|
||||
acl: ACL
|
||||
tokens: Token
|
||||
|
||||
@pytest.fixture()
|
||||
async def create_user_with_org():
|
||||
async def inner_function(email,
|
||||
username="user",
|
||||
name="awesome",
|
||||
surname="user",
|
||||
password="password-dont-use",
|
||||
organization_name="simple organization",
|
||||
organization_type=OrganizationType.HOME,
|
||||
is_admin=False) -> List[user_creation_return_type]:
|
||||
org: Organization = await Organization.create(
|
||||
name=organization_name,
|
||||
type=organization_type
|
||||
)
|
||||
|
||||
acl: ACL = await ACL.create(
|
||||
READ=True,
|
||||
WRITE=True,
|
||||
REPORT=True,
|
||||
MANAGE=True if is_admin else False,
|
||||
ADMIN=True if is_admin else False,
|
||||
)
|
||||
|
||||
user: User = await User.create(
|
||||
email=email,
|
||||
username=username,
|
||||
name=name,
|
||||
surname=surname,
|
||||
password=crypt.hash(password),
|
||||
)
|
||||
|
||||
await Membership.create(
|
||||
organization=org,
|
||||
user=user,
|
||||
acl=acl
|
||||
)
|
||||
|
||||
tokens: Token = await create_jwt_tokens(user=user)
|
||||
|
||||
return [user, org, acl, tokens]
|
||||
return inner_function
|
||||
|
||||
@pytest.mark.usefixtures("create_user_with_org")
|
||||
class Test:
|
||||
class Test():
|
||||
pass
|
||||
@@ -2,9 +2,9 @@ import asyncio
|
||||
from contextlib import asynccontextmanager
|
||||
from typing import AsyncGenerator
|
||||
import httpx, pytest
|
||||
from glob import glob
|
||||
|
||||
from asgi_lifespan import LifespanManager # type: ignore
|
||||
from tests.fixtures.account import *
|
||||
|
||||
try:
|
||||
from main import app
|
||||
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
import pytest
|
||||
from dataclasses import dataclass
|
||||
from modules.auth.utils import create_jwt_tokens
|
||||
from modules.organizations.models import Organization, OrganizationType
|
||||
from modules.users.models import ACL, Membership, User
|
||||
from modules.auth.models import Token
|
||||
|
||||
from config import settings
|
||||
|
||||
crypt = settings.CRYPT
|
||||
|
||||
@dataclass
|
||||
class user_creation_return_type:
|
||||
user: User
|
||||
organization: Organization
|
||||
acl: ACL
|
||||
tokens: Token
|
||||
|
||||
@pytest.fixture()
|
||||
async def create_user_with_org():
|
||||
async def inner_function(email="user@localhost.com",
|
||||
username="user",
|
||||
name="awesome",
|
||||
surname="user",
|
||||
password="password-dont-use",
|
||||
organization_name="simple organization",
|
||||
organization_type=OrganizationType.HOME,
|
||||
is_admin=False) -> user_creation_return_type:
|
||||
org: Organization = await Organization.create(
|
||||
name=organization_name,
|
||||
type=organization_type
|
||||
)
|
||||
|
||||
acl: ACL = await ACL.create(
|
||||
READ=True,
|
||||
WRITE=True,
|
||||
REPORT=True,
|
||||
MANAGE=True if is_admin else False,
|
||||
ADMIN=True if is_admin else False,
|
||||
)
|
||||
|
||||
user: User = await User.create(
|
||||
email=email,
|
||||
username=username,
|
||||
name=name,
|
||||
surname=surname,
|
||||
password=crypt.hash(password),
|
||||
)
|
||||
|
||||
await Membership.create(
|
||||
organization=org,
|
||||
user=user,
|
||||
acl=acl
|
||||
)
|
||||
|
||||
tokens: Token = await create_jwt_tokens(user=user)
|
||||
|
||||
return user, org, acl, tokens
|
||||
return inner_function
|
||||
@@ -26,7 +26,7 @@ class TestAuthentication(Test):
|
||||
async def test_authentication_with_existing_user_and_wrong_password(
|
||||
self, client: AsyncClient, create_user_with_org
|
||||
):
|
||||
_, _, _, _ = create_user_with_org(email="admin@localhost.com")
|
||||
_, _, _, _ = await create_user_with_org(email="admin@localhost.com")
|
||||
response = await client.post(
|
||||
"https://localhost/api/v1/auth/login",
|
||||
data={
|
||||
@@ -41,7 +41,7 @@ class TestAuthentication(Test):
|
||||
async def test_authentication_with_existing_user_and_password(
|
||||
self, client: AsyncClient, create_user_with_org
|
||||
):
|
||||
user, _, _, _ = create_user_with_org(email="admin@localhost.com", password="adminpassword")
|
||||
user, _, _, _ = await create_user_with_org(email="admin@localhost.com", password="adminpassword")
|
||||
response = await client.post(
|
||||
"https://localhost/api/v1/auth/login",
|
||||
data={
|
||||
@@ -68,7 +68,7 @@ class TestAuthentication(Test):
|
||||
async def test_logging_out_destroys_tokens(
|
||||
self, client: AsyncClient, create_user_with_org
|
||||
):
|
||||
user, _, _, _ = create_user_with_org(email="user@localhost.com", password="userpassword")
|
||||
user, _, _, _ = await create_user_with_org(email="user@localhost.com", password="userpassword")
|
||||
response = await client.post(
|
||||
"https://localhost/api/v1/auth/login",
|
||||
data={
|
||||
@@ -114,7 +114,7 @@ class TestAuthentication(Test):
|
||||
async def test_create_new_tokens_upon_refresh(
|
||||
self, client: AsyncClient, create_user_with_org
|
||||
):
|
||||
user, _, _, _ = create_user_with_org(email="admin@localhost.com", password="adminpassword")
|
||||
user, _, _, _ = await create_user_with_org(email="admin@localhost.com", password="adminpassword")
|
||||
token = await client.post(
|
||||
"https://localhost/api/v1/auth/login",
|
||||
data={
|
||||
@@ -123,6 +123,8 @@ class TestAuthentication(Test):
|
||||
"grant_type": "password",
|
||||
},
|
||||
)
|
||||
|
||||
assert token.__dict__ == True
|
||||
assert token.status_code == 200
|
||||
assert token.json() == {
|
||||
"jwt": {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import pytest # type: ignore
|
||||
import pytest
|
||||
from httpx import AsyncClient
|
||||
from config import settings
|
||||
from unittest.mock import ANY
|
||||
@@ -12,7 +12,7 @@ class TestOrganizationRoute(Test):
|
||||
async def test_get_organizations_from_api(
|
||||
self, client: AsyncClient, create_user_with_org
|
||||
):
|
||||
_,_,_,tokens = create_user_with_org
|
||||
_,_,_,tokens = await create_user_with_org()
|
||||
|
||||
organizations = await client.get(
|
||||
"https://localhost/api/v1/organizations/",
|
||||
@@ -27,8 +27,8 @@ class TestOrganizationRoute(Test):
|
||||
"disabled_at": None,
|
||||
"id": ANY,
|
||||
"modified_at": ANY,
|
||||
"name": "Superadmin's Organization",
|
||||
"type": "non_profit",
|
||||
"name": "simple organization",
|
||||
"type": "home",
|
||||
"street_name": None,
|
||||
"zip_code": None,
|
||||
"state": None,
|
||||
@@ -41,7 +41,7 @@ class TestOrganizationRoute(Test):
|
||||
async def test_create_organization(
|
||||
self, client: AsyncClient, create_user_with_org
|
||||
):
|
||||
_,_,_,tokens = create_user_with_org
|
||||
_,_,_,tokens = await create_user_with_org()
|
||||
|
||||
organizations = await client.post(
|
||||
"https://localhost/api/v1/organizations/",
|
||||
@@ -77,7 +77,7 @@ class TestOrganizationRoute(Test):
|
||||
async def test_delete_organization(
|
||||
self, client: AsyncClient, create_user_with_org
|
||||
):
|
||||
_,_,_,tokens = create_user_with_org
|
||||
_,_,_,tokens = await create_user_with_org()
|
||||
|
||||
organizations = await client.post(
|
||||
"https://localhost/api/v1/organizations/",
|
||||
|
||||
Reference in New Issue
Block a user