From 661986d99005dc4046bd149dff6f9c93282aeabc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=97=D0=B0=D1=85=D0=B0=D1=80=D0=BE=D0=B2=20=D0=98=D0=B2?= =?UTF-8?q?=D0=B0=D0=BD=20=D0=9C=D0=B8=D1=85=D0=B0=D0=B9=D0=BB=D0=BE=D0=B2?= =?UTF-8?q?=D0=B8=D1=87?= Date: Sat, 14 Dec 2024 03:56:00 +0000 Subject: [PATCH 1/2] fixing tests --- rating_api/routes/comment.py | 32 ++++++++------------ rating_api/schemas/models.py | 4 +-- tests/test_routes/test_comment.py | 49 ------------------------------- 3 files changed, 14 insertions(+), 71 deletions(-) diff --git a/rating_api/routes/comment.py b/rating_api/routes/comment.py index b228216..bb2a05f 100644 --- a/rating_api/routes/comment.py +++ b/rating_api/routes/comment.py @@ -20,33 +20,25 @@ @comment.post("", response_model=CommentGet) async def create_comment(lecturer_id: int, comment_info: CommentPost, user=Depends(UnionAuth())) -> CommentGet: """ - Scopes: `["rating.comment.import"]` Создает комментарий к преподавателю в базе данных RatingAPI Для создания комментария нужно быть авторизованным - Для возможности создания комментария с указанием времени создания и изменения необходим скоуп ["rating.comment.import"] """ lecturer = Lecturer.get(session=db.session, id=lecturer_id) if not lecturer: raise ObjectNotFound(Lecturer, lecturer_id) - - has_create_scope = "rating.comment.import" in [scope['name'] for scope in user.get('session_scopes')] - if (comment_info.create_ts or comment_info.update_ts) and not has_create_scope: - raise ForbiddenAction(Comment) - - if not has_create_scope: - user_comments: list[LecturerUserComment] = ( - LecturerUserComment.query(session=db.session).filter(LecturerUserComment.user_id == user.get("id")).all() - ) - for user_comment in user_comments: - if datetime.datetime.utcnow() - user_comment.update_ts < datetime.timedelta( - minutes=settings.COMMENT_CREATE_FREQUENCY_IN_MINUTES - ): - raise TooManyCommentRequests( - dtime=user_comment.update_ts - + datetime.timedelta(minutes=settings.COMMENT_CREATE_FREQUENCY_IN_MINUTES) - - datetime.datetime.utcnow() - ) + user_comments: list[LecturerUserComment] = ( + LecturerUserComment.query(session=db.session).filter(LecturerUserComment.user_id == user.get("id")).all() + ) + for user_comment in user_comments: + if datetime.datetime.utcnow() - user_comment.update_ts < datetime.timedelta( + minutes=settings.COMMENT_CREATE_FREQUENCY_IN_MINUTES + ): + raise TooManyCommentRequests( + dtime=user_comment.update_ts + + datetime.timedelta(minutes=settings.COMMENT_CREATE_FREQUENCY_IN_MINUTES) + - datetime.datetime.utcnow() + ) # Сначала добавляем с user_id, который мы получили при авторизации, # в LecturerUserComment, чтобы нельзя было слишком быстро добавлять комментарии diff --git a/rating_api/schemas/models.py b/rating_api/schemas/models.py index 4cfa074..5dd66a3 100644 --- a/rating_api/schemas/models.py +++ b/rating_api/schemas/models.py @@ -24,8 +24,6 @@ class CommentGet(Base): class CommentPost(Base): subject: str text: str - create_ts: datetime.datetime | None = None - update_ts: datetime.datetime | None = None mark_kindness: int mark_freebie: int mark_clarity: int @@ -41,6 +39,8 @@ def validate_mark(cls, value): class CommentImport(CommentPost): lecturer_id: int + create_ts: datetime.datetime | None = None + update_ts: datetime.datetime | None = None subject: str | None = None diff --git a/tests/test_routes/test_comment.py b/tests/test_routes/test_comment.py index d093d92..c891640 100644 --- a/tests/test_routes/test_comment.py +++ b/tests/test_routes/test_comment.py @@ -77,19 +77,6 @@ 3, status.HTTP_404_NOT_FOUND, ), - ( - { - "subject": "test_subject", - "text": "test_text", - "create_ts": "2077-11-16T19:15:27.306Z", - "update_ts": "2077-11-16T19:15:27.306Z", - "mark_kindness": 1, - "mark_freebie": -2, - "mark_clarity": 0, - }, - 0, - status.HTTP_200_OK, - ), ( # Anonymous comment { "subject": "test_subject", @@ -102,18 +89,6 @@ 0, status.HTTP_200_OK, ), - ( - { - "subject": "test_subject", - "text": "test_text", - "update_ts": "2077-11-16T19:15:27.306Z", - "mark_kindness": 1, - "mark_freebie": -2, - "mark_clarity": 0, - }, - 0, - status.HTTP_200_OK, - ), ( # NotAnonymous comment { "subject": "test_subject", @@ -126,30 +101,6 @@ 0, status.HTTP_200_OK, ), - ( - { - "subject": "test_subject", - "text": "test_text", - "create_ts": "2077-11-16T19:15:27.306Z", - "mark_kindness": 1, - "mark_freebie": -2, - "mark_clarity": 0, - }, - 0, - status.HTTP_200_OK, - ), - ( # wrong date - { - "subject": "test_subject", - "text": "test_text", - "create_ts": "wasd", - "mark_kindness": 1, - "mark_freebie": -2, - "mark_clarity": 0, - }, - 0, - status.HTTP_422_UNPROCESSABLE_ENTITY, - ), ( # Bad anonymity { "subject": "test_subject", From ebb461c1487b726e7ac59e4dcb364524bd205474 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=97=D0=B0=D1=85=D0=B0=D1=80=D0=BE=D0=B2=20=D0=98=D0=B2?= =?UTF-8?q?=D0=B0=D0=BD=20=D0=9C=D0=B8=D1=85=D0=B0=D0=B9=D0=BB=D0=BE=D0=B2?= =?UTF-8?q?=D0=B8=D1=87?= Date: Sat, 14 Dec 2024 04:24:03 +0000 Subject: [PATCH 2/2] post comment logic back --- rating_api/routes/comment.py | 32 ++++++++++++++++++++------------ rating_api/schemas/models.py | 4 ++-- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/rating_api/routes/comment.py b/rating_api/routes/comment.py index bb2a05f..b228216 100644 --- a/rating_api/routes/comment.py +++ b/rating_api/routes/comment.py @@ -20,25 +20,33 @@ @comment.post("", response_model=CommentGet) async def create_comment(lecturer_id: int, comment_info: CommentPost, user=Depends(UnionAuth())) -> CommentGet: """ + Scopes: `["rating.comment.import"]` Создает комментарий к преподавателю в базе данных RatingAPI Для создания комментария нужно быть авторизованным + Для возможности создания комментария с указанием времени создания и изменения необходим скоуп ["rating.comment.import"] """ lecturer = Lecturer.get(session=db.session, id=lecturer_id) if not lecturer: raise ObjectNotFound(Lecturer, lecturer_id) - user_comments: list[LecturerUserComment] = ( - LecturerUserComment.query(session=db.session).filter(LecturerUserComment.user_id == user.get("id")).all() - ) - for user_comment in user_comments: - if datetime.datetime.utcnow() - user_comment.update_ts < datetime.timedelta( - minutes=settings.COMMENT_CREATE_FREQUENCY_IN_MINUTES - ): - raise TooManyCommentRequests( - dtime=user_comment.update_ts - + datetime.timedelta(minutes=settings.COMMENT_CREATE_FREQUENCY_IN_MINUTES) - - datetime.datetime.utcnow() - ) + + has_create_scope = "rating.comment.import" in [scope['name'] for scope in user.get('session_scopes')] + if (comment_info.create_ts or comment_info.update_ts) and not has_create_scope: + raise ForbiddenAction(Comment) + + if not has_create_scope: + user_comments: list[LecturerUserComment] = ( + LecturerUserComment.query(session=db.session).filter(LecturerUserComment.user_id == user.get("id")).all() + ) + for user_comment in user_comments: + if datetime.datetime.utcnow() - user_comment.update_ts < datetime.timedelta( + minutes=settings.COMMENT_CREATE_FREQUENCY_IN_MINUTES + ): + raise TooManyCommentRequests( + dtime=user_comment.update_ts + + datetime.timedelta(minutes=settings.COMMENT_CREATE_FREQUENCY_IN_MINUTES) + - datetime.datetime.utcnow() + ) # Сначала добавляем с user_id, который мы получили при авторизации, # в LecturerUserComment, чтобы нельзя было слишком быстро добавлять комментарии diff --git a/rating_api/schemas/models.py b/rating_api/schemas/models.py index 5dd66a3..4cfa074 100644 --- a/rating_api/schemas/models.py +++ b/rating_api/schemas/models.py @@ -24,6 +24,8 @@ class CommentGet(Base): class CommentPost(Base): subject: str text: str + create_ts: datetime.datetime | None = None + update_ts: datetime.datetime | None = None mark_kindness: int mark_freebie: int mark_clarity: int @@ -39,8 +41,6 @@ def validate_mark(cls, value): class CommentImport(CommentPost): lecturer_id: int - create_ts: datetime.datetime | None = None - update_ts: datetime.datetime | None = None subject: str | None = None