-
Notifications
You must be signed in to change notification settings - Fork 318
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add option to create initial superuser
- Loading branch information
1 parent
e4636c0
commit 6dbc99a
Showing
4 changed files
with
100 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import os | ||
import logging | ||
|
||
from django.core.management.base import BaseCommand | ||
from django.contrib.auth import get_user_model | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "Creates an initial superuser for a deployment using env variables" | ||
|
||
def handle(self, *args, **options): | ||
User = get_user_model() | ||
superuser_name = os.getenv('LD_SUPERUSER_NAME', None) | ||
superuser_password = os.getenv('LD_SUPERUSER_PASSWORD', None) | ||
|
||
# Skip if option is undefined | ||
if not superuser_name: | ||
logger.info('Skip creating initial superuser, LD_SUPERUSER_NAME option is not defined') | ||
return | ||
|
||
# Skip if user already exists | ||
user_exists = User.objects.filter(username=superuser_name).exists() | ||
if user_exists: | ||
logger.info('Skip creating initial superuser, user already exists') | ||
return | ||
|
||
user = User(username=superuser_name, is_superuser=True, is_staff=True) | ||
|
||
if superuser_password: | ||
user.set_password(superuser_password) | ||
else: | ||
user.set_unusable_password() | ||
|
||
user.save() | ||
logger.info('Created initial superuser') |
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 @@ | ||
import os | ||
from unittest import mock | ||
|
||
from django.test import TestCase | ||
|
||
from bookmarks.models import User | ||
from bookmarks.management.commands.create_initial_superuser import Command | ||
|
||
|
||
class TestCreateInitialSuperuserCommand(TestCase): | ||
|
||
@mock.patch.dict(os.environ, {'LD_SUPERUSER_NAME': 'john', 'LD_SUPERUSER_PASSWORD': 'password123'}) | ||
def test_create_with_password(self): | ||
Command().handle() | ||
|
||
self.assertEqual(1, User.objects.count()) | ||
|
||
user = User.objects.first() | ||
self.assertEqual('john', user.username) | ||
self.assertTrue(user.has_usable_password()) | ||
self.assertTrue(user.check_password('password123')) | ||
|
||
@mock.patch.dict(os.environ, {'LD_SUPERUSER_NAME': 'john'}) | ||
def test_create_without_password(self): | ||
Command().handle() | ||
|
||
self.assertEqual(1, User.objects.count()) | ||
|
||
user = User.objects.first() | ||
self.assertEqual('john', user.username) | ||
self.assertFalse(user.has_usable_password()) | ||
|
||
def test_create_without_options(self): | ||
Command().handle() | ||
|
||
self.assertEqual(0, User.objects.count()) | ||
|
||
@mock.patch.dict(os.environ, {'LD_SUPERUSER_NAME': 'john', 'LD_SUPERUSER_PASSWORD': 'password123'}) | ||
def test_create_multiple_times(self): | ||
Command().handle() | ||
Command().handle() | ||
Command().handle() | ||
|
||
self.assertEqual(1, User.objects.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
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