def test_secret(self, env: yaenv.Env): """it can get and generate secret keys""" assert env.secret() == 'notsosecret' assert 'NEW_SECRET_KEY' not in env _secret = env.secret('NEW_SECRET_KEY') assert _secret is not None assert _secret != env.secret('NEW_SECRET_KEY2') del env['NEW_SECRET_KEY'], env['NEW_SECRET_KEY2']
def test_list(self, env: yaenv.Env): """it can cast to list""" _val = env.list('LIST_VAR', separator=':') _expect = ['item1', 'item2'] assert _val == _expect and type(_val) == list _expect.append('item3') _val = env.list('MISSING', _expect) assert _val == _expect and type(_val) == list assert env.list('MISSING') is None
def test_float(self, env: yaenv.Env): """it can cast to float""" _val = env.float('FLOAT_VAR') assert _val == 10.0 and type(_val) == float _val = env.float('MISSING', -3.1) assert _val == -3.1 and type(_val) == float with pytest.raises(yaenv.EnvError) as err: _ = env.float('LIST_VAR') assert 'Invalid numerical' in str(err.value) assert env.float('MISSING') is None
def test_int(self, env: yaenv.Env): """it can cast to int""" _val = env.int('INT_VAR') assert _val == 1 and type(_val) == int _val = env.int('MISSING', -2) assert _val == -2 and type(_val) == int with pytest.raises(yaenv.EnvError) as err: _ = env.int('LIST_VAR') assert 'Invalid integer' in str(err.value) assert env.int('MISSING') is None
def test_bool(self, env: yaenv.Env): """it can cast to bool""" _val = env.bool('BOOL_VAR') assert not _val and type(_val) == bool _val = env.bool('INT_VAR') assert _val and type(_val) == bool _val = env.bool('MISSING', True) assert _val and type(_val) == bool with pytest.raises(yaenv.EnvError) as err: _ = env.bool('FLOAT_VAR') assert 'Invalid boolean' in str(err.value) assert env.bool('MISSING') is None
def test_no_final_eol(self, env: yaenv.Env): """it can parse a dotenv file without a final EOL""" from tempfile import mkstemp env.envfile = mkstemp()[-1] with open(env, 'w') as f: f.write('EOL=no') env['BLANK'] = '' with open(env, 'r') as f: assert len(f.readlines()) == 2
def test_email(self, env: yaenv.Env): """it can parse e-mail URLs""" _email = { 'EMAIL_BACKEND': yaenv.email.SCHEMES['dummy'], 'EMAIL_HOST_USER': '', 'EMAIL_HOST_PASSWORD': '', 'EMAIL_HOST': 'localhost' } assert env.email('EMAIL_URL') == _email _email.update({ 'EMAIL_BACKEND': yaenv.email.SCHEMES['console'], 'EMAIL_HOST': '127.0.0.1' }) assert env.email('EMAIL_URL_MISSING', 'console://127.0.0.1') == _email with pytest.raises(yaenv.EnvError) as err: _ = env.email('INVALID_URL', 'invalid') assert 'Invalid e-mail' in str(err.value) assert env.email('MISSING') is None
def test_db(self, env: yaenv.Env): """it can parse database URLs""" _db = { 'ENGINE': yaenv.db.SCHEMES['sqlite'], 'NAME': 'db.sqlite3', 'USER': '', 'PASSWORD': '', 'HOST': '', 'PORT': '', } assert env.db('DB_URL') == _db _db = { 'ENGINE': yaenv.db.SCHEMES['sqlite'], 'NAME': ':memory:', } assert env.db('DB_URL_DEFAULT', 'sqlite://:memory:') == _db with pytest.raises(yaenv.EnvError) as err: _ = env.db('INVALID_URL', 'invalid') assert 'Invalid database' in str(err.value) assert env.db('MISSING') is None
def test_get(self, env: yaenv.Env): """it returns default values for optional variables""" assert env.get('BLANK', 'default') == '' assert env.get('MISSING') is None assert env.get('MISSING', 'default') == 'default'
def test_setenv(self, env: yaenv.Env): """it can update os.environ""" from os import environ assert 'EMAIL' not in environ env.setenv() assert 'EMAIL' in environ
# should also be modified accordingly. import re from importlib.util import find_spec from pathlib import Path from yaenv import Env from MangAdventure import __version__ as VERSION from MangAdventure.bad_bots import BOTS #: Build paths inside the project like this: ``BASE_DIR / ...``. BASE_DIR = Path(__file__).resolve().parents[1] # Load environment variables from .env file. env = Env(BASE_DIR / '.env') ############### # Basic # ############### #: A list of host/domain names that this site can serve. #: See :setting:`ALLOWED_HOSTS`. ALLOWED_HOSTS = env.list('ALLOWED_HOSTS', [ '127.0.0.1', '0.0.0.0', 'localhost', '[::1]', # From https://stackoverflow.com/questions/9626535/#36609868 env['DOMAIN'].split('//')[-1].split('/')[0].split('?')[0] ]) #: | A boolean that turns debug mode on/off. See :setting:`DEBUG`. #: | **SECURITY WARNING: never turn this on in production!**