diff --git a/src/scitacean/testing/backend/_backend.py b/src/scitacean/testing/backend/_backend.py index 866666fa..ecf3309e 100644 --- a/src/scitacean/testing/backend/_backend.py +++ b/src/scitacean/testing/backend/_backend.py @@ -4,6 +4,7 @@ import os import time from copy import deepcopy +from pathlib import Path from typing import Any, Dict, Union from urllib.parse import urljoin @@ -35,7 +36,9 @@ def _docker_compose_template() -> Dict[str, Any]: return template # type: ignore[no-any-return] -def _apply_config(template: Dict[str, Any]) -> Dict[str, Any]: +def _apply_config( + template: Dict[str, Any], account_config_path: Path +) -> Dict[str, Any]: res = deepcopy(template) scicat = res["services"]["scicat"] ports = scicat["ports"][0].split(":") @@ -46,6 +49,10 @@ def _apply_config(template: Dict[str, Any]) -> Dict[str, Any]: env["PID_PREFIX"] = config.PID_PREFIX env["SITE"] = config.SITE + scicat["volumes"] = [ + f"/home/node/app/functionalAccounts.json:{account_config_path}", + ] + return res @@ -57,7 +64,9 @@ def configure(target_path: _PathLike) -> None: target_path: Generate a docker-compose file at this path. """ - c = yaml.dump(_apply_config(_docker_compose_template())) + account_config_path = Path(target_path).parent / "functionalAccounts.json" + config.dump_account_config(account_config_path) + c = yaml.dump(_apply_config(_docker_compose_template(), account_config_path)) if "PLACEHOLDER" in c: raise RuntimeError("Incorrect config") diff --git a/src/scitacean/testing/backend/config.py b/src/scitacean/testing/backend/config.py index 387269c3..e800f8ba 100644 --- a/src/scitacean/testing/backend/config.py +++ b/src/scitacean/testing/backend/config.py @@ -2,8 +2,9 @@ # Copyright (c) 2024 SciCat Project (https://github.com/SciCatProject/scitacean) """Backend configuration.""" +import json from dataclasses import dataclass -from typing import Dict +from pathlib import Path @dataclass @@ -23,7 +24,7 @@ class SciCatUser: group: str @property - def credentials(self) -> Dict[str, str]: + def credentials(self) -> dict[str, str]: """Return login credentials for this user. User as @@ -37,6 +38,16 @@ def credentials(self) -> Dict[str, str]: "password": self.password, } + def dump(self) -> dict[str, str]: + """Return a dict that can be serialized to functionalAccounts.json.""" + return { + "username": self.username, + "password": self.password, + "email": self.email, + "role": self.group, + "global": False, + } + # see https://github.com/SciCatProject/scicat-backend-next/blob/master/src/config/configuration.ts USERS = { @@ -116,3 +127,9 @@ def local_access(user: str) -> SciCatAccess: Parameters for the local SciCat backend. """ return SciCatAccess(url=f"http://localhost:{SCICAT_PORT}/api/v3/", user=USERS[user]) + + +def dump_account_config(path: Path) -> None: + """Write a functional account config for the backend.""" + with path.open("w") as f: + json.dump([user.dump() for user in USERS.values()], f)