Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider recent transcriptions for check percentage #306

Merged
merged 5 commits into from
Jan 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 43 additions & 5 deletions api/tests/submissions/test_submission_done.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import datetime
import json
from unittest.mock import MagicMock, PropertyMock, call, patch

import pytest
import pytz
from django.test import Client
from django.urls import reverse
from rest_framework import status

from api.views.slack_helpers import client as slack_client
from api.views.submission import _is_returning_transcriber
from utils.test_helpers import (
create_submission,
create_transcription,
Expand Down Expand Up @@ -163,7 +166,11 @@ def test_done_random_checks(
# Mock both the gamma property and the random.random function.
with patch(
"authentication.models.BlossomUser.gamma", new_callable=PropertyMock
) as mock, patch("random.random", lambda: probability):
) as mock, patch(
"api.views.submission._is_returning_transcriber", return_value=False
), patch(
"random.random", lambda: probability
):
mock.return_value = gamma
# Mock the Slack client to catch the sent messages by the function under test.
slack_client.chat_postMessage = MagicMock()
Expand Down Expand Up @@ -191,6 +198,33 @@ def test_done_random_checks(
else:
assert slack_client.chat_postMessage.call_count == 0

@pytest.mark.parametrize(
"recent_gamma, total_gamma, expected",
[(0, 10000, True), (5, 10000, True), (6, 6, False), (10, 10000, False)],
)
def test_is_returning_transcriber(
self, client: Client, recent_gamma: int, total_gamma: int, expected: bool,
) -> None:
"""Test whether returning transcribers are determined correctly."""
# Mock both the total gamma
with patch(
"authentication.models.BlossomUser.gamma",
new_callable=PropertyMock,
return_value=total_gamma,
):
# Mock the Slack client to catch the sent messages by the function under test.
client, headers, user = setup_user_client(client)
now = datetime.datetime.now(tz=pytz.UTC)

# Create the recent transcriptions
for i in range(0, recent_gamma):
submission = create_submission(
id=i + 100, claimed_by=user, completed_by=user, complete_time=now,
)
create_transcription(submission, user, id=i + 100, create_time=now)

assert _is_returning_transcriber(user) == expected

def test_send_transcription_to_slack(self, client: Client) -> None:
"""Verify that a new user gets a different welcome message."""
# Mock both the gamma property and the random.random function.
Expand Down Expand Up @@ -301,8 +335,10 @@ def test_check_for_rank_up(self, client: Client) -> None:

submission = create_submission(claimed_by=user, original_id=25)

# patch out random so that the "check transcription" doesn't fire
with patch("random.random", lambda: 1):
# patch out transcription check
with patch(
"api.views.submission._should_check_transcription", return_value=False,
):
result = client.patch(
reverse("submission-done", args=[submission.id]),
json.dumps({"username": user.username, "mod_override": "True"}),
Expand All @@ -324,10 +360,12 @@ def test_check_for_rank_up(self, client: Client) -> None:
create_transcription(submission, user)

# now it shouldn't trigger on the next transcription
# patch out random so that the "check transcription" doesn't fire
# patch out transcription check
old_count_of_slack_calls = len(slack_client.chat_postMessage.call_args_list)

with patch("random.random", lambda: 1):
with patch(
"api.views.submission._should_check_transcription", return_value=False,
):
result = client.patch(
reverse("submission-done", args=[submission.id]),
json.dumps({"username": user.username}),
Expand Down
14 changes: 7 additions & 7 deletions api/tests/submissions/test_submission_transcribot_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from django.test import Client
from django.urls import reverse

from api.views.submission import SubmissionViewSet
from api.views.submission import _get_limit_value
from utils.test_helpers import (
create_submission,
create_transcription,
Expand Down Expand Up @@ -171,25 +171,25 @@ def test_get_limit() -> None:
"""Verify that get_limit_value returns the requested value or 10."""
request = MagicMock()
request.query_params.get.return_value = None
return_value = SubmissionViewSet()._get_limit_value(request)
return_value = _get_limit_value(request)
assert return_value == 10

request.query_params.get.return_value = None
return_value = SubmissionViewSet()._get_limit_value(request, default=200)
return_value = _get_limit_value(request, default=200)
assert return_value == 200

request.query_params.get.return_value = "999"
return_value = SubmissionViewSet()._get_limit_value(request)
return_value = _get_limit_value(request)
assert return_value == 999

request.query_params.get.return_value = "none"
return_value = SubmissionViewSet()._get_limit_value(request)
return_value = _get_limit_value(request)
assert return_value is None

request.query_params.get.return_value = "aaa"
return_value = SubmissionViewSet()._get_limit_value(request)
return_value = _get_limit_value(request)
assert return_value == 10

request.query_params.get.return_value = "!@#$%&%)%^&"
return_value = SubmissionViewSet()._get_limit_value(request)
return_value = _get_limit_value(request)
assert return_value == 10
48 changes: 47 additions & 1 deletion api/views/slack_helpers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import binascii
import hashlib
import hmac
import logging
import os
import re
import time
Expand All @@ -11,16 +12,19 @@
import slack
from django.conf import settings
from django.http import HttpRequest
from slack import WebClient

from api.helpers import fire_and_forget
from api.models import Submission
from api.models import Submission, Transcription
from api.serializers import VolunteerSerializer
from api.views.misc import Summary
from app.reddit_actions import remove_post
from authentication.models import BlossomUser
from blossom.errors import ConfigurationError
from blossom.strings import translation

logger = logging.getLogger("api.views.slack_helpers")

if settings.ENABLE_SLACK is True:
try:
client = slack.WebClient(token=os.environ["SLACK_API_KEY"]) # pragma: no cover
Expand Down Expand Up @@ -452,3 +456,45 @@ def is_valid_slack_request(request: HttpRequest) -> bool:
)

return hmac.compare_digest(signature, slack_signature)


def _send_transcription_to_slack(
transcription: Transcription,
submission: Submission,
user: BlossomUser,
slack_client: WebClient,
) -> None:
"""Notify slack for the transcription check."""
url = None
# it's possible that we either won't pull a transcription object OR that
# a transcription object won't have a URL. If either fails, then we default
# to the submission's URL.
if transcription:
url = transcription.url
if not url:
url = submission.tor_url

url = "https://reddit.com" + url if submission.source == "reddit" else url

msg = f"Please check the following transcription of " f"u/{user.username}: {url}."

if user.overwrite_check_percentage is not None:
# Let the mods know that the user is being watched
percentage = user.overwrite_check_percentage
msg += (
f"\n\nThis user is being watched with a chance of {percentage:.0%}.\n"
+ f"Undo this using the `unwatch {user.username}` command."
)

# the `done` process is still going here, so they technically don't have
# a transcription yet. It's about to get assigned, but for right now the
# value is still zero.
if user.gamma == 0:
msg = ":rotating_light: First transcription! :rotating_light: " + msg

try:
slack_client.chat_postMessage(
channel="#transcription_check", text=msg,
)
except: # noqa
logger.warning(f"Cannot post message to slack. Msg: {msg}")
Loading