misc: add User.set_random_password() (#47943)

This commit is contained in:
Benjamin Dauvergne 2020-11-03 21:45:51 +01:00
parent 8c3902b2c2
commit 4a2305459f
2 changed files with 13 additions and 0 deletions

View File

@ -17,7 +17,9 @@
from __future__ import unicode_literals
import base64
import datetime
import os
import random
from django.db import models, transaction
@ -368,6 +370,9 @@ class User(AbstractBaseUser, PermissionMixin):
if save:
self.save(update_fields=['is_active', 'deleted'])
def set_random_password(self):
self.set_password(base64.b64encode(os.urandom(32)))
class DeletedUser(models.Model):
deleted = models.DateTimeField(

View File

@ -258,3 +258,11 @@ def test_save_userexternalid_on_delete_user(db):
'external_id': '4567',
}
]
def test_set_random_password():
user = User()
user.set_unusable_password()
assert not user.has_usable_password()
user.set_random_password()
assert user.has_usable_password()