Exemple #1
0
def test_get_value_from_enviroment_file_var_empty(monkeypatch):
    """Test get_value_from_env_file with empty env var."""
    monkeypatch.setenv(TEST_ENV_VAR, "")
    with pytest.raises(EnvironmentNotSetError) as exc_info:
        utils.get_value_from_env_file(TEST_ENV_VAR)
    assert exc_info.type is EnvironmentNotSetError
    assert 'set but empty' in exc_info.value.args[0]
Exemple #2
0
def test_get_value_from_enviroment_file_var_not_set(monkeypatch):
    """Test get_value_from_env_file with no set env var."""
    monkeypatch.delenv(TEST_ENV_VAR, False)
    with pytest.raises(EnvironmentNotSetError) as exc_info:
        utils.get_value_from_env_file(TEST_ENV_VAR)
    assert exc_info.type is EnvironmentNotSetError
    assert 'not set' in exc_info.value.args[0]
Exemple #3
0
def test_get_value_from_enviroment_file_no_file(monkeypatch, tmpdir):
    """Test get_value_from_env_file with no file at the var path."""
    test_file_path = os.path.join(tmpdir, 'test.txt')
    monkeypatch.setenv(TEST_ENV_VAR, test_file_path)
    with pytest.raises(EnvironmentNotSetError) as exc_info:
        utils.get_value_from_env_file(TEST_ENV_VAR)
    assert exc_info.type is EnvironmentNotSetError
    assert 'not exist' in exc_info.value.args[0]
Exemple #4
0
def test_get_value_from_enviroment_file_file_empty(monkeypatch, tmpdir):
    """Test get_value_from_env_file with empty file at the var path."""
    test_file_path = os.path.join(tmpdir, 'test.txt')
    monkeypatch.setenv(TEST_ENV_VAR, test_file_path)
    with open(test_file_path, 'w') as f:
        f.write('')

    with pytest.raises(EnvironmentNotSetError) as exc_info:
        utils.get_value_from_env_file(TEST_ENV_VAR)
    assert exc_info.type is EnvironmentNotSetError
    assert 'is empty' in exc_info.value.args[0]
Exemple #5
0
def test_get_value_from_env_file_all_set(monkeypatch, tmpdir):
    """Test get_value_from_env_file with set var and file."""
    test_file_path = os.path.join(tmpdir, 'test.txt')
    monkeypatch.setenv(TEST_ENV_VAR, test_file_path)
    with open(test_file_path, 'w') as f:
        f.write('TestString')

    value = utils.get_value_from_env_file(TEST_ENV_VAR)
    assert value == 'TestString'
Exemple #6
0
    def _init_mongo_client(self) -> MongoClient:
        """Initialize and return the mongo client for connecting to database.

        Returns:
            A client object connected and authenticated with the database.

        Raises:
            EnvironmentNotSetError: if a needed value from the environment to
                init the client is not set in the environment.
        """
        username = utils.get_value_from_env_file(_DB_USERNAME_FILE_ENV_VAR)
        password = utils.get_value_from_env_file(_DB_PASSWORD_FILE_ENV_VAR)
        hostname = utils.get_value_from_env_variable(_DB_HOST_ENV_VAR)

        mongo_client = MongoClient(host=hostname,
                                   port=_DB_PORT,
                                   username=username,
                                   password=password,
                                   authSource=self._DB_NAME)
        _log.debug('Connected to MongoDB at %s:%s as user %s',
                   mongo_client.address[0], mongo_client.address[1], username)

        return mongo_client
Exemple #7
0
    def _connect_to_cache(self) -> None:
        """Init connection to the cache if necessary.

        Does nothing if the connection to the cache has already been
        initialized.
        """
        if self._redis_client is not None:
            return

        hostname = utils.get_value_from_env_variable(
            _NEXT_PAGE_CACHE_HOST_ENV_VAR)
        password = utils.get_value_from_env_file(
            _NEXT_PAGE_CACHE_PASSWORD_FILE_ENV_VAR)
        self._redis_client = _init_redis_client(hostname, password)
Exemple #8
0
from typing import Dict, List

from myaku.utils import get_value_from_env_file

debug_mode_flag = os.environ.get('DJANGO_DEBUG_MODE', 1)
if int(debug_mode_flag) == 1:
    DEBUG = True
else:
    DEBUG = False

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

if DEBUG:
    if os.environ.get('DJANGO_SECRET_KEY_FILE') is not None:
        SECRET_KEY = get_value_from_env_file('DJANGO_SECRET_KEY_FILE')
    else:
        SECRET_KEY = 'DevUseOnlySecretKey'
else:
    # Always use secret key from docker secret in prod
    SECRET_KEY = get_value_from_env_file('DJANGO_SECRET_KEY_FILE')

if DEBUG:
    ALLOWED_HOSTS: List[str] = []
else:
    allowed_hosts_filedata = get_value_from_env_file(
        'MYAKUWEB_ALLOWED_HOSTS_FILE'
    )
    ALLOWED_HOSTS = [
        host for host in allowed_hosts_filedata.split() if len(host) > 0
    ]
Exemple #9
0
import os

from celery import Celery
from django.conf import settings

from myaku import utils

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myakuweb.settings')

rabbit_host = os.environ.get('RABBIT_HOST', None)
if settings.DEBUG and rabbit_host is None:
    rabbit_url = 'amqp://'
else:
    # Enforce that a username and password from docker secrets be used if not
    # in DEBUG mode.
    rabbit_username = utils.get_value_from_env_file('RABBIT_USERNAME_FILE')
    rabbit_password = utils.get_value_from_env_file('RABBIT_PASSWORD_FILE')
    rabbit_url = (
        f'amqp://{rabbit_username}:{rabbit_password}@{rabbit_host}:5672/'
    )

celery_app = Celery('myakuweb', broker=rabbit_url)

celery_app.config_from_object('django.conf:settings', namespace='CELERY')

# Load task modules from all registered Django app configs.
celery_app.autodiscover_tasks()

celery_app.finalize()