From 4ff319aba38862c21154f4b121619acd0b07b89b Mon Sep 17 00:00:00 2001 From: m-o-d-e-r Date: Sun, 25 Jun 2023 15:19:31 +0300 Subject: [PATCH 1/6] replaced str values of groups, statuses, and faculties with int-based values (id) --- Makefile | 3 ++ burrito/__main__.py | 7 +++ burrito/apps/admin/views.py | 10 ++-- burrito/apps/anon/views.py | 4 +- burrito/apps/meta/views.py | 6 --- burrito/apps/profile/utils.py | 9 +++- burrito/apps/profile/views.py | 2 +- burrito/apps/registration/views.py | 8 +-- burrito/apps/tickets/views.py | 16 +++--- burrito/schemas/admin_schema.py | 6 +-- burrito/schemas/anon_schema.py | 2 +- burrito/schemas/meta_schema.py | 2 +- burrito/schemas/profile_schema.py | 10 +++- burrito/schemas/registration_schema.py | 4 +- burrito/schemas/tickets_schema.py | 8 +-- burrito/utils/converter.py | 74 ++++++++++++++------------ burrito/utils/query_util.py | 8 +-- burrito/utils/users_util.py | 27 ++++++---- tests/admin_tests.py | 23 ++++++-- tests/anon_test.py | 2 +- tests/comments_test.py | 1 + tests/meta_test.py | 4 +- tests/profile_test.py | 8 +-- tests/registration_test.py | 7 +-- tests/run_tests.py | 2 +- tests/tickets_test.py | 14 +++-- 26 files changed, 156 insertions(+), 111 deletions(-) diff --git a/Makefile b/Makefile index d3305bfc..907401a2 100644 --- a/Makefile +++ b/Makefile @@ -36,3 +36,6 @@ _delete_docker_container: docker_run: _delete_docker_container Dockerfile docker run --env-file .env --name burrito_love burrito + +rmi: + docker rmi $(docker images -q) 2> /dev/null diff --git a/burrito/__main__.py b/burrito/__main__.py index 18f3cb79..0dae034f 100644 --- a/burrito/__main__.py +++ b/burrito/__main__.py @@ -19,6 +19,13 @@ init_manager.run_cycle() +#from burrito.utils.db_preprocessor import LocalDataBasePreprocessor +#db_preprocessor = LocalDataBasePreprocessor( +# {"filename": "./preprocessor_config.json"} +#) +#db_preprocessor.apply_data() + + if not init_manager.critical: import uvicorn from prometheus_fastapi_instrumentator import Instrumentator, metrics diff --git a/burrito/apps/admin/views.py b/burrito/apps/admin/views.py index 50c639e5..dc17da62 100644 --- a/burrito/apps/admin/views.py +++ b/burrito/apps/admin/views.py @@ -80,7 +80,7 @@ async def admin__update_ticket_data( ) ticket.faculty = faculty_object - queue_object = QueueStrToModel.convert(admin_updates.queue, admin_updates.faculty) + queue_object = QueueStrToModel.convert(admin_updates.queue) if admin_updates.queue else None if queue_object: # queue_id must be > 1 create_ticket_action( ticket_id=admin_updates.ticket_id, @@ -128,8 +128,8 @@ async def admin__get_ticket_list_by_filter( available_filters = { "hidden": q_is_hidden(filters.hidden), "anonymous": q_is_anonymous(filters.anonymous), - "faculty": q_is_valid_faculty(filters.faculty), - "queue": q_is_valid_queue(filters.queue, filters.faculty), + "faculty": q_is_valid_faculty(filters.faculty) if filters.faculty else None, + "queue": q_is_valid_queue(filters.queue) if filters.queue else None, "status": q_is_valid_status_list(filters.status) } final_filters = select_filters(available_filters, filters) @@ -328,12 +328,12 @@ async def admin__become_an_assignee( ticket_id=ticket_data.ticket_id, author_id=token_payload.user_id, field_name="assignee", - old_value=ticket.assignee.login, + old_value="None", new_value=current_admin.login ) ticket.assignee = current_admin - new_status = StatusStrToModel.convert("OPEN") + new_status = StatusStrToModel.convert(1) create_ticket_action( ticket_id=ticket_data.ticket_id, author_id=token_payload.user_id, diff --git a/burrito/apps/anon/views.py b/burrito/apps/anon/views.py index 7468a7ac..b0a72639 100644 --- a/burrito/apps/anon/views.py +++ b/burrito/apps/anon/views.py @@ -34,8 +34,8 @@ async def anon__get_ticket_list_by_filter(filters: AnonTicketListRequestSchema): available_filters = { "anonymous": q_is_anonymous(filters.anonymous), - "faculty": q_is_valid_faculty(filters.faculty), - "queue": q_is_valid_queue(filters.queue, filters.faculty), + "faculty": q_is_valid_faculty(filters.faculty) if filters.faculty else None, + "queue": q_is_valid_queue(filters.queue, filters.faculty) if filters.faculty and filters.faculty else None, "status": q_is_valid_status_list(filters.status) } final_filters = select_filters(available_filters, filters) + [ diff --git a/burrito/apps/meta/views.py b/burrito/apps/meta/views.py index c68dae57..8dd478f8 100644 --- a/burrito/apps/meta/views.py +++ b/burrito/apps/meta/views.py @@ -54,12 +54,6 @@ async def meta__faculties_list(): async def meta__get_queues_list(faculty_data: RequestQueueListSchema): faculty_object = FacultyStrToModel.convert(faculty_data.faculty) - if not faculty_object: - return JSONResponse( - status_code=status.HTTP_403_FORBIDDEN, - content={"detail": "Faculty name is wrong"} - ) - response_list: list[QueueResponseSchema] = [] for queue in Queues.select().where( Queues.faculty == faculty_object diff --git a/burrito/apps/profile/utils.py b/burrito/apps/profile/utils.py index de626f55..3b27ffa6 100644 --- a/burrito/apps/profile/utils.py +++ b/burrito/apps/profile/utils.py @@ -1,3 +1,5 @@ +from playhouse.shortcuts import model_to_dict + from burrito.utils.auth import get_auth_core from burrito.utils.users_util import get_user_by_id @@ -8,6 +10,9 @@ from burrito.models.group_model import Groups from burrito.schemas.profile_schema import ResponseProfileSchema +from burrito.schemas.faculty_schema import FacultyResponseSchema +from burrito.schemas.group_schema import GroupResponseSchema + __all__ = ( "get_auth_core", @@ -27,8 +32,8 @@ async def view_profile_by_user_id(user_id: int) -> ResponseProfileSchema | None: firstname=current_user.firstname, lastname=current_user.lastname, login=current_user.login, - faculty=faculty_object.name if faculty_object else None, - group=group_object.name if group_object else None, + faculty=FacultyResponseSchema(**model_to_dict(faculty_object)) if faculty_object else None, + group=GroupResponseSchema(**model_to_dict(group_object)) if group_object else None, phone=current_user.phone, email=current_user.email, registration_date=str(current_user.registration_date) diff --git a/burrito/apps/profile/views.py b/burrito/apps/profile/views.py index 9e9ba7d7..88576cd9 100644 --- a/burrito/apps/profile/views.py +++ b/burrito/apps/profile/views.py @@ -52,7 +52,7 @@ async def profile__check_by_id( @check_permission(permission_list={"UPDATE_PROFILE"}) async def profile__update_my_profile( - profile_updated_data: RequestUpdateProfileSchema, + profile_updated_data: RequestUpdateProfileSchema | None = RequestUpdateProfileSchema(), Authorize: AuthJWT = Depends(get_auth_core()) ): """Update profile data""" diff --git a/burrito/apps/registration/views.py b/burrito/apps/registration/views.py index ed18b7e3..61b01d38 100644 --- a/burrito/apps/registration/views.py +++ b/burrito/apps/registration/views.py @@ -7,6 +7,8 @@ from burrito.models.user_model import Users +from burrito.utils.converter import GroupStrToModel, FacultyStrToModel + from burrito.utils.auth import get_auth_core from burrito.utils.auth_token_util import ( AuthTokenPayload, @@ -47,8 +49,8 @@ async def registration__user_registration( current_user: Users | None = create_user_tmp_foo( user_data.login, get_hash(user_data.password), - user_data.group, - user_data.faculty + GroupStrToModel.convert(user_data.group), + FacultyStrToModel.convert(user_data.faculty) ) if current_user: @@ -75,5 +77,5 @@ async def registration__user_registration( return JSONResponse( status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, - content={"detail": "Group or faculty is not valid"} + content={"detail": "..."} ) diff --git a/burrito/apps/tickets/views.py b/burrito/apps/tickets/views.py index 36e4a270..8a36a7d0 100644 --- a/burrito/apps/tickets/views.py +++ b/burrito/apps/tickets/views.py @@ -53,7 +53,8 @@ from burrito.utils.logger import get_logger from burrito.utils.converter import ( FacultyStrToModel, - StatusStrToModel + StatusStrToModel, + QueueStrToModel ) from .utils import ( @@ -79,8 +80,7 @@ async def tickets__create_new_ticket( ) faculty_id = FacultyStrToModel.convert(ticket_creation_data.faculty) - - queue: Queues | None = Queues.get_or_none(Queues.queue_id == ticket_creation_data.queue) + queue: Queues = QueueStrToModel.convert(ticket_creation_data.queue) ticket: Tickets = Tickets.create( creator=token_payload.user_id, @@ -326,8 +326,8 @@ async def tickets__show_tickets_list_by_filter( "creator": q_is_creator(filters.creator), "hidden": q_is_hidden(filters.hidden), "anonymous": q_is_anonymous(filters.anonymous), - "faculty": q_is_valid_faculty(filters.faculty), - "queue": q_is_valid_queue(filters.queue, filters.faculty), + "faculty": q_is_valid_faculty(filters.faculty) if filters.faculty else None, + "queue": q_is_valid_queue(filters.queue) if filters.queue else None, "status": q_is_valid_status_list(filters.status) } final_filters = select_filters(available_filters, filters) + ( @@ -545,11 +545,11 @@ async def tickets__close_own_ticket( token_payload.user_id ) - status_name = "CLOSE" - status_object = StatusStrToModel.convert(status_name) + status_id = 6 + status_object = StatusStrToModel.convert(status_id) if not status_object: - get_logger().critical(f"Status {status_name} is not exist in database") + get_logger().critical(f"Status {status_id} is not exist in database") return JSONResponse( status_code=500, diff --git a/burrito/schemas/admin_schema.py b/burrito/schemas/admin_schema.py index b29e65c2..2000e429 100644 --- a/burrito/schemas/admin_schema.py +++ b/burrito/schemas/admin_schema.py @@ -10,9 +10,9 @@ class AdminUpdateTicketSchema(BaseModel): ticket_id: int - faculty: str | None - queue: str | None - status: str | None + faculty: int | None + queue: int | None + status: int | None class AdminGetTicketListSchema(BurritoPagination): diff --git a/burrito/schemas/anon_schema.py b/burrito/schemas/anon_schema.py index 3fa6fc54..7154bda8 100644 --- a/burrito/schemas/anon_schema.py +++ b/burrito/schemas/anon_schema.py @@ -10,7 +10,7 @@ class AnonTicketListRequestSchema(BurritoPagination): anonymous: bool | None faculty: str | None queue: str | None - status: list[str] | None + status: list[int] | None class AnonTicketUsersInfoSchema(BaseModel): diff --git a/burrito/schemas/meta_schema.py b/burrito/schemas/meta_schema.py index 50ed0973..4b31f247 100644 --- a/burrito/schemas/meta_schema.py +++ b/burrito/schemas/meta_schema.py @@ -19,7 +19,7 @@ class ResponseFacultiesListSchema(BaseModel): class RequestQueueListSchema(BaseModel): - faculty: str + faculty: int class ResponseQueueListSchema(BaseModel): diff --git a/burrito/schemas/profile_schema.py b/burrito/schemas/profile_schema.py index dc6a6eb9..9ef2d430 100644 --- a/burrito/schemas/profile_schema.py +++ b/burrito/schemas/profile_schema.py @@ -1,5 +1,8 @@ from pydantic import BaseModel +from burrito.schemas.faculty_schema import FacultyResponseSchema +from burrito.schemas.group_schema import GroupResponseSchema + class CheckProfileSchema(BaseModel): user_id: int | None @@ -28,8 +31,8 @@ class BaseProfile(BaseModel): login: str - faculty: str | None - group: str | None + faculty: int | None + group: int | None phone: str | None email: str | None @@ -44,6 +47,9 @@ class ResponseProfileSchema(BaseProfile): registration_date (str): date when user registered account """ + faculty: FacultyResponseSchema | None + group: GroupResponseSchema | None + registration_date: str diff --git a/burrito/schemas/registration_schema.py b/burrito/schemas/registration_schema.py index 6eaadebf..c9165a2b 100644 --- a/burrito/schemas/registration_schema.py +++ b/burrito/schemas/registration_schema.py @@ -4,5 +4,5 @@ class RegistrationSchema(BaseModel): login: str password: str - group: str - faculty: str + group: int + faculty: int diff --git a/burrito/schemas/tickets_schema.py b/burrito/schemas/tickets_schema.py index 9a127d28..63f3e030 100644 --- a/burrito/schemas/tickets_schema.py +++ b/burrito/schemas/tickets_schema.py @@ -14,7 +14,7 @@ class CreateTicketSchema(BaseModel): hidden: bool anonymous: bool queue: int | None - faculty: str + faculty: int class UpdateTicketSchema(BaseModel): @@ -29,9 +29,9 @@ class TicketListRequestSchema(BurritoPagination): creator: int | None hidden: bool | None anonymous: bool | None - faculty: str | None - queue: str | None - status: list[str] | None + faculty: int | None + queue: int | None + status: list[int] | None class TicketIDValueSchema(BaseModel): diff --git a/burrito/utils/converter.py b/burrito/utils/converter.py index b1d76661..775ea644 100644 --- a/burrito/utils/converter.py +++ b/burrito/utils/converter.py @@ -1,5 +1,7 @@ from cachetools import cached +from fastapi import HTTPException + from burrito.models.group_model import Groups from burrito.models.faculty_model import Faculties from burrito.models.queues_model import Queues @@ -8,93 +10,99 @@ from burrito.utils.cache_util import BurritoCache +def _raise_converter_error(detail) -> None: + raise HTTPException( + status_code=422, + detail=detail + ) + + class Converter: @staticmethod - def _is_empty(str_value: str | None) -> bool: - return not bool(str_value) + def _is_empty(int_value: int | None, error_detail: str = "") -> None: + if not isinstance(int_value, int) or int_value <= 0: + _raise_converter_error(error_detail) @staticmethod - def convert(str_value: str | None): + def convert(int_value: int | None): raise NotImplementedError("This is an abstract method") class GroupStrToModel(Converter): @staticmethod - @cached(BurritoCache()) - def convert(str_value: str | None) -> Groups | None: + def convert(int_value: int | None) -> Groups | None: """_summary_ Args: - str_value (str): group name + int_value (int): group id Returns: Groups | None: group object or None value """ + GroupStrToModel._is_empty(int_value, f"Group {int_value} is invalid") - if GroupStrToModel._is_empty(str_value): - return None + group_object = Groups.get_or_none(Groups.group_id == int_value) + if not group_object: + _raise_converter_error(f"Group {int_value} is not found") - return Groups.get_or_none(Groups.name == str_value) + return group_object class FacultyStrToModel(Converter): @staticmethod - @cached(BurritoCache()) - def convert(str_value: str | None) -> Faculties | None: + def convert(int_value: int | None) -> Faculties | None: """_summary_ Args: - str_value (str): faculty name + int_value (int): faculty id Returns: Faculties | None: faculty object or None value """ + FacultyStrToModel._is_empty(int_value, f"Faculty {int_value} is invalid") - if FacultyStrToModel._is_empty(str_value): - return None + faculty_object = Faculties.get_or_none(Faculties.faculty_id == int_value) + if not faculty_object: + _raise_converter_error(f"Faculty {int_value} is not found") - return Faculties.get_or_none(Faculties.name == str_value) + return faculty_object class QueueStrToModel(Converter): @staticmethod - @cached(BurritoCache()) - def convert(str_value: str | None, faculty: Faculties = None) -> Queues | None: + def convert(int_value: int | None) -> Queues | None: """_summary_ Args: - str_value (str): queue name + int_value (int): queue id Returns: Queues | None: queue object or None value """ + QueueStrToModel._is_empty(int_value, f"Queue {int_value} is invalid") - if QueueStrToModel._is_empty(str_value): - return None - - if not faculty: - return None + queue_object = Queues.get_or_none(Queues.queue_id == int_value) + if not queue_object: + _raise_converter_error(f"Queue {int_value} is not found") - return Queues.get_or_none( - Queues.name == str_value, - Queues.faculty == faculty - ) + return queue_object class StatusStrToModel(Converter): @staticmethod - @cached(BurritoCache()) - def convert(str_value: str | None) -> Statuses | None: + def convert(int_value: int | None) -> Statuses | None: """_summary_ Args: - str_value (str): 'status' name + int_value (int): 'status' id Returns: Statuses | None: 'status' object or None value """ + StatusStrToModel._is_empty(int_value, "Status is invalid") - if StatusStrToModel._is_empty(str_value): - return None + status_object = Statuses.get_or_none(Statuses.status_id == int_value) + if not status_object: + _raise_converter_error("Status is invalid") - return Statuses.get_or_none(Statuses.name == str_value) + return status_object diff --git a/burrito/utils/query_util.py b/burrito/utils/query_util.py index f4c97342..ab60b2f8 100644 --- a/burrito/utils/query_util.py +++ b/burrito/utils/query_util.py @@ -12,7 +12,7 @@ _PROTECTED_STATUSES: tuple[int] = ( - StatusStrToModel.convert("NEW").status_id, + StatusStrToModel.convert(1).status_id, ) STATUSES_FOR_USER: list[int] = [] @@ -47,7 +47,7 @@ def q_protected_statuses() -> Expression: return Tickets.status.not_in(_PROTECTED_STATUSES) -def q_is_valid_faculty(value: str) -> Expression: +def q_is_valid_faculty(value: int) -> Expression: return Tickets.faculty == FacultyStrToModel.convert(value) @@ -55,8 +55,8 @@ def q_is_valid_queue(queue: str, faculty: str) -> Expression: return Tickets.queue == QueueStrToModel.convert(queue, faculty) -def q_is_valid_status(value: str) -> Expression: - return Tickets.status == StatusStrToModel.convert(value) +def q_is_valid_status(value: int) -> Expression: + return Tickets.status == value def q_is_valid_status_list(values: list[str]) -> Expression | None: diff --git a/burrito/utils/users_util.py b/burrito/utils/users_util.py index dfff97fd..680053fd 100644 --- a/burrito/utils/users_util.py +++ b/burrito/utils/users_util.py @@ -5,11 +5,13 @@ from burrito.models.roles_model import Roles from burrito.models.user_model import Users +from burrito.models.group_model import Groups +from burrito.models.faculty_model import Faculties def create_user_tmp_foo( login: str, hashed_password: str, - group: str, faculty: str + group: Groups, faculty: Faculties ) -> Users | None: """_summary_ @@ -23,22 +25,27 @@ def create_user_tmp_foo( bool: status creating new user """ - try: - group_id = GroupStrToModel.convert(group) - faculty_id = FacultyStrToModel.convert(faculty) - - if not (group_id and faculty_id): - return + role_object: Roles = Roles.get(Roles.name == "ALL") + try: user: Users = Users.create( login=login, password=hashed_password, - group=group_id, - faculty=faculty_id, - role=Roles.get(Roles.name == "ALL") + group=group, + faculty=faculty, + role=role_object ) return user except Exception as e: # pylint: disable=broad-except, invalid-name + get_logger().info( + f""" + login: {login} + group: {group.name} + faculty: {faculty.name} + role: {role_object.name} + + """ + ) get_logger().error(e) diff --git a/tests/admin_tests.py b/tests/admin_tests.py index dae18ccd..9134c4c4 100644 --- a/tests/admin_tests.py +++ b/tests/admin_tests.py @@ -22,9 +22,9 @@ def test_001_admin_update_ticket(self): json={ "ticket_id": TicketsTestCase.first_ticket, "status": random.choice( - ["ACCEPTED", "OPEN", "WAITING", "REJECTED", "CLOSE"] + [1, 2, 3, 4, 5] ), - "faculty": "Biem" + "faculty": 1 }, timeout=TIMEOUT ) @@ -41,7 +41,7 @@ def test_002_admin_ticket_list_view(self): "Authorization": f"Bearer {AuthTestCase.access_token}" }, json={ - "status": ["OPEN"], + "status": [1], # "hidden": True, # "anonymous": True }, @@ -87,3 +87,20 @@ def test_004_admin_delete_ticket(self): response.status_code, 200 ) + + def test_005_admin_become_assignee(self): + response = requests.post( + f"http://{get_config().BURRITO_HOST}:{get_config().BURRITO_PORT}/admin/tickets/become_assignee", + headers={ + "Authorization": f"Bearer {AuthTestCase.access_token}" + }, + json={ + "ticket_id": TicketsTestCase.first_ticket + }, + timeout=TIMEOUT + ) + + self.assertEqual( + response.status_code, + 200 + ) diff --git a/tests/anon_test.py b/tests/anon_test.py index 1e224c61..410af681 100644 --- a/tests/anon_test.py +++ b/tests/anon_test.py @@ -12,7 +12,7 @@ def test_anon_tickets_list_filter(self): response = requests.post( f"http://{get_config().BURRITO_HOST}:{get_config().BURRITO_PORT}/anon/ticket_list", json={ - "status": ["OPEN", "WAITING"], + "status": [1, 2], # "anonymous": True }, timeout=TIMEOUT diff --git a/tests/comments_test.py b/tests/comments_test.py index f1cffaec..184b1d71 100644 --- a/tests/comments_test.py +++ b/tests/comments_test.py @@ -28,6 +28,7 @@ def test_001_comments_create(self): response.status_code, 200 ) + return response.json()["comment_id"] def test_002_comments_edit(self): diff --git a/tests/meta_test.py b/tests/meta_test.py index ae87784b..1c20faa0 100644 --- a/tests/meta_test.py +++ b/tests/meta_test.py @@ -45,7 +45,7 @@ def test_queues_list(self): response = requests.post( f"http://{get_config().BURRITO_HOST}:{get_config().BURRITO_PORT}/meta/get_queues", json={ - "faculty": "EliT" + "faculty": 1 }, timeout=TIMEOUT ) @@ -66,5 +66,5 @@ def test_queues_list_with_wrong_faculty(self): self.assertEqual( response.status_code, - 403 + 422 ) diff --git a/tests/profile_test.py b/tests/profile_test.py index 4f67fe90..65c5379b 100644 --- a/tests/profile_test.py +++ b/tests/profile_test.py @@ -16,9 +16,6 @@ def test_view_profile_noexist(self): response = requests.get( f"http://{get_config().BURRITO_HOST}:{get_config().BURRITO_PORT}/profile/1000000", - json={ - "user_id": 1000000 - }, timeout=0.5 ) @@ -90,7 +87,6 @@ def test_update_profile_without_auth(self): response = requests.post( f"http://{get_config().BURRITO_HOST}:{get_config().BURRITO_PORT}/profile/update", - json={}, timeout=0.5 ) @@ -112,8 +108,8 @@ def test_update_profile_with_auth(self): "lastname": "".join(random.sample(string.ascii_letters, 5)) if random.randint(0, 10) % 2 == 0 else None, "email": "".join(random.sample(string.ascii_letters, 5)) if random.randint(0, 10) % 2 == 0 else None, "phone": "".join(random.sample(string.ascii_letters, 5)) if random.randint(0, 10) % 2 == 0 else None, - "faculty": random.choice(["EliT", "Biem"]), - "group": random.choice(["IT-11", "LOL-11"]), + "faculty": random.choice([1, 2]), + "group": random.choice([1, 2]), }, timeout=0.5 ) diff --git a/tests/registration_test.py b/tests/registration_test.py index 7efb0caf..80eded66 100644 --- a/tests/registration_test.py +++ b/tests/registration_test.py @@ -12,8 +12,8 @@ def make_user_registration( login: str = "".join(random.sample(string.ascii_letters, 5)), password: str = "".join(random.sample(string.ascii_letters, 8)), - group: str = random.choice(["IT-11", "LOL-11"]), - faculty: str = random.choice(["EliT", "Biem"]) + group: int = random.choice([1, 2]), + faculty: int = random.choice([1, 2]) ): response = requests.post( f"http://{get_config().BURRITO_HOST}:{get_config().BURRITO_PORT}/registration/", @@ -25,6 +25,7 @@ def make_user_registration( }, timeout=TIMEOUT ) + if response.json().get("user_id"): return response.json().get("user_id") @@ -50,7 +51,7 @@ def test_do_registration(self): user_data = make_user_registration( login=RegistrationTestCase.random_login, - password=RegistrationTestCase.random_password + password=RegistrationTestCase.random_password, ) self.assertIsInstance(user_data, int) diff --git a/tests/run_tests.py b/tests/run_tests.py index 51b9c590..4bc92171 100644 --- a/tests/run_tests.py +++ b/tests/run_tests.py @@ -28,7 +28,7 @@ unittest.TestLoader().loadTestsFromTestCase(AdminTestCase), unittest.TestLoader().loadTestsFromTestCase(AnonTestCase), unittest.TestLoader().loadTestsFromTestCase(MetaTestCase), - #unittest.TestLoader().loadTestsFromTestCase(IOFilesTestCase) +# #unittest.TestLoader().loadTestsFromTestCase(IOFilesTestCase) unittest.TestLoader().loadTestsFromTestCase(CommentsTestCase) ] ) diff --git a/tests/tickets_test.py b/tests/tickets_test.py index dea55441..1c97c1f2 100644 --- a/tests/tickets_test.py +++ b/tests/tickets_test.py @@ -24,7 +24,7 @@ def create_ticket_get_id(subject: str) -> int: "hidden": True if random.randint(0, 9) % 2 == 0 else False, "anonymous": True if random.randint(0, 9) % 2 == 0 else False, "queue": 1, - "faculty": random.choice(["EliT", "Biem"]), + "faculty": 1, }, timeout=TIMEOUT ).json()["ticket_id"] @@ -49,7 +49,7 @@ def test_001_create_ticket(self): "hidden": True if random.randint(0, 9) % 2 == 0 else False, "anonymous": True if random.randint(0, 9) % 2 == 0 else False, "queue": 1, - "faculty": random.choice(["EliT", "Biem"]), + "faculty": 1, }, timeout=TIMEOUT ) @@ -115,7 +115,7 @@ def test_004_like_ticket(self): "hidden": False, "anonymous": True if random.randint(0, 9) % 2 == 0 else False, "queue": 1, - "faculty": random.choice(["EliT", "Biem"]), + "faculty": random.choice([1, 2]), }, timeout=TIMEOUT ).json()["ticket_id"] @@ -169,7 +169,7 @@ def test_006_bookmark_ticket(self): "hidden": False, "anonymous": True if random.randint(0, 9) % 2 == 0 else False, "queue": 1, - "faculty": random.choice(["EliT", "Biem"]), + "faculty": random.choice([1, 2]), }, timeout=TIMEOUT ).json()["ticket_id"] @@ -216,9 +216,7 @@ def test_008_tickets_filter(self): "Authorization": f"Bearer {AuthTestCase.access_token}" }, json={ - "status": ["OPEN", "WAITING"] -# "anonymous": True, -# "hidden": False, + "status": [1, 2, 3] }, timeout=TIMEOUT ) @@ -240,7 +238,7 @@ def test_009_ticket_detail_view(self): "hidden": False, "anonymous": False, "queue": 1, - "faculty": "EliT", + "faculty": 1, }, timeout=TIMEOUT ).json()["ticket_id"] From 27741e4907c04cd75a1791fb1368feab0fee132b Mon Sep 17 00:00:00 2001 From: m-o-d-e-r Date: Sun, 25 Jun 2023 16:16:31 +0300 Subject: [PATCH 2/6] added function for pretty output when an error occurs in the tests --- tests/about_test.py | 31 +++++--- tests/admin_tests.py | 51 ++++++++---- tests/anon_test.py | 11 ++- tests/auth_test.py | 37 ++++++++- tests/comments_test.py | 33 +++++--- tests/meta_test.py | 51 ++++++++---- tests/profile_test.py | 51 ++++++++---- tests/registration_test.py | 50 ++++++++++-- tests/tickets_test.py | 141 +++++++++++++++++++++++---------- tests/utils/exceptions_tool.py | 16 ++++ 10 files changed, 353 insertions(+), 119 deletions(-) create mode 100644 tests/utils/exceptions_tool.py diff --git a/tests/about_test.py b/tests/about_test.py index 2766580a..8871a8e1 100644 --- a/tests/about_test.py +++ b/tests/about_test.py @@ -2,6 +2,7 @@ import requests from burrito.utils.config_reader import get_config +from utils.exceptions_tool import check_error TIMEOUT = 5 @@ -14,9 +15,13 @@ def test_check_version(self): timeout=TIMEOUT ) - self.assertEqual( - response.status_code, - 200 + check_error( + self.assertEqual, + { + "first": response.status_code, + "second": 200 + }, + response ) def test_check_updates(self): @@ -25,9 +30,13 @@ def test_check_updates(self): timeout=TIMEOUT ) - self.assertEqual( - response.status_code, - 200 + check_error( + self.assertEqual, + { + "first": response.status_code, + "second": 200 + }, + response ) def test_check_team(self): @@ -36,7 +45,11 @@ def test_check_team(self): timeout=TIMEOUT ) - self.assertEqual( - response.status_code, - 200 + check_error( + self.assertEqual, + { + "first": response.status_code, + "second": 200 + }, + response ) diff --git a/tests/admin_tests.py b/tests/admin_tests.py index 9134c4c4..dc93fded 100644 --- a/tests/admin_tests.py +++ b/tests/admin_tests.py @@ -7,6 +7,7 @@ from tickets_test import TicketsTestCase from burrito.utils.config_reader import get_config +from utils.exceptions_tool import check_error TIMEOUT = 10 @@ -29,9 +30,13 @@ def test_001_admin_update_ticket(self): timeout=TIMEOUT ) - self.assertEqual( - response.status_code, - 200 + check_error( + self.assertEqual, + { + "first": response.status_code, + "second": 200 + }, + response ) def test_002_admin_ticket_list_view(self): @@ -48,9 +53,13 @@ def test_002_admin_ticket_list_view(self): timeout=TIMEOUT ) - self.assertEqual( - response.status_code, - 200 + check_error( + self.assertEqual, + { + "first": response.status_code, + "second": 200 + }, + response ) def test_003_admin_ticket_detail_view(self): @@ -65,9 +74,13 @@ def test_003_admin_ticket_detail_view(self): timeout=TIMEOUT ) - self.assertEqual( - response.status_code, - 200 + check_error( + self.assertEqual, + { + "first": response.status_code, + "second": 200 + }, + response ) @unittest.skip @@ -83,9 +96,13 @@ def test_004_admin_delete_ticket(self): timeout=TIMEOUT ) - self.assertEqual( - response.status_code, - 200 + check_error( + self.assertEqual, + { + "first": response.status_code, + "second": 200 + }, + response ) def test_005_admin_become_assignee(self): @@ -100,7 +117,11 @@ def test_005_admin_become_assignee(self): timeout=TIMEOUT ) - self.assertEqual( - response.status_code, - 200 + check_error( + self.assertEqual, + { + "first": response.status_code, + "second": 200 + }, + response ) diff --git a/tests/anon_test.py b/tests/anon_test.py index 410af681..6b5b5cd6 100644 --- a/tests/anon_test.py +++ b/tests/anon_test.py @@ -2,6 +2,7 @@ import requests from burrito.utils.config_reader import get_config +from utils.exceptions_tool import check_error TIMEOUT = 5 @@ -18,7 +19,11 @@ def test_anon_tickets_list_filter(self): timeout=TIMEOUT ) - self.assertEqual( - response.status_code, - 200 + check_error( + self.assertEqual, + { + "first": response.status_code, + "second": 200 + }, + response ) diff --git a/tests/auth_test.py b/tests/auth_test.py index f50179dc..c52fac8d 100644 --- a/tests/auth_test.py +++ b/tests/auth_test.py @@ -4,6 +4,7 @@ from registration_test import RegistrationTestCase from burrito.utils.config_reader import get_config +from utils.exceptions_tool import check_error def do_auth(): @@ -36,8 +37,22 @@ def test_do_password_auth(self): access_token = result[1].get("access_token") AuthTestCase.access_token = access_token - self.assertEqual(result[0], 200) - self.assertIsNotNone(access_token) + check_error( + self.assertEqual, + { + "first": result[0], + "second": 200 + }, + result[1] + ) + + check_error( + self.assertIsNotNone, + { + "obj": access_token + }, + result[1] + ) def test_do_token_auth(self): """ @@ -55,7 +70,14 @@ def test_do_token_auth(self): timeout=5 ) - self.assertEqual(response.status_code, 200) + check_error( + self.assertEqual, + { + "first": response.status_code, + "second": 200 + }, + response + ) def test_do_token_auth_with_old_token(self): """ @@ -75,4 +97,11 @@ def test_do_token_auth_with_old_token(self): timeout=5 ) - self.assertEqual(response.status_code, 200) + check_error( + self.assertEqual, + { + "first": response.status_code, + "second": 200 + }, + response + ) diff --git a/tests/comments_test.py b/tests/comments_test.py index 184b1d71..eb57d287 100644 --- a/tests/comments_test.py +++ b/tests/comments_test.py @@ -5,6 +5,7 @@ from auth_test import AuthTestCase from tickets_test import create_ticket_get_id +from utils.exceptions_tool import check_error TIMEOUT = 5 @@ -24,9 +25,13 @@ def test_001_comments_create(self): timeout=TIMEOUT ) - self.assertEqual( - response.status_code, - 200 + check_error( + self.assertEqual, + { + "first": response.status_code, + "second": 200 + }, + response ) return response.json()["comment_id"] @@ -44,9 +49,13 @@ def test_002_comments_edit(self): timeout=TIMEOUT ) - self.assertEqual( - response.status_code, - 200 + check_error( + self.assertEqual, + { + "first": response.status_code, + "second": 200 + }, + response ) def test_003_comments_delete(self): @@ -60,8 +69,12 @@ def test_003_comments_delete(self): }, timeout=TIMEOUT ) - - self.assertEqual( - response.status_code, - 200 + + check_error( + self.assertEqual, + { + "first": response.status_code, + "second": 200 + }, + response ) diff --git a/tests/meta_test.py b/tests/meta_test.py index 1c20faa0..7fbd68a1 100644 --- a/tests/meta_test.py +++ b/tests/meta_test.py @@ -2,6 +2,7 @@ import requests from burrito.utils.config_reader import get_config +from utils.exceptions_tool import check_error TIMEOUT = 5 @@ -14,9 +15,13 @@ def test_get_statuses_list(self): timeout=TIMEOUT ) - self.assertEqual( - response.status_code, - 200 + check_error( + self.assertEqual, + { + "first": response.status_code, + "second": 200 + }, + response ) def test_groups_list(self): @@ -25,9 +30,13 @@ def test_groups_list(self): timeout=TIMEOUT ) - self.assertEqual( - response.status_code, - 200 + check_error( + self.assertEqual, + { + "first": response.status_code, + "second": 200 + }, + response ) def test_faculties_list(self): @@ -36,9 +45,13 @@ def test_faculties_list(self): timeout=TIMEOUT ) - self.assertEqual( - response.status_code, - 200 + check_error( + self.assertEqual, + { + "first": response.status_code, + "second": 200 + }, + response ) def test_queues_list(self): @@ -50,9 +63,13 @@ def test_queues_list(self): timeout=TIMEOUT ) - self.assertEqual( - response.status_code, - 200 + check_error( + self.assertEqual, + { + "first": response.status_code, + "second": 200 + }, + response ) def test_queues_list_with_wrong_faculty(self): @@ -64,7 +81,11 @@ def test_queues_list_with_wrong_faculty(self): timeout=TIMEOUT ) - self.assertEqual( - response.status_code, - 422 + check_error( + self.assertEqual, + { + "first": response.status_code, + "second": 422 + }, + response ) diff --git a/tests/profile_test.py b/tests/profile_test.py index 65c5379b..e021f9ca 100644 --- a/tests/profile_test.py +++ b/tests/profile_test.py @@ -8,6 +8,7 @@ from registration_test import RegistrationTestCase from burrito.utils.config_reader import get_config +from utils.exceptions_tool import check_error class ProfileTestCase(unittest.TestCase): @@ -19,9 +20,13 @@ def test_view_profile_noexist(self): timeout=0.5 ) - self.assertEqual( - response.status_code, - 404 + check_error( + self.assertEqual, + { + "first": response.status_code, + "second": 404 + }, + response ) def test_view_profile_without_auth_with_id(self): @@ -32,9 +37,13 @@ def test_view_profile_without_auth_with_id(self): timeout=0.5 ) - self.assertEqual( - response.status_code, - 200 + check_error( + self.assertEqual, + { + "first": response.status_code, + "second": 200 + }, + response ) def test_view_profile_without_auth_without_id(self): @@ -61,9 +70,13 @@ def test_view_profile_with_auth_with_id(self): timeout=0.5 ) - self.assertEqual( - response.status_code, - 200 + check_error( + self.assertEqual, + { + "first": response.status_code, + "second": 200 + }, + response ) def test_view_profile_with_auth_without_id(self): @@ -77,9 +90,13 @@ def test_view_profile_with_auth_without_id(self): timeout=0.5 ) - self.assertEqual( - response.status_code, - 200 + check_error( + self.assertEqual, + { + "first": response.status_code, + "second": 200 + }, + response ) def test_update_profile_without_auth(self): @@ -114,7 +131,11 @@ def test_update_profile_with_auth(self): timeout=0.5 ) - self.assertEqual( - response.status_code, - 200 + check_error( + self.assertEqual, + { + "first": response.status_code, + "second": 200 + }, + response ) diff --git a/tests/registration_test.py b/tests/registration_test.py index 80eded66..136bd1f4 100644 --- a/tests/registration_test.py +++ b/tests/registration_test.py @@ -4,6 +4,7 @@ import requests from burrito.utils.config_reader import get_config +from utils.exceptions_tool import check_error TIMEOUT = 5 @@ -54,7 +55,14 @@ def test_do_registration(self): password=RegistrationTestCase.random_password, ) - self.assertIsInstance(user_data, int) + check_error( + self.assertIsInstance, + { + "obj": user_data, + "cls": int + } + ) + RegistrationTestCase.user_id = user_data def test_do_registration_with_invalid_login(self): @@ -65,7 +73,13 @@ def test_do_registration_with_invalid_login(self): password=RegistrationTestCase.random_password ) - self.assertEqual(user_data.status_code, 422) + check_error( + self.assertEqual, + { + "first": user_data.status_code, + "second": 422 + } + ) def test_do_registration_with_invalid_password(self): """make registration with invalid datas""" @@ -75,7 +89,13 @@ def test_do_registration_with_invalid_password(self): password="." ) - self.assertEqual(user_data.status_code, 422) + check_error( + self.assertEqual, + { + "first": user_data.status_code, + "second": 422 + } + ) def test_do_registration_with_invalid_group(self): """make registration with invalid datas""" @@ -84,7 +104,13 @@ def test_do_registration_with_invalid_group(self): group="hello_man_11" ) - self.assertEqual(user_data.status_code, 422) + check_error( + self.assertEqual, + { + "first": user_data.status_code, + "second": 422 + } + ) def test_do_registration_with_invalid_faculty(self): """make registration with invalid datas""" @@ -93,7 +119,13 @@ def test_do_registration_with_invalid_faculty(self): faculty="hello_man_11" ) - self.assertEqual(user_data.status_code, 422) + check_error( + self.assertEqual, + { + "first": user_data.status_code, + "second": 422 + } + ) def test_do_registration_with_the_same_login(self): """Test case when users try to register with the existent login""" @@ -103,4 +135,10 @@ def test_do_registration_with_the_same_login(self): password=RegistrationTestCase.random_password ) - self.assertEqual(user_data.status_code, 422) + check_error( + self.assertEqual, + { + "first": user_data.status_code, + "second": 422 + } + ) diff --git a/tests/tickets_test.py b/tests/tickets_test.py index 1c97c1f2..ebd34c62 100644 --- a/tests/tickets_test.py +++ b/tests/tickets_test.py @@ -7,6 +7,7 @@ from auth_test import AuthTestCase from burrito.utils.config_reader import get_config +from utils.exceptions_tool import check_error TIMEOUT = 10 @@ -54,9 +55,13 @@ def test_001_create_ticket(self): timeout=TIMEOUT ) - self.assertEqual( - response.status_code, - 200 + check_error( + self.assertEqual, + { + "first": response.status_code, + "second": 200 + }, + response ) TicketsTestCase.first_ticket = response.json()["ticket_id"] @@ -77,9 +82,13 @@ def test_002_delete_ticket(self): timeout=TIMEOUT ) - self.assertEqual( - response.status_code, - 200 + check_error( + self.assertEqual, + { + "first": response.status_code, + "second": 200 + }, + response ) def test_003_delete_ticket_noexist(self): @@ -96,9 +105,13 @@ def test_003_delete_ticket_noexist(self): timeout=TIMEOUT ) - self.assertEqual( - response.status_code, - 403 + check_error( + self.assertEqual, + { + "first": response.status_code, + "second": 403 + }, + response ) def test_004_like_ticket(self): @@ -131,9 +144,13 @@ def test_004_like_ticket(self): timeout=TIMEOUT ) - self.assertEqual( - response.status_code, - 200 + check_error( + self.assertEqual, + { + "first": response.status_code, + "second": 200 + }, + response ) def test_005_like_ticket_noexist(self): @@ -150,9 +167,13 @@ def test_005_like_ticket_noexist(self): timeout=TIMEOUT ) - self.assertEqual( - response.status_code, - 403 + check_error( + self.assertEqual, + { + "first": response.status_code, + "second": 403 + }, + response ) def test_006_bookmark_ticket(self): @@ -185,9 +206,13 @@ def test_006_bookmark_ticket(self): timeout=TIMEOUT ) - self.assertEqual( - response.status_code, - 200 + check_error( + self.assertEqual, + { + "first": response.status_code, + "second": 200 + }, + response ) def test_007_bookmark_ticket_noexist(self): @@ -204,9 +229,13 @@ def test_007_bookmark_ticket_noexist(self): timeout=TIMEOUT ) - self.assertEqual( - response.status_code, - 403 + check_error( + self.assertEqual, + { + "first": response.status_code, + "second": 403 + }, + response ) def test_008_tickets_filter(self): @@ -221,9 +250,13 @@ def test_008_tickets_filter(self): timeout=TIMEOUT ) - self.assertEqual( - response.status_code, - 200 + check_error( + self.assertEqual, + { + "first": response.status_code, + "second": 200 + }, + response ) def test_009_ticket_detail_view(self): @@ -254,9 +287,13 @@ def test_009_ticket_detail_view(self): timeout=TIMEOUT ) - self.assertEqual( - response.status_code, - 200 + check_error( + self.assertEqual, + { + "first": response.status_code, + "second": 200 + }, + response ) def test_010_update_ticket(self): @@ -278,9 +315,13 @@ def test_010_update_ticket(self): timeout=TIMEOUT ) - self.assertEqual( - response.status_code, - 200 + check_error( + self.assertEqual, + { + "first": response.status_code, + "second": 200 + }, + response ) def test_011_close_ticket(self): @@ -297,9 +338,13 @@ def test_011_close_ticket(self): timeout=TIMEOUT ) - self.assertEqual( - response.status_code, - 200 + check_error( + self.assertEqual, + { + "first": response.status_code, + "second": 200 + }, + response ) def test_012_get_liked_ticket(self): @@ -311,9 +356,13 @@ def test_012_get_liked_ticket(self): timeout=TIMEOUT ) - self.assertEqual( - response.status_code, - 200 + check_error( + self.assertEqual, + { + "first": response.status_code, + "second": 200 + }, + response ) def test_013_get_bookmarked_ticket(self): @@ -325,9 +374,13 @@ def test_013_get_bookmarked_ticket(self): timeout=TIMEOUT ) - self.assertEqual( - response.status_code, - 200 + check_error( + self.assertEqual, + { + "first": response.status_code, + "second": 200 + }, + response ) def test_014_get_deleted_ticket(self): @@ -339,7 +392,11 @@ def test_014_get_deleted_ticket(self): timeout=TIMEOUT ) - self.assertEqual( - response.status_code, - 200 + check_error( + self.assertEqual, + { + "first": response.status_code, + "second": 200 + }, + response ) diff --git a/tests/utils/exceptions_tool.py b/tests/utils/exceptions_tool.py new file mode 100644 index 00000000..077c5a2f --- /dev/null +++ b/tests/utils/exceptions_tool.py @@ -0,0 +1,16 @@ +import inspect +from pprint import pprint + + +def check_error(foo, foo_args: dict, response = None): + try: + foo(**foo_args) + except Exception as e: + pprint( + { + "name_of_test": inspect.stack()[1][3], + "response": response.json() if response else response, + "exception": e, + } + ) + print() From ded91d7b6a077b90bbe6ebcdd03cbc68a9b23e52 Mon Sep 17 00:00:00 2001 From: m-o-d-e-r Date: Sun, 25 Jun 2023 16:24:34 +0300 Subject: [PATCH 3/6] renamed converters --- burrito/apps/admin/views.py | 14 +++++++------- burrito/apps/meta/views.py | 4 ++-- burrito/apps/profile/views.py | 8 ++++---- burrito/apps/registration/views.py | 6 +++--- burrito/apps/tickets/views.py | 12 ++++++------ burrito/utils/converter.py | 16 ++++++++-------- burrito/utils/query_util.py | 12 ++++++------ burrito/utils/users_util.py | 2 +- 8 files changed, 37 insertions(+), 37 deletions(-) diff --git a/burrito/apps/admin/views.py b/burrito/apps/admin/views.py index dc17da62..6cf4b335 100644 --- a/burrito/apps/admin/views.py +++ b/burrito/apps/admin/views.py @@ -43,9 +43,9 @@ ) from burrito.utils.auth import get_auth_core from burrito.utils.converter import ( - StatusStrToModel, - FacultyStrToModel, - QueueStrToModel + StatusConverter, + FacultyConverter, + QueueConverter ) from .utils import ( @@ -69,7 +69,7 @@ async def admin__update_ticket_data( admin_updates.ticket_id ) - faculty_object = FacultyStrToModel.convert(admin_updates.faculty) + faculty_object = FacultyConverter.convert(admin_updates.faculty) if faculty_object: # faculty_id must be > 1 create_ticket_action( ticket_id=admin_updates.ticket_id, @@ -80,7 +80,7 @@ async def admin__update_ticket_data( ) ticket.faculty = faculty_object - queue_object = QueueStrToModel.convert(admin_updates.queue) if admin_updates.queue else None + queue_object = QueueConverter.convert(admin_updates.queue) if admin_updates.queue else None if queue_object: # queue_id must be > 1 create_ticket_action( ticket_id=admin_updates.ticket_id, @@ -94,7 +94,7 @@ async def admin__update_ticket_data( current_admin: Users | None = get_user_by_id(token_payload.user_id) status_object = None if ticket.assignee == current_admin: - status_object = StatusStrToModel.convert(admin_updates.status) + status_object = StatusConverter.convert(admin_updates.status) if status_object: # status_id must be > 1 create_ticket_action( ticket_id=admin_updates.ticket_id, @@ -333,7 +333,7 @@ async def admin__become_an_assignee( ) ticket.assignee = current_admin - new_status = StatusStrToModel.convert(1) + new_status = StatusConverter.convert(1) create_ticket_action( ticket_id=ticket_data.ticket_id, author_id=token_payload.user_id, diff --git a/burrito/apps/meta/views.py b/burrito/apps/meta/views.py index 8dd478f8..678819c1 100644 --- a/burrito/apps/meta/views.py +++ b/burrito/apps/meta/views.py @@ -20,7 +20,7 @@ from burrito.schemas.status_schema import StatusResponseSchema from burrito.schemas.queue_schema import QueueResponseSchema -from burrito.utils.converter import FacultyStrToModel +from burrito.utils.converter import FacultyConverter async def meta__get_statuses_list(): @@ -52,7 +52,7 @@ async def meta__faculties_list(): async def meta__get_queues_list(faculty_data: RequestQueueListSchema): - faculty_object = FacultyStrToModel.convert(faculty_data.faculty) + faculty_object = FacultyConverter.convert(faculty_data.faculty) response_list: list[QueueResponseSchema] = [] for queue in Queues.select().where( diff --git a/burrito/apps/profile/views.py b/burrito/apps/profile/views.py index 88576cd9..eab51c4a 100644 --- a/burrito/apps/profile/views.py +++ b/burrito/apps/profile/views.py @@ -16,8 +16,8 @@ ) from burrito.utils.converter import ( - FacultyStrToModel, - GroupStrToModel + FacultyConverter, + GroupConverter ) from .utils import ( @@ -77,12 +77,12 @@ async def profile__update_my_profile( current_user.email = profile_updated_data.email # check faculty - faculty_id = FacultyStrToModel.convert(profile_updated_data.faculty) + faculty_id = FacultyConverter.convert(profile_updated_data.faculty) if faculty_id and profile_updated_data.faculty: current_user.faculty = faculty_id # check group - group_id = GroupStrToModel.convert(profile_updated_data.group) + group_id = GroupConverter.convert(profile_updated_data.group) if group_id and profile_updated_data.group: current_user.group = group_id diff --git a/burrito/apps/registration/views.py b/burrito/apps/registration/views.py index 61b01d38..d291cc25 100644 --- a/burrito/apps/registration/views.py +++ b/burrito/apps/registration/views.py @@ -7,7 +7,7 @@ from burrito.models.user_model import Users -from burrito.utils.converter import GroupStrToModel, FacultyStrToModel +from burrito.utils.converter import GroupConverter, FacultyConverter from burrito.utils.auth import get_auth_core from burrito.utils.auth_token_util import ( @@ -49,8 +49,8 @@ async def registration__user_registration( current_user: Users | None = create_user_tmp_foo( user_data.login, get_hash(user_data.password), - GroupStrToModel.convert(user_data.group), - FacultyStrToModel.convert(user_data.faculty) + GroupConverter.convert(user_data.group), + FacultyConverter.convert(user_data.faculty) ) if current_user: diff --git a/burrito/apps/tickets/views.py b/burrito/apps/tickets/views.py index 8a36a7d0..3a12b9a0 100644 --- a/burrito/apps/tickets/views.py +++ b/burrito/apps/tickets/views.py @@ -52,9 +52,9 @@ ) from burrito.utils.logger import get_logger from burrito.utils.converter import ( - FacultyStrToModel, - StatusStrToModel, - QueueStrToModel + FacultyConverter, + StatusConverter, + QueueConverter ) from .utils import ( @@ -79,8 +79,8 @@ async def tickets__create_new_ticket( Authorize.get_jwt_subject() ) - faculty_id = FacultyStrToModel.convert(ticket_creation_data.faculty) - queue: Queues = QueueStrToModel.convert(ticket_creation_data.queue) + faculty_id = FacultyConverter.convert(ticket_creation_data.faculty) + queue: Queues = QueueConverter.convert(ticket_creation_data.queue) ticket: Tickets = Tickets.create( creator=token_payload.user_id, @@ -546,7 +546,7 @@ async def tickets__close_own_ticket( ) status_id = 6 - status_object = StatusStrToModel.convert(status_id) + status_object = StatusConverter.convert(status_id) if not status_object: get_logger().critical(f"Status {status_id} is not exist in database") diff --git a/burrito/utils/converter.py b/burrito/utils/converter.py index 775ea644..b7092d05 100644 --- a/burrito/utils/converter.py +++ b/burrito/utils/converter.py @@ -28,7 +28,7 @@ def convert(int_value: int | None): raise NotImplementedError("This is an abstract method") -class GroupStrToModel(Converter): +class GroupConverter(Converter): @staticmethod def convert(int_value: int | None) -> Groups | None: """_summary_ @@ -39,7 +39,7 @@ def convert(int_value: int | None) -> Groups | None: Returns: Groups | None: group object or None value """ - GroupStrToModel._is_empty(int_value, f"Group {int_value} is invalid") + GroupConverter._is_empty(int_value, f"Group {int_value} is invalid") group_object = Groups.get_or_none(Groups.group_id == int_value) if not group_object: @@ -48,7 +48,7 @@ def convert(int_value: int | None) -> Groups | None: return group_object -class FacultyStrToModel(Converter): +class FacultyConverter(Converter): @staticmethod def convert(int_value: int | None) -> Faculties | None: """_summary_ @@ -59,7 +59,7 @@ def convert(int_value: int | None) -> Faculties | None: Returns: Faculties | None: faculty object or None value """ - FacultyStrToModel._is_empty(int_value, f"Faculty {int_value} is invalid") + FacultyConverter._is_empty(int_value, f"Faculty {int_value} is invalid") faculty_object = Faculties.get_or_none(Faculties.faculty_id == int_value) if not faculty_object: @@ -68,7 +68,7 @@ def convert(int_value: int | None) -> Faculties | None: return faculty_object -class QueueStrToModel(Converter): +class QueueConverter(Converter): @staticmethod def convert(int_value: int | None) -> Queues | None: """_summary_ @@ -79,7 +79,7 @@ def convert(int_value: int | None) -> Queues | None: Returns: Queues | None: queue object or None value """ - QueueStrToModel._is_empty(int_value, f"Queue {int_value} is invalid") + QueueConverter._is_empty(int_value, f"Queue {int_value} is invalid") queue_object = Queues.get_or_none(Queues.queue_id == int_value) if not queue_object: @@ -88,7 +88,7 @@ def convert(int_value: int | None) -> Queues | None: return queue_object -class StatusStrToModel(Converter): +class StatusConverter(Converter): @staticmethod def convert(int_value: int | None) -> Statuses | None: """_summary_ @@ -99,7 +99,7 @@ def convert(int_value: int | None) -> Statuses | None: Returns: Statuses | None: 'status' object or None value """ - StatusStrToModel._is_empty(int_value, "Status is invalid") + StatusConverter._is_empty(int_value, "Status is invalid") status_object = Statuses.get_or_none(Statuses.status_id == int_value) if not status_object: diff --git a/burrito/utils/query_util.py b/burrito/utils/query_util.py index ab60b2f8..aed14146 100644 --- a/burrito/utils/query_util.py +++ b/burrito/utils/query_util.py @@ -5,14 +5,14 @@ from burrito.models.deleted_model import Deleted from burrito.utils.converter import ( - StatusStrToModel, - FacultyStrToModel, - QueueStrToModel + StatusConverter, + FacultyConverter, + QueueConverter ) _PROTECTED_STATUSES: tuple[int] = ( - StatusStrToModel.convert(1).status_id, + StatusConverter.convert(1).status_id, ) STATUSES_FOR_USER: list[int] = [] @@ -48,11 +48,11 @@ def q_protected_statuses() -> Expression: def q_is_valid_faculty(value: int) -> Expression: - return Tickets.faculty == FacultyStrToModel.convert(value) + return Tickets.faculty == FacultyConverter.convert(value) def q_is_valid_queue(queue: str, faculty: str) -> Expression: - return Tickets.queue == QueueStrToModel.convert(queue, faculty) + return Tickets.queue == QueueConverter.convert(queue, faculty) def q_is_valid_status(value: int) -> Expression: diff --git a/burrito/utils/users_util.py b/burrito/utils/users_util.py index 680053fd..7100c5eb 100644 --- a/burrito/utils/users_util.py +++ b/burrito/utils/users_util.py @@ -1,6 +1,6 @@ from fastapi import HTTPException, status -from burrito.utils.converter import GroupStrToModel, FacultyStrToModel +from burrito.utils.converter import GroupConverter, FacultyConverter from burrito.utils.logger import get_logger from burrito.models.roles_model import Roles From 7c631ffc5d2ceb4dcf96cb71e2f1e02ad2703b98 Mon Sep 17 00:00:00 2001 From: m-o-d-e-r Date: Sun, 25 Jun 2023 16:46:39 +0300 Subject: [PATCH 4/6] deleted cache tools --- burrito/utils/cache_util.py | 16 ---------------- burrito/utils/converter.py | 4 ---- 2 files changed, 20 deletions(-) delete mode 100644 burrito/utils/cache_util.py diff --git a/burrito/utils/cache_util.py b/burrito/utils/cache_util.py deleted file mode 100644 index 3cb9f069..00000000 --- a/burrito/utils/cache_util.py +++ /dev/null @@ -1,16 +0,0 @@ -from cachetools import TTLCache - -from .singleton_pattern import singleton - - -@singleton -class BurritoCache(TTLCache): - def __init__(self, *, maxsize: int = 2024, ttl: float = 3036): - super().__init__( - maxsize=maxsize, - ttl=ttl - ) - - -def get_ttl_cache_core(): - return BurritoCache() diff --git a/burrito/utils/converter.py b/burrito/utils/converter.py index b7092d05..d0340b11 100644 --- a/burrito/utils/converter.py +++ b/burrito/utils/converter.py @@ -1,5 +1,3 @@ -from cachetools import cached - from fastapi import HTTPException from burrito.models.group_model import Groups @@ -7,8 +5,6 @@ from burrito.models.queues_model import Queues from burrito.models.statuses_model import Statuses -from burrito.utils.cache_util import BurritoCache - def _raise_converter_error(detail) -> None: raise HTTPException( From 13a94586047a336d885ec3fa7e24e7eada6f56d2 Mon Sep 17 00:00:00 2001 From: m-o-d-e-r Date: Sun, 25 Jun 2023 22:07:49 +0300 Subject: [PATCH 5/6] renamed field 'author' to 'user' in 'Actions' model --- burrito/apps/admin/views.py | 10 +++++----- burrito/apps/tickets/views.py | 2 +- burrito/models/actions_model.py | 2 +- burrito/schemas/action_schema.py | 2 +- burrito/utils/tickets_util.py | 6 +++--- tests/tickets_test.py | 5 +++-- 6 files changed, 14 insertions(+), 13 deletions(-) diff --git a/burrito/apps/admin/views.py b/burrito/apps/admin/views.py index 6cf4b335..52bf2d8b 100644 --- a/burrito/apps/admin/views.py +++ b/burrito/apps/admin/views.py @@ -73,7 +73,7 @@ async def admin__update_ticket_data( if faculty_object: # faculty_id must be > 1 create_ticket_action( ticket_id=admin_updates.ticket_id, - author_id=token_payload.user_id, + user_id=token_payload.user_id, field_name="faculty", old_value=ticket.faculty.name, new_value=faculty_object.name @@ -84,7 +84,7 @@ async def admin__update_ticket_data( if queue_object: # queue_id must be > 1 create_ticket_action( ticket_id=admin_updates.ticket_id, - author_id=token_payload.user_id, + user_id=token_payload.user_id, field_name="queue", old_value=ticket.queue.name, new_value=queue_object.name @@ -98,7 +98,7 @@ async def admin__update_ticket_data( if status_object: # status_id must be > 1 create_ticket_action( ticket_id=admin_updates.ticket_id, - author_id=token_payload.user_id, + user_id=token_payload.user_id, field_name="status", old_value=ticket.status.name, new_value=status_object.name @@ -326,7 +326,7 @@ async def admin__become_an_assignee( ) create_ticket_action( ticket_id=ticket_data.ticket_id, - author_id=token_payload.user_id, + user_id=token_payload.user_id, field_name="assignee", old_value="None", new_value=current_admin.login @@ -336,7 +336,7 @@ async def admin__become_an_assignee( new_status = StatusConverter.convert(1) create_ticket_action( ticket_id=ticket_data.ticket_id, - author_id=token_payload.user_id, + user_id=token_payload.user_id, field_name="status", old_value=ticket.status.name, new_value=new_status.name diff --git a/burrito/apps/tickets/views.py b/burrito/apps/tickets/views.py index 3a12b9a0..667ce6b7 100644 --- a/burrito/apps/tickets/views.py +++ b/burrito/apps/tickets/views.py @@ -558,7 +558,7 @@ async def tickets__close_own_ticket( create_ticket_action( ticket_id=data_to_close_ticket.ticket_id, - author_id=token_payload.user_id, + user_id=token_payload.user_id, field_name="status", old_value=ticket.status.name, new_value=status_object.name diff --git a/burrito/models/actions_model.py b/burrito/models/actions_model.py index f37a92cd..3b28a166 100644 --- a/burrito/models/actions_model.py +++ b/burrito/models/actions_model.py @@ -18,7 +18,7 @@ class Actions(BurritoBasicModel): on_delete="NO ACTION" ) - author = ForeignKeyField( + user = ForeignKeyField( Users, field="user_id", on_delete="NO ACTION" diff --git a/burrito/schemas/action_schema.py b/burrito/schemas/action_schema.py index 7ba9e70b..0883e7a1 100644 --- a/burrito/schemas/action_schema.py +++ b/burrito/schemas/action_schema.py @@ -4,7 +4,7 @@ class ActionSchema(BaseModel): action_id: int ticket_id: int - author_id: int + user_id: int action_date: str field_name: str old_value: str diff --git a/burrito/utils/tickets_util.py b/burrito/utils/tickets_util.py index 8be42c5d..10e9b79f 100644 --- a/burrito/utils/tickets_util.py +++ b/burrito/utils/tickets_util.py @@ -117,14 +117,14 @@ def get_filtered_tickets( def create_ticket_action( *, ticket_id: int, - author_id: int, + user_id: int, field_name: str, old_value: str, new_value: str ) -> None: Actions.create( ticket=ticket_id, - author=author_id, + user=user_id, field_name=field_name, old_value=old_value, new_value=new_value @@ -136,7 +136,7 @@ def get_ticket_actions(ticket_id: int) -> list[Actions]: ActionSchema( action_id=action.action_id, ticket_id=action.ticket_id, - author_id=action.author_id, + user_id=action.user_id, action_date=str(action.action_date), field_name=action.field_name, old_value=action.old_value, diff --git a/tests/tickets_test.py b/tests/tickets_test.py index ebd34c62..e13cd62f 100644 --- a/tests/tickets_test.py +++ b/tests/tickets_test.py @@ -245,7 +245,7 @@ def test_008_tickets_filter(self): "Authorization": f"Bearer {AuthTestCase.access_token}" }, json={ - "status": [1, 2, 3] + "status": [6] }, timeout=TIMEOUT ) @@ -286,7 +286,8 @@ def test_009_ticket_detail_view(self): }, timeout=TIMEOUT ) - + from pprint import pprint + pprint(response.json()) check_error( self.assertEqual, { From b3517ced5b65d0c99dd7675fd941e89f0a256eca Mon Sep 17 00:00:00 2001 From: m-o-d-e-r Date: Sun, 25 Jun 2023 22:12:35 +0300 Subject: [PATCH 6/6] update version label --- burrito/__init__.py | 2 +- pyproject.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/burrito/__init__.py b/burrito/__init__.py index 5550e148..30f0159a 100644 --- a/burrito/__init__.py +++ b/burrito/__init__.py @@ -1 +1 @@ -__version__ = "0.5.5 indev" +__version__ = "0.5.6 indev" diff --git a/pyproject.toml b/pyproject.toml index 41d74d7c..07fc2e3e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "Burrito" -version = "0.5.5.dev1" +version = "0.5.6.dev1" description = "API for the issue tracker" authors = ["DimonBor", "m-o-d-e-r"] readme = "README.md"