Skip to content

Commit

Permalink
LITE-28135 added coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Sainomori committed Nov 21, 2023
1 parent e4e30fc commit 229d7fb
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 15 deletions.
40 changes: 25 additions & 15 deletions connect/eaas/runner/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
TRANSFORMATION_TASK_MAX_EXECUTION_TIME,
TRANSFORMATION_WRITE_QUEUE_TIMEOUT,
)
from django.core.exceptions import ImproperlyConfigured # noqa: I001


logger = logging.getLogger('connect.eaas')
Expand Down Expand Up @@ -626,13 +627,18 @@ def get_features_table(features):
return table


def enforce_and_override_django_settings(
config,
django_secret_key_variable,
overridden_settings,
):
def enforce_and_override_django_settings(config, django_secret_key_variable, overridden_settings):
from django.conf import settings # noqa: I001
from django.core.exceptions import ImproperlyConfigured # noqa: I001
_validate_django_secret_key(config, django_secret_key_variable)
_set_django_secret_key(config, django_secret_key_variable, settings)
overrides = copy.copy(overridden_settings)
_override_and_validate_settings(config, overrides)
_override_database_settings(overrides, settings)
_apply_enforced_settings(settings)
_configure_host_and_debug(config, settings)


def _validate_django_secret_key(config, django_secret_key_variable):
if not django_secret_key_variable:
raise ImproperlyConfigured(
'Your extension class must be decorated with the '
Expand All @@ -645,41 +651,45 @@ def enforce_and_override_django_settings(
'not been found and it is mandatory to setup django.',
)


def _set_django_secret_key(config, django_secret_key_variable, settings):
settings.SECRET_KEY = config.variables[django_secret_key_variable]

overrides = copy.copy(overridden_settings)

def _override_and_validate_settings(config, overrides):
required_overrides = list(DJANGO_REQUIRED_OVERRIDE_SETTINGS)
if config.environment_runtime == 'cloud':
required_overrides.append('DATABASES')
for required_setting in required_overrides:
if required_setting not in overrides.keys():
if required_setting not in overrides:
raise ImproperlyConfigured(
f'The settings `{",".join(required_overrides)}` must be overridden.',
)
for non_overrideable_setting in DJANGO_NON_OVERRIDEABLE_SETTINGS:
if non_overrideable_setting in overrides.keys():
if non_overrideable_setting in overrides:
raise ImproperlyConfigured(
'The settings '
f'`{",".join(DJANGO_NON_OVERRIDEABLE_SETTINGS)}` cannot be overridden.',
)
databases_override = overrides.pop('DATABASES', None)

for setting, setting_value in overrides.items():
setattr(settings, setting, setting_value)

def _override_database_settings(overrides, settings):
databases_override = overrides.pop('DATABASES', None)
if databases_override:
for db, db_config in databases_override.items():
for prop, propvalue in db_config.items():
settings.DATABASES[db][prop] = propvalue
for prop, prop_value in db_config.items():
settings.DATABASES[db][prop] = prop_value


def _apply_enforced_settings(settings):
for setting, setting_value in DJANGO_ENFORCED_SETTINGS.items():
setattr(settings, setting, setting_value)


def _configure_host_and_debug(config, settings):
if config.environment_hostname and config.environment_domain:
settings.ALLOWED_HOSTS = [
f'{config.environment_hostname}.{config.environment_domain}',
]

if config.environment_runtime == 'cloud':
settings.DEBUG = False
37 changes: 37 additions & 0 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -945,3 +945,40 @@ def test_enforce_and_override_django_settings_enforced(mocker):

for setting, setting_value in DJANGO_ENFORCED_SETTINGS.items():
assert getattr(settings_mock, setting) == setting_value


def test_missing_django_secret(mocker):
mocker.patch.object(django.conf, 'settings', mocker.MagicMock())
config_mock = mocker.MagicMock(
environment_runtime='cloud',
environment_hostname='srvc-1234-dev',
environment_domain='extensions.io',
variables={'DJ_SECRET': 'my-secret'},
)
overrides = {}

with pytest.raises(ImproperlyConfigured) as cv:
enforce_and_override_django_settings(config_mock, False, overrides)

assert str(cv.value) == (
"Your extension class must be decorated with the `@django_secret_key_variable` "
"to specify the name of the environment variable that store the django `SECRET_KEY`."
)


def test_improperly_configured_django_secret_settings(mocker):
mocker.patch.object(django.conf, 'settings', mocker.MagicMock())
config_mock = mocker.MagicMock(
environment_runtime='cloud',
environment_hostname='srvc-1234-dev',
environment_domain='extensions.io',
variables={'DJ_SECRET': 'my-secret'},
)
overrides = {}

with pytest.raises(ImproperlyConfigured) as cv:
enforce_and_override_django_settings(config_mock, 'False', overrides)

assert str(cv.value) == (
"The environment variable False has not been found and it is mandatory to setup django."
)
34 changes: 34 additions & 0 deletions tests/workers/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -1840,6 +1840,40 @@ async def test_ensure_connection_exit_max_attemps(mocker, caplog):
assert 'max connection attemps reached, exit!' in caplog.text


@pytest.mark.asyncio
async def test_connection_refused(mocker, caplog):
mocker.patch.object(
EventsApp,
'load_application',
)
mocker.patch('connect.eaas.runner.workers.base.MAX_RETRY_TIME_GENERIC_SECONDS', 1)
mocker.patch('connect.eaas.runner.workers.base.MAX_RETRY_DELAY_TIME_SECONDS', .01)
mocker.patch('connect.eaas.runner.workers.base.MAX_RETRY_TIME_MAINTENANCE_SECONDS', .1)
mocker.patch(
'connect.eaas.runner.workers.base.websockets.connect',
side_effect=ConnectionClosedError(1006, 'disconnected'),
)

config = ConfigHelper(secure=False)
ext_handler = EventsApp(config)

worker = EventsWorker(
ext_handler,
mocker.MagicMock(),
mocker.MagicMock(),
mocker.MagicMock(),
)
worker.run_event.set()
worker.get_url = lambda: 'ws://test'

with pytest.raises(CommunicationError):
with caplog.at_level(logging.INFO):
await worker.ensure_connection()

assert 'connection closed by the host...' in caplog.text
assert '1st communication attempt failed, backing off waiting' in caplog.text


@pytest.mark.asyncio
async def test_shutdown_pending_task_timeout(
mocker, ws_server, unused_port, settings_payload, caplog,
Expand Down

0 comments on commit 229d7fb

Please sign in to comment.