mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2026-10-03 06:40:31 +00:00
* 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
162 lines
4.8 KiB
Python
162 lines
4.8 KiB
Python
import os
|
|
from unittest import mock
|
|
|
|
from django.conf import settings
|
|
from django.test import override_settings
|
|
from rest_framework import status
|
|
from rest_framework.test import APITestCase
|
|
|
|
from paperless.settings import _parse_remote_user_settings
|
|
from paperless_testing.dirs import DirectoriesMixin
|
|
from paperless_testing.factories import UserFactory
|
|
|
|
|
|
class TestRemoteUser(DirectoriesMixin, APITestCase):
|
|
def setUp(self) -> None:
|
|
super().setUp()
|
|
|
|
self.user = UserFactory(username="temp_admin", superuser=True)
|
|
|
|
# _parse_remote_user_settings() mutates these shared lists in place,
|
|
# so undo that after the test instead of leaking remote-user auth
|
|
# into every test that runs afterward.
|
|
original_middleware = list(settings.MIDDLEWARE)
|
|
original_auth_backends = list(settings.AUTHENTICATION_BACKENDS)
|
|
original_auth_classes = list(
|
|
settings.REST_FRAMEWORK["DEFAULT_AUTHENTICATION_CLASSES"],
|
|
)
|
|
|
|
def _restore_remote_user_settings() -> None:
|
|
settings.MIDDLEWARE[:] = original_middleware
|
|
settings.AUTHENTICATION_BACKENDS[:] = original_auth_backends
|
|
settings.REST_FRAMEWORK["DEFAULT_AUTHENTICATION_CLASSES"][:] = (
|
|
original_auth_classes
|
|
)
|
|
|
|
self.addCleanup(_restore_remote_user_settings)
|
|
|
|
def test_remote_user(self) -> None:
|
|
"""
|
|
GIVEN:
|
|
- Configured user
|
|
- Remote user auth is enabled
|
|
WHEN:
|
|
- Call is made to root
|
|
THEN:
|
|
- Call succeeds
|
|
"""
|
|
|
|
with mock.patch.dict(
|
|
os.environ,
|
|
{
|
|
"PAPERLESS_ENABLE_HTTP_REMOTE_USER": "True",
|
|
},
|
|
):
|
|
_parse_remote_user_settings()
|
|
|
|
response = self.client.get("/documents/")
|
|
|
|
self.assertEqual(
|
|
response.status_code,
|
|
status.HTTP_302_FOUND,
|
|
)
|
|
|
|
response = self.client.get(
|
|
"/documents/",
|
|
headers={
|
|
"Remote-User": self.user.username,
|
|
},
|
|
)
|
|
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
|
|
|
def test_remote_user_api(self) -> None:
|
|
"""
|
|
GIVEN:
|
|
- Configured user
|
|
- Remote user auth is enabled for the API
|
|
WHEN:
|
|
- API call is made to get documents
|
|
THEN:
|
|
- Call succeeds
|
|
"""
|
|
|
|
with mock.patch.dict(
|
|
os.environ,
|
|
{
|
|
"PAPERLESS_ENABLE_HTTP_REMOTE_USER_API": "True",
|
|
},
|
|
):
|
|
_parse_remote_user_settings()
|
|
|
|
response = self.client.get("/api/documents/")
|
|
|
|
# 403 testing locally, 401 on ci...
|
|
self.assertIn(
|
|
response.status_code,
|
|
[status.HTTP_401_UNAUTHORIZED, status.HTTP_403_FORBIDDEN],
|
|
)
|
|
|
|
response = self.client.get(
|
|
"/api/documents/",
|
|
headers={
|
|
"Remote-User": self.user.username,
|
|
},
|
|
)
|
|
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
|
|
|
@override_settings(
|
|
REST_FRAMEWORK={
|
|
**settings.REST_FRAMEWORK,
|
|
"DEFAULT_AUTHENTICATION_CLASSES": [
|
|
"rest_framework.authentication.BasicAuthentication",
|
|
"rest_framework.authentication.TokenAuthentication",
|
|
"rest_framework.authentication.SessionAuthentication",
|
|
],
|
|
},
|
|
)
|
|
def test_remote_user_api_disabled(self) -> None:
|
|
"""
|
|
GIVEN:
|
|
- Configured user
|
|
- Remote user auth enabled for frontend but disabled for the API
|
|
- Note that REST_FRAMEWORK['DEFAULT_AUTHENTICATION_CLASSES'] is set in settings.py in production
|
|
WHEN:
|
|
- API call is made to get documents
|
|
THEN:
|
|
- Call fails
|
|
"""
|
|
response = self.client.get(
|
|
"/api/documents/",
|
|
headers={
|
|
"Remote-User": self.user.username,
|
|
},
|
|
)
|
|
|
|
self.assertIn(
|
|
response.status_code,
|
|
[status.HTTP_401_UNAUTHORIZED, status.HTTP_403_FORBIDDEN],
|
|
)
|
|
|
|
def test_remote_user_header_setting(self) -> None:
|
|
"""
|
|
GIVEN:
|
|
- Remote user header name is set
|
|
WHEN:
|
|
- Settings are parsed
|
|
THEN:
|
|
- Correct header name is returned
|
|
"""
|
|
|
|
with mock.patch.dict(
|
|
os.environ,
|
|
{
|
|
"PAPERLESS_ENABLE_HTTP_REMOTE_USER": "True",
|
|
"PAPERLESS_HTTP_REMOTE_USER_HEADER_NAME": "HTTP_FOO",
|
|
},
|
|
):
|
|
header_name = _parse_remote_user_settings()
|
|
|
|
self.assertEqual(header_name, "HTTP_FOO")
|