Files
paperless-ngx/src/documents/tests/test_management_superuser.py
T
Trenton H b457610ffb Chore: Fix bugs in the test suite (#14244)
* Fix: redirect SHARE_LINK_BUNDLE_DIR to the test temp layout instead of the real media directory

* Fix: include f_to in test_filters subTest labels so each of the 8 cases reports distinctly

* Fix: run the post_consume error-log assertion after the raising call and match the actual paperless_mail logger name

* Fix: rename the blank-password workflow test to match its behavior and add a real wrong-password-fails test

* Fix: use the created social account's actual pk and remove an accidental tuple wrapping the mock provider

* Fix: assert against the created documents' actual pks instead of hardcoded 1 and 2

* Fix: assert test_compression actually produces a valid LZMA-compressed zip

* Fix: clear os.environ when patching PAPERLESS_ADMIN_* vars so a host-set value can't leak into the no-user test

* Fix: restore MIDDLEWARE, AUTHENTICATION_BACKENDS and REST_FRAMEWORK auth classes after each remote-user settings test instead of leaking the mutation into later tests

* Fix: use a guaranteed-nonexistent temp path instead of hardcoded /tmp/foo/bar in test_export_target_not_exists
2026-09-23 19:11:58 +00:00

184 lines
6.1 KiB
Python

import os
from io import StringIO
from unittest import mock
import pytest
from django.contrib.auth.models import User
from django.core.management import call_command
from django.test import TestCase
from paperless_testing.dirs import DirectoriesMixin
@pytest.mark.management
class TestManageSuperUser(DirectoriesMixin, TestCase):
def call_command(self, environ):
out = StringIO()
with mock.patch.dict(os.environ, environ, clear=True):
call_command(
"manage_superuser",
"--no-color",
stdout=out,
stderr=StringIO(),
skip_checks=True,
)
return out.getvalue()
def test_no_user(self) -> None:
"""
GIVEN:
- Environment does not contain admin user info
THEN:
- No admin user is created
"""
out = self.call_command(environ={})
# just the consumer user which is created
# during migration, and AnonymousUser
self.assertEqual(User.objects.count(), 1)
self.assertEqual(User.objects.filter(is_superuser=True).count(), 0)
self.assertEqual(
out,
"Please check if PAPERLESS_ADMIN_PASSWORD has been set in the environment\n",
)
def test_create(self) -> None:
"""
GIVEN:
- Environment does contain admin user password
THEN:
- admin user is created
"""
out = self.call_command(environ={"PAPERLESS_ADMIN_PASSWORD": "123456"})
# count is 3 as there's the consumer
# user already created during migration, and AnonymousUser
user: User = User.objects.get_by_natural_key("admin")
self.assertEqual(User.objects.count(), 2)
self.assertTrue(user.is_superuser)
self.assertEqual(user.email, "root@localhost")
self.assertEqual(out, 'Created superuser "admin" with provided password.\n')
def test_some_superuser_exists(self) -> None:
"""
GIVEN:
- A super user already exists
- Environment does contain admin user password
THEN:
- admin user is NOT created
"""
User.objects.create_superuser("someuser", "root@localhost", "password")
out = self.call_command(environ={"PAPERLESS_ADMIN_PASSWORD": "123456"})
self.assertEqual(User.objects.count(), 2)
with self.assertRaises(User.DoesNotExist):
User.objects.get_by_natural_key("admin")
self.assertEqual(
out,
"Did not create superuser, the DB already contains superusers\n",
)
def test_admin_superuser_exists(self) -> None:
"""
GIVEN:
- A super user already exists
- The existing superuser's username is admin
- Environment does contain admin user password
THEN:
- Password remains unchanged
"""
User.objects.create_superuser("admin", "root@localhost", "password")
out = self.call_command(environ={"PAPERLESS_ADMIN_PASSWORD": "123456"})
self.assertEqual(User.objects.count(), 2)
user: User = User.objects.get_by_natural_key("admin")
self.assertTrue(user.check_password("password"))
self.assertEqual(out, "Did not create superuser, a user admin already exists\n")
def test_admin_user_exists(self) -> None:
"""
GIVEN:
- A user already exists with the username admin
- Environment does contain admin user password
THEN:
- Password remains unchanged
- User is not upgraded to superuser
"""
User.objects.create_user("admin", "root@localhost", "password")
out = self.call_command(environ={"PAPERLESS_ADMIN_PASSWORD": "123456"})
self.assertEqual(User.objects.count(), 2)
user: User = User.objects.get_by_natural_key("admin")
self.assertTrue(user.check_password("password"))
self.assertFalse(user.is_superuser)
self.assertEqual(out, "Did not create superuser, a user admin already exists\n")
def test_no_password(self) -> None:
"""
GIVEN:
- No environment data is set
THEN:
- No user is created
"""
out = self.call_command(environ={})
with self.assertRaises(User.DoesNotExist):
User.objects.get_by_natural_key("admin")
self.assertEqual(
out,
"Please check if PAPERLESS_ADMIN_PASSWORD has been set in the environment\n",
)
def test_user_email(self) -> None:
"""
GIVEN:
- Environment does contain admin user password
- Environment contains user email
THEN:
- admin user is created
"""
out = self.call_command(
environ={
"PAPERLESS_ADMIN_PASSWORD": "123456",
"PAPERLESS_ADMIN_MAIL": "hello@world.com",
},
)
user: User = User.objects.get_by_natural_key("admin")
self.assertEqual(User.objects.count(), 2)
self.assertTrue(user.is_superuser)
self.assertEqual(user.email, "hello@world.com")
self.assertEqual(user.username, "admin")
self.assertEqual(out, 'Created superuser "admin" with provided password.\n')
def test_user_username(self) -> None:
"""
GIVEN:
- Environment does contain admin user password
- Environment contains user username
THEN:
- admin user is created
"""
out = self.call_command(
environ={
"PAPERLESS_ADMIN_PASSWORD": "123456",
"PAPERLESS_ADMIN_MAIL": "hello@world.com",
"PAPERLESS_ADMIN_USER": "super",
},
)
user: User = User.objects.get_by_natural_key("super")
self.assertEqual(User.objects.count(), 2)
self.assertTrue(user.is_superuser)
self.assertEqual(user.email, "hello@world.com")
self.assertEqual(user.username, "super")
self.assertEqual(out, 'Created superuser "super" with provided password.\n')