Skip to content

Commit

Permalink
added tests and refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
zipperman1 committed Nov 16, 2024
1 parent ffba3b5 commit 94e7cb8
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 8 deletions.
19 changes: 11 additions & 8 deletions rating_api/routes/comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,36 +23,39 @@ async def create_comment(lecturer_id: int, comment_info: CommentPost, user=Depen
Scopes: `["rating.comment.review"]`
Создает комментарий к преподавателю в базе данных RatingAPI
Для создания комментария нужно быть авторизованным
Скоуп нужен для создания комментариев с заданым timestamp
"""
lecturer = Lecturer.get(session=db.session, id=lecturer_id)
if not lecturer:
raise ObjectNotFound(Lecturer, lecturer_id)

current_time = datetime.datetime.utcnow()
if comment_info.create_ts or comment_info.update_ts:
if not "rating.comment.review" in [scope['name'] for scope in user.get('session_scopes')]:
if "rating.comment.review" not in [scope['name'] for scope in user.get('session_scopes')]:
raise ForbiddenAction(Comment)
else:
if not comment_info.create_ts:
comment_info.create_ts = datetime.datetime.utcnow()
comment_info.create_ts = current_time
if not comment_info.update_ts:
comment_info.update_ts = datetime.datetime.utcnow()
comment_info.update_ts = current_time
else:
comment_info.create_ts = datetime.datetime.utcnow()
comment_info.update_ts = datetime.datetime.utcnow()
comment_info.create_ts = current_time
comment_info.update_ts = current_time

user_comments: list[LecturerUserComment] = (
LecturerUserComment.query(session=db.session).filter(LecturerUserComment.user_id == user.get("id")).all()
)

if not "rating.comment.review" in [scope['name'] for scope in user.get('session_scopes')]:
if "rating.comment.review" not in [scope['name'] for scope in user.get('session_scopes')]:
for user_comment in user_comments:
if datetime.datetime.utcnow() - user_comment.update_ts < datetime.timedelta(
if current_time - 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()
- current_time
)

LecturerUserComment.create(session=db.session, lecturer_id=lecturer_id, user_id=user.get('id'))
Expand Down
44 changes: 44 additions & 0 deletions tests/test_routes/test_comment.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
import uuid
import datetime

import pytest
from starlette import status
Expand Down Expand Up @@ -61,6 +62,43 @@
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,
),
(
{
"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,
),
(
{
"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,
)
],
)
def test_create_comment(client, dbsession, lecturers, body, lecturer_n, response_status):
Expand All @@ -70,6 +108,12 @@ def test_create_comment(client, dbsession, lecturers, body, lecturer_n, response
if response_status == status.HTTP_200_OK:
comment = Comment.query(session=dbsession).filter(Comment.uuid == post_response.json()["uuid"]).one_or_none()
assert comment is not None

if "create_ts" in body:
assert comment.create_ts == datetime.datetime.fromisoformat(body["create_ts"]).replace(tzinfo=None)
if "update_ts" in body:
assert comment.update_ts == datetime.datetime.fromisoformat(body["update_ts"]).replace(tzinfo=None)

user_comment = (
LecturerUserComment.query(session=dbsession)
.filter(LecturerUserComment.lecturer_id == lecturers[lecturer_n].id)
Expand Down

0 comments on commit 94e7cb8

Please sign in to comment.