Skip to content
This repository has been archived by the owner on Feb 15, 2024. It is now read-only.

Commit

Permalink
Merge pull request #6 from jotelha/2022-11-22-config
Browse files Browse the repository at this point in the history
 All refactored repos to main
  • Loading branch information
jotelha authored May 14, 2023
2 parents 4482085 + e1b3768 commit ab6fde5
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 30 deletions.
15 changes: 12 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ jobs:
matrix:
python-version: ['3.7', '3.8', '3.9', '3.10']
mongodb-version: ['4.2', '4.4', '5.0', '6.0']
dtool-lookup-server-version: [ main ]
dtool-lookup-server-retrieve-plugin-mongo-version: [ main ]

steps:
- name: Git checkout
Expand All @@ -38,18 +40,25 @@ jobs:
# pip install --upgrade setuptools wheel setuptools-scm[toml] importlib-metadata
pip install flake8 pytest pytest-cov
- name: Install server refactor and retrieve plugin
- name: Install server and retrieve plugin
run: |
# This should move into the strategy matrix once released
pip install git+https://github.com/jic-dtool/dtool-lookup-server.git@mongo-refactor
pip install git+https://github.com/jic-dtool/dtool-lookup-server-retrieve-plugin-mongo.git@main
pip install git+https://github.com/jotelha/dtool-lookup-server.git@${{ matrix.dtool-lookup-server-version }}
pip install git+https://github.com/jotelha/dtool-lookup-server-retrieve-plugin-mongo.git@${{ matrix.dtool-lookup-server-retrieve-plugin-mongo-version }}
- name: Install own requirements
run: |
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
pip install .
pip list
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Test with pytest
run: |
pytest -sv
23 changes: 6 additions & 17 deletions dtool_lookup_server_search_plugin_mongo/config.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,12 @@
import json
import os

import dtool_lookup_server_search_plugin_mongo

CONFIG_SECRETS_TO_OBFUSCATE = [
"SEARCH_MONGO_URI",
"SEARCH_MONGO_DB",
"SEARCH_MONGO_COLLECTION"
]

class Config(object):
SEARCH_MONGO_URI = os.environ.get("SEARCH_MONGO_URI", "mongodb://localhost:27017/")
SEARCH_MONGO_DB = os.environ.get("SEARCH_MONGO_DB", "dtool_info")
SEARCH_MONGO_COLLECTION = os.environ.get("SEARCH_MONGO_COLLECTION", "datasets")

@classmethod
def to_dict(cls):
"""Convert server configuration into dict."""
exclusions = [
# sensitive data, i.e. password
] # config keys to exclude
d = {"version": dtool_lookup_server_search_plugin_mongo.__version__}
for k, v in cls.__dict__.items():
# select only capitalized fields
if k.upper() == k and k not in exclusions:
d[k.lower()] = v
return d
SEARCH_MONGO_COLLECTION = os.environ.get("SEARCH_MONGO_COLLECTION", "datasets")
7 changes: 6 additions & 1 deletion dtool_lookup_server_search_plugin_mongo/utils_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
extract_frozen_at_as_datetime,
)

from dtool_lookup_server_search_plugin_mongo.config import Config
from dtool_lookup_server_search_plugin_mongo.config import (
Config, CONFIG_SECRETS_TO_OBFUSCATE)


VALID_MONGO_QUERY_KEYS = (
Expand Down Expand Up @@ -219,3 +220,7 @@ def lookup_uris(self, uuid, base_uris):
def get_config(self):
"""Return initial Config object, available app-instance independent."""
return Config

def get_config_secrets_to_obfuscate(self):
"""Return config secrets never to be exposed clear text."""
return CONFIG_SECRETS_TO_OBFUSCATE
8 changes: 5 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ def snowwhite_token():
@pytest.fixture
def tmp_app_with_users(request):
"""Provide app with users"""
from dtool_lookup_server import create_app, sql_db, retrieve, search
from flask import current_app
from dtool_lookup_server import create_app, sql_db
from dtool_lookup_server.utils import (
register_users,
register_base_uri,
Expand All @@ -37,6 +38,7 @@ def tmp_app_with_users(request):
config = {
"API_TITLE": 'dtool-lookup-server API',
"API_VERSION": 'v1',
"CONFIG_SECRETS_TO_OBFUSCATE": [],
"OPENAPI_VERSION": '3.0.2',
"SECRET_KEY": "secret",
"FLASK_ENV": "development",
Expand Down Expand Up @@ -82,8 +84,8 @@ def tmp_app_with_users(request):

@request.addfinalizer
def teardown():
retrieve.client.drop_database(tmp_mongo_db_name)
search.client.drop_database(tmp_mongo_db_name)
current_app.retrieve.client.drop_database(tmp_mongo_db_name)
current_app.search.client.drop_database(tmp_mongo_db_name)
sql_db.session.remove()

return app.test_client()
24 changes: 18 additions & 6 deletions tests/test_config_route.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,24 @@ def test_config_info_route(tmp_app_with_users, snowwhite_token): # NOQA
assert r.status_code == 200

expected_content = {
'dtool_lookup_server_search_plugin_mongo': {
'search_mongo_collection': 'datasets',
'search_mongo_db': 'dtool_info',
'search_mongo_uri': 'mongodb://localhost:27017/',
'version': dtool_lookup_server_search_plugin_mongo.__version__
}
'search_mongo_collection': 'datasets',
'search_mongo_uri': 'mongodb://localhost:27017/',
}

response = json.loads(r.data.decode("utf-8"))

assert compare_nested(expected_content, response)


def test_config_version_route(tmp_app_with_users): # NOQA

r = tmp_app_with_users.get(
"/config/versions",
)
assert r.status_code == 200

expected_content = {
'dtool_lookup_server_search_plugin_mongo': dtool_lookup_server_search_plugin_mongo.__version__,
}

response = json.loads(r.data.decode("utf-8"))
Expand Down

0 comments on commit ab6fde5

Please sign in to comment.