Skip to content

Commit c811c3d

Browse files
committed
Allow customizing settings module from get_setting
1 parent d758999 commit c811c3d

File tree

4 files changed

+22
-15
lines changed

4 files changed

+22
-15
lines changed

ansible_base/lib/utils/requests.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from typing import Optional
33

44
from crum import get_current_request
5+
from django.conf import LazySettings, settings
56
from django.http import HttpRequest
67

78
from ansible_base.jwt_consumer.common.util import validate_x_trusted_proxy_header
@@ -24,7 +25,7 @@ def split_header(value: str) -> list[str]:
2425
return values
2526

2627

27-
def get_remote_hosts(request: HttpRequest, get_first_only: bool = False) -> list[str]:
28+
def get_remote_hosts(request: HttpRequest, get_first_only: bool = False, settings_module: LazySettings = settings) -> list[str]:
2829
'''
2930
Get all IPs from the allowed headers
3031
NOTE: this function does not unique the hosts to preserve the order of hosts found in the variables
@@ -34,7 +35,7 @@ def get_remote_hosts(request: HttpRequest, get_first_only: bool = False) -> list
3435
if not request or not hasattr(request, 'META'):
3536
return remote_hosts
3637

37-
headers = get_setting('REMOTE_HOST_HEADERS', ['REMOTE_ADDR', 'REMOTE_HOST'])
38+
headers = get_setting('REMOTE_HOST_HEADERS', ['REMOTE_ADDR', 'REMOTE_HOST'], settings_module=settings_module)
3839

3940
for header in headers:
4041
for value in split_header(request.META.get(header, '')):

ansible_base/lib/utils/settings.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import logging
33
from typing import Any
44

5-
from django.conf import settings
5+
from django.conf import LazySettings, settings
66
from django.utils.translation import gettext_lazy as _
77

88
from ansible_base.lib.utils.validation import to_python_boolean
@@ -14,9 +14,9 @@ class SettingNotSetException(Exception):
1414
pass
1515

1616

17-
def get_setting(name: str, default: Any = None, log_exception: bool = True) -> Any:
17+
def get_setting(name: str, default: Any = None, log_exception: bool = True, settings_module: LazySettings = settings) -> Any:
1818
try:
19-
the_function = get_function_from_setting('ANSIBLE_BASE_SETTINGS_FUNCTION')
19+
the_function = get_function_from_setting('ANSIBLE_BASE_SETTINGS_FUNCTION', settings_module=settings_module)
2020
if the_function:
2121
setting = the_function(name)
2222
return setting
@@ -32,11 +32,11 @@ def get_setting(name: str, default: Any = None, log_exception: bool = True) -> A
3232
)
3333
)
3434

35-
return getattr(settings, name, default)
35+
return getattr(settings_module, name, default)
3636

3737

38-
def get_function_from_setting(setting_name: str) -> Any:
39-
setting = getattr(settings, setting_name, None)
38+
def get_function_from_setting(setting_name: str, settings_module: LazySettings = settings) -> Any:
39+
setting = getattr(settings_module, setting_name, None)
4040
if not setting:
4141
return None
4242

ansible_base/rbac/policies.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from django.apps import apps
2-
from django.conf import settings
2+
from django.conf import LazySettings, settings
33
from django.db.models import Model
44
from django.db.models.query import QuerySet
55
from django.utils.translation import gettext_lazy as _
@@ -12,12 +12,12 @@
1212
from ansible_base.rbac.validators import permissions_allowed_for_role
1313

1414

15-
def visible_users(request_user, queryset=None, always_show_superusers=True, always_show_self=True) -> QuerySet:
15+
def visible_users(request_user, queryset=None, always_show_superusers=True, always_show_self=True, settings_module: LazySettings = settings) -> QuerySet:
1616
"""Gives a queryset of users that another user should be able to view"""
1717
user_cls = permission_registry.user_model
1818
org_cls = apps.get_model(settings.ANSIBLE_BASE_ORGANIZATION_MODEL)
1919

20-
if can_view_all_users(request_user):
20+
if can_view_all_users(request_user, settings_module=settings_module):
2121
if queryset is not None:
2222
return queryset
2323
else:
@@ -38,22 +38,22 @@ def visible_users(request_user, queryset=None, always_show_superusers=True, alwa
3838
return queryset.distinct()
3939

4040

41-
def can_view_all_users(request_user):
41+
def can_view_all_users(request_user, settings_module: LazySettings = settings):
4242
org_cls = apps.get_model(settings.ANSIBLE_BASE_ORGANIZATION_MODEL)
4343

4444
return has_super_permission(request_user, 'view') or (
45-
get_setting('ORG_ADMINS_CAN_SEE_ALL_USERS', False) and org_cls.access_ids_qs(request_user, 'change').exists()
45+
get_setting('ORG_ADMINS_CAN_SEE_ALL_USERS', False, settings_module=settings_module) and org_cls.access_ids_qs(request_user, 'change').exists()
4646
)
4747

4848

49-
def can_change_user(request_user, target_user) -> bool:
49+
def can_change_user(request_user, target_user, settings_module: LazySettings = settings) -> bool:
5050
"""Tells if the request user can modify details of the target user"""
5151
if request_user.is_superuser:
5252
return True
5353
elif target_user.is_superuser:
5454
return False # target is a superuser and request user is not
5555

56-
if not get_setting('MANAGE_ORGANIZATION_AUTH', False):
56+
if not get_setting('MANAGE_ORGANIZATION_AUTH', False, settings_module=settings_module):
5757
return False
5858

5959
# All users can change their own password and other details

test_app/tests/lib/utils/test_settings.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from unittest import mock
2+
from types import SimpleNamespace
23

34
import pytest
45
from django.test import override_settings
@@ -50,6 +51,11 @@ def test_settings_from_function(setting_name, default, expected_value):
5051
assert value == expected_value
5152

5253

54+
def test_get_setting_custom_module():
55+
fake_settings = SimpleNamespace(FOO_SETTING_TEST='fooz')
56+
assert get_setting('FOO_SETTING_TEST', settings_module=fake_settings) == 'fooz'
57+
58+
5359
@pytest.mark.parametrize(
5460
"setting_value,expected_value,expected_log_output",
5561
[

0 commit comments

Comments
 (0)