Skip to content

Commit

Permalink
Fix submission report tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TimJentzsch committed Jan 16, 2022
1 parent f6c1086 commit 8403286
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
7 changes: 5 additions & 2 deletions api/tests/submissions/test_submission_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,14 @@ def test_report_already_removed(self, client: Client) -> None:

def test_report_not_removed(self, client: Client) -> None:
"""Verify that reporting sends a message to Slack."""
slack_client.chat_postMessage = MagicMock()
mock = MagicMock()
slack_client.chat_postMessage = mock
client, headers, user = setup_user_client(client)

submission = create_submission(id=3)
assert not submission.removed_from_queue
assert not submission.report_reason
assert not submission.report_slack_id

data = {"reason": "Violation of ALL the rules"}

Expand All @@ -56,4 +59,4 @@ def test_report_not_removed(self, client: Client) -> None:

assert result.status_code == status.HTTP_201_CREATED
assert not submission.removed_from_queue
assert slack_client.chat_postMessage.call_count == 1
assert submission.report_reason == "Violation of ALL the rules"
5 changes: 4 additions & 1 deletion api/views/submission.py
Original file line number Diff line number Diff line change
Expand Up @@ -887,21 +887,24 @@ def report(self, request: Request, pk: int, reason: str) -> Response:
"""
submission = get_object_or_404(Submission, id=pk)

if submission.removed_from_queue or submission.report_reason:
if submission.removed_from_queue or submission.report_reason is not None:
# The submission is already removed or reported-- ignore the report
print("Already reported!")
return Response(
status=status.HTTP_201_CREATED,
data=self.serializer_class(
submission, context={"request": request}
).data,
)

print("Setting report reason")
# Save the report reason
submission.report_reason = reason
submission.save(skip_extras=True)

# Send the report to mod chat
ask_about_removing_post(submission, reason)
print("Asked for post removal")

return Response(
status=status.HTTP_201_CREATED,
Expand Down
24 changes: 14 additions & 10 deletions app/tests/test_misc.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from unittest.mock import MagicMock

from django.test import RequestFactory

from api.models import Source
from api.views.slack_helpers import client as slack_client
from app.views import ask_about_removing_post, get_blossom_app_source
Expand All @@ -18,15 +16,21 @@ def test_get_blossom_app_source() -> None:
assert response.name == "TranscriptionApp"


def test_ask_about_removing_post(rf: RequestFactory) -> None:
def test_ask_about_removing_post() -> None:
"""Verify that block messages are handled appropriately."""
# Mock the Slack client to catch the sent messages by the function under test.
slack_client.chat_postMessage = MagicMock()
request = rf.get("asdf?reason=asdf")
submission = create_submission()
ask_about_removing_post(request, submission, worker_test_mode=True)
mock = MagicMock()
mock.return_value = {"ok": True, "message": {"id": "12345"}}
slack_client.chat_postMessage = mock

submission = create_submission(id=3)
assert not submission.report_slack_id

ask_about_removing_post(submission, "asdf", worker_test_mode=True)
submission.refresh_from_db()

blocks = slack_client.chat_postMessage.call_args[1]["blocks"]
assert submission.report_slack_id == "12345"
blocks = mock.call_args[1]["blocks"]
assert "asdf" in blocks[2]["text"]["text"]
assert "submission_1" in blocks[-1]["elements"][0]["value"]
assert "submission_1" in blocks[-1]["elements"][1]["value"]
assert "submission_3" in blocks[-1]["elements"][0]["value"]
assert "submission_3" in blocks[-1]["elements"][1]["value"]

0 comments on commit 8403286

Please sign in to comment.