Fix Token generation and test

This commit is contained in:
2025-02-14 14:09:54 +02:00
parent 109140cb4d
commit 394fb9aee5
6 changed files with 32 additions and 6 deletions
-1
View File
@@ -17,7 +17,6 @@ from modules.organizations.router import router as organizations_router
async def lifespan(_: FastAPI): async def lifespan(_: FastAPI):
await migrate_db() await migrate_db()
yield yield
print(_.state.testing)
await end_connections_to_db() await end_connections_to_db()
@@ -0,0 +1,13 @@
from tortoise import BaseDBAsyncClient
async def upgrade(db: BaseDBAsyncClient) -> str:
return """
ALTER TABLE "token" ALTER COLUMN "refresh_token" TYPE TEXT USING "refresh_token"::TEXT;
ALTER TABLE "token" ALTER COLUMN "access_token" TYPE TEXT USING "access_token"::TEXT;"""
async def downgrade(db: BaseDBAsyncClient) -> str:
return """
ALTER TABLE "token" ALTER COLUMN "refresh_token" TYPE VARCHAR(128) USING "refresh_token"::VARCHAR(128);
ALTER TABLE "token" ALTER COLUMN "access_token" TYPE VARCHAR(128) USING "access_token"::VARCHAR(128);"""
+2 -2
View File
@@ -17,8 +17,8 @@ class Token(Model, CMDMixin):
id: uuid = fields.UUIDField(primary_key=True) id: uuid = fields.UUIDField(primary_key=True)
user: uuid = fields.ForeignKeyField("models.User") user: uuid = fields.ForeignKeyField("models.User")
token_type: str = fields.CharField(max_length=128, default="Bearer") token_type: str = fields.CharField(max_length=128, default="Bearer")
access_token: str = fields.CharField(max_length=128, null=True) access_token: str = fields.TextField(null=True)
refresh_token: str = fields.CharField(max_length=128, null=True) refresh_token: str = fields.TextField(null=True)
disabled: bool = fields.BooleanField(default=False) disabled: bool = fields.BooleanField(default=False)
def delete(self) -> None: def delete(self) -> None:
+1 -1
View File
@@ -38,7 +38,7 @@ async def login(form: Annotated[OAuth2PasswordRequestForm, Depends()]):
) )
token = await Token.create( token = await Token.create(
user=user.id, user=user,
access_token=auth_token, access_token=auth_token,
refresh_token=refresh_token, refresh_token=refresh_token,
) )
+1 -1
View File
@@ -10,7 +10,7 @@ crypt = settings.CRYPT
async def use_user_account(): async def use_user_account():
org = await Organization.create(name="User's Organization", type="home") org = await Organization.create(name="User's Organization", type="home")
acl = await ACL.create( acl = await ACL.create(
READ=True, WRITE=True, REPORT=True, MANAGE=True, ADMIN=True READ=True, WRITE=True, REPORT=True, MANAGE=False, ADMIN=False
) )
user = await User.create( user = await User.create(
email="user@localhost.com", email="user@localhost.com",
@@ -1,6 +1,7 @@
import pytest # type: ignore import pytest # type: ignore
from httpx import AsyncClient from httpx import AsyncClient
from config import settings from config import settings
from unittest.mock import ANY
crypt = settings.CRYPT crypt = settings.CRYPT
@@ -40,6 +41,7 @@ class TestAuthentication(object):
async def test_authentication_with_existing_user_and_password( async def test_authentication_with_existing_user_and_password(
self, client: AsyncClient, use_admin_account self, client: AsyncClient, use_admin_account
): ):
_, _, user, _ = use_admin_account
response = await client.post( response = await client.post(
"http://localhost/api/v1/auth/", "http://localhost/api/v1/auth/",
data={ data={
@@ -49,4 +51,16 @@ class TestAuthentication(object):
}, },
) )
assert response.status_code == 200 assert response.status_code == 200
assert response.text == "" assert response.json() == {
"jwt": {
"created_at": ANY,
"user_id": str(user.id),
"id": ANY,
"modified_at": ANY,
"disabled_at": None,
"refresh_token": ANY,
"disabled": False,
"access_token": ANY,
"token_type": "Bearer",
}
}