From 3640c4ec2ab603b7d96a70e65880da5f4e112288 Mon Sep 17 00:00:00 2001 From: Denis Bezykornov Date: Sat, 29 Jun 2024 14:55:29 +0300 Subject: [PATCH 1/5] Reformat schema files --- backend/alembic/script.py.mako | 2 ++ .../versions/004d29805949_added_table_processing_status.py | 2 ++ .../versions/0ba6579fbbca_added_original_xliff_doc_field.py | 3 +++ .../versions/68219fe4ad46_added_document_upload_date.py | 2 ++ .../9356f0b5cdd9_add_unique_constraint_for_user_email.py | 1 + backend/alembic/versions/94eb09ac97fe_added_user_table.py | 2 +- backend/alembic/versions/ab1574d1a1da_added_tmx_tables.py | 3 +++ backend/alembic/versions/d2f116d3976e_added_xliff_tables.py | 3 +++ .../dc95266888cd_added_creation_and_update_time_for_tmx_.py | 2 +- .../ebad9129920d_added_original_segment_id_to_xliff.py | 3 +++ .../alembic/versions/f83699ff2dd0_added_tasks_generation.py | 2 ++ 11 files changed, 23 insertions(+), 2 deletions(-) diff --git a/backend/alembic/script.py.mako b/backend/alembic/script.py.mako index fbc4b07..d6adbdc 100644 --- a/backend/alembic/script.py.mako +++ b/backend/alembic/script.py.mako @@ -11,6 +11,8 @@ from alembic import op import sqlalchemy as sa ${imports if imports else ""} +# pylint: disable=E1101 + # revision identifiers, used by Alembic. revision: str = ${repr(up_revision)} down_revision: Union[str, None] = ${repr(down_revision)} diff --git a/backend/alembic/versions/004d29805949_added_table_processing_status.py b/backend/alembic/versions/004d29805949_added_table_processing_status.py index 978818f..e3e55ca 100644 --- a/backend/alembic/versions/004d29805949_added_table_processing_status.py +++ b/backend/alembic/versions/004d29805949_added_table_processing_status.py @@ -12,6 +12,8 @@ import sqlalchemy as sa +# pylint: disable=E1101 + # revision identifiers, used by Alembic. revision: str = "004d29805949" down_revision: Union[str, None] = "ebad9129920d" diff --git a/backend/alembic/versions/0ba6579fbbca_added_original_xliff_doc_field.py b/backend/alembic/versions/0ba6579fbbca_added_original_xliff_doc_field.py index a7fa5be..8c4700e 100644 --- a/backend/alembic/versions/0ba6579fbbca_added_original_xliff_doc_field.py +++ b/backend/alembic/versions/0ba6579fbbca_added_original_xliff_doc_field.py @@ -5,12 +5,15 @@ Create Date: 2023-12-27 21:17:14.641409 """ + from typing import Sequence, Union from alembic import op import sqlalchemy as sa +# pylint: disable=E1101 + # revision identifiers, used by Alembic. revision: str = "0ba6579fbbca" down_revision: Union[str, None] = "d2f116d3976e" diff --git a/backend/alembic/versions/68219fe4ad46_added_document_upload_date.py b/backend/alembic/versions/68219fe4ad46_added_document_upload_date.py index 1b7d83a..038211e 100644 --- a/backend/alembic/versions/68219fe4ad46_added_document_upload_date.py +++ b/backend/alembic/versions/68219fe4ad46_added_document_upload_date.py @@ -12,6 +12,8 @@ import sqlalchemy as sa +# pylint: disable=E1101 + # revision identifiers, used by Alembic. revision: str = "68219fe4ad46" down_revision: Union[str, None] = "f83699ff2dd0" diff --git a/backend/alembic/versions/9356f0b5cdd9_add_unique_constraint_for_user_email.py b/backend/alembic/versions/9356f0b5cdd9_add_unique_constraint_for_user_email.py index e4b7d0a..503413d 100644 --- a/backend/alembic/versions/9356f0b5cdd9_add_unique_constraint_for_user_email.py +++ b/backend/alembic/versions/9356f0b5cdd9_add_unique_constraint_for_user_email.py @@ -11,6 +11,7 @@ from alembic import op import sqlalchemy as sa + # pylint: disable=E1101 # revision identifiers, used by Alembic. diff --git a/backend/alembic/versions/94eb09ac97fe_added_user_table.py b/backend/alembic/versions/94eb09ac97fe_added_user_table.py index 55de424..eaa5caf 100644 --- a/backend/alembic/versions/94eb09ac97fe_added_user_table.py +++ b/backend/alembic/versions/94eb09ac97fe_added_user_table.py @@ -11,8 +11,8 @@ from alembic import op import sqlalchemy as sa -# pylint: disable=E1101 +# pylint: disable=E1101 # revision identifiers, used by Alembic. revision: str = "94eb09ac97fe" diff --git a/backend/alembic/versions/ab1574d1a1da_added_tmx_tables.py b/backend/alembic/versions/ab1574d1a1da_added_tmx_tables.py index 67e0300..1b4d65d 100644 --- a/backend/alembic/versions/ab1574d1a1da_added_tmx_tables.py +++ b/backend/alembic/versions/ab1574d1a1da_added_tmx_tables.py @@ -5,12 +5,15 @@ Create Date: 2023-12-06 21:40:05.738555 """ + from typing import Sequence, Union from alembic import op import sqlalchemy as sa +# pylint: disable=E1101 + # revision identifiers, used by Alembic. revision: str = "ab1574d1a1da" down_revision: Union[str, None] = None diff --git a/backend/alembic/versions/d2f116d3976e_added_xliff_tables.py b/backend/alembic/versions/d2f116d3976e_added_xliff_tables.py index 6c267b4..838af75 100644 --- a/backend/alembic/versions/d2f116d3976e_added_xliff_tables.py +++ b/backend/alembic/versions/d2f116d3976e_added_xliff_tables.py @@ -5,12 +5,15 @@ Create Date: 2023-12-07 00:56:49.742016 """ + from typing import Sequence, Union from alembic import op import sqlalchemy as sa +# pylint: disable=E1101 + # revision identifiers, used by Alembic. revision: str = "d2f116d3976e" down_revision: Union[str, None] = "ab1574d1a1da" diff --git a/backend/alembic/versions/dc95266888cd_added_creation_and_update_time_for_tmx_.py b/backend/alembic/versions/dc95266888cd_added_creation_and_update_time_for_tmx_.py index 2a91ee5..a9bc50a 100644 --- a/backend/alembic/versions/dc95266888cd_added_creation_and_update_time_for_tmx_.py +++ b/backend/alembic/versions/dc95266888cd_added_creation_and_update_time_for_tmx_.py @@ -12,8 +12,8 @@ from alembic import op import sqlalchemy as sa -# pylint: disable=E1101 +# pylint: disable=E1101 # revision identifiers, used by Alembic. revision: str = "dc95266888cd" diff --git a/backend/alembic/versions/ebad9129920d_added_original_segment_id_to_xliff.py b/backend/alembic/versions/ebad9129920d_added_original_segment_id_to_xliff.py index a90b20b..0bd5c23 100644 --- a/backend/alembic/versions/ebad9129920d_added_original_segment_id_to_xliff.py +++ b/backend/alembic/versions/ebad9129920d_added_original_segment_id_to_xliff.py @@ -5,12 +5,15 @@ Create Date: 2024-01-02 20:17:06.133690 """ + from typing import Sequence, Union from alembic import op import sqlalchemy as sa +# pylint: disable=E1101 + # revision identifiers, used by Alembic. revision: str = "ebad9129920d" down_revision: Union[str, None] = "0ba6579fbbca" diff --git a/backend/alembic/versions/f83699ff2dd0_added_tasks_generation.py b/backend/alembic/versions/f83699ff2dd0_added_tasks_generation.py index 0176c1e..cf9bbb3 100644 --- a/backend/alembic/versions/f83699ff2dd0_added_tasks_generation.py +++ b/backend/alembic/versions/f83699ff2dd0_added_tasks_generation.py @@ -12,6 +12,8 @@ import sqlalchemy as sa +# pylint: disable=E1101 + # revision identifiers, used by Alembic. revision: str = "f83699ff2dd0" down_revision: Union[str, None] = "004d29805949" From bcff4e355c50881084dda4691e3bac7d84977773 Mon Sep 17 00:00:00 2001 From: Denis Bezykornov Date: Sun, 30 Jun 2024 21:45:26 +0300 Subject: [PATCH 2/5] Add created_by field to tmx and xliff files --- ...b_add_links_between_users_and_documents.py | 54 +++++++++++++++++++ backend/app/schema.py | 11 ++++ 2 files changed, 65 insertions(+) create mode 100644 backend/alembic/versions/a61da93aeb2b_add_links_between_users_and_documents.py diff --git a/backend/alembic/versions/a61da93aeb2b_add_links_between_users_and_documents.py b/backend/alembic/versions/a61da93aeb2b_add_links_between_users_and_documents.py new file mode 100644 index 0000000..ca3229a --- /dev/null +++ b/backend/alembic/versions/a61da93aeb2b_add_links_between_users_and_documents.py @@ -0,0 +1,54 @@ +"""Add links between users and documents + +Revision ID: a61da93aeb2b +Revises: 9356f0b5cdd9 +Create Date: 2024-06-29 14:53:17.965879 + +""" + +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa + + +# pylint: disable=E1101 + +# revision identifiers, used by Alembic. +revision: str = "a61da93aeb2b" +down_revision: Union[str, None] = "9356f0b5cdd9" +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + # TMX part + op.add_column("tmx_document", sa.Column("created_by", sa.Integer())) + update = sa.update(sa.table("tmx_document", sa.Column("created_by"))).values( + created_by=1 # first administrator account + ) + op.execute(update) + op.alter_column("tmx_document", "created_by", nullable=False) + op.create_foreign_key( + "tmx_document_user_id_key", "tmx_document", "user", ["created_by"], ["id"] + ) + + # XLIFF part + op.add_column("xliff_document", sa.Column("created_by", sa.Integer())) + update = sa.update(sa.table("xliff_document", sa.Column("created_by"))).values( + created_by=1 # first administrator account + ) + op.execute(update) + op.alter_column("xliff_document", "created_by", nullable=False) + op.create_foreign_key( + "xliff_document_user_id_key", "xliff_document", "user", ["created_by"], ["id"] + ) + + +def downgrade() -> None: + op.drop_constraint( + "xliff_document_user_id_key", "xliff_document", type_="foreignkey" + ) + op.drop_column("xliff_document", "created_by") + op.drop_constraint("tmx_document_user_id_key", "tmx_document", type_="foreignkey") + op.drop_column("tmx_document", "created_by") diff --git a/backend/app/schema.py b/backend/app/schema.py index 9fe4e9e..e0eebc4 100644 --- a/backend/app/schema.py +++ b/backend/app/schema.py @@ -14,10 +14,12 @@ class TmxDocument(Base): id: Mapped[int] = mapped_column(primary_key=True) name: Mapped[str] = mapped_column() + created_by: Mapped[str] = mapped_column(ForeignKey("user.id")) records: Mapped[list["TmxRecord"]] = relationship( back_populates="document", cascade="all, delete-orphan", order_by="TmxRecord.id" ) + user: Mapped["User"] = relationship(back_populates="tmxs") class TmxRecord(Base): @@ -38,6 +40,7 @@ class XliffDocument(Base): id: Mapped[int] = mapped_column(primary_key=True) name: Mapped[str] = mapped_column() + created_by: Mapped[str] = mapped_column(ForeignKey("user.id")) original_document: Mapped[str] = mapped_column() processing_status: Mapped[str] = mapped_column() upload_time: Mapped[datetime] = mapped_column(default=datetime.now(UTC)) @@ -47,6 +50,7 @@ class XliffDocument(Base): cascade="all, delete-orphan", order_by="XliffRecord.id", ) + user: Mapped["User"] = relationship(back_populates="xliffs") class XliffRecord(Base): @@ -78,3 +82,10 @@ class User(Base): email: Mapped[str] = mapped_column(unique=True) role: Mapped[str] = mapped_column(default="user") disabled: Mapped[bool] = mapped_column(default=False) + + tmxs: Mapped[list["TmxDocument"]] = relationship( + back_populates="user", cascade="all, delete-orphan", order_by="TmxDocument.id" + ) + xliffs: Mapped[list["XliffDocument"]] = relationship( + back_populates="user", cascade="all, delete-orphan", order_by="XliffDocument.id" + ) From 022bece85d10ec64a536074caa773163d06fe3c1 Mon Sep 17 00:00:00 2001 From: Denis Bezykornov Date: Sun, 30 Jun 2024 21:46:23 +0300 Subject: [PATCH 3/5] Add user information on resource creation --- backend/app/routers/tmx.py | 11 ++++++++--- backend/app/routers/xliff.py | 7 +++++-- backend/tests/conftest.py | 11 +++++++++++ backend/tests/test_routes_auth.py | 2 ++ backend/tests/test_routes_tmx.py | 13 +++++++++---- backend/tests/test_routes_xliff.py | 27 +++++++++++++++++++++++---- backend/tests/test_tmx.py | 2 ++ backend/tests/test_worker.py | 29 ++++++++++++++++++++++------- backend/tests/test_xliff.py | 2 ++ backend/tests/test_yandex.py | 2 ++ 10 files changed, 86 insertions(+), 20 deletions(-) diff --git a/backend/app/routers/tmx.py b/backend/app/routers/tmx.py index 5e6e438..0559a33 100644 --- a/backend/app/routers/tmx.py +++ b/backend/app/routers/tmx.py @@ -4,7 +4,7 @@ from sqlalchemy.orm import Session from app import schema -from app.auth import has_user_role +from app.auth import has_user_role, get_current_user_id from app.db import get_db from app.tmx import extract_tmx_content from app.models import TmxFile, TmxFileWithRecords, TmxFileRecord, StatusMessage @@ -39,13 +39,18 @@ def get_tmx(tmx_id: int, db: Annotated[Session, Depends(get_db)]) -> TmxFileWith @router.post("/") async def create_tmx( - file: Annotated[UploadFile, File()], db: Annotated[Session, Depends(get_db)] + file: Annotated[UploadFile, File()], + db: Annotated[Session, Depends(get_db)], + current_user: Annotated[int, Depends(get_current_user_id)], ) -> TmxFile: name = file.filename tmx_data = await file.read() segments = extract_tmx_content(tmx_data) - doc = schema.TmxDocument(name=name) + doc = schema.TmxDocument( + name=name, + created_by=current_user, + ) db.add(doc) db.commit() diff --git a/backend/app/routers/xliff.py b/backend/app/routers/xliff.py index 7b5e03b..190f4e6 100644 --- a/backend/app/routers/xliff.py +++ b/backend/app/routers/xliff.py @@ -7,7 +7,7 @@ from sqlalchemy.orm import Session from app import schema, models -from app.auth import has_user_role +from app.auth import has_user_role, get_current_user_id from app.db import get_db from app.xliff import extract_xliff_content @@ -96,7 +96,9 @@ def delete_xliff( @router.post("/") async def create_xliff( - file: Annotated[UploadFile, File()], db: Annotated[Session, Depends(get_db)] + file: Annotated[UploadFile, File()], + db: Annotated[Session, Depends(get_db)], + current_user: Annotated[int, Depends(get_current_user_id)], ) -> models.XliffFile: cutoff_date = datetime.now() - timedelta(days=1) @@ -120,6 +122,7 @@ async def create_xliff( original_document=original_document, processing_status=models.DocumentStatus.UPLOADED.value, upload_time=datetime.now(), + created_by=current_user, ) db.add(doc) db.commit() diff --git a/backend/tests/conftest.py b/backend/tests/conftest.py index a654080..aa1ad3a 100644 --- a/backend/tests/conftest.py +++ b/backend/tests/conftest.py @@ -8,6 +8,8 @@ from app import create_app, db, schema, models from app.db import init_connection, get_db +# pylint: disable=C0116 + @contextmanager def session(): @@ -50,6 +52,15 @@ def user_logged_client(fastapi_client: TestClient): disabled=False, ) ) + s.add( + schema.User( + username="test-admin", + password="$pbkdf2-sha256$29000$R4gxRkjpnXNOqXXundP6Xw$pzr2kyXZjurvt6sUv7NF4dQhpHdv9RBtlGbOStnFyUM", + email="admin@test.com", + role=models.UserRole.ADMIN.value, + disabled=False, + ) + ) s.commit() fastapi_client.post( diff --git a/backend/tests/test_routes_auth.py b/backend/tests/test_routes_auth.py index fe2b0c8..638c323 100644 --- a/backend/tests/test_routes_auth.py +++ b/backend/tests/test_routes_auth.py @@ -6,6 +6,8 @@ from app import schema, models from app.db import get_db +# pylint: disable=C0116 + @contextmanager def session(): diff --git a/backend/tests/test_routes_tmx.py b/backend/tests/test_routes_tmx.py index 0d951a9..7181cb8 100644 --- a/backend/tests/test_routes_tmx.py +++ b/backend/tests/test_routes_tmx.py @@ -6,6 +6,8 @@ from app import schema from app.db import get_db +# pylint: disable=C0116 + @contextmanager def session(): @@ -14,8 +16,8 @@ def session(): def test_can_return_list_of_tmx_docs(user_logged_client: TestClient): with session() as s: - s.add(schema.TmxDocument(name="first_doc.tmx")) - s.add(schema.TmxDocument(name="another_doc.tmx")) + s.add(schema.TmxDocument(name="first_doc.tmx", created_by=1)) + s.add(schema.TmxDocument(name="another_doc.tmx", created_by=1)) s.commit() response = user_logged_client.get("/tmx/") @@ -38,7 +40,9 @@ def test_can_get_tmx_file(user_logged_client: TestClient): schema.TmxRecord(source="User Interface", target="UI"), ] with session() as s: - s.add(schema.TmxDocument(name="test_doc.tmx", records=tmx_records)) + s.add( + schema.TmxDocument(name="test_doc.tmx", records=tmx_records, created_by=1) + ) s.commit() response = user_logged_client.get("/tmx/1") @@ -68,7 +72,7 @@ def test_returns_404_when_tmx_file_not_found(user_logged_client: TestClient): def test_can_delete_tmx_doc(user_logged_client: TestClient): with session() as s: - s.add(schema.TmxDocument(name="first_doc.tmx")) + s.add(schema.TmxDocument(name="first_doc.tmx", created_by=1)) s.commit() response = user_logged_client.delete("/tmx/1") @@ -97,6 +101,7 @@ def test_can_upload_tmx(user_logged_client: TestClient): doc = s.query(schema.TmxDocument).filter_by(id=1).first() assert doc assert doc.name == "small.tmx" + assert doc.created_by == 1 assert len(doc.records) == 1 assert "Handbook" in doc.records[0].source assert doc.records[0].creation_date == datetime(2022, 7, 3, 7, 59, 19) diff --git a/backend/tests/test_routes_xliff.py b/backend/tests/test_routes_xliff.py index e34d647..2a6b3c9 100644 --- a/backend/tests/test_routes_xliff.py +++ b/backend/tests/test_routes_xliff.py @@ -7,6 +7,8 @@ from app import schema, models from app.db import get_db +# pylint: disable=C0116 + @contextmanager def session(): @@ -17,7 +19,10 @@ def test_can_get_list_of_xliff_docs(user_logged_client: TestClient): with session() as s: s.add( schema.XliffDocument( - name="first_doc.tmx", original_document="", processing_status="pending" + name="first_doc.tmx", + original_document="", + processing_status="pending", + created_by=1, ) ) s.add( @@ -25,6 +30,7 @@ def test_can_get_list_of_xliff_docs(user_logged_client: TestClient): name="another_doc.tmx", original_document="", processing_status="processing", + created_by=1, ) ) s.commit() @@ -51,6 +57,7 @@ def test_can_get_xliff_file(user_logged_client: TestClient): original_document="Something", records=xliff_records, processing_status="pending", + created_by=1, ) ) s.commit() @@ -78,6 +85,7 @@ def test_can_get_xliff_records(user_logged_client: TestClient): original_document="Something", records=xliff_records, processing_status="pending", + created_by=1, ) ) s.commit() @@ -109,7 +117,10 @@ def test_can_delete_xliff_doc(user_logged_client: TestClient): with session() as s: s.add( schema.XliffDocument( - name="first_doc.tmx", original_document="", processing_status="waiting" + name="first_doc.tmx", + original_document="", + processing_status="waiting", + created_by=1, ) ) s.commit() @@ -122,7 +133,9 @@ def test_can_delete_xliff_doc(user_logged_client: TestClient): assert s.query(schema.XliffDocument).count() == 0 -def test_returns_404_when_deleting_nonexistent_xliff_doc(user_logged_client: TestClient): +def test_returns_404_when_deleting_nonexistent_xliff_doc( + user_logged_client: TestClient, +): response = user_logged_client.delete("/xliff/1") assert response.status_code == 404 @@ -136,6 +149,7 @@ def test_upload(user_logged_client: TestClient): doc = s.query(schema.XliffDocument).filter_by(id=1).first() assert doc is not None assert doc.name == "small.xliff" + assert doc.created_by == 1 assert doc.processing_status == "uploaded" assert doc.original_document.startswith(" Date: Sun, 30 Jun 2024 23:28:16 +0300 Subject: [PATCH 4/5] Return information about who created a resource from backend --- backend/app/models.py | 2 ++ backend/app/routers/tmx.py | 7 +++++-- backend/app/routers/xliff.py | 3 +++ backend/app/schema.py | 4 ++-- backend/tests/test_routes_tmx.py | 3 +++ backend/tests/test_routes_xliff.py | 5 +++-- frontend/src/client/schemas/TmxFile.ts | 1 + frontend/src/client/schemas/TmxFileWithRecords.ts | 1 + frontend/src/client/schemas/XliffFile.ts | 1 + 9 files changed, 21 insertions(+), 6 deletions(-) diff --git a/backend/app/models.py b/backend/app/models.py index f059db1..0890559 100644 --- a/backend/app/models.py +++ b/backend/app/models.py @@ -33,6 +33,7 @@ class Identified(BaseModel): class TmxFile(Identified): name: str + created_by: int class TmxFileRecord(Identified): @@ -47,6 +48,7 @@ class TmxFileWithRecords(TmxFile): class XliffFile(Identified): name: str status: DocumentStatus + created_by: int class XliffFileRecord(Identified): diff --git a/backend/app/routers/tmx.py b/backend/app/routers/tmx.py index 0559a33..0ff5c65 100644 --- a/backend/app/routers/tmx.py +++ b/backend/app/routers/tmx.py @@ -16,7 +16,9 @@ @router.get("/") def get_tmxs(db: Annotated[Session, Depends(get_db)]) -> list[TmxFile]: docs = db.query(schema.TmxDocument).order_by(schema.TmxDocument.id).all() - return [TmxFile(id=doc.id, name=doc.name) for doc in docs] + return [ + TmxFile(id=doc.id, name=doc.name, created_by=doc.created_by) for doc in docs + ] @router.get("/{tmx_id}") @@ -30,6 +32,7 @@ def get_tmx(tmx_id: int, db: Annotated[Session, Depends(get_db)]) -> TmxFileWith return TmxFileWithRecords( id=doc.id, name=doc.name, + created_by=doc.created_by, records=[ TmxFileRecord(id=record.id, source=record.source, target=record.target) for record in doc.records @@ -70,7 +73,7 @@ async def create_tmx( ) assert new_doc - return TmxFile(id=new_doc.id, name=new_doc.name) + return TmxFile(id=new_doc.id, name=new_doc.name, created_by=doc.created_by) @router.delete("/{tmx_id}") diff --git a/backend/app/routers/xliff.py b/backend/app/routers/xliff.py index 190f4e6..56899eb 100644 --- a/backend/app/routers/xliff.py +++ b/backend/app/routers/xliff.py @@ -32,6 +32,7 @@ def get_xliffs(db: Annotated[Session, Depends(get_db)]) -> list[models.XliffFile id=xliff.id, name=xliff.name, status=models.DocumentStatus(xliff.processing_status), + created_by=xliff.created_by, ) for xliff in xliffs ] @@ -51,6 +52,7 @@ def get_xliff(doc_id: int, db: Annotated[Session, Depends(get_db)]) -> models.Xl id=doc.id, name=doc.name, status=models.DocumentStatus(doc.processing_status), + created_by=doc.created_by, ) @@ -134,6 +136,7 @@ async def create_xliff( id=new_doc.id, name=new_doc.name, status=models.DocumentStatus(new_doc.processing_status), + created_by=new_doc.created_by, ) diff --git a/backend/app/schema.py b/backend/app/schema.py index e0eebc4..61379b7 100644 --- a/backend/app/schema.py +++ b/backend/app/schema.py @@ -14,7 +14,7 @@ class TmxDocument(Base): id: Mapped[int] = mapped_column(primary_key=True) name: Mapped[str] = mapped_column() - created_by: Mapped[str] = mapped_column(ForeignKey("user.id")) + created_by: Mapped[int] = mapped_column(ForeignKey("user.id")) records: Mapped[list["TmxRecord"]] = relationship( back_populates="document", cascade="all, delete-orphan", order_by="TmxRecord.id" @@ -40,7 +40,7 @@ class XliffDocument(Base): id: Mapped[int] = mapped_column(primary_key=True) name: Mapped[str] = mapped_column() - created_by: Mapped[str] = mapped_column(ForeignKey("user.id")) + created_by: Mapped[int] = mapped_column(ForeignKey("user.id")) original_document: Mapped[str] = mapped_column() processing_status: Mapped[str] = mapped_column() upload_time: Mapped[datetime] = mapped_column(default=datetime.now(UTC)) diff --git a/backend/tests/test_routes_tmx.py b/backend/tests/test_routes_tmx.py index 7181cb8..a57d0cf 100644 --- a/backend/tests/test_routes_tmx.py +++ b/backend/tests/test_routes_tmx.py @@ -26,10 +26,12 @@ def test_can_return_list_of_tmx_docs(user_logged_client: TestClient): { "id": 1, "name": "first_doc.tmx", + "created_by": 1, }, { "id": 2, "name": "another_doc.tmx", + "created_by": 1, }, ] @@ -50,6 +52,7 @@ def test_can_get_tmx_file(user_logged_client: TestClient): assert response.json() == { "id": 1, "name": "test_doc.tmx", + "created_by": 1, "records": [ { "id": 1, diff --git a/backend/tests/test_routes_xliff.py b/backend/tests/test_routes_xliff.py index 2a6b3c9..8a45621 100644 --- a/backend/tests/test_routes_xliff.py +++ b/backend/tests/test_routes_xliff.py @@ -38,8 +38,8 @@ def test_can_get_list_of_xliff_docs(user_logged_client: TestClient): response = user_logged_client.get("/xliff") assert response.status_code == 200 assert response.json() == [ - {"id": 1, "name": "first_doc.tmx", "status": "pending"}, - {"id": 2, "name": "another_doc.tmx", "status": "processing"}, + {"id": 1, "name": "first_doc.tmx", "status": "pending", "created_by": 1}, + {"id": 2, "name": "another_doc.tmx", "status": "processing", "created_by": 1}, ] @@ -68,6 +68,7 @@ def test_can_get_xliff_file(user_logged_client: TestClient): "id": 1, "name": "test_doc.xliff", "status": "pending", + "created_by": 1, } diff --git a/frontend/src/client/schemas/TmxFile.ts b/frontend/src/client/schemas/TmxFile.ts index 13ca59f..bf62583 100644 --- a/frontend/src/client/schemas/TmxFile.ts +++ b/frontend/src/client/schemas/TmxFile.ts @@ -3,4 +3,5 @@ export interface TmxFile { id: number name: string + created_by: number } diff --git a/frontend/src/client/schemas/TmxFileWithRecords.ts b/frontend/src/client/schemas/TmxFileWithRecords.ts index 87dbd87..778b1b8 100644 --- a/frontend/src/client/schemas/TmxFileWithRecords.ts +++ b/frontend/src/client/schemas/TmxFileWithRecords.ts @@ -5,5 +5,6 @@ import {TmxFileRecord} from './TmxFileRecord' export interface TmxFileWithRecords { id: number name: string + created_by: number records: TmxFileRecord[] } diff --git a/frontend/src/client/schemas/XliffFile.ts b/frontend/src/client/schemas/XliffFile.ts index 54ae714..f2ecd6f 100644 --- a/frontend/src/client/schemas/XliffFile.ts +++ b/frontend/src/client/schemas/XliffFile.ts @@ -6,4 +6,5 @@ export interface XliffFile { id: number name: string status: DocumentStatus + created_by: number } From 61e9d6d20ca0286a291d40a13fb0408c5f7d892f Mon Sep 17 00:00:00 2001 From: Denis Bezykornov Date: Sun, 30 Jun 2024 23:28:21 +0300 Subject: [PATCH 5/5] Add tests --- backend/tests/test_routes_users.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 backend/tests/test_routes_users.py diff --git a/backend/tests/test_routes_users.py b/backend/tests/test_routes_users.py new file mode 100644 index 0000000..bb0e132 --- /dev/null +++ b/backend/tests/test_routes_users.py @@ -0,0 +1,30 @@ +from contextlib import contextmanager + +from fastapi.testclient import TestClient + +from app import models +from app.db import get_db + +# pylint: disable=C0116 + + +@contextmanager +def session(): + return get_db() + + +def test_can_get_current_user(user_logged_client: TestClient): + response = user_logged_client.get("/user/") + assert response.status_code == 200 + assert response.json() == { + "id": 1, + "username": "test", + "email": "test@test.com", + "role": models.UserRole.USER.value, + "disabled": False, + } + + +def test_cannot_get_current_user_for_non_logged_in(fastapi_client: TestClient): + response = fastapi_client.get("/user/") + assert response.status_code == 401