Skip to content

Commit

Permalink
Fixing test case: assertFormError has changed in Django 4.1 and so ol…
Browse files Browse the repository at this point in the history
…der versions of Django won't support the form I'm using in the tests.
  • Loading branch information
molokov committed Dec 24, 2023
1 parent 4015354 commit f10dc82
Showing 1 changed file with 32 additions and 25 deletions.
57 changes: 32 additions & 25 deletions tests/test_accounts.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
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
Expand Down Expand Up @@ -82,26 +83,29 @@ def test_account(self):

self.client.logout()

# 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')
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, but case is different
data['username'] = 'TEST1'
form = ProfileForm(data=data)
self.assertFormError(form, 'username', 'This username is already registered')
# 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 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 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 with different case
data['email'] = '[email protected]'
form = ProfileForm(data=data)
self.assertFormError(form, 'email', 'This email 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):
Expand Down Expand Up @@ -222,12 +226,15 @@ def test_account_password_reset(self):
self._verify_password_reset_email(new_user, emails)
self.client.logout()

# Reset password with invalid username
rdata = {'username': 'badusername'}
form = PasswordResetForm(data=rdata)
self.assertFormError(form, None, 'Invalid username/email')
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')
# Reset password with invalid email
rdata = {'username': '[email protected]'}
form = PasswordResetForm(data=rdata)
self.assertFormError(form, None, 'Invalid username/email')

0 comments on commit f10dc82

Please sign in to comment.