create central config.py based on pydantic_settings
This commit is contained in:
parent
0a60389152
commit
2b02de2012
8 changed files with 69 additions and 92 deletions
|
|
@ -2,10 +2,10 @@
|
|||
|
||||
"""Common API dependencies"""
|
||||
|
||||
import config
|
||||
from scopes import config
|
||||
|
||||
def storage():
|
||||
stf = config.StorageFactory(config)
|
||||
return stf(config.dbschema)
|
||||
stf = config.StorageFactory(config.settings)
|
||||
return stf(config.settings.dbschema)
|
||||
|
||||
|
||||
|
|
|
|||
29
scopes/config.py
Normal file
29
scopes/config.py
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
# py-scopes/apidemo/config.py
|
||||
from pydantic_settings import BaseSettings, SettingsConfigDict
|
||||
from scopes.storage.db.postgres import StorageFactory
|
||||
|
||||
class Settings(BaseSettings):
|
||||
model_config = SettingsConfigDict(env_file='.env', extra='ignore')
|
||||
#, env_prefix='SCOPES_')
|
||||
|
||||
base_url: str = 'https://test.cy7.eu'
|
||||
|
||||
log_file: str = 'log/scopes.log'
|
||||
log_level: str = 'INFO'
|
||||
log_format: str = '%(asctime)s %(levelname)s %(name)s %(message)s'
|
||||
log_dateformat: str = '%Y-%m-%dT%H:%M:%S'
|
||||
|
||||
dbengine: str = 'postgresql+psycopg'
|
||||
dbname: str = 'demo'
|
||||
dbuser: str = 'demo'
|
||||
dbpassword: str = 'secret'
|
||||
dbschema: str = 'demo'
|
||||
|
||||
settings = Settings()
|
||||
|
||||
import logging
|
||||
|
||||
logging.basicConfig(filename=settings.log_file, level=settings.log_level,
|
||||
format=settings.log_format, datefmt=settings.log_dateformat)
|
||||
logging.getLogger('uvicorn.error').propagate = True
|
||||
|
||||
|
|
@ -2,17 +2,3 @@
|
|||
|
||||
"""Common stuff (config, fixtures, ...) for testing"""
|
||||
|
||||
import os, sys
|
||||
sys.path = [os.path.dirname(__file__)] + sys.path
|
||||
|
||||
import config
|
||||
from scopes.storage.db import postgres
|
||||
|
||||
config.dbengine = 'postgresql+psycopg'
|
||||
config.dbname = 'testdb'
|
||||
config.dbuser = 'testuser'
|
||||
config.dbpassword = 'secret'
|
||||
config.dbschema = 'testing'
|
||||
config.StorageFactory = postgres.StorageFactory
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,65 +0,0 @@
|
|||
# py-scopes/tests/config.py
|
||||
|
||||
import logging
|
||||
from pydantic_settings import BaseSettings, SettingsConfigDict
|
||||
|
||||
from scopes.storage.db.postgres import StorageFactory
|
||||
|
||||
class Settings(BaseSettings):
|
||||
model_config = SettingsConfigDict(env_file='.env')
|
||||
|
||||
base_url: str = 'https://test.cy7.eu'
|
||||
|
||||
log_file: str = 'scopes/tests/log/scopes-test.log'
|
||||
log_level: str = 'INFO'
|
||||
log_format: str = '%(asctime)s %(levelname)s %(name)s %(message)s'
|
||||
log_dateformat: str = '%Y-%m-%dT%H:%M:%S'
|
||||
|
||||
dbengine: str = 'postgresql+psycopg'
|
||||
dbname: str = 'testdb'
|
||||
dbuser: str = 'testuser'
|
||||
dbpassword: str = 'secret'
|
||||
dbschema: str = 'testing'
|
||||
|
||||
settings = Settings()
|
||||
|
||||
from os import getenv
|
||||
import sys
|
||||
|
||||
base_url = settings.base_url
|
||||
|
||||
logging.basicConfig(filename=settings.log_file, level=settings.log_level,
|
||||
format=settings.log_format, datefmt=settings.log_dateformat)
|
||||
|
||||
# storage settings
|
||||
dbengine = settings.dbengine
|
||||
dbname = settings.dbname
|
||||
dbuser = settings.dbuser
|
||||
dbpassword = settings.dbpassword
|
||||
dbschema = settings.dbschema
|
||||
|
||||
# special testing stuff
|
||||
from scopes.tests import data_auth # add oidc URIs and keys to dummy_requests data
|
||||
from scopes.tests import dummy_requests
|
||||
sys.modules['requests'] = dummy_requests
|
||||
|
||||
# authentication settings
|
||||
oidc_provider = 'test://oidc'
|
||||
oidc_client_id = getenv('OIDC_CLIENT_ID', '12345')
|
||||
oidc_params = dict(
|
||||
op_config_url=oidc_provider + '/.well-known/openid-configuration',
|
||||
op_uris=None,
|
||||
op_keys=None,
|
||||
op_project_scope='urn:zitadel:iam:org:project:id:zitadel:aud',
|
||||
callback_url=getenv('OIDC_CALLBACK_URL', base_url + '/auth/callback'),
|
||||
client_id=oidc_client_id,
|
||||
principal_prefix=getenv('OIDC_PRINCIPAL_PREFIX', 'loops.'),
|
||||
cookie_name=getenv('OIDC_COOKIE_NAME', 'oidc_' + oidc_client_id),
|
||||
cookie_domain=getenv('OIDC_COOKIE_DOMAIN', None),
|
||||
cookie_lifetime=getenv('OIDC_COOKIE_LIFETIME', '86400'),
|
||||
cookie_crypt=getenv('OIDC_COOKIE_CRYPT', None),
|
||||
private_key_file=getenv('OIDC_SERVICE_USER_PRIVATE_KEY_FILE',
|
||||
'scopes/tests/test-private-key.json'),
|
||||
organization_id=getenv('OIDC_ORGANIZATION_ID', '12346'),
|
||||
project_id=getenv('OIDC_PROJECT_ID', None),
|
||||
)
|
||||
16
tests/test.env
Normal file
16
tests/test.env
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
# tests/test.env
|
||||
|
||||
BASE_URL=http://test
|
||||
|
||||
DBNAME=testdb
|
||||
DBUSER=testuser
|
||||
DBPASSWORD=secret
|
||||
DBSCHEMA=testing
|
||||
|
||||
OIDC_PROVIDER=https://oidc.example.com
|
||||
OIDC_CLIENT_ID=12345
|
||||
OIDC_COOKIE_CRYPT=secret_crypt
|
||||
OIDC_ORGANIZATION_ID=12346
|
||||
OIDC_PROJECT_ID=12347
|
||||
|
||||
|
||||
|
|
@ -2,12 +2,7 @@
|
|||
|
||||
"""Tests for the fastapi- (and asyncio-) based stuff"""
|
||||
|
||||
from scopes import testing
|
||||
|
||||
import os, sys
|
||||
|
||||
import config
|
||||
from scopes.storage import tracking
|
||||
from scopes import config
|
||||
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
|
|
@ -15,10 +10,11 @@ from httpx2 import ASGITransport, AsyncClient
|
|||
|
||||
from scopes.api import main
|
||||
from scopes.api.main import app
|
||||
from scopes.storage import tracking
|
||||
|
||||
@pytest.fixture(scope='module')
|
||||
def storage():
|
||||
storage = config.StorageFactory(config)(config.dbschema)
|
||||
storage = config.StorageFactory(config.settings)(config.settings.dbschema)
|
||||
storage.dropTable('tracks')
|
||||
storage.commit()
|
||||
return storage
|
||||
|
|
|
|||
16
tests/test_config.py
Normal file
16
tests/test_config.py
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
# py-scopes: tests.test_config
|
||||
|
||||
"""Tests for scopes configuration settings via config.py and .env"""
|
||||
|
||||
import os
|
||||
import pytest
|
||||
|
||||
from scopes import config
|
||||
config.settings = config.Settings(_env_file='tests/test.env')
|
||||
|
||||
from scopes.config import settings
|
||||
|
||||
def test_config():
|
||||
assert settings.dbname == 'testdb'
|
||||
assert settings.dbschema == 'testing'
|
||||
|
||||
|
|
@ -2,8 +2,7 @@
|
|||
|
||||
""" Tests for the (PostgreSQL) storage-related functionalities"""
|
||||
|
||||
from scopes import testing
|
||||
import config
|
||||
from scopes import config
|
||||
|
||||
from datetime import datetime
|
||||
import pytest
|
||||
|
|
@ -17,7 +16,7 @@ k('session system')
|
|||
|
||||
@pytest.fixture(scope='module')
|
||||
def storage():
|
||||
storage = config.StorageFactory(config)(config.dbschema)
|
||||
storage = config.StorageFactory(config.settings)(config.settings.dbschema)
|
||||
storage.dropTable('tracks')
|
||||
storage.dropTable('folders')
|
||||
storage.dropTable('types')
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue