generated from ocadotechnology/codeforlife-template-backend
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into portal-frontend-70
- Loading branch information
Showing
12 changed files
with
156 additions
and
82 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,8 +4,9 @@ | |
""" | ||
|
||
import typing as t | ||
from copy import deepcopy | ||
from datetime import date | ||
from unittest.mock import Mock, patch | ||
from unittest.mock import Mock, call, patch | ||
|
||
from codeforlife.tests import ModelSerializerTestCase | ||
from codeforlife.user.models import ( | ||
|
@@ -16,10 +17,15 @@ | |
StudentUser, | ||
User, | ||
) | ||
from django.conf import settings | ||
from django.contrib.auth.hashers import make_password | ||
from django.db.models import Count | ||
from django.urls import reverse | ||
|
||
from ..auth import password_reset_token_generator | ||
from ..auth import ( | ||
email_verification_token_generator, | ||
password_reset_token_generator, | ||
) | ||
from .user import ( | ||
BaseUserSerializer, | ||
CreateUserSerializer, | ||
|
@@ -264,7 +270,7 @@ def test_validate_requesting_to_join_class__no_longer_accepts_requests( | |
error_code="no_longer_accepts_requests", | ||
) | ||
|
||
def test_update(self): | ||
def test_update__requesting_to_join_class(self): | ||
"""Can update the class an independent user is requesting join.""" | ||
self.assert_update( | ||
instance=self.indy_user, | ||
|
@@ -276,6 +282,56 @@ def test_update(self): | |
new_data={"new_student": {"pending_class_request": self.class_2}}, | ||
) | ||
|
||
@patch("src.api.serializers.user.send_mail") | ||
def test_update__email(self, send_mail: Mock): | ||
"""Requesting to update the email field sends a notification email to | ||
the old address and a verification email to the new address.""" | ||
instance = self.admin_school_teacher_user | ||
previous_email = instance.email | ||
new_email = "[email protected]" | ||
validated_data = {"email": new_email} | ||
|
||
serializer = self._init_model_serializer() | ||
|
||
with patch.object( | ||
email_verification_token_generator, | ||
"make_token", | ||
return_value=email_verification_token_generator.make_token( | ||
instance, new_email | ||
), | ||
) as make_token: | ||
serializer.update(instance, deepcopy(validated_data)) | ||
|
||
make_token.assert_called_once_with(instance.pk, new_email) | ||
|
||
send_mail.assert_has_calls( | ||
[ | ||
call( | ||
settings.DOTDIGITAL_CAMPAIGN_IDS["Email will change"], | ||
to_addresses=[previous_email], | ||
personalization_values={"NEW_EMAIL_ADDRESS": new_email}, | ||
), | ||
call( | ||
settings.DOTDIGITAL_CAMPAIGN_IDS[ | ||
"Verify changed user email" | ||
], | ||
to_addresses=[new_email], | ||
personalization_values={ | ||
"VERIFICATION_LINK": ( | ||
settings.SERVICE_BASE_URL | ||
+ reverse( | ||
"user-verify-email-address", | ||
kwargs={ | ||
"pk": instance.pk, | ||
"token": make_token.return_value, | ||
}, | ||
) | ||
) | ||
}, | ||
), | ||
] | ||
) | ||
|
||
|
||
class TestHandleIndependentUserJoinClassRequestSerializer( | ||
ModelSerializerTestCase[User, IndependentUser] | ||
|
@@ -470,11 +526,18 @@ def test_validate_token__does_not_match(self): | |
) | ||
|
||
def test_update(self): | ||
"""Can successfully reset a user's password.""" | ||
"""Can successfully verify a user's email.""" | ||
email = "[email protected]" | ||
|
||
self.assert_update( | ||
instance=self.user, | ||
validated_data={}, | ||
new_data={"userprofile": {"is_verified": True}}, | ||
non_model_fields={"token"}, | ||
validated_data={"token": {"email": email}}, | ||
new_data={ | ||
"userprofile": {"is_verified": True}, | ||
"email": email, | ||
"username": email, | ||
}, | ||
) | ||
|
||
|
||
|
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
Oops, something went wrong.