Skip to content
Snippets Groups Projects
Verified Commit b20a9566 authored by Jonathan Weth's avatar Jonathan Weth :keyboard:
Browse files

Add test for custom authentication backends and fix a little bit

parent 5680c5d0
No related branches found
No related tags found
1 merge request!334Resolve "Support custom authentication backends"
Pipeline #3117 failed
...@@ -2,6 +2,7 @@ from typing import Any, List, Optional, Tuple ...@@ -2,6 +2,7 @@ from typing import Any, List, Optional, Tuple
import django.apps import django.apps
from django.conf import settings from django.conf import settings
from django.db import ProgrammingError
from django.http import HttpRequest from django.http import HttpRequest
from django.utils.module_loading import autodiscover_modules from django.utils.module_loading import autodiscover_modules
...@@ -51,15 +52,23 @@ class CoreConfig(AppConfig): ...@@ -51,15 +52,23 @@ class CoreConfig(AppConfig):
self._refresh_authentication_backends() self._refresh_authentication_backends()
def _refresh_authentication_backends(self): @classmethod
def _refresh_authentication_backends(cls):
"""Refresh config list of enabled authentication backends.""" """Refresh config list of enabled authentication backends."""
from .preferences import AuthenticationBackends # noqa from .preferences import AuthenticationBackends # noqa
idx = settings.AUTHENTICATION_BACKENDS.index("django.contrib.auth.backends.ModelBackend") idx = settings.AUTHENTICATION_BACKENDS.index("django.contrib.auth.backends.ModelBackend")
for backend in get_site_preferences()["auth__backends"]: try:
settings._wrapped.AUTHENTICATION_BACKENDS.insert(idx, backend) # Don't set array directly in order to keep object reference
idx += 1 settings._wrapped.AUTHENTICATION_BACKENDS.clear()
settings._wrapped.AUTHENTICATION_BACKENDS += settings.ORIGINAL_AUTHENTICATION_BACKENDS
for backend in get_site_preferences()["auth__backends"]:
settings._wrapped.AUTHENTICATION_BACKENDS.insert(idx, backend)
idx += 1
except ProgrammingError:
pass
def preference_updated( def preference_updated(
self, self,
......
...@@ -191,5 +191,7 @@ class AuthenticationBackends(MultipleChoicePreference): ...@@ -191,5 +191,7 @@ class AuthenticationBackends(MultipleChoicePreference):
section = auth section = auth
name = "backends" name = "backends"
default = None default = None
choices = [(b, b) for b in settings.CUSTOM_AUTHENTICATION_BACKENDS]
verbose_name = _("Enabled custom authentication backends") verbose_name = _("Enabled custom authentication backends")
def get_choices(self):
return [(b, b) for b in settings.CUSTOM_AUTHENTICATION_BACKENDS]
...@@ -690,3 +690,5 @@ HEALTH_CHECK = { ...@@ -690,3 +690,5 @@ HEALTH_CHECK = {
"DISK_USAGE_MAX": _settings.get("health.disk_usage_max_percent", 90), "DISK_USAGE_MAX": _settings.get("health.disk_usage_max_percent", 90),
"MEMORY_MIN": _settings.get("health.memory_min_mb", 500), "MEMORY_MIN": _settings.get("health.memory_min_mb", 500),
} }
ORIGINAL_AUTHENTICATION_BACKENDS = AUTHENTICATION_BACKENDS[:]
from typing import Optional
from django.contrib.auth import authenticate
from django.contrib.auth.backends import BaseBackend
from django.contrib.auth.base_user import AbstractBaseUser
from django.contrib.auth.models import User
import pytest
from aleksis.core.apps import CoreConfig
from aleksis.core.util.core_helpers import get_site_preferences
pytestmark = pytest.mark.django_db
class DummyBackend(BaseBackend):
def authenticate(
self, request, username: str, password: str, **kwargs
) -> Optional[AbstractBaseUser]:
if username == "foo" and password == "baz":
return User.objects.get_or_create(username="foo")[0]
backend_name = "aleksis.core.tests.test_authentication_backends.DummyBackend"
def test_backends_simple(settings):
assert not authenticate(username="foo", password="baz")
assert backend_name not in settings.AUTHENTICATION_BACKENDS
settings.AUTHENTICATION_BACKENDS.append(backend_name)
assert backend_name in settings.AUTHENTICATION_BACKENDS
assert authenticate(username="foo", password="baz")
settings.AUTHENTICATION_BACKENDS.remove(backend_name)
assert not authenticate(username="foo", password="baz")
def test_backends_with_activation(settings):
assert not authenticate(username="foo", password="baz")
settings.CUSTOM_AUTHENTICATION_BACKENDS.append(backend_name)
assert backend_name not in get_site_preferences()["auth__backends"]
assert backend_name not in settings.AUTHENTICATION_BACKENDS
assert not authenticate(username="foo", password="baz")
print(get_site_preferences()["auth__backends"])
print(get_site_preferences()["auth__backends"].append(backend_name))
get_site_preferences()["auth__backends"] = [backend_name]
assert backend_name in get_site_preferences()["auth__backends"]
assert backend_name in settings.AUTHENTICATION_BACKENDS
assert authenticate(username="foo", password="baz")
get_site_preferences()["auth__backends"] = []
assert backend_name not in get_site_preferences()["auth__backends"]
assert backend_name not in settings.AUTHENTICATION_BACKENDS
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment