Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow for configuring setting in settings.py or environment #5

Merged
merged 1 commit into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions src/django_opfield/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,20 @@ class AppSettings:
def __getattr__(self, __name: str) -> Any:
return self._get_user_settings(__name, super().__getattribute__(__name))

def _get_user_settings(
self, setting: str | None = None, fallback: Any = None
) -> Any:
def _get_user_settings(self, setting: str, fallback: Any = None) -> Any:
user_settings = getattr(settings, OPFIELD_SETTINGS_NAME, {})
return user_settings.get(setting, fallback)

if user_setting := user_settings.get(setting, fallback):
ret = user_setting
else:
ret = os.environ.get(setting, None)

return ret

@property
def OP_CLI_PATH(self) -> Path:
if user_cli_path := self._get_user_settings("OP_CLI_PATH"):
path = user_cli_path
elif env_cli_path := os.environ.get("OP_CLI_PATH", None):
path = env_cli_path
else:
path = shutil.which("op")

Expand All @@ -39,10 +41,7 @@ def OP_CLI_PATH(self) -> Path:

@property
def OP_SERVICE_ACCOUNT_TOKEN(self) -> str:
if user_token := self._get_user_settings("OP_SERVICE_ACCOUNT_TOKEN"):
token = user_token
else:
token = os.environ.get("OP_SERVICE_ACCOUNT_TOKEN", None)
token = self._get_user_settings("OP_SERVICE_ACCOUNT_TOKEN")

if not token:
raise ImproperlyConfigured("OP_SERVICE_ACCOUNT_TOKEN is not set")
Expand Down
16 changes: 15 additions & 1 deletion tests/test_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,23 @@
import pytest
from django.test import override_settings

from django_opfield.conf import OPFIELD_SETTINGS_NAME
from django_opfield.conf import app_settings


class TestGetUserSettings:
def test_not_set(self):
assert app_settings._get_user_settings("TEST_SETTING") is None

@override_settings(**{OPFIELD_SETTINGS_NAME: {"TEST_SETTING": "test value"}})
def test_set_in_settings(self):
assert app_settings._get_user_settings("TEST_SETTING") == "test value"

@patch.dict(os.environ, {"TEST_SETTING": "test value"})
def test_set_in_env(self):
assert app_settings._get_user_settings("TEST_SETTING") == "test value"


@patch.dict(os.environ, {"OP_CLI_PATH": ""})
class TestOPCliPath:
@patch("shutil.which")
Expand All @@ -18,7 +32,7 @@ def test_default(self, mock_which):
with pytest.raises(ImportError):
assert app_settings.OP_CLI_PATH

@override_settings(DJANGO_OPFIELD={"OP_CLI_PATH": "path/to/op"})
@override_settings(**{OPFIELD_SETTINGS_NAME: {"OP_CLI_PATH": "path/to/op"}})
def test_user_setting(self):
assert "path/to/op" in str(app_settings.OP_CLI_PATH)

Expand Down