diff --git a/api/asset_manager/src/main.py b/api/asset_manager/src/main.py index 28a30c9c..11d84db5 100644 --- a/api/asset_manager/src/main.py +++ b/api/asset_manager/src/main.py @@ -17,7 +17,6 @@ from modules.organizations.router import router as organizations_router async def lifespan(_: FastAPI): await migrate_db() yield - print(_.state.testing) await end_connections_to_db() diff --git a/api/asset_manager/src/migrations/models/2_20250214131414_update.py b/api/asset_manager/src/migrations/models/2_20250214131414_update.py new file mode 100644 index 00000000..3b9f1a5e --- /dev/null +++ b/api/asset_manager/src/migrations/models/2_20250214131414_update.py @@ -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);""" diff --git a/api/asset_manager/src/modules/auth/models.py b/api/asset_manager/src/modules/auth/models.py index 8598affa..2eae3bc0 100644 --- a/api/asset_manager/src/modules/auth/models.py +++ b/api/asset_manager/src/modules/auth/models.py @@ -17,8 +17,8 @@ class Token(Model, CMDMixin): id: uuid = fields.UUIDField(primary_key=True) user: uuid = fields.ForeignKeyField("models.User") token_type: str = fields.CharField(max_length=128, default="Bearer") - access_token: str = fields.CharField(max_length=128, null=True) - refresh_token: str = fields.CharField(max_length=128, null=True) + access_token: str = fields.TextField(null=True) + refresh_token: str = fields.TextField(null=True) disabled: bool = fields.BooleanField(default=False) def delete(self) -> None: diff --git a/api/asset_manager/src/modules/auth/router.py b/api/asset_manager/src/modules/auth/router.py index 309abb5f..2f69d801 100644 --- a/api/asset_manager/src/modules/auth/router.py +++ b/api/asset_manager/src/modules/auth/router.py @@ -38,7 +38,7 @@ async def login(form: Annotated[OAuth2PasswordRequestForm, Depends()]): ) token = await Token.create( - user=user.id, + user=user, access_token=auth_token, refresh_token=refresh_token, ) diff --git a/api/asset_manager/src/tests/fixtures/account_fixtures.py b/api/asset_manager/src/tests/fixtures/account_fixtures.py index e0837062..d6e5d52d 100644 --- a/api/asset_manager/src/tests/fixtures/account_fixtures.py +++ b/api/asset_manager/src/tests/fixtures/account_fixtures.py @@ -10,7 +10,7 @@ crypt = settings.CRYPT async def use_user_account(): org = await Organization.create(name="User's Organization", type="home") 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( email="user@localhost.com", diff --git a/api/asset_manager/src/tests/test_authentication/test_authentication.py b/api/asset_manager/src/tests/test_authentication/test_authentication.py index 3fa90460..768a3f0b 100644 --- a/api/asset_manager/src/tests/test_authentication/test_authentication.py +++ b/api/asset_manager/src/tests/test_authentication/test_authentication.py @@ -1,6 +1,7 @@ import pytest # type: ignore from httpx import AsyncClient from config import settings +from unittest.mock import ANY crypt = settings.CRYPT @@ -40,6 +41,7 @@ class TestAuthentication(object): async def test_authentication_with_existing_user_and_password( self, client: AsyncClient, use_admin_account ): + _, _, user, _ = use_admin_account response = await client.post( "http://localhost/api/v1/auth/", data={ @@ -49,4 +51,16 @@ class TestAuthentication(object): }, ) 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", + } + }