Skip to content

Commit

Permalink
LITE-28127 add decorator for django secret key variable
Browse files Browse the repository at this point in the history
  • Loading branch information
Francesco Faraone committed Jul 21, 2023
1 parent dea8150 commit a223fa0
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 31 deletions.
1 change: 1 addition & 0 deletions connect/eaas/core/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
DEVOPS_PAGES_ATTR_NAME = '_eaas_devops_pages'
PROXIED_CONNECT_API_ATTR_NAME = '_eaas_proxied_connect_api'
CUSTOMER_PAGES_ATTR_NAME = '_eaas_customer_pages'
DJANGO_SECRET_KEY_VAR_ATTR_NAME = '_eaas_django_secret_key_var'


PROXIED_CONNECT_API_ENDPOINTS_MAX_ALLOWED_NUMBER = 100
Expand Down
15 changes: 15 additions & 0 deletions connect/eaas/core/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
ANVIL_KEY_VAR_ATTR_NAME,
CUSTOMER_PAGES_ATTR_NAME,
DEVOPS_PAGES_ATTR_NAME,
DJANGO_SECRET_KEY_VAR_ATTR_NAME,
EVENT_INFO_ATTR_NAME,
MODULE_PAGES_ATTR_NAME,
PROXIED_CONNECT_API_ATTR_NAME,
Expand Down Expand Up @@ -625,6 +626,20 @@ def wrapper(cls):
return wrapper


def django_secret_key_variable(name):
def wrapper(cls):
setattr(cls, DJANGO_SECRET_KEY_VAR_ATTR_NAME, name)
variables = []
if not hasattr(cls, VARIABLES_INFO_ATTR_NAME):
setattr(cls, VARIABLES_INFO_ATTR_NAME, variables)
else:
variables = getattr(cls, VARIABLES_INFO_ATTR_NAME)
if len(list(filter(lambda x: x['name'] == name, variables))) == 0:
variables.append({'name': name, 'initial_value': 'changeme!', 'secure': True})
return cls
return wrapper


router = InferringRouter()
web_app = cbv
"""
Expand Down
11 changes: 10 additions & 1 deletion connect/eaas/core/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
ANVIL_KEY_VAR_ATTR_NAME,
CUSTOMER_PAGES_ATTR_NAME,
DEVOPS_PAGES_ATTR_NAME,
DJANGO_SECRET_KEY_VAR_ATTR_NAME,
EVENT_INFO_ATTR_NAME,
MODULE_PAGES_ATTR_NAME,
PROXIED_CONNECT_API_ATTR_NAME,
Expand Down Expand Up @@ -80,6 +81,14 @@ def get_variables(cls) -> dict:
"""
return getattr(cls, VARIABLES_INFO_ATTR_NAME, [])

@classmethod
def get_django_secret_key_variable(cls) -> str:
"""
Returns the name of the environment variable that
stores the Anvil Server Uplink key.
"""
return getattr(cls, DJANGO_SECRET_KEY_VAR_ATTR_NAME, None)


class InstallationAdminClientMixin:
def get_installation_admin_client(self, installation_id):
Expand Down Expand Up @@ -284,7 +293,7 @@ def get_anvil_key_variable(cls) -> str:
Returns the name of the environment variable that
stores the Anvil Server Uplink key.
"""
return getattr(cls, ANVIL_KEY_VAR_ATTR_NAME, [])
return getattr(cls, ANVIL_KEY_VAR_ATTR_NAME, None)

@classmethod
def get_anvil_callables(cls):
Expand Down
32 changes: 7 additions & 25 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ responses = ">=0.14.0,<1"
pytest-httpx = ">=0.20"
Faker = "^14.2.0"
flake8-isort = "^6.0.0"
django = ">=4.2,<5"
pytest-django = "^4.5.2"
django-rql = "^4.4.0"

[tool.poetry.group.docs.dependencies]
Expand Down
3 changes: 0 additions & 3 deletions tests/connect/eaas/core/deployment/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ def test_extract_arguments_ok(mocker):
}


@pytest.mark.django_db
def test_extract_arguments_ok_with_overview(mocker):
tempdir_mock = mocker.MagicMock()
tempdir_mock.__enter__.return_value = '/tmp'
Expand All @@ -96,7 +95,6 @@ def test_extract_arguments_ok_with_overview(mocker):
assert deploy_args['overview'] is not None


@pytest.mark.django_db
def test_extract_arguments_ok_with_non_existing_overview(mocker):
tempdir_mock = mocker.MagicMock()
tempdir_mock.__enter__.return_value = '/tmp'
Expand Down Expand Up @@ -124,7 +122,6 @@ def test_extract_arguments_ok_with_non_existing_overview(mocker):
assert deploy_args['overview'] is None


@pytest.mark.django_db
def test_extract_arguments_clone_error(mocker):
tempdir_mock = mocker.MagicMock()
tempdir_mock.__enter__.return_value = '/tmp'
Expand Down
66 changes: 66 additions & 0 deletions tests/connect/eaas/core/test_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
anvil_key_variable,
customer_pages,
devops_pages,
django_secret_key_variable,
event,
guest,
manual_transformation,
Expand Down Expand Up @@ -836,3 +837,68 @@ def transform_row(self, row):
assert installation_admin_client.endpoint == extension_client.endpoint
assert installation_admin_client.default_headers == extension_client.default_headers
assert installation_admin_client.logger == extension_client.logger


@pytest.mark.parametrize(
'app_base_class',
(
AnvilApplicationBase,
EventsApplicationBase,
TransformationsApplicationBase,
WebApplicationBase,
),
)
def test_get_django_secret_key_variable(app_base_class):

@django_secret_key_variable('DJANGO_SECRET_KEY')
class MyApp(app_base_class):
pass

assert MyApp.get_django_secret_key_variable() == 'DJANGO_SECRET_KEY'
assert MyApp.get_variables()[0] == {
'name': 'DJANGO_SECRET_KEY',
'initial_value': 'changeme!',
'secure': True,
}


@pytest.mark.parametrize(
'vars',
(
[
{
'name': 'VAR',
'initial_value': 'VALUE',
},
],
[
{
'name': 'DJANGO_SECRET_KEY',
'initial_value': 'changeme!',
'secure': True,
},
],
),
)
@pytest.mark.parametrize(
'app_base_class',
(
AnvilApplicationBase,
EventsApplicationBase,
TransformationsApplicationBase,
WebApplicationBase,
),
)
def test_get_django_secret_key_variable_with_variables(app_base_class, vars):

@django_secret_key_variable('DJANGO_SECRET_KEY')
@variables(vars)
class MyApp(app_base_class):
pass

assert MyApp.get_django_secret_key_variable() == 'DJANGO_SECRET_KEY'
assert {
'name': 'DJANGO_SECRET_KEY',
'initial_value': 'changeme!',
'secure': True,
} in MyApp.get_variables()

0 comments on commit a223fa0

Please sign in to comment.