mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-07-28 18:24:38 -05:00
add test case, update password if changed
This commit is contained in:
@@ -1,9 +1,13 @@
|
||||
import logging
|
||||
import os
|
||||
|
||||
from django.contrib.auth.models import User
|
||||
from django.core.management.base import BaseCommand, CommandError
|
||||
|
||||
|
||||
logger = logging.getLogger("paperless.management.superuser")
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
|
||||
help = """
|
||||
@@ -12,29 +16,25 @@ class Command(BaseCommand):
|
||||
|
||||
def handle(self, *args, **options):
|
||||
|
||||
PAPERLESS_ADMIN_USER = os.getenv('PAPERLESS_ADMIN_USER')
|
||||
PAPERLESS_ADMIN_MAIL = os.getenv('PAPERLESS_ADMIN_MAIL', 'root@localhost')
|
||||
PAPERLESS_ADMIN_PASSWORD = os.getenv('PAPERLESS_ADMIN_PASSWORD')
|
||||
username = os.getenv('PAPERLESS_ADMIN_USER')
|
||||
if not username:
|
||||
return
|
||||
|
||||
# If PAPERLESS_ADMIN_USER env variable is set
|
||||
if PAPERLESS_ADMIN_USER:
|
||||
try:
|
||||
# Check if user exists already, leave as is if it does
|
||||
if User.objects.filter(
|
||||
username=PAPERLESS_ADMIN_USER).exists():
|
||||
self.stdout.write(
|
||||
f'The user "{PAPERLESS_ADMIN_USER}" already exists! Leaving user as is.')
|
||||
elif PAPERLESS_ADMIN_PASSWORD:
|
||||
# Create superuser based on env variables
|
||||
User.objects.create_superuser(
|
||||
PAPERLESS_ADMIN_USER, PAPERLESS_ADMIN_MAIL, PAPERLESS_ADMIN_PASSWORD)
|
||||
self.stdout.write(
|
||||
f'Created superuser "{PAPERLESS_ADMIN_USER}" with provided password.')
|
||||
else:
|
||||
self.stdout.write(
|
||||
f'Did not create superuser "{PAPERLESS_ADMIN_USER}".')
|
||||
self.stdout.write(
|
||||
'Make sure you specified "PAPERLESS_ADMIN_PASSWORD" in your "docker-compose.env" file.')
|
||||
except Exception as error:
|
||||
self.stdout.write(
|
||||
f'Exception occured while creating superuser: {error}')
|
||||
mail = os.getenv('PAPERLESS_ADMIN_MAIL', 'root@localhost')
|
||||
password = os.getenv('PAPERLESS_ADMIN_PASSWORD')
|
||||
|
||||
# Check if user exists already, leave as is if it does
|
||||
if User.objects.filter(username=username).exists():
|
||||
user: User = User.objects.get_by_natural_key(username)
|
||||
user.set_password(password)
|
||||
user.save()
|
||||
elif password:
|
||||
# Create superuser based on env variables
|
||||
User.objects.create_superuser(username, mail, password)
|
||||
self.stdout.write(
|
||||
f'Created superuser "{username}" with provided password.')
|
||||
else:
|
||||
self.stdout.write(
|
||||
f'Did not create superuser "{username}".')
|
||||
self.stdout.write(
|
||||
'Make sure you specified "PAPERLESS_ADMIN_PASSWORD" in your "docker-compose.env" file.')
|
||||
|
66
src/documents/tests/test_management_superuser.py
Normal file
66
src/documents/tests/test_management_superuser.py
Normal file
@@ -0,0 +1,66 @@
|
||||
import os
|
||||
import shutil
|
||||
from unittest import mock
|
||||
|
||||
from django.contrib.auth.models import User
|
||||
from django.core.management import call_command
|
||||
from django.test import TestCase
|
||||
|
||||
from documents.management.commands.document_thumbnails import _process_document
|
||||
from documents.models import Document, Tag, Correspondent, DocumentType
|
||||
from documents.tests.utils import DirectoriesMixin
|
||||
|
||||
|
||||
class TestManageSuperUser(DirectoriesMixin, TestCase):
|
||||
|
||||
def reset_environment(self):
|
||||
if "PAPERLESS_ADMIN_USER" in os.environ:
|
||||
del os.environ["PAPERLESS_ADMIN_USER"]
|
||||
if "PAPERLESS_ADMIN_PASSWORD" in os.environ:
|
||||
del os.environ["PAPERLESS_ADMIN_PASSWORD"]
|
||||
|
||||
def setUp(self) -> None:
|
||||
super().setUp()
|
||||
self.reset_environment()
|
||||
|
||||
def tearDown(self) -> None:
|
||||
super().tearDown()
|
||||
self.reset_environment()
|
||||
|
||||
def test_no_user(self):
|
||||
call_command("manage_superuser")
|
||||
|
||||
# just the consumer user.
|
||||
self.assertEqual(User.objects.count(), 1)
|
||||
self.assertTrue(User.objects.filter(username="consumer").exists())
|
||||
|
||||
def test_create(self):
|
||||
os.environ["PAPERLESS_ADMIN_USER"] = "new_user"
|
||||
os.environ["PAPERLESS_ADMIN_PASSWORD"] = "123456"
|
||||
|
||||
call_command("manage_superuser")
|
||||
|
||||
user: User = User.objects.get_by_natural_key("new_user")
|
||||
self.assertTrue(user.check_password("123456"))
|
||||
|
||||
def test_update(self):
|
||||
os.environ["PAPERLESS_ADMIN_USER"] = "new_user"
|
||||
os.environ["PAPERLESS_ADMIN_PASSWORD"] = "123456"
|
||||
|
||||
call_command("manage_superuser")
|
||||
|
||||
os.environ["PAPERLESS_ADMIN_USER"] = "new_user"
|
||||
os.environ["PAPERLESS_ADMIN_PASSWORD"] = "more_secure_pwd_7645"
|
||||
|
||||
call_command("manage_superuser")
|
||||
|
||||
user: User = User.objects.get_by_natural_key("new_user")
|
||||
self.assertTrue(user.check_password("more_secure_pwd_7645"))
|
||||
|
||||
def test_no_password(self):
|
||||
os.environ["PAPERLESS_ADMIN_USER"] = "new_user"
|
||||
|
||||
call_command("manage_superuser")
|
||||
|
||||
with self.assertRaises(User.DoesNotExist):
|
||||
User.objects.get_by_natural_key("new_user")
|
Reference in New Issue
Block a user