Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
with self.settings(USER_PASSWORD_REPEAT_WINDOW=365):
self.assertTrue(PasswordHistory.is_password_repeat(self.plain, "Password1 "))
self.assertFalse(PasswordHistory.is_password_repeat(self.plain, "anotherpassword"))
# move our history into the past
history.set_on = timezone.now() - timedelta(days=366)
history.save()
# still a repeat because it is our current password
self.assertTrue(PasswordHistory.is_password_repeat(self.plain, "Password1 "))
# change our password under the covers
self.plain.set_password("my new password")
# now this one is fine
self.assertFalse(PasswordHistory.is_password_repeat(self.plain, "Password1 "))
with self.settings(USER_PASSWORD_REPEAT_WINDOW=-1):
history.set_on = timezone.now()
history.save()
self.assertFalse(PasswordHistory.is_password_repeat(self.plain, "Password1 "))
def testPasswordRepeat(self):
history = PasswordHistory.objects.create(user=self.plain,
password=self.plain.password)
with self.settings(USER_PASSWORD_REPEAT_WINDOW=365):
self.assertTrue(PasswordHistory.is_password_repeat(self.plain, "Password1 "))
self.assertFalse(PasswordHistory.is_password_repeat(self.plain, "anotherpassword"))
# move our history into the past
history.set_on = timezone.now() - timedelta(days=366)
history.save()
# still a repeat because it is our current password
self.assertTrue(PasswordHistory.is_password_repeat(self.plain, "Password1 "))
# change our password under the covers
self.plain.set_password("my new password")
# now this one is fine
self.assertFalse(PasswordHistory.is_password_repeat(self.plain, "Password1 "))
with self.settings(USER_PASSWORD_REPEAT_WINDOW=-1):
history.set_on = timezone.now()
def testPasswordRepeat(self):
history = PasswordHistory.objects.create(user=self.plain,
password=self.plain.password)
with self.settings(USER_PASSWORD_REPEAT_WINDOW=365):
self.assertTrue(PasswordHistory.is_password_repeat(self.plain, "Password1 "))
self.assertFalse(PasswordHistory.is_password_repeat(self.plain, "anotherpassword"))
# move our history into the past
history.set_on = timezone.now() - timedelta(days=366)
history.save()
# still a repeat because it is our current password
self.assertTrue(PasswordHistory.is_password_repeat(self.plain, "Password1 "))
# change our password under the covers
self.plain.set_password("my new password")
# now this one is fine
self.assertFalse(PasswordHistory.is_password_repeat(self.plain, "Password1 "))
with self.settings(USER_PASSWORD_REPEAT_WINDOW=-1):
def clean_new_password(self):
password = self.cleaned_data['new_password']
if password and not is_password_complex(password):
raise forms.ValidationError(_("Passwords must have at least 8 characters, including one uppercase, "
"one lowercase and one number"))
if password and PasswordHistory.is_password_repeat(self.instance, password):
raise forms.ValidationError(_("You have used this password before in the past year, "
"please use a new password."))
return password
def clean_confirm_new_password(self):
if 'new_password' not in self.cleaned_data:
return None
if not self.cleaned_data['confirm_new_password'] and self.cleaned_data['new_password']:
raise forms.ValidationError(_("Confirm the new password by filling the this field"))
if self.cleaned_data['new_password'] != self.cleaned_data['confirm_new_password']:
raise forms.ValidationError(_("New password doesn't match with its confirmation"))
password = self.cleaned_data['new_password']
if password and not is_password_complex(password):
raise forms.ValidationError(_("Passwords must have at least 8 characters, including one uppercase, "
"one lowercase and one number"))
if password and PasswordHistory.is_password_repeat(self.instance, password):
raise forms.ValidationError(_("You have used this password before in the past year, "
"please use a new password."))
return self.cleaned_data['new_password']