Skip to content

Commit

Permalink
Fix utcnow() patching and related tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Darky2020 committed Oct 13, 2024
1 parent ba3369c commit 7ef1c17
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 19 deletions.
41 changes: 29 additions & 12 deletions tests/comments/test_comments_edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ async def test_comments_edit_empty_markdown(
assert response.json()["code"] == "system:validation_error"


async def test_comments_edit_rate_limit(
async def test_comments_edit_count_limit(
client,
aggregator_anime,
aggregator_anime_info,
Expand All @@ -90,23 +90,40 @@ async def test_comments_edit_rate_limit(
response = await request_comments_write(
client, get_test_token, "edit", "17", "Old text"
)
comment_reference = response.json()["reference"]

edit_count_limit = 5

for index, _ in enumerate(range(0, 5)):
await request_comments_edit(
client, get_test_token, response.json()["reference"], "New text"
for index, _ in enumerate(range(0, edit_count_limit + 1)):
response = await request_comments_edit(
client,
get_test_token,
response.json()["reference"],
f"New text {index}",
)

if index != 5:
continue
# Make sure request prior to the count limit is good
if index == edit_count_limit - 2:
comment = await test_session.scalar(
select(Comment).filter(Comment.id == comment_reference)
)
await test_session.refresh(comment)

comment = await test_session.scalar(
select(Comment).filter(Comment.id == response.json()["reference"])
)
assert comment.is_editable is True

assert response.status_code == status.HTTP_200_OK
assert "code" not in response.json()

if index == edit_count_limit:
comment = await test_session.scalar(
select(Comment).filter(Comment.id == comment_reference)
)
await test_session.refresh(comment)

assert comment.is_editable is False
assert comment.is_editable is False

assert response.status_code == status.HTTP_400_BAD_REQUEST
assert response.json()["code"] == "comment:not_editable"
assert response.status_code == status.HTTP_400_BAD_REQUEST
assert response.json()["code"] == "comment:not_editable"


async def test_comments_edit_time_limit(
Expand Down
12 changes: 9 additions & 3 deletions tests/comments/test_comments_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,23 @@ async def test_comments_write_rate_limit(
aggregator_anime_info,
create_test_user,
get_test_token,
mock_utcnow,
):
comments_limit = 100

for index, _ in enumerate(range(0, comments_limit)):
for index, _ in enumerate(range(0, comments_limit + 1)):
response = await request_comments_write(
client, get_test_token, "edit", "17", f"{index} comment, yay!"
)

# Make sure request prior to the rate limit is good
if index == comments_limit - 1:
assert response.status_code == status.HTTP_200_OK
assert "code" not in response.json()

if index == comments_limit:
assert response.status_code == status.HTTP_400_BAD_REQUEST
assert response.json()["code"] == "comment:rate-limit"
assert response.status_code == status.HTTP_429_TOO_MANY_REQUESTS
assert response.json()["code"] == "comment:rate_limit"


async def test_comments_write_empty_markdown(
Expand Down
13 changes: 9 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,16 @@ def mock_s3_upload_file():


# Fix utcnow() datetime for tests that rely on it not changing within the duration of the test
# https://docs.pytest.org/en/stable/how-to/monkeypatch.html
@pytest.fixture(autouse=False)
def mock_utcnow():
with mock.patch("app.utils.utcnow") as mocked:
mocked.return_value = datetime(2024, 2, 17, 10, 23, 29, 305502)
yield mocked
def mock_utcnow(monkeypatch):
fixed_time = datetime(2024, 2, 17, 10, 23, 29, 305502)

# When a function is imported, it becomes a part of the namespace of the
# module that imported it. We need to patch the specific function reference
# in the module we're testing
monkeypatch.setattr("app.edit.service.utcnow", lambda: fixed_time)
monkeypatch.setattr("app.comments.service.utcnow", lambda: fixed_time)


# Aggregator fixtures
Expand Down

0 comments on commit 7ef1c17

Please sign in to comment.