From 5cee9dee125c40669cdf2ebd2c37aa17ae11bc66 Mon Sep 17 00:00:00 2001 From: shanukun Date: Mon, 5 Apr 2021 22:11:59 +0530 Subject: [PATCH] test_email_change: User create_confirmation_link for url. This is a prep refactor, instead of creating Confirmation object and using `confirmation_url` for generating confirmation link/url, using `create_confirmation_link` would be a cleaner approach, also this can help us avoid failing test in case Confirmation model is changed. Part of #16359. --- zerver/tests/test_email_change.py | 37 ++++++++++--------------------- 1 file changed, 12 insertions(+), 25 deletions(-) diff --git a/zerver/tests/test_email_change.py b/zerver/tests/test_email_change.py index 236b8bf7a0..6c5d3fc7d6 100644 --- a/zerver/tests/test_email_change.py +++ b/zerver/tests/test_email_change.py @@ -1,10 +1,16 @@ import datetime +from unittest import mock from django.conf import settings from django.core import mail from django.utils.timezone import now -from confirmation.models import Confirmation, confirmation_url, generate_key +from confirmation.models import ( + Confirmation, + confirmation_url, + create_confirmation_link, + generate_key, +) from zerver.lib.actions import do_set_realm_property, do_start_email_change_process from zerver.lib.test_classes import ZulipTestCase from zerver.models import ( @@ -46,15 +52,10 @@ class EmailChangeTestCase(ZulipTestCase): user_profile=user_profile, realm=user_profile.realm, ) - key = generate_key() date_sent = now() - datetime.timedelta(days=2) - Confirmation.objects.create( - content_object=obj, - date_sent=date_sent, - confirmation_key=key, - type=Confirmation.EMAIL_CHANGE, - ) - url = confirmation_url(key, user_profile.realm, Confirmation.EMAIL_CHANGE) + with mock.patch("confirmation.models.timezone_now", return_value=date_sent): + url = create_confirmation_link(obj, Confirmation.EMAIL_CHANGE) + response = self.client_get(url) self.assert_in_success_response( ["The confirmation link has expired or been deactivated."], response @@ -79,14 +80,7 @@ class EmailChangeTestCase(ZulipTestCase): user_profile=user_profile, realm=user_profile.realm, ) - key = generate_key() - Confirmation.objects.create( - content_object=obj, - date_sent=now(), - confirmation_key=key, - type=Confirmation.EMAIL_CHANGE, - ) - url = confirmation_url(key, user_profile.realm, Confirmation.EMAIL_CHANGE) + url = create_confirmation_link(obj, Confirmation.EMAIL_CHANGE) response = self.client_get(url) self.assertEqual(response.status_code, 200) @@ -233,14 +227,7 @@ class EmailChangeTestCase(ZulipTestCase): user_profile=user_profile, realm=user_profile.realm, ) - key = generate_key() - Confirmation.objects.create( - content_object=obj, - date_sent=now(), - confirmation_key=key, - type=Confirmation.EMAIL_CHANGE, - ) - url = confirmation_url(key, user_profile.realm, Confirmation.EMAIL_CHANGE) + url = create_confirmation_link(obj, Confirmation.EMAIL_CHANGE) response = self.client_get(url) self.assertEqual(response.status_code, 200)