diff --git a/docs/getting-started/prerequisites.md b/docs/getting-started/prerequisites.md index e3a5567..87c02ac 100644 --- a/docs/getting-started/prerequisites.md +++ b/docs/getting-started/prerequisites.md @@ -1,4 +1,4 @@ -1. **Benchling App with Admin Privileges**: Liminal builds on top of Benchling's LIMS system. You will need to have a Benchling App with admin privileges to your Benchling tenant(s). Liminal needs admin privileges in order to manipulate Benchling schemas through the Benchling API and SDK. Ensure the app is configured as an admin on all your entity schemas and dropdowns. [Follow instructions here](https://docs.benchling.com/docs/getting-started-benchling-apps#installing-your-app) to install a Benchling App with admin privileges. +1. **Benchling App with Admin Privileges**: Liminal builds on top of Benchling's LIMS system. You will need to have a Benchling App with admin privileges to your Benchling tenant(s). Liminal needs admin privileges in order to manipulate Benchling schemas through the Benchling API and SDK. Ensure the app is configured as an admin on your registry and on all entity schemas and dropdowns within the registry. [Follow instructions here](https://docs.benchling.com/docs/getting-started-benchling-apps#installing-your-app) to install a Benchling App with admin privileges. 2. **Python**: Liminal supports Python versions 3.10 through 3.13 for package users. diff --git a/docs/reference/benchling-connection.md b/docs/reference/benchling-connection.md index fa899e1..8a5fd10 100644 --- a/docs/reference/benchling-connection.md +++ b/docs/reference/benchling-connection.md @@ -43,6 +43,13 @@ connection = BenchlingConnection( Whether your Benchling tenant has access to fieldsets. +- **registry_id: Optional[str] = None** + + The API id of the registry to scope this connection to, for example `src_abc123`. Only needed when the tenant has more than one registry. When set, Liminal reads and writes only the entity schemas and dropdowns in that registry, and the results schemas owned by the registry's organization. For a tenant with multiple registries and no `registry_id` set, Liminal raises an error in `BenchlingService`. + + !!! tip + For a tenant with several registries, give each registry its own Liminal directory (`env.py` and `versions/`), with one `BenchlingConnection` per tenant that defines that registry's `registry_id`. Each registry then keeps its own revision history in its own `liminal_remote` schema. + - **config_flags: TenantConfigFlags = TenantConfigFlags()** Set of config flags that are configured on the tenant level, that defines specific features of the tenant. These can be updated on Benchling's end by contacting their support team. diff --git a/liminal/base/base_dropdown.py b/liminal/base/base_dropdown.py index 6ffe625..1bf922b 100644 --- a/liminal/base/base_dropdown.py +++ b/liminal/base/base_dropdown.py @@ -2,7 +2,7 @@ from typing import Any from liminal.connection.benchling_service import BenchlingService -from liminal.dropdowns.utils import get_benchling_dropdown_summaries +from liminal.dropdowns.utils import get_benchling_dropdown_id_by_name class BaseDropdown(ABC): @@ -52,15 +52,9 @@ def get_id(cls, benchling_service: BenchlingService) -> str: str The id of the dropdown. """ - all_dropdowns = get_benchling_dropdown_summaries(benchling_service) - dropdowns_found_by_name = [ - d for d in all_dropdowns if d.name == cls.__benchling_name__ - ] - if len(dropdowns_found_by_name) == 0: - raise ValueError(f"No dropdowns found with name '{cls.__benchling_name__}'") - else: - dropdown = dropdowns_found_by_name[0] - return dropdown.id + return get_benchling_dropdown_id_by_name( + benchling_service, cls.__benchling_name__ + ) @classmethod def validate(cls, *values: str | None) -> None: diff --git a/liminal/connection/benchling_connection.py b/liminal/connection/benchling_connection.py index eddfb47..29029bc 100644 --- a/liminal/connection/benchling_connection.py +++ b/liminal/connection/benchling_connection.py @@ -45,6 +45,9 @@ class BenchlingConnection(BaseModel): DEPRECATED: Liminal no longer uses Playwright to log into Benchling's internal API, so this is ignored. It will be removed in v6. fieldsets: bool = False Whether your Benchling tenant has access to fieldsets. + registry_id: str | None = None + The API id of the registry to scope this connection to, e.g. src_abc123. Only needed when the tenant has more than one registry. + When set, Liminal reads and writes only the schemas and dropdowns in this registry, and the result schemas owned by its organization. config_flags: TenantConfigFlags = TenantConfigFlags() Set of config flags that are configured on the tenant level. These can be updated on Benchling's end by contacting their support team. """ @@ -59,6 +62,7 @@ class BenchlingConnection(BaseModel): internal_api_admin_password: str | None = None playwright_data_dir: str | None = None fieldsets: bool = False + registry_id: str | None = None config_flags: TenantConfigFlags = TenantConfigFlags() @model_validator(mode="before") diff --git a/liminal/connection/benchling_service.py b/liminal/connection/benchling_service.py index 3fe3cc3..a11161d 100644 --- a/liminal/connection/benchling_service.py +++ b/liminal/connection/benchling_service.py @@ -1,10 +1,12 @@ import logging import warnings +from functools import cached_property from typing import TYPE_CHECKING, Any from benchling_sdk.auth.client_credentials_oauth2 import ClientCredentialsOAuth2 from benchling_sdk.benchling import Benchling, BenchlingApiClientDecorator from benchling_sdk.helpers.retry_helpers import RetryStrategy +from pydantic import BaseModel from sqlalchemy import create_engine from sqlalchemy.engine import Engine from sqlalchemy.orm import Session, configure_mappers @@ -28,6 +30,18 @@ REMOTE_REVISION_ID_FIELD_WH_NAME = "revision_id" +class RegistryRef(BaseModel): + """The registry a connection writes to, and the organization that owns it.""" + + name: str + id: str + owner_id: str + + +class AmbiguousRegistryError(ValueError): + """A lookup matched objects that may sit in different registries, and the connection doesn't name one.""" + + class BenchlingService(Benchling): """ Class that creates a connection object that can be used to connect to Benchling's API and/or Postgres warehouse. @@ -95,6 +109,17 @@ def __init__( raise ValueError( "use_db is True but warehouse_connection_string not provided in BenchlingConnection." ) + if use_api and self.has_ambiguous_registries(): + raise ValueError( + f"Tenant {self.benchling_tenant} has {len(self._registries)} registries. " + "Liminal does not allow ambiguous registries. Set registry_id on the BenchlingConnection to choose one. " + f"Registries found: {self._registries}" + ) + + @property + def tenant_registry_name(self) -> str: + """The name of the tenant and registry this service is connected to. Raises if it can't be determined.""" + return f"[Tenant {self.benchling_tenant}: Registry {self.registry_name}]" @property def session(self) -> Session: @@ -103,17 +128,66 @@ def session(self) -> Session: @property def registry_id(self) -> str: - # This assumes there is only one registry (which has always been the case at DynoTx) - registries = self.registry.registries() - assert len(registries) == 1 - return registries[0].id + """The id of the registry this connection writes to. Raises if it can't be determined.""" + return self._registry_ref.id + + @property + def registry_name(self) -> str: + """The name of the registry this connection writes to. Raises if it can't be determined.""" + return self._registry_ref.name @property def organization_id(self) -> str: - # This assumes there is only one registry (which has always been the case at DynoTx) - registries = self.registry.registries() - assert len(registries) == 1 - return registries[0].owner.id + """The id of the organization that owns this connection's registry. Raises if it can't be determined.""" + return self._registry_ref.owner_id + + @cached_property + def _registries(self) -> list[RegistryRef]: + """Every registry on the tenant.""" + from liminal.utils import list_all_items_v3 + + return [ + RegistryRef(id=r["id"], name=r["name"], owner_id=r["owner"]["id"]) + for r in list_all_items_v3( + self, "/api/v3/registry/items", include_archived=None + ) + ] + + @property + def _registry_ref(self) -> RegistryRef: + if self.connection.registry_id is not None: + registry_ref = next( + (r for r in self._registries if r.id == self.connection.registry_id), + None, + ) + if registry_ref is None: + raise ValueError( + f"Tenant {self.benchling_tenant}: registry {self.connection.registry_id} not found." + ) + return registry_ref + if len(self._registries) != 1: + raise ValueError( + f"Tenant {self.benchling_tenant} has {len(self._registries)} registries. " + "Set registry_id on the BenchlingConnection to choose one." + ) + return self._registries[0] + + def is_in_registry_scope(self, record: dict[str, Any]) -> bool: + """Whether a v3 API record belongs to this connection's registry.""" + registry_id = (record.get("registry") or {}).get("id") + if registry_id: + return registry_id == self.registry_id + return False + + def is_in_organization_scope(self, record: dict[str, Any]) -> bool: + """Whether a v3 API record belongs to this connection's organization.""" + organization_id = (record.get("organization") or {}).get("id") + if organization_id: + return organization_id == self.organization_id + return False + + def has_ambiguous_registries(self) -> bool: + return len(self._registries) > 1 and self.connection.registry_id is None def __enter__(self) -> Session: self._session = self.get_session() diff --git a/liminal/dropdowns/api.py b/liminal/dropdowns/api.py index 068b55c..cbc338b 100644 --- a/liminal/dropdowns/api.py +++ b/liminal/dropdowns/api.py @@ -28,7 +28,10 @@ def list_dropdowns( ) -> list[dict[str, Any]]: """Fetch dropdowns from the v3 API.""" return list_all_items_v3( - benchling_service, f"{_DROPDOWN_API_PATH}/items", include_archived + benchling_service, + f"{_DROPDOWN_API_PATH}/items", + include_archived, + registry_scoped=True, ) diff --git a/liminal/dropdowns/operations.py b/liminal/dropdowns/operations.py index caa0d3b..c148243 100644 --- a/liminal/dropdowns/operations.py +++ b/liminal/dropdowns/operations.py @@ -23,7 +23,7 @@ from liminal.dropdowns.utils import ( dropdown_exists_in_benchling, get_benchling_dropdown_by_name, - get_benchling_dropdown_summary_by_name, + get_benchling_dropdown_id_by_name, ) LOGGER = logging.getLogger(__name__) @@ -118,7 +118,7 @@ def __init__(self, dropdown_name: str, new_dropdown_name: str) -> None: self.new_dropdown_name = new_dropdown_name def execute(self, benchling_service: BenchlingService) -> dict[str, Any]: - dropdown = get_benchling_dropdown_summary_by_name( + dropdown_id = get_benchling_dropdown_id_by_name( benchling_service, self.dropdown_name ) if dropdown_exists_in_benchling(benchling_service, self.new_dropdown_name): @@ -126,7 +126,7 @@ def execute(self, benchling_service: BenchlingService) -> dict[str, Any]: f"Dropdown {self.new_dropdown_name} already exists in Benchling." ) return update_dropdown_name( - benchling_service, dropdown.id, self.new_dropdown_name + benchling_service, dropdown_id, self.new_dropdown_name ) def describe_operation(self) -> str: diff --git a/liminal/dropdowns/utils.py b/liminal/dropdowns/utils.py index 830e462..3fb94eb 100644 --- a/liminal/dropdowns/utils.py +++ b/liminal/dropdowns/utils.py @@ -1,7 +1,7 @@ from typing import Any from benchling_sdk.models import ArchiveRecord -from benchling_sdk.models import Dropdown, DropdownOption, DropdownSummary +from benchling_sdk.models import Dropdown, DropdownOption from liminal.connection import BenchlingService from liminal.dropdowns.api import list_dropdowns, list_dropdowns_with_options @@ -11,7 +11,7 @@ def get_benchling_dropdown_id_name_map( benchling_service: BenchlingService, ) -> dict[str, str]: - return {d.id: d.name for d in get_benchling_dropdown_summaries(benchling_service)} + return {d["id"]: d["name"] for d in list_dropdowns(benchling_service)} def get_benchling_dropdown_name_to_classname_map( @@ -27,37 +27,26 @@ def get_benchling_dropdown_name_to_classname_map( } -def get_benchling_dropdown_summaries( - benchling_service: BenchlingService, -) -> list[DropdownSummary]: - return [ - dropdown - for sublist in benchling_service.dropdowns.list() - for dropdown in sublist - ] - - -def get_benchling_dropdown_summary_by_name( +def get_benchling_dropdown_id_by_name( benchling_service: BenchlingService, name: str -) -> DropdownSummary: - for dropdown in get_benchling_dropdown_summaries(benchling_service): - if dropdown.name == name: - return dropdown - raise Exception(f"Dropdown {name} not found in given list.") +) -> str: + """Finds the id of a dropdown by name in the connection's registry. Names are unique within a registry.""" + dropdown = next( + (d for d in list_dropdowns(benchling_service) if d["name"] == name), None + ) + if dropdown is None: + raise ValueError( + f"Dropdown {name} not found in Benchling {benchling_service.tenant_registry_name}." + ) + return dropdown["id"] def get_benchling_dropdown_by_name( benchling_service: BenchlingService, name: str ) -> Dropdown: - dropdown = None - for d in get_benchling_dropdown_summaries(benchling_service): - if d.name == name: - dropdown = d - if dropdown is None: - raise Exception( - f"Dropdown {name} not found in Benchling {benchling_service.benchling_tenant}." - ) - return benchling_service.dropdowns.get_by_id(dropdown.id) + return benchling_service.dropdowns.get_by_id( + get_benchling_dropdown_id_by_name(benchling_service, name) + ) def _convert_dropdown_from_v3_to_v2_sdk( @@ -107,9 +96,7 @@ def get_benchling_dropdowns_dict( def dropdown_exists_in_benchling( benchling_service: BenchlingService, name: str ) -> bool: - return any( - d.name == name for d in get_benchling_dropdown_summaries(benchling_service) - ) + return any(d["name"] == name for d in list_dropdowns(benchling_service)) def get_schemas_with_dropdown(dropdown_name: str) -> list[str]: diff --git a/liminal/entity_schemas/api.py b/liminal/entity_schemas/api.py index ded2a16..1cbade8 100644 --- a/liminal/entity_schemas/api.py +++ b/liminal/entity_schemas/api.py @@ -54,13 +54,14 @@ def list_entity_schemas( @lru_cache -def list_entity_schemas_cached( +def list_entity_schemas_v2_cached( benchling_service: BenchlingService, ) -> list[EntitySchema]: return [ s for schemas in benchling_service.schemas.list_entity_schemas() for s in schemas + if s.registry_id == benchling_service.registry_id ] @@ -74,6 +75,7 @@ def _list_entity_schemas_for_endpoint( benchling_service, f"{_entity_schema_specific_api_path(endpoint)}/items", include_archived, + organization_scoped=True, ) diff --git a/liminal/entity_schemas/entity_schema_models.py b/liminal/entity_schemas/entity_schema_models.py index 17ac2d8..901fbf1 100644 --- a/liminal/entity_schemas/entity_schema_models.py +++ b/liminal/entity_schemas/entity_schema_models.py @@ -16,7 +16,7 @@ from liminal.base.properties.base_name_template import BaseNameTemplate from liminal.base.properties.base_schema_properties import BaseSchemaProperties from liminal.connection.benchling_service import BenchlingService -from liminal.dropdowns.utils import get_benchling_dropdown_summary_by_name +from liminal.dropdowns.utils import get_benchling_dropdown_id_by_name from liminal.entity_schemas.api import ( attach_entity_schema_field_definitions, list_entity_schemas, @@ -165,11 +165,11 @@ def update_from_props( raise ValueError( "Benchling SDK must be provided to update dropdown field." ) - dropdown_summary_id = get_benchling_dropdown_summary_by_name( + dropdown_id = get_benchling_dropdown_id_by_name( benchling_service, update_props.dropdown_link - ).id + ) self.linkDefinition = FieldLinkDefinitionModel( - id=dropdown_summary_id, + id=dropdown_id, typename=BenchlingLinkDefinitionType.DROPDOWN, ) self.displayPrecision = ( @@ -304,9 +304,9 @@ def from_benchling_props( ) dropdown_id = None if field_props.dropdown_link is not None: - dropdown_id = get_benchling_dropdown_summary_by_name( + dropdown_id = get_benchling_dropdown_id_by_name( benchling_service, field_props.dropdown_link - ).id + ) unit_id = None if field_props.unit_name is not None and field_type.supports_unit(): unit_id = get_unit_id_from_name(benchling_service, field_props.unit_name) @@ -554,7 +554,7 @@ def get_one( ) if schema is None: raise ValueError( - f"Schema {wh_schema_name} not found in Benchling {benchling_service.benchling_tenant}." + f"Schema {wh_schema_name} not found in Benchling {benchling_service.tenant_registry_name}." ) schema_with_fields = attach_entity_schema_field_definitions( benchling_service, schema diff --git a/liminal/migrate/components.py b/liminal/migrate/components.py index 428dca6..53c373a 100644 --- a/liminal/migrate/components.py +++ b/liminal/migrate/components.py @@ -30,11 +30,11 @@ def get_full_migration_operations( ] if len(dropdown_operations) == 0: print( - f"[bold]Dropdowns are in sync for {benchling_service.benchling_tenant} tenant!" + f"[bold]Dropdowns are in sync for {benchling_service.tenant_registry_name}!" ) else: print( - f"[bold]Dropdowns are out of sync for {benchling_service.benchling_tenant} tenant." + f"[bold]Dropdowns are out of sync for {benchling_service.tenant_registry_name}." ) if not entity_schema_names or len(entity_schema_names) > 0: @@ -48,11 +48,11 @@ def get_full_migration_operations( ] if len(entity_schema_operations) == 0: print( - f"[bold]Entity schemas are in sync for {benchling_service.benchling_tenant} tenant!" + f"[bold]Entity schemas are in sync for {benchling_service.tenant_registry_name}!" ) else: print( - f"[bold]Entity schemas are out of sync for {benchling_service.benchling_tenant} tenant." + f"[bold]Entity schemas are out of sync for {benchling_service.tenant_registry_name}." ) all_operations: list[CompareOperation] = ( diff --git a/liminal/orm/base_model.py b/liminal/orm/base_model.py index 10937ca..48f36d3 100644 --- a/liminal/orm/base_model.py +++ b/liminal/orm/base_model.py @@ -14,7 +14,7 @@ from liminal.base.base_dropdown import BaseDropdown from liminal.base.base_validation_filters import BaseValidatorFilters from liminal.connection.benchling_service import BenchlingService -from liminal.entity_schemas.api import list_entity_schemas_cached +from liminal.entity_schemas.api import list_entity_schemas_v2_cached from liminal.enums import BenchlingNamingStrategy from liminal.enums.benchling_entity_type import BenchlingEntityType from liminal.enums.sequence_constraint import SequenceConstraint @@ -308,7 +308,7 @@ def get_id(cls, benchling_service: BenchlingService) -> str: str The id of the schema. """ - all_schemas = list_entity_schemas_cached(benchling_service) + all_schemas = list_entity_schemas_v2_cached(benchling_service) schemas_found_by_name = [ s for s in all_schemas if s.name == cls.__schema_properties__.name diff --git a/liminal/orm/base_results_model.py b/liminal/orm/base_results_model.py index 6a4fe80..a1e2496 100644 --- a/liminal/orm/base_results_model.py +++ b/liminal/orm/base_results_model.py @@ -16,7 +16,7 @@ from liminal.orm.base import Base from liminal.orm.base_tables.user import User from liminal.orm.results_schema_properties import ResultsSchemaProperties -from liminal.results_schemas.utils import get_benchling_results_schemas +from liminal.results_schemas.utils import list_benchling_results_schemas_v2_cached from liminal.validation import BenchlingValidatorReport T = TypeVar("T") @@ -99,7 +99,7 @@ def get_id(cls, benchling_service: BenchlingService) -> str: str The id of the results schema. """ - all_schemas = get_benchling_results_schemas(benchling_service) + all_schemas = list_benchling_results_schemas_v2_cached(benchling_service) schemas_found_by_name = [ s for s in all_schemas if s.name == cls.__schema_properties__.name diff --git a/liminal/results_schemas/api.py b/liminal/results_schemas/api.py index d7671e7..9c89ce7 100644 --- a/liminal/results_schemas/api.py +++ b/liminal/results_schemas/api.py @@ -13,8 +13,13 @@ def list_results_schemas( benchling_service: BenchlingService, ) -> list[dict[str, Any]]: - """Fetch result schemas from the v3 API.""" - return list_all_items_v3(benchling_service, f"{_RESULT_SCHEMA_API_PATH}/items") + """Fetch result schemas from the v3 API. Result schemas carry an organization but no registry, + so they are scoped to the organization that owns the connection's registry.""" + return list_all_items_v3( + benchling_service, + f"{_RESULT_SCHEMA_API_PATH}/items", + organization_scoped=True, + ) def list_result_schema_field_definitions( diff --git a/liminal/results_schemas/models/results_schema_model.py b/liminal/results_schemas/models/results_schema_model.py index 2a33ebb..9306cdc 100644 --- a/liminal/results_schemas/models/results_schema_model.py +++ b/liminal/results_schemas/models/results_schema_model.py @@ -142,13 +142,12 @@ def get_one( schema for schema in schemas_data if schema["systemName"] == wh_schema_name - # and schema["registryId"] == benchling_service.registry_id ), None, ) if schema is None: raise ValueError( - f"Schema {wh_schema_name} not found in Benchling {benchling_service.benchling_tenant}." + f"Schema {wh_schema_name} not found in Benchling {benchling_service.tenant_registry_name}." ) return cls.model_validate(schema) diff --git a/liminal/results_schemas/utils.py b/liminal/results_schemas/utils.py index 300b610..ab1e7c9 100644 --- a/liminal/results_schemas/utils.py +++ b/liminal/results_schemas/utils.py @@ -99,9 +99,12 @@ def get_converted_results_schemas( @lru_cache -def get_benchling_results_schemas( +def list_benchling_results_schemas_v2_cached( benchling_service: BenchlingService, ) -> list[AssayResultSchema]: return [ - s for loe in benchling_service.schemas.list_assay_result_schemas() for s in loe + s + for loe in benchling_service.schemas.list_assay_result_schemas() + for s in loe + if s.organization.id == benchling_service.organization_id ] diff --git a/liminal/tests/test_benchling_service.py b/liminal/tests/test_benchling_service.py index ff80d36..afef995 100644 --- a/liminal/tests/test_benchling_service.py +++ b/liminal/tests/test_benchling_service.py @@ -1,5 +1,9 @@ +from collections.abc import Iterator +from types import SimpleNamespace from typing import cast +from unittest.mock import patch +import pytest from benchling_api_client.v2.benchling_client import BenchlingApiClient from liminal.connection.benchling_connection import BenchlingConnection @@ -17,6 +21,18 @@ def _connection() -> BenchlingConnection: ) +@pytest.fixture(autouse=True) +def _single_registry_tenant() -> Iterator[None]: + """BenchlingService lists the tenant's registries on init, so serve one instead of calling Benchling.""" + page = SimpleNamespace( + parsed={ + "items": [{"id": "src_main", "name": "Main", "owner": {"id": "ent_main"}}] + } + ) + with patch("liminal.utils.rate_limited_get_v3", return_value=page): + yield + + class TestBenchlingServiceClientDecorator: def test_default_timeout_is_sdk_default(self) -> None: service = BenchlingService(_connection(), use_db=False) diff --git a/liminal/tests/test_registry_scoping.py b/liminal/tests/test_registry_scoping.py new file mode 100644 index 0000000..23f0c47 --- /dev/null +++ b/liminal/tests/test_registry_scoping.py @@ -0,0 +1,236 @@ +from collections.abc import Callable, Iterator +from contextlib import contextmanager +from types import SimpleNamespace +from typing import Any +from unittest.mock import patch + +import pytest + +from liminal.connection.benchling_connection import BenchlingConnection +from liminal.connection.benchling_service import BenchlingService +from liminal.dropdowns.api import list_dropdowns +from liminal.entity_schemas.api import list_entity_schemas +from liminal.enums import BenchlingEntitySchemaEndpointType +from liminal.results_schemas.api import list_results_schemas + +MAIN = SimpleNamespace(id="src_main", name="Main", owner=SimpleNamespace(id="ent_main")) +OTHER = SimpleNamespace( + id="src_other", name="Other", owner=SimpleNamespace(id="ent_other") +) + + +_TENANT_REGISTRIES: list[Any] = [] + + +def _registry_page() -> Any: + return SimpleNamespace( + parsed={ + "items": [ + {"id": r.id, "name": r.name, "owner": {"id": r.owner.id}} + for r in _TENANT_REGISTRIES + ] + } + ) + + +@contextmanager +def _v3_api(handler: Callable[[str], list[dict[str, Any]]]) -> Iterator[None]: + """Serves the v3 registry list from _TENANT_REGISTRIES and every other v3 list from handler.""" + + def fake_get(_service: Any, url: str) -> Any: + if url.startswith("/api/v3/registry/items"): + return _registry_page() + return SimpleNamespace(parsed={"items": handler(url)}) + + with patch("liminal.utils.rate_limited_get_v3", side_effect=fake_get): + yield + + +@pytest.fixture(autouse=True) +def _fake_registry_api(monkeypatch: pytest.MonkeyPatch) -> None: + """Serves the v3 registry list from _TENANT_REGISTRIES.""" + monkeypatch.setattr( + "liminal.utils.rate_limited_get_v3", + lambda _service, _url: _registry_page(), + ) + + +def _service( + registries: list[Any], registry_id: str | None = None, use_api: bool = True +) -> BenchlingService: + _TENANT_REGISTRIES[:] = registries + connection = BenchlingConnection( + tenant_name="test-tenant", + api_client_id="test-id", + api_client_secret="test-secret", + registry_id=registry_id, + ) + return BenchlingService(connection, use_api=use_api) + + +def _schema(system_name: str, registry: Any) -> dict[str, Any]: + """Entity schema records carry the owning organization but no registry.""" + return { + "id": f"ts_{system_name}_{registry.id}", + "systemName": system_name, + "organization": {"id": registry.owner.id}, + } + + +def _dropdown(name: str, registry: Any) -> dict[str, Any]: + return { + "id": f"sfs_{name}_{registry.id}", + "name": name, + "registry": {"id": registry.id}, + "archived": False, + "options": [], + } + + +class TestRegistryResolution: + def test_single_registry_without_name_resolves(self) -> None: + service = _service([MAIN]) + assert service.registry_id == "src_main" + assert service.organization_id == "ent_main" + + def test_several_registries_without_name_fail_on_init(self) -> None: + with pytest.raises(ValueError, match="does not allow ambiguous registries"): + _service([MAIN, OTHER]) + + def test_single_registry_with_name_is_not_ambiguous(self) -> None: + service = _service([MAIN], registry_id="src_main") + assert not service.has_ambiguous_registries() + assert service.registry_id == "src_main" + + def test_several_registries_with_name_are_not_ambiguous(self) -> None: + service = _service([MAIN, OTHER], registry_id="src_main") + assert not service.has_ambiguous_registries() + + def test_registry_id_selects_registry_and_owner(self) -> None: + service = _service([MAIN, OTHER], registry_id="src_other") + assert service.registry_id == "src_other" + assert service.organization_id == "ent_other" + + def test_unknown_registry_id_on_single_registry_raises(self) -> None: + service = _service([MAIN], registry_id="src_missing") + with pytest.raises(ValueError, match="registry src_missing not found"): + service.registry_id + + def test_without_api_registries_are_not_listed_on_init(self) -> None: + with patch("liminal.utils.rate_limited_get_v3") as get: + _service([MAIN, OTHER], use_api=False) + get.assert_not_called() + + def test_unknown_registry_id_raises_on_access_not_init(self) -> None: + service = _service([MAIN, OTHER], registry_id="src_missing") + with pytest.raises(ValueError, match="registry src_missing not found"): + service.registry_id + + def test_registries_are_listed_once(self) -> None: + with patch( + "liminal.utils.rate_limited_get_v3", return_value=_registry_page() + ) as get: + service = _service([MAIN, OTHER], registry_id="src_other") + service.registry_id + service.organization_id + get.assert_called_once() + # The registry list rejects an archived filter. + url = get.call_args.args[1] + assert url.startswith("/api/v3/registry/items") + assert "archived" not in url + + +class TestScopeFilter: + def test_registry_scope_matches_registry(self) -> None: + service = _service([MAIN, OTHER], registry_id="src_main") + assert service.is_in_registry_scope(_dropdown("Species", MAIN)) + assert not service.is_in_registry_scope(_dropdown("Species", OTHER)) + assert not service.is_in_registry_scope(_schema("plasmid", MAIN)) + assert not service.is_in_registry_scope({"id": "no_owner"}) + + def test_organization_scope_matches_organization(self) -> None: + service = _service([MAIN, OTHER], registry_id="src_main") + assert service.is_in_organization_scope(_schema("plasmid", MAIN)) + assert not service.is_in_organization_scope(_schema("plasmid", OTHER)) + assert not service.is_in_organization_scope({"id": "no_owner"}) + + def test_single_registry_without_name_scopes_to_it(self) -> None: + service = _service([MAIN]) + assert service.is_in_organization_scope(_schema("plasmid", MAIN)) + assert not service.is_in_organization_scope(_schema("plasmid", OTHER)) + + +def _results_schema(registry: Any) -> dict[str, Any]: + return { + "id": f"assaysch_{registry.id}", + "systemName": "assay", + "organization": {"id": registry.owner.id}, + } + + +def _list_entity_schemas(service: BenchlingService) -> list[dict[str, Any]]: + first_endpoint = next(iter(BenchlingEntitySchemaEndpointType)) + records = [_schema("plasmid", MAIN), _schema("plasmid", OTHER)] + + def handler(url: str) -> list[dict[str, Any]]: + return records if url.startswith(f"/api/v3/{first_endpoint.value}/") else [] + + with _v3_api(handler): + return list_entity_schemas(service) + + +def _list_dropdowns(service: BenchlingService) -> list[dict[str, Any]]: + records = [_dropdown("Species", MAIN), _dropdown("Species", OTHER)] + with _v3_api(lambda _url: records): + return list_dropdowns(service) + + +def _list_results_schemas(service: BenchlingService) -> list[dict[str, Any]]: + records = [_results_schema(MAIN), _results_schema(OTHER)] + with _v3_api(lambda _url: records): + return list_results_schemas(service) + + +# Each lister returns records from both MAIN and OTHER. `owner_of` reads the scoping value off a +# record and `scope_of` gives the value a registry should produce: the registry id for dropdowns, +# the owning organization id for entity schemas and results schemas. +_LISTERS = pytest.mark.parametrize( + "lister, owner_of, scope_of", + [ + ( + _list_entity_schemas, + lambda r: r["organization"]["id"], + lambda reg: reg.owner.id, + ), + (_list_dropdowns, lambda r: r["registry"]["id"], lambda reg: reg.id), + ( + _list_results_schemas, + lambda r: r["organization"]["id"], + lambda reg: reg.owner.id, + ), + ], + ids=["entity_schemas", "dropdowns", "results_schemas"], +) + + +class TestListFunctions: + @_LISTERS + def test_one_registry_without_name_keeps_only_that_registry( + self, lister: Callable, owner_of: Callable, scope_of: Callable + ) -> None: + results = lister(_service([MAIN])) + assert [owner_of(r) for r in results] == [scope_of(MAIN)] + + @_LISTERS + def test_two_registries_without_name_raise( + self, lister: Callable, owner_of: Callable, scope_of: Callable + ) -> None: + with pytest.raises(ValueError, match="does not allow ambiguous registries"): + lister(_service([MAIN, OTHER])) + + @_LISTERS + def test_two_registries_with_name_keep_only_that_registry( + self, lister: Callable, owner_of: Callable, scope_of: Callable + ) -> None: + results = lister(_service([MAIN, OTHER], registry_id="src_other")) + assert [owner_of(r) for r in results] == [scope_of(OTHER)] diff --git a/liminal/utils.py b/liminal/utils.py index a9c3a74..cb2eaf4 100644 --- a/liminal/utils.py +++ b/liminal/utils.py @@ -204,11 +204,22 @@ def rate_limited_get_v3(benchling_service: BenchlingService, url: str) -> Any: def list_all_items_v3( benchling_service: BenchlingService, url: str, - include_archived: bool = False, + include_archived: bool | None = False, page_size: int | None = MAX_PAGE_SIZE, + registry_scoped: bool = False, + organization_scoped: bool = False, ) -> list[dict[str, Any]]: - """Fetch every item from a v3 list endpoint, following nextToken pagination.""" - query_params = [f"archived.anyOf={'true,false' if include_archived else 'false'}"] + """Fetch every item from a v3 list endpoint, following nextToken pagination. + + With registry_scoped, drops items outside the connection's registry. With organization_scoped, + drops items outside the organization that owns the connection's registry. Only use them on + endpoints whose items carry a registry or an organization respectively, and never while + resolving the registry itself.""" + query_params = ( + [] + if include_archived is None + else [f"archived.anyOf={'true,false' if include_archived else 'false'}"] + ) if page_size: query_params.append(f"pageSize={page_size}") base_url = f"{url}?{'&'.join(query_params)}" @@ -223,9 +234,20 @@ def list_all_items_v3( if parsed_response is None: raise ValueError(f"No response body returned for {url}.") if isinstance(parsed_response, list): - return items + parsed_response - items.extend(parsed_response.get("items", [])) - next_token = parsed_response.get("nextToken") + page_items = parsed_response + next_token = None + else: + page_items = parsed_response.get("items", []) + next_token = parsed_response.get("nextToken") + if registry_scoped: + page_items = [ + i for i in page_items if benchling_service.is_in_registry_scope(i) + ] + if organization_scoped: + page_items = [ + i for i in page_items if benchling_service.is_in_organization_scope(i) + ] + items.extend(page_items) if not next_token: return items