-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Accounts: For signup, login and password reset, username and password should be treated case insensitive #2067
Open
molokov
wants to merge
6
commits into
stephenmcd:master
Choose a base branch
from
molokov:accounts-2066
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9b43cc4
Fixing scenario where a JPG/GIF thumbnail is created with padding, re…
molokov c98e2d8
Merge branch 'stephenmcd:master' into master
molokov e7f7491
In utils/html, convert ALLOWED_PROTOCOLS to list before appending ["t…
molokov a89ef45
Changing ANTIALIAS to LANCZOS in thumbnail() Fixes #2065
molokov 4015354
Username and email checking is now case insensitive for signup, login…
molokov f10dc82
Fixing test case: assertFormError has changed in Django 4.1 and so ol…
molokov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,13 @@ | ||
from django.contrib.auth import get_user_model | ||
import django | ||
from django.contrib.auth import get_user, get_user_model | ||
from django.contrib.auth.tokens import default_token_generator | ||
from django.core import mail | ||
from django.forms.fields import DateField, DateTimeField | ||
from django.urls import reverse | ||
from django.utils.http import int_to_base36 | ||
|
||
from mezzanine.accounts import ProfileNotConfigured | ||
from mezzanine.accounts.forms import ProfileForm | ||
from mezzanine.accounts.forms import ProfileForm, PasswordResetForm | ||
from mezzanine.conf import settings | ||
from mezzanine.utils.tests import TestCase | ||
|
||
|
@@ -79,3 +80,161 @@ def test_account(self): | |
self.assertEqual(response.status_code, 200) | ||
users = User.objects.filter(email=data["email"], is_active=True) | ||
self.assertEqual(len(users), 1) | ||
|
||
self.client.logout() | ||
|
||
if django.VERSION[0:1] >= (4, 1): | ||
# This form of assertFormError is only available since Django 4.1 | ||
|
||
# Create another account with the same user name | ||
settings.ACCOUNTS_VERIFICATION_REQUIRED = False | ||
data = self.account_data("test1") | ||
form = ProfileForm(data=data) | ||
self.assertFormError(form, 'username', 'This username is already registered') | ||
|
||
# Create another account with the same user name, but case is different | ||
data['username'] = 'TEST1' | ||
form = ProfileForm(data=data) | ||
self.assertFormError(form, 'username', 'This username is already registered') | ||
|
||
# Create another account with a different username, but same email | ||
data['username'] = 'test3' | ||
form = ProfileForm(data=data) | ||
self.assertFormError(form, 'email', 'This email is already registered') | ||
|
||
# Create another account with a different username, but same email with different case | ||
data['email'] = '[email protected]' | ||
form = ProfileForm(data=data) | ||
self.assertFormError(form, 'email', 'This email is already registered') | ||
|
||
|
||
def test_account_login(self): | ||
""" | ||
Test account login | ||
""" | ||
# Create test user account | ||
data = self.account_data("test1") | ||
settings.ACCOUNTS_VERIFICATION_REQUIRED = False | ||
response = self.client.post(reverse("signup"), data, follow=True) | ||
self.assertEqual(response.status_code, 200) | ||
# Find the valid user | ||
users = User.objects.filter(email=data["email"], is_active=True) | ||
self.assertEqual(len(users), 1) | ||
test_user = users[0] | ||
|
||
self.client.logout() | ||
|
||
# Log in with username/password | ||
self.assertTrue(self.client.login(username=data['username'], | ||
password=data['password1'])) | ||
user = get_user(self.client) | ||
self.assertEqual(user, test_user) | ||
self.assertTrue(user.is_authenticated) | ||
self.client.logout() | ||
|
||
# Log in with email/password | ||
self.assertTrue(self.client.login(username=data['email'], | ||
password=data['password1'])) | ||
user = get_user(self.client) | ||
self.assertEqual(user, test_user) | ||
self.assertTrue(user.is_authenticated) | ||
self.client.logout() | ||
|
||
# Log in with bad password | ||
self.assertFalse(self.client.login(username=data['username'], | ||
password=data['password1'] + 'badbit')) | ||
user = get_user(self.client) | ||
self.assertFalse(user.is_authenticated) | ||
self.client.logout() | ||
|
||
# Log in with username (different case) and password | ||
self.assertTrue(self.client.login(username=data['username'].upper(), | ||
password=data['password1'])) | ||
user = get_user(self.client) | ||
self.assertEqual(user, test_user) | ||
self.assertTrue(user.is_authenticated) | ||
self.client.logout() | ||
|
||
# Log in with email (different case) and password | ||
self.assertTrue(self.client.login(username=data['email'].upper(), | ||
password=data['password1'])) | ||
user = get_user(self.client) | ||
self.assertEqual(user, test_user) | ||
self.assertTrue(user.is_authenticated) | ||
self.client.logout() | ||
|
||
def _verify_password_reset_email(self, new_user, num_emails): | ||
# Check email was sent | ||
self.assertEqual(len(mail.outbox), num_emails + 1) | ||
self.assertEqual(len(mail.outbox[0].to), 1) | ||
self.assertEqual(mail.outbox[0].to[0], new_user.email) | ||
verification_url = reverse( | ||
"password_reset_verify", | ||
kwargs={ | ||
"uidb36": int_to_base36(new_user.id), | ||
"token": default_token_generator.make_token(new_user), | ||
}, | ||
) | ||
response = self.client.get(verification_url, follow=True) | ||
self.assertEqual(response.status_code, 200) | ||
|
||
|
||
def test_account_password_reset(self): | ||
""" | ||
Test account password reset verification email | ||
""" | ||
# Create test user account | ||
data = self.account_data("test1") | ||
settings.ACCOUNTS_VERIFICATION_REQUIRED = False | ||
response = self.client.post(reverse("signup"), data, follow=True) | ||
self.assertEqual(response.status_code, 200) | ||
# Find the valid user | ||
users = User.objects.filter(email=data["email"], is_active=True) | ||
self.assertEqual(len(users), 1) | ||
new_user = users[0] | ||
self.client.logout() | ||
|
||
# Reset password with username | ||
emails = len(mail.outbox) | ||
rdata = {'username': data['username']} | ||
response = self.client.post(reverse("mezzanine_password_reset"), rdata, follow=True) | ||
self.assertEqual(response.status_code, 200) | ||
self._verify_password_reset_email(new_user, emails) | ||
self.client.logout() | ||
|
||
# Reset password with email | ||
emails = len(mail.outbox) | ||
rdata = {'username': data['email']} | ||
response = self.client.post(reverse("mezzanine_password_reset"), rdata, follow=True) | ||
self.assertEqual(response.status_code, 200) | ||
self._verify_password_reset_email(new_user, emails) | ||
self.client.logout() | ||
|
||
# Reset password with username (different case) | ||
emails = len(mail.outbox) | ||
rdata = {'username': data['username'].upper()} | ||
response = self.client.post(reverse("mezzanine_password_reset"), rdata, follow=True) | ||
self.assertEqual(response.status_code, 200) | ||
self._verify_password_reset_email(new_user, emails) | ||
self.client.logout() | ||
|
||
# Reset password with email (different case) | ||
emails = len(mail.outbox) | ||
rdata = {'username': data['email'].upper()} | ||
response = self.client.post(reverse("mezzanine_password_reset"), rdata, follow=True) | ||
self.assertEqual(response.status_code, 200) | ||
self._verify_password_reset_email(new_user, emails) | ||
self.client.logout() | ||
|
||
if django.VERSION[0:1] >= (4, 1): | ||
# This form of assertFormError is only available since Django 4.1 | ||
|
||
# Reset password with invalid username | ||
rdata = {'username': 'badusername'} | ||
form = PasswordResetForm(data=rdata) | ||
self.assertFormError(form, None, 'Invalid username/email') | ||
|
||
# Reset password with invalid email | ||
rdata = {'username': '[email protected]'} | ||
form = PasswordResetForm(data=rdata) | ||
self.assertFormError(form, None, 'Invalid username/email') |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this change probably doesn't need to go back to main, I'm sure there's a better solution to changing a JPG's thumbnail to PNG (to allow for transparency background when using padding). Feel free to skip these lines on merge