Skip to content

Commit

Permalink
fix: Update URLs in cron jobs (ocadotechnology#168)
Browse files Browse the repository at this point in the history
* fix: Update URLs in cron jobs

* Shorten URLs

* Fix tests
  • Loading branch information
faucomte97 authored Sep 1, 2023
1 parent 1a4f937 commit 7519ee0
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 23 deletions.
2 changes: 2 additions & 0 deletions backend/portal/tests/views/cron/test_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def send_verify_email_reminder(
subject=ANY,
title=ANY,
text_content=ANY,
replace_url=ANY,
)

send_email.assert_any_call(
Expand All @@ -81,6 +82,7 @@ def send_verify_email_reminder(
subject=ANY,
title=ANY,
text_content=ANY,
replace_url=ANY,
)

# Check only two emails are sent - the student should never be included.
Expand Down
68 changes: 45 additions & 23 deletions backend/portal/views/cron/user.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import logging
from datetime import timedelta
from itertools import chain
from typing import List

from common.models import Teacher, Student
from django.conf import settings
from django.contrib.auth.models import User
from django.urls import reverse
from django.utils import timezone
Expand All @@ -27,15 +27,15 @@
USER_2ND_VERIFY_EMAIL_REMINDER_TEXT = (
"Please go to the link below to verify your email address:"
"\n{email_verification_url}."
"You will not be able to use your account until it is verified."
"\nYou will not be able to use your account until it is verified."
"\n\nBy activating the account you confirm that you have read and agreed to"
" our terms ({terms_url}) and our privacy notice ({privacy_notice_url}). If"
" your account is not verified within 5 days we will delete it."
)
USER_DELETE_UNVERIFIED_ACCOUNT_DAYS = 19


def get_unverified_emails(days: int):
def get_unverified_emails(days: int) -> List[str]:
now = timezone.now()

teacher_emails = Teacher.objects.filter(
Expand All @@ -54,33 +54,50 @@ def get_unverified_emails(days: int):
return list(chain(teacher_emails, student_emails))


def build_absolute_google_uri(request, location: str) -> str:
"""
This is needed specifically for emails sent by cron jobs as the protocol for cron jobs is HTTP
and the service name is wrongly parsed.
"""
url = request.build_absolute_uri(location)
url = url.replace("http", "https")
url = url.replace(".decent", "-dot-decent")

return url


class FirstVerifyEmailReminderView(CronMixin, APIView):
def get(self, request):
emails = get_unverified_emails(USER_1ST_VERIFY_EMAIL_REMINDER_DAYS)

logging.info(f"{len(emails)} emails unverified.")

if emails:
terms_url = build_absolute_google_uri(request, reverse("terms"))
privacy_notice_url = build_absolute_google_uri(request, reverse("privacy_notice"))

sent_email_count = 0
for email in emails:
email_verification_url = build_absolute_google_uri(
request,
reverse(
"verify_email",
kwargs={"token": generate_token_for_email(email)},
),
)

try:
send_email(
sender=NOTIFICATION_EMAIL,
recipients=[email],
subject="Awaiting verification",
title="Awaiting verification",
text_content=USER_1ST_VERIFY_EMAIL_REMINDER_TEXT.format(
email_verification_url=request.build_absolute_uri(
reverse(
"verify_email",
kwargs={
"token": generate_token_for_email(email)
},
)
),
terms_url=f"{settings.FRONTEND_URL}/terms-of-use/terms-of-use",
privacy_notice_url=f"{settings.FRONTEND_URL}/privacy-notice/privacy-notice",
email_verification_url=email_verification_url,
terms_url=terms_url,
privacy_notice_url=privacy_notice_url,
),
replace_url={"verify_url": email_verification_url},
)

sent_email_count += 1
Expand All @@ -99,26 +116,31 @@ def get(self, request):
logging.info(f"{len(emails)} emails unverified.")

if emails:
terms_url = build_absolute_google_uri(request, reverse("terms"))
privacy_notice_url = build_absolute_google_uri(request, reverse("privacy_notice"))

sent_email_count = 0
for email in emails:
email_verification_url = build_absolute_google_uri(
request,
reverse(
"verify_email",
kwargs={"token": generate_token_for_email(email)},
),
)

try:
send_email(
sender=NOTIFICATION_EMAIL,
recipients=[email],
subject="Your account needs verification",
title="Your account needs verification",
text_content=USER_2ND_VERIFY_EMAIL_REMINDER_TEXT.format(
email_verification_url=request.build_absolute_uri(
reverse(
"verify_email",
kwargs={
"token": generate_token_for_email(email)
},
)
),
terms_url=f"{settings.FRONTEND_URL}/terms-of-use/terms-of-use",
privacy_notice_url=f"{settings.FRONTEND_URL}/privacy-notice/privacy-notice",
email_verification_url=email_verification_url,
terms_url=terms_url,
privacy_notice_url=privacy_notice_url,
),
replace_url={"verify_url": email_verification_url},
)

sent_email_count += 1
Expand Down

0 comments on commit 7519ee0

Please sign in to comment.