-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Fix #1140 -- Update change-email process * Addressing the review * Forgot the other template * Add custom authentication backend * Add infor message when email is not verified * Fix message
- Loading branch information
1 parent
b5049c5
commit 99f3054
Showing
13 changed files
with
167 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from allauth.account.auth_backends import AuthenticationBackend as Backend | ||
from .models import User | ||
|
||
|
||
class AuthenticationBackend(Backend): | ||
def _authenticate_by_email(self, **credentials): | ||
# Even though allauth will pass along `email`, other apps may | ||
# not respect this setting. For example, when using | ||
# django-tastypie basic authentication, the login is always | ||
# passed as `username`. So let's place nice with other apps | ||
# and use username as fallback | ||
email = credentials.get('email', credentials.get('username')) | ||
try: | ||
user = User.objects.get(email__iexact=email) | ||
if user.check_password(credentials["password"]): | ||
return user | ||
except User.DoesNotExist: | ||
pass | ||
|
||
return None |
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,37 @@ | ||
from django.test import TestCase | ||
from allauth.account.models import EmailAddress | ||
from core.tests.utils.cases import UserTestCase | ||
from ..backends import AuthenticationBackend | ||
from .factories import UserFactory | ||
|
||
|
||
class AuthBackendTest(UserTestCase, TestCase): | ||
def setUp(self): | ||
super().setUp() | ||
self.user = UserFactory.create( | ||
username='kindofblue', | ||
email='[email protected]', | ||
password='PlayTh3Trumpet!' | ||
) | ||
self.backend = AuthenticationBackend() | ||
|
||
def test_login_with_email(self): | ||
credentials = {'email': '[email protected]', | ||
'password': 'PlayTh3Trumpet!'} | ||
assert self.backend._authenticate_by_email(**credentials) == self.user | ||
|
||
def test_login_with_unapproved_email(self): | ||
EmailAddress.objects.create( | ||
user=self.user, | ||
email='[email protected]', | ||
verified=False, | ||
primary=True | ||
) | ||
credentials = {'email': '[email protected]', | ||
'password': 'PlayTh3Trumpet!'} | ||
assert self.backend._authenticate_by_email(**credentials) is None | ||
|
||
def test_auth_in_username_field(self): | ||
credentials = {'username': '[email protected]', | ||
'password': 'PlayTh3Trumpet!'} | ||
assert self.backend._authenticate_by_email(**credentials) == self.user |
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 |
---|---|---|
|
@@ -231,8 +231,11 @@ def test_update_user(self): | |
|
||
user.refresh_from_db() | ||
assert user.full_name == 'John Lennon' | ||
assert user.email_verified is False | ||
assert len(mail.outbox) == 1 | ||
assert user.email == '[email protected]' | ||
assert user.email_verified is True | ||
assert len(mail.outbox) == 2 | ||
assert '[email protected]' in mail.outbox[0].to | ||
assert '[email protected]' in mail.outbox[1].to | ||
|
||
def test_display_name(self): | ||
user = UserFactory.create(username='imagine71', | ||
|
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 |
---|---|---|
|
@@ -26,9 +26,11 @@ def test_update_email_address(self): | |
data = {'email': '[email protected]', 'username': 'imagine71'} | ||
response = self.request(method='PUT', post_data=data, user=self.user) | ||
assert response.status_code == 200 | ||
assert len(mail.outbox) == 1 | ||
assert len(mail.outbox) == 2 | ||
assert '[email protected]' in mail.outbox[0].to | ||
assert '[email protected]' in mail.outbox[1].to | ||
self.user.refresh_from_db() | ||
assert self.user.email_verified is False | ||
assert self.user.email_verified is True | ||
|
||
def test_keep_email_address(self): | ||
"""Service should not send a verification email when the user does not | ||
|
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 |
---|---|---|
|
@@ -20,7 +20,8 @@ class ProfileTest(ViewTestCase, UserTestCase, TestCase): | |
|
||
def setup_template_context(self): | ||
return { | ||
'form': ProfileForm(instance=self.user) | ||
'form': ProfileForm(instance=self.user), | ||
'emails_to_verify': False | ||
} | ||
|
||
def test_get_profile(self): | ||
|
@@ -30,6 +31,32 @@ def test_get_profile(self): | |
assert response.status_code == 200 | ||
assert response.content == self.expected_content | ||
|
||
def test_get_profile_with_unverified_email(self): | ||
self.user = UserFactory.create() | ||
EmailAddress.objects.create( | ||
user=self.user, | ||
email='[email protected]', | ||
verified=False, | ||
primary=True | ||
) | ||
response = self.request(user=self.user) | ||
|
||
assert response.status_code == 200 | ||
assert response.content == self.render_content(emails_to_verify=True) | ||
|
||
def test_get_profile_with_verified_email(self): | ||
self.user = UserFactory.create() | ||
EmailAddress.objects.create( | ||
user=self.user, | ||
email=self.user.email, | ||
verified=True, | ||
primary=True | ||
) | ||
response = self.request(user=self.user) | ||
|
||
assert response.status_code == 200 | ||
assert response.content == self.expected_content | ||
|
||
def test_update_profile(self): | ||
user = UserFactory.create(username='John') | ||
post_data = { | ||
|
@@ -144,6 +171,27 @@ def test_activate(self): | |
self.email_address.refresh_from_db() | ||
assert self.email_address.verified is True | ||
|
||
def test_activate_changed_email(self): | ||
self.email_address.email = '[email protected]' | ||
self.email_address.save() | ||
|
||
EmailConfirmation.objects.create( | ||
email_address=self.email_address, | ||
sent=datetime.datetime.now(), | ||
key='456' | ||
) | ||
response = self.request(user=self.user, url_kwargs={'key': '456'}) | ||
assert response.status_code == 302 | ||
assert 'dashboard' in response.location | ||
|
||
self.user.refresh_from_db() | ||
assert self.user.email_verified is True | ||
assert self.user.is_active is True | ||
assert self.user.email == '[email protected]' | ||
|
||
self.email_address.refresh_from_db() | ||
assert self.email_address.verified is True | ||
|
||
def test_activate_with_invalid_token(self): | ||
response = self.request(user=self.user, url_kwargs={'key': 'abc'}) | ||
assert response.status_code == 200 | ||
|
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 @@ | ||
from django.conf import settings | ||
from django.utils.translation import ugettext as _ | ||
from django.core.mail import send_mail | ||
from django.template.loader import render_to_string | ||
|
||
|
||
def send_email_update_notification(email): | ||
msg_body = render_to_string( | ||
'accounts/email/email_changed_notification.txt') | ||
send_mail( | ||
_("Change of email at Cadasta Platform"), | ||
msg_body, | ||
settings.DEFAULT_FROM_EMAIL, | ||
[email], | ||
fail_silently=False, | ||
) |
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
11 changes: 11 additions & 0 deletions
11
cadasta/templates/accounts/email/email_changed_notification.txt
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,11 @@ | ||
{% load i18n %}{% autoescape off %} | ||
|
||
{% blocktrans %} | ||
You are receiving this email because a user at Cadasta Platform changed the email address for the account previously linked to this email address. | ||
|
||
If it wasn't you who changed the email address, please contact us immediately under [email protected]. | ||
{% endblocktrans %} | ||
|
||
{% blocktrans %}The Cadasta Team. {% endblocktrans %} | ||
|
||
{% endautoescape %} |
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
4 changes: 2 additions & 2 deletions
4
cadasta/templates/allauth/account/email/email_confirmation_message.txt
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