Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions app/db/triggers.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,18 +140,18 @@ def unauthorized_private_reference_trigger(model: type[Entity], field_name: str)
# list of protected relationships between entities as (model, field_name)
protected_entity_relationships = [
(BrainAtlasRegion, "brain_atlas_id"),
(CellMorphology, "cell_morphology_protocol_id"),
(CellMorphology, "cell_morphology_protocol_id"), # tested
(Circuit, "atlas_id"),
(Circuit, "root_circuit_id"),
(ElectricalRecordingStimulus, "recording_id"),
(EMCellMesh, "em_dense_reconstruction_dataset_id"),
(EModel, "exemplar_morphology_id"),
(ElectricalRecordingStimulus, "recording_id"), # tested
(EMCellMesh, "em_dense_reconstruction_dataset_id"), # tested
(EModel, "exemplar_morphology_id"), # tested
(ExperimentalBoutonDensity, "subject_id"),
(ExperimentalNeuronDensity, "subject_id"),
(ExperimentalSynapsesPerConnection, "subject_id"),
(MEModel, "emodel_id"),
(MEModel, "morphology_id"),
(MEModelCalibrationResult, "calibrated_entity_id"),
(MEModel, "emodel_id"), # tested
(MEModel, "morphology_id"), # tested
(MEModelCalibrationResult, "calibrated_entity_id"), # tested
(ScientificArtifact, "subject_id"),
(Simulation, "entity_id"),
(Simulation, "simulation_campaign_id"),
Expand Down
50 changes: 50 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1470,6 +1470,24 @@ def em_dense_reconstruction_dataset_json_data(subject_id, brain_region_id):

@pytest.fixture
def em_dense_reconstruction_dataset(db, em_dense_reconstruction_dataset_json_data, person_id):
return add_db(
db,
EMDenseReconstructionDataset(
**em_dense_reconstruction_dataset_json_data
| {
"created_by_id": person_id,
"updated_by_id": person_id,
"authorized_public": False,
"authorized_project_id": PROJECT_ID,
}
),
)


@pytest.fixture
def public_em_dense_reconstruction_dataset(
db, em_dense_reconstruction_dataset_json_data, person_id
):
return add_db(
db,
EMDenseReconstructionDataset(
Expand Down Expand Up @@ -1498,6 +1516,22 @@ def em_cell_mesh_json_data(em_dense_reconstruction_dataset, subject_id, brain_re
}


@pytest.fixture
def public_em_cell_mesh_json_data(
public_em_dense_reconstruction_dataset, subject_id, brain_region_id
):
return {
"subject_id": str(subject_id),
"brain_region_id": str(brain_region_id),
"release_version": 1,
"dense_reconstruction_cell_id": 2**63 - 1, # max signed bigint
"generation_method": "marching_cubes",
"level_of_detail": 10,
"mesh_type": "static",
"em_dense_reconstruction_dataset_id": str(public_em_dense_reconstruction_dataset.id),
}


@pytest.fixture
def em_cell_mesh(db, em_cell_mesh_json_data, person_id):
return add_db(
Expand All @@ -1523,6 +1557,22 @@ def cell_morphology_protocol_json_data():

@pytest.fixture
def cell_morphology_protocol(db, cell_morphology_protocol_json_data, person_id):
return add_db(
db,
PlaceholderCellMorphologyProtocol(
**cell_morphology_protocol_json_data
| {
"created_by_id": person_id,
"updated_by_id": person_id,
"authorized_project_id": PROJECT_ID,
"authorized_public": False,
}
),
)


@pytest.fixture
def public_cell_morphology_protocol(db, cell_morphology_protocol_json_data, person_id):
return add_db(
db,
PlaceholderCellMorphologyProtocol(
Expand Down
68 changes: 47 additions & 21 deletions tests/test_cell_morphology.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@
check_entity_update_one,
create_cell_morphology_id,
)
from tests.utils.api_create import create_cell_morphology_protocol_id
from tests.utils.check import check_auth_triggers

ROUTE = "/cell-morphology"
ADMIN_ROUTE = "/admin/cell-morphology"


@pytest.fixture
def json_data(subject_id, license_id, brain_region_id, cell_morphology_protocol):
def common_json_data(subject_id, license_id, brain_region_id):
return {
"brain_region_id": str(brain_region_id),
"subject_id": str(subject_id),
Expand All @@ -43,12 +45,25 @@ def json_data(subject_id, license_id, brain_region_id, cell_morphology_protocol)
"location": {"x": 10, "y": 20, "z": 30},
"legacy_id": ["Test Legacy ID"],
"license_id": str(license_id),
"cell_morphology_protocol_id": str(cell_morphology_protocol.id),
"contact_email": "test@example.com",
"experiment_date": "2025-01-01T00:00:00",
}


@pytest.fixture
def json_data(common_json_data, cell_morphology_protocol):
return common_json_data | {
"cell_morphology_protocol_id": str(cell_morphology_protocol.id),
}


@pytest.fixture
def public_json_data(common_json_data, public_cell_morphology_protocol):
return common_json_data | {
"cell_morphology_protocol_id": str(public_cell_morphology_protocol.id),
}


def test_create_one(
client,
brain_region_id,
Expand Down Expand Up @@ -92,13 +107,13 @@ def test_create_one(
assert data[0]["cell_morphology_protocol"] == expected_cell_morphology_protocol_json_data


def test_delete_one(db, clients, json_data):
def test_delete_one(db, clients, public_json_data):
check_entity_delete_one(
db=db,
clients=clients,
route=ROUTE,
admin_route=ADMIN_ROUTE,
json_data=json_data,
json_data=public_json_data,
expected_counts_before={CellMorphology: 1, CellMorphologyProtocol: 1},
expected_counts_after={
CellMorphology: 0,
Expand All @@ -107,12 +122,12 @@ def test_delete_one(db, clients, json_data):
)


def test_update_one(clients, json_data):
def test_update_one(clients, public_json_data):
check_entity_update_one(
route=ROUTE,
admin_route=ADMIN_ROUTE,
clients=clients,
json_data=json_data,
json_data=public_json_data,
patch_payload={
"name": "name",
"description": "description",
Expand Down Expand Up @@ -478,24 +493,35 @@ def test_query_cell_morphology_species_join(db, client, brain_region_id, subject
}


def test_authorization(
def test_authorization(client_user_1, client_user_2, client_no_project, public_json_data):
check_authorization(ROUTE, client_user_1, client_user_2, client_no_project, public_json_data)


def test_auth_triggers(
client_user_1,
client_user_2,
client_no_project,
subject_id,
license_id,
brain_region_id,
public_json_data,
cell_morphology_protocol,
public_cell_morphology_protocol,
):
json_data = {
"location": {"x": 10, "y": 20, "z": 30},
"brain_region_id": str(brain_region_id),
"description": "morph description",
"legacy_id": ["Test Legacy ID"],
"license_id": license_id,
"name": "Test Morphology Name",
"subject_id": str(subject_id),
}
check_authorization(ROUTE, client_user_1, client_user_2, client_no_project, json_data)
linked_private_u2 = create_cell_morphology_protocol_id(
client_user_2,
authorized_public=False,
)
linked_public_u2 = create_cell_morphology_protocol_id(
client_user_2,
authorized_public=True,
)
check_auth_triggers(
ROUTE,
client_user_1=client_user_1,
json_data=public_json_data,
link_key="cell_morphology_protocol_id",
linked_private_u1_id=str(cell_morphology_protocol.id),
linked_public_u1_id=str(public_cell_morphology_protocol.id),
linked_private_u2_id=linked_private_u2,
linked_public_u2_id=linked_public_u2,
)


def test_pagination(db, client, brain_region_id, person_id):
Expand Down
28 changes: 28 additions & 0 deletions tests/test_electrical_recording_stimulus.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
check_entity_update_one,
check_missing,
check_pagination,
create_electrical_cell_recording_id,
)
from tests.utils.check import check_auth_triggers

ROUTE = "/electrical-recording-stimulus"
ADMIN_ROUTE = "/admin/electrical-recording-stimulus"
Expand Down Expand Up @@ -122,6 +124,32 @@ def test_authorization(
check_authorization(ROUTE, client_user_1, client_user_2, client_no_project, public_json_data)


def test_auth_triggers(
client_user_1,
client_user_2,
public_json_data,
trace_id_minimal,
public_trace_id_minimal,
electrical_cell_recording_json_data,
):
linked_private_u2 = create_electrical_cell_recording_id(
client_user_2, electrical_cell_recording_json_data | {"authorized_public": False}
)
linked_public_u2 = create_electrical_cell_recording_id(
client_user_2, electrical_cell_recording_json_data | {"authorized_public": True}
)
check_auth_triggers(
ROUTE,
client_user_1=client_user_1,
json_data=public_json_data,
link_key="recording_id",
linked_private_u1_id=trace_id_minimal,
linked_public_u1_id=public_trace_id_minimal,
linked_private_u2_id=linked_private_u2,
linked_public_u2_id=linked_public_u2,
)


def test_pagination(client, create_id):
check_pagination(ROUTE, client, create_id)

Expand Down
55 changes: 50 additions & 5 deletions tests/test_em_cell_mesh.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import pytest

from app.db.model import EMCellMesh
from app.db.model import EMCellMesh, EMDenseReconstructionDataset
from app.db.types import EntityType

from .utils import (
PROJECT_ID,
UNRELATED_PROJECT_ID,
add_all_db,
assert_request,
check_authorization,
Expand All @@ -13,6 +14,8 @@
check_missing,
check_pagination,
)
from tests.utils.check import check_auth_triggers
from tests.utils.db_create import create_entity

ROUTE = "/em-cell-mesh"
ADMIN_ROUTE = "/admin/em-cell-mesh"
Expand All @@ -24,6 +27,11 @@ def json_data(em_cell_mesh_json_data):
return em_cell_mesh_json_data


@pytest.fixture
def public_json_data(public_em_cell_mesh_json_data):
return public_em_cell_mesh_json_data


@pytest.fixture
def model(em_cell_mesh):
return em_cell_mesh
Expand Down Expand Up @@ -68,8 +76,45 @@ def test_missing(client):
check_missing(ROUTE, client)


def test_authorization(client_user_1, client_user_2, client_no_project, json_data):
check_authorization(ROUTE, client_user_1, client_user_2, client_no_project, json_data)
def test_authorization(client_user_1, client_user_2, client_no_project, public_json_data):
check_authorization(ROUTE, client_user_1, client_user_2, client_no_project, public_json_data)


def test_auth_triggers(
client_user_1,
db,
person_id,
public_json_data,
em_dense_reconstruction_dataset,
public_em_dense_reconstruction_dataset,
em_dense_reconstruction_dataset_json_data,
):
linked_private_u2 = create_entity(
db,
EMDenseReconstructionDataset,
person_id=person_id,
authorized_public=False,
authorized_project_id=UNRELATED_PROJECT_ID,
json_data=em_dense_reconstruction_dataset_json_data,
)
linked_public_u2 = create_entity(
db,
EMDenseReconstructionDataset,
person_id=person_id,
authorized_public=True,
authorized_project_id=UNRELATED_PROJECT_ID,
json_data=em_dense_reconstruction_dataset_json_data,
)
check_auth_triggers(
ROUTE,
client_user_1=client_user_1,
json_data=public_json_data,
link_key="em_dense_reconstruction_dataset_id",
linked_private_u1_id=em_dense_reconstruction_dataset.id,
linked_public_u1_id=public_em_dense_reconstruction_dataset.id,
linked_private_u2_id=linked_private_u2.id,
linked_public_u2_id=linked_public_u2.id,
)


def test_pagination(client, create_id):
Expand Down Expand Up @@ -151,13 +196,13 @@ def test_filtering(client, models, brain_region_id, species_id, strain_id):
}


def test_delete_one(db, clients, json_data):
def test_delete_one(db, clients, public_json_data):
check_entity_delete_one(
db=db,
route=ROUTE,
admin_route=ADMIN_ROUTE,
clients=clients,
json_data=json_data,
json_data=public_json_data,
expected_counts_before={
EMCellMesh: 1,
},
Expand Down
Loading