Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_expiration(self):
# create a fake password set 90 days ago
ninety_days_ago = timezone.now() - timedelta(days=90)
history = PasswordHistory.objects.create(user=self.plain,
password=self.plain.password)
history.set_on = ninety_days_ago
history.save()
# log in
self.client.logout()
post_data = dict(username='plain', password='Password1 ')
response = self.client.post(reverse('users.user_login'), post_data, follow=True)
# assert we are being taken to our new password page
self.assertTrue('form' in response.context)
self.assertTrue('new_password' in response.context['form'].fields)
# try to go to a different page
response = self.client.get(reverse('blog.post_list'), follow=True)
def testNoExpiration(self):
# create a fake password set 90 days ago
ninety_days_ago = timezone.now() - timedelta(days=90)
history = PasswordHistory.objects.create(user=self.plain,
password="asdfasdf")
history.set_on = ninety_days_ago
history.save()
# log in
self.client.logout()
post_data = dict(username='plain', password='Password1 ')
response = self.client.post(reverse('users.user_login'), post_data, follow=True)
self.assertTrue(response.context['user'].is_authenticated)
# we shouldn't be on a page asking us for a new password
self.assertFalse('form' in response.context)
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")
def post_save(self, obj):
obj = super(UserCRUDL.Profile, self).post_save(obj)
if 'new_password' in self.form.cleaned_data and self.form.cleaned_data['new_password']:
FailedLogin.objects.filter(user=self.object).delete()
PasswordHistory.objects.create(user=obj, password=obj.password)
return obj
def post_save(self, obj):
"""
Make sure our groups are up to date
"""
if 'groups' in self.form.cleaned_data:
obj.groups.clear()
for group in self.form.cleaned_data['groups']:
obj.groups.add(group)
# if a new password was set, reset our failed logins
if 'new_password' in self.form.cleaned_data and self.form.cleaned_data['new_password']:
FailedLogin.objects.filter(user=self.object).delete()
PasswordHistory.objects.create(user=obj, password=obj.password)
return obj