-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changes: - Change token to code to be consistent with other models. - Ensure only one EmailVerification record is created per email. - Add email verification expiry.
- Loading branch information
1 parent
4cd26ca
commit 19ff22e
Showing
10 changed files
with
255 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{% extends "layout/sidebar.html" %} | ||
{% import "forms/forms.html" as forms %} | ||
|
||
{% block page_title %}Email Verification Expired{% endblock %} | ||
{#{% block context_header %}Email Verification Expired{% endblock %}#} | ||
|
||
|
||
{% block main_content %} | ||
<h3>This link has now expired</h3> | ||
<div class="info-box info-box-warning"> | ||
This email verification link has now expired. | ||
</div> | ||
{%- call forms.form(action=icms_url('user-send-verify-email',kwargs={'email_pk': email.pk}), method='post', csrf_input=csrf_input) -%} | ||
{{ forms.submit_button(padding_cols="zero", btn_label="Resend verification email") }} | ||
{%- endcall -%} | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import datetime as dt | ||
|
||
from django.contrib.sites.models import Site | ||
from django.utils import timezone | ||
from freezegun import freeze_time | ||
|
||
from web.domains.user.utils import send_and_create_email_verification | ||
from web.mail.constants import EmailTypes | ||
from web.mail.url_helpers import get_email_verification_url | ||
from web.models import Email, EmailVerification | ||
from web.sites import SiteName, get_importer_site_domain | ||
from web.tests.helpers import check_gov_notify_email_was_sent | ||
|
||
|
||
def test_send_and_create_email_verification(importer_one_contact): | ||
email = Email.objects.create( | ||
email="[email protected]", type=Email.WORK, user=importer_one_contact # /PS-IGNORE | ||
) | ||
site = Site.objects.get(name=SiteName.IMPORTER) | ||
|
||
send_and_create_email_verification(email, site) | ||
|
||
email.refresh_from_db() | ||
assert email.emailverification_set.count() == 1 | ||
_assert_email_sent(email.emailverification_set.first()) | ||
|
||
|
||
def test_resend_and_create_email_verification(importer_one_contact): | ||
email = Email.objects.create( | ||
email="[email protected]", type=Email.WORK, user=importer_one_contact # /PS-IGNORE | ||
) | ||
verification = EmailVerification.objects.create(email=email) | ||
|
||
site = Site.objects.get(name=SiteName.IMPORTER) | ||
|
||
send_and_create_email_verification(email, site) | ||
|
||
email.refresh_from_db() | ||
|
||
assert email.emailverification_set.count() == 1 | ||
assert email.emailverification_set.first() == verification | ||
_assert_email_sent(verification) | ||
|
||
|
||
def test_resend_and_create_email_verification_for_expired_email_verification(importer_one_contact): | ||
email = Email.objects.create( | ||
email="[email protected]", type=Email.WORK, user=importer_one_contact # /PS-IGNORE | ||
) | ||
older_than_two_weeks = timezone.now() - dt.timedelta(weeks=2, microseconds=1) | ||
with freeze_time(older_than_two_weeks): | ||
verification = EmailVerification.objects.create(email=email) | ||
|
||
site = Site.objects.get(name=SiteName.IMPORTER) | ||
|
||
send_and_create_email_verification(email, site) | ||
|
||
latest_verification = email.emailverification_set.last() | ||
|
||
email.refresh_from_db() | ||
verification.refresh_from_db() | ||
|
||
assert email.emailverification_set.count() == 2 | ||
assert email.emailverification_set.last() != verification | ||
assert verification.processed is True | ||
assert latest_verification.processed is False | ||
_assert_email_sent(latest_verification) | ||
|
||
|
||
def _assert_email_sent(verification: EmailVerification) -> None: | ||
check_gov_notify_email_was_sent( | ||
1, | ||
["[email protected]"], # /PS-IGNORE | ||
EmailTypes.EMAIL_VERIFICATION, | ||
{ | ||
"icms_url": get_importer_site_domain(), | ||
"service_name": SiteName.IMPORTER.label, | ||
"email_verification_url": get_email_verification_url( | ||
verification, get_importer_site_domain() | ||
), | ||
}, | ||
) |
Oops, something went wrong.