-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #136 from Theodrem/command_management_reset_password
add reset_password and update create_superuser_with_email command managements
- Loading branch information
Showing
8 changed files
with
256 additions
and
47 deletions.
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
63 changes: 63 additions & 0 deletions
63
concrete_datastore/concrete/management/commands/reset_password.py
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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# coding: utf-8 | ||
import random | ||
import string | ||
|
||
from django.apps import apps | ||
from django.conf import settings | ||
from django.contrib.auth import get_user_model | ||
from django.core.management.base import BaseCommand | ||
from django.core.management.base import CommandError | ||
|
||
|
||
def generate_random_password( | ||
length=24, chars=string.ascii_letters + string.digits | ||
): | ||
return ''.join(random.choice(chars) for _ in range(length)) | ||
|
||
|
||
def get_admin_url(): | ||
port = '' | ||
if int(settings.PORT) not in (80, 443): | ||
port = ':{}'.format(settings.PORT) | ||
|
||
admin_url = '{}://{}{}/concrete-datastore-admin/'.format( | ||
settings.SCHEME, settings.HOSTNAME, port | ||
) | ||
return admin_url | ||
|
||
|
||
class Command(BaseCommand): | ||
help = 'Reset password' | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument('email', type=str, help='user email') | ||
|
||
def handle(self, *args, **options): | ||
concrete = apps.get_app_config('concrete') | ||
EmailModel = concrete.models['email'] | ||
UserModel = get_user_model() | ||
email = options['email'].lower() | ||
|
||
try: | ||
user = UserModel.objects.get(email=email) | ||
except UserModel.DoesNotExist: | ||
raise CommandError(f"This email: {email} does not exist") | ||
|
||
password = generate_random_password() | ||
user.set_password(password) | ||
user.save() | ||
|
||
admin_url = get_admin_url() | ||
email_instance = EmailModel( | ||
created_by=user, | ||
subject='Reset password to Concrete Instance', | ||
body=settings.RESET_PASSWORD_EMAIL_MESSAGE_BODY.format( | ||
hostname=settings.HOSTNAME, | ||
admin_url=admin_url, | ||
email=email, | ||
password=password, | ||
), | ||
receiver=user, | ||
) | ||
|
||
email_instance.save() |
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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# coding: utf-8 | ||
from django.core.management import call_command | ||
from django.core.management.base import CommandError | ||
|
||
|
||
from django.test import TestCase | ||
|
||
from concrete_datastore.concrete.models import User | ||
|
||
|
||
class CreateSuperUserTests(TestCase): | ||
def setUp(self): | ||
self.user = User.objects.create_user( | ||
email='johndoe@netsach.org', password='plop' | ||
) | ||
self.user.save() | ||
|
||
def test_create_superuser(self): | ||
self.assertEqual(User.objects.all().count(), 1) | ||
call_command("create_superuser", "new_superuser@netsach.org", "plop") | ||
new_superuser = User.objects.get(email="new_superuser@netsach.org") | ||
|
||
self.assertEqual(User.objects.all().count(), 2) | ||
|
||
self.assertEqual(new_superuser.level, "superuser") | ||
self.assertTrue(new_superuser.admin) | ||
self.assertTrue(new_superuser.check_password("plop")) | ||
|
||
def test_create_superuser_email_already_exist(self): | ||
self.assertEqual(User.objects.all().count(), 1) | ||
call_command("create_superuser", "johndoe@netsach.org", "plop") | ||
|
||
self.assertEqual(User.objects.all().count(), 1) | ||
|
||
def test_create_superuser_args_are_none(self): | ||
with self.assertRaises(CommandError) as e: | ||
call_command('create_superuser') | ||
self.assertEqual( | ||
str(e.exception), | ||
"Error: the following arguments are required: email, password", | ||
) |
48 changes: 48 additions & 0 deletions
48
tests/tests_command_manager/test_create_superuser_with_email.py
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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from django.core.management import call_command | ||
from django.core.management.base import CommandError | ||
|
||
|
||
from django.test import TestCase | ||
|
||
from concrete_datastore.concrete.models import User, Email | ||
|
||
|
||
class CreateSuperUserWithEmailTests(TestCase): | ||
def setUp(self): | ||
self.super_user = User.objects.create_user( | ||
email='super_user@netsach.org', password='plop' | ||
) | ||
self.super_user.save() | ||
|
||
def test_create_superuser(self): | ||
self.assertEqual(User.objects.all().count(), 1) | ||
call_command( | ||
'create_superuser_with_email', "new_superuser@netsach.org" | ||
) | ||
new_superuser = User.objects.get(email="new_superuser@netsach.org") | ||
email_sent = Email.objects.get(created_by=new_superuser) | ||
|
||
self.assertEqual(Email.objects.all().count(), 1) | ||
self.assertEqual(User.objects.all().count(), 2) | ||
|
||
self.assertIn("Welcome to Concrete", email_sent.body) | ||
self.assertEqual(new_superuser.level, "superuser") | ||
self.assertTrue(new_superuser.admin) | ||
self.assertEqual("Access to Concrete Instance", email_sent.subject) | ||
|
||
def test_create_superuser_email_exists(self): | ||
self.assertEqual(User.objects.all().count(), 1) | ||
|
||
call_command('create_superuser_with_email', self.super_user.email) | ||
self.assertEqual(Email.objects.all().count(), 0) | ||
self.assertEqual(User.objects.all().count(), 1) | ||
|
||
def test_create_superuser_email_is_none(self): | ||
with self.assertRaises(CommandError) as e: | ||
call_command('create_superuser_with_email') | ||
self.assertEqual( | ||
str(e.exception), | ||
"Error: the following arguments are required: email", | ||
) | ||
|
||
self.assertEqual(0, Email.objects.all().count()) |
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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
from django.core.management import call_command | ||
from django.core.management.base import CommandError | ||
from django.test import TestCase | ||
|
||
from concrete_datastore.concrete.models import User, Email | ||
|
||
|
||
class ResetPasswordCommandManagementTests(TestCase): | ||
def setUp(self): | ||
self.user = User.objects.create_user( | ||
email='johndoe@netsach.org', password='plop' | ||
) | ||
self.user.save() | ||
|
||
def test_reset_password_user(self): | ||
self.assertTrue(self.user.check_password("plop")) | ||
call_command('reset_password', self.user.email) | ||
email_sent = Email.objects.get(created_by=self.user) | ||
self.user.refresh_from_db() | ||
|
||
self.assertEqual(1, Email.objects.all().count()) | ||
self.assertIn("You have requested a new password", email_sent.body) | ||
self.assertEqual( | ||
"Reset password to Concrete Instance", email_sent.subject | ||
) | ||
self.assertFalse(self.user.check_password("plop")) | ||
|
||
def test_reset_password_email_does_not_exist(self): | ||
email = "anonymous@netsach.org" | ||
self.assertEqual(Email.objects.all().count(), 0) | ||
with self.assertRaises(CommandError) as e: | ||
call_command('reset_password', email) | ||
self.assertEqual( | ||
str(e.exception), f"This email: {email} does not exist" | ||
) | ||
|
||
def test_reset_password_email_is_none(self): | ||
with self.assertRaises(CommandError) as e: | ||
call_command('reset_password') | ||
self.assertEqual( | ||
str(e.exception), | ||
"Error: the following arguments are required: email", | ||
) | ||
|
||
self.assertEqual(Email.objects.all().count(), 0) |