diff --git a/swh/deposit/config.py b/swh/deposit/config.py index 585b3be4..aaf2ebd9 100644 --- a/swh/deposit/config.py +++ b/swh/deposit/config.py @@ -1,122 +1,123 @@ # Copyright (C) 2017-2022 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information import os -from typing import Any, Dict +from typing import Any, Dict, Optional from swh.core import config from swh.deposit import __version__ from swh.model.model import MetadataAuthority, MetadataAuthorityType, MetadataFetcher from swh.scheduler import get_scheduler from swh.scheduler.interface import SchedulerInterface from swh.storage import get_storage from swh.storage.interface import StorageInterface # IRIs (Internationalized Resource identifier) sword 2.0 specified EDIT_IRI = "edit_iri" SE_IRI = "se_iri" EM_IRI = "em_iri" CONT_FILE_IRI = "cont_file_iri" SD_IRI = "servicedocument" COL_IRI = "upload" STATE_IRI = "state_iri" PRIVATE_GET_RAW_CONTENT = "private-download" PRIVATE_CHECK_DEPOSIT = "check-deposit" PRIVATE_PUT_DEPOSIT = "private-update" PRIVATE_GET_DEPOSIT_METADATA = "private-read" PRIVATE_LIST_DEPOSITS = "private-deposit-list" PRIVATE_LIST_DEPOSITS_DATATABLES = "private-deposit-list-datatables" ARCHIVE_KEY = "archive" RAW_METADATA_KEY = "raw-metadata" ARCHIVE_TYPE = "archive" METADATA_TYPE = "metadata" AUTHORIZED_PLATFORMS = ["development", "production", "testing"] DEPOSIT_STATUS_REJECTED = "rejected" DEPOSIT_STATUS_PARTIAL = "partial" DEPOSIT_STATUS_DEPOSITED = "deposited" DEPOSIT_STATUS_VERIFIED = "verified" DEPOSIT_STATUS_LOAD_SUCCESS = "done" DEPOSIT_STATUS_LOAD_FAILURE = "failed" # Release author for deposit SWH_PERSON = { "name": "Software Heritage", "fullname": "Software Heritage", "email": "robot@softwareheritage.org", } DEFAULT_CONFIG = { "max_upload_size": 209715200, "checks": True, } -def setup_django_for(platform=None, config_file=None): - """Setup function for command line tools (swh.deposit.create_user) to - initialize the needed db access. +def setup_django_for(platform: Optional[str] = None, config_file: Optional[str] = None): + """Setup function for command line tools (e.g. swh.deposit.create_user) to + initialize the needed db access. Note: Do not import any django related module prior to this function - call. Otherwise, this will raise an - django.core.exceptions.ImproperlyConfigured error message. + call. Otherwise, this will raise a django.core.exceptions.ImproperlyConfigured + error message. Args: - platform (str): the platform the scheduling is running - config_file (str): Extra configuration file (typically for the - production platform) + platform: the platform to use when running program (e.g. cli, ...) + config_file: Extra configuration file (typically for the production platform) Raises: - ValueError in case of wrong platform inputs. + ValueError in case of wrong platform inputs """ if platform is not None: if platform not in AUTHORIZED_PLATFORMS: - raise ValueError("Platform should be one of %s" % AUTHORIZED_PLATFORMS) + raise ValueError(f"Platform should be one of {AUTHORIZED_PLATFORMS}") if "DJANGO_SETTINGS_MODULE" not in os.environ: - os.environ["DJANGO_SETTINGS_MODULE"] = "swh.deposit.settings.%s" % platform + os.environ["DJANGO_SETTINGS_MODULE"] = f"swh.deposit.settings.{platform}" if config_file: + # Hack to set the environment variable which in some cases is required (e.g. + # production) os.environ.setdefault("SWH_CONFIG_FILENAME", config_file) - import django + from django import setup - django.setup() + setup() class APIConfig: """API Configuration centralized class. This loads explicitly the configuration file out of the SWH_CONFIG_FILENAME environment variable. """ def __init__(self): self.config: Dict[str, Any] = config.load_from_envvar(DEFAULT_CONFIG) self.scheduler: SchedulerInterface = get_scheduler(**self.config["scheduler"]) self.tool = { "name": "swh-deposit", "version": __version__, "configuration": {"sword_version": "2"}, } self.storage: StorageInterface = get_storage(**self.config["storage"]) self.storage_metadata: StorageInterface = get_storage( **self.config["storage_metadata"] ) def swh_deposit_authority(self): return MetadataAuthority( type=MetadataAuthorityType.REGISTRY, url=self.config["swh_authority_url"], ) def swh_deposit_fetcher(self): return MetadataFetcher( name=self.tool["name"], version=self.tool["version"], ) diff --git a/swh/deposit/tests/test_config.py b/swh/deposit/tests/test_config.py new file mode 100644 index 00000000..82fd404a --- /dev/null +++ b/swh/deposit/tests/test_config.py @@ -0,0 +1,38 @@ +# Copyright (C) 2022 The Software Heritage developers +# See the AUTHORS file at the top-level directory of this distribution +# License: GNU General Public License version 3, or any later version +# See top-level LICENSE file for more information + +import os + +import pytest + +from swh.deposit.config import setup_django_for + + +def test_setup_django_for_raise_unknown_platform(): + """Unknown platform should make the function setup raise""" + with pytest.raises(ValueError, match="Platform should be"): + setup_django_for(platform="unknown") + + +def test_setup_django__for_set_django_settings_module(monkeypatch, deposit_config_path): + monkeypatch.delenv("DJANGO_SETTINGS_MODULE") + platform = "testing" + setup_django_for(platform) + + assert os.environ["DJANGO_SETTINGS_MODULE"] == f"swh.deposit.settings.{platform}" + + +def test_setup_django_for_ok_set_django_settings_module( + monkeypatch, deposit_config_path +): + monkeypatch.delenv("SWH_CONFIG_FILENAME") + setup_django_for("testing", deposit_config_path) + + assert os.environ["SWH_CONFIG_FILENAME"] == deposit_config_path + + +def test_setup_django_for_ok(deposit_config_path): + """Everything is fine, moving along (fixture sets environment appropriately)""" + setup_django_for()