Skip to content

Commit bfe8f3f

Browse files
Add tests for authorization triggers
1 parent 6a20aa9 commit bfe8f3f

12 files changed

+568
-302
lines changed

app/db/triggers.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -140,18 +140,18 @@ def unauthorized_private_reference_trigger(model: type[Entity], field_name: str)
140140
# list of protected relationships between entities as (model, field_name)
141141
protected_entity_relationships = [
142142
(BrainAtlasRegion, "brain_atlas_id"),
143-
(CellMorphology, "cell_morphology_protocol_id"),
143+
(CellMorphology, "cell_morphology_protocol_id"), # tested
144144
(Circuit, "atlas_id"),
145145
(Circuit, "root_circuit_id"),
146-
(ElectricalRecordingStimulus, "recording_id"),
147-
(EMCellMesh, "em_dense_reconstruction_dataset_id"),
148-
(EModel, "exemplar_morphology_id"),
146+
(ElectricalRecordingStimulus, "recording_id"), # tested
147+
(EMCellMesh, "em_dense_reconstruction_dataset_id"), # tested
148+
(EModel, "exemplar_morphology_id"), # tested
149149
(ExperimentalBoutonDensity, "subject_id"),
150150
(ExperimentalNeuronDensity, "subject_id"),
151151
(ExperimentalSynapsesPerConnection, "subject_id"),
152-
(MEModel, "emodel_id"),
153-
(MEModel, "morphology_id"),
154-
(MEModelCalibrationResult, "calibrated_entity_id"),
152+
(MEModel, "emodel_id"), # tested
153+
(MEModel, "morphology_id"), # tested
154+
(MEModelCalibrationResult, "calibrated_entity_id"), # tested
155155
(ScientificArtifact, "subject_id"),
156156
(Simulation, "entity_id"),
157157
(Simulation, "simulation_campaign_id"),

tests/conftest.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1470,6 +1470,24 @@ def em_dense_reconstruction_dataset_json_data(subject_id, brain_region_id):
14701470

14711471
@pytest.fixture
14721472
def em_dense_reconstruction_dataset(db, em_dense_reconstruction_dataset_json_data, person_id):
1473+
return add_db(
1474+
db,
1475+
EMDenseReconstructionDataset(
1476+
**em_dense_reconstruction_dataset_json_data
1477+
| {
1478+
"created_by_id": person_id,
1479+
"updated_by_id": person_id,
1480+
"authorized_public": False,
1481+
"authorized_project_id": PROJECT_ID,
1482+
}
1483+
),
1484+
)
1485+
1486+
1487+
@pytest.fixture
1488+
def public_em_dense_reconstruction_dataset(
1489+
db, em_dense_reconstruction_dataset_json_data, person_id
1490+
):
14731491
return add_db(
14741492
db,
14751493
EMDenseReconstructionDataset(
@@ -1498,6 +1516,22 @@ def em_cell_mesh_json_data(em_dense_reconstruction_dataset, subject_id, brain_re
14981516
}
14991517

15001518

1519+
@pytest.fixture
1520+
def public_em_cell_mesh_json_data(
1521+
public_em_dense_reconstruction_dataset, subject_id, brain_region_id
1522+
):
1523+
return {
1524+
"subject_id": str(subject_id),
1525+
"brain_region_id": str(brain_region_id),
1526+
"release_version": 1,
1527+
"dense_reconstruction_cell_id": 2**63 - 1, # max signed bigint
1528+
"generation_method": "marching_cubes",
1529+
"level_of_detail": 10,
1530+
"mesh_type": "static",
1531+
"em_dense_reconstruction_dataset_id": str(public_em_dense_reconstruction_dataset.id),
1532+
}
1533+
1534+
15011535
@pytest.fixture
15021536
def em_cell_mesh(db, em_cell_mesh_json_data, person_id):
15031537
return add_db(
@@ -1523,6 +1557,22 @@ def cell_morphology_protocol_json_data():
15231557

15241558
@pytest.fixture
15251559
def cell_morphology_protocol(db, cell_morphology_protocol_json_data, person_id):
1560+
return add_db(
1561+
db,
1562+
PlaceholderCellMorphologyProtocol(
1563+
**cell_morphology_protocol_json_data
1564+
| {
1565+
"created_by_id": person_id,
1566+
"updated_by_id": person_id,
1567+
"authorized_project_id": PROJECT_ID,
1568+
"authorized_public": False,
1569+
}
1570+
),
1571+
)
1572+
1573+
1574+
@pytest.fixture
1575+
def public_cell_morphology_protocol(db, cell_morphology_protocol_json_data, person_id):
15261576
return add_db(
15271577
db,
15281578
PlaceholderCellMorphologyProtocol(

tests/test_cell_morphology.py

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,15 @@
2828
check_entity_update_one,
2929
create_cell_morphology_id,
3030
)
31+
from tests.utils.api_create import create_cell_morphology_protocol_id
32+
from tests.utils.check import check_auth_triggers
3133

3234
ROUTE = "/cell-morphology"
3335
ADMIN_ROUTE = "/admin/cell-morphology"
3436

3537

3638
@pytest.fixture
37-
def json_data(subject_id, license_id, brain_region_id, cell_morphology_protocol):
39+
def common_json_data(subject_id, license_id, brain_region_id):
3840
return {
3941
"brain_region_id": str(brain_region_id),
4042
"subject_id": str(subject_id),
@@ -43,12 +45,25 @@ def json_data(subject_id, license_id, brain_region_id, cell_morphology_protocol)
4345
"location": {"x": 10, "y": 20, "z": 30},
4446
"legacy_id": ["Test Legacy ID"],
4547
"license_id": str(license_id),
46-
"cell_morphology_protocol_id": str(cell_morphology_protocol.id),
4748
"contact_email": "test@example.com",
4849
"experiment_date": "2025-01-01T00:00:00",
4950
}
5051

5152

53+
@pytest.fixture
54+
def json_data(common_json_data, cell_morphology_protocol):
55+
return common_json_data | {
56+
"cell_morphology_protocol_id": str(cell_morphology_protocol.id),
57+
}
58+
59+
60+
@pytest.fixture
61+
def public_json_data(common_json_data, public_cell_morphology_protocol):
62+
return common_json_data | {
63+
"cell_morphology_protocol_id": str(public_cell_morphology_protocol.id),
64+
}
65+
66+
5267
def test_create_one(
5368
client,
5469
brain_region_id,
@@ -92,13 +107,13 @@ def test_create_one(
92107
assert data[0]["cell_morphology_protocol"] == expected_cell_morphology_protocol_json_data
93108

94109

95-
def test_delete_one(db, clients, json_data):
110+
def test_delete_one(db, clients, public_json_data):
96111
check_entity_delete_one(
97112
db=db,
98113
clients=clients,
99114
route=ROUTE,
100115
admin_route=ADMIN_ROUTE,
101-
json_data=json_data,
116+
json_data=public_json_data,
102117
expected_counts_before={CellMorphology: 1, CellMorphologyProtocol: 1},
103118
expected_counts_after={
104119
CellMorphology: 0,
@@ -107,12 +122,12 @@ def test_delete_one(db, clients, json_data):
107122
)
108123

109124

110-
def test_update_one(clients, json_data):
125+
def test_update_one(clients, public_json_data):
111126
check_entity_update_one(
112127
route=ROUTE,
113128
admin_route=ADMIN_ROUTE,
114129
clients=clients,
115-
json_data=json_data,
130+
json_data=public_json_data,
116131
patch_payload={
117132
"name": "name",
118133
"description": "description",
@@ -478,24 +493,35 @@ def test_query_cell_morphology_species_join(db, client, brain_region_id, subject
478493
}
479494

480495

481-
def test_authorization(
496+
def test_authorization(client_user_1, client_user_2, client_no_project, public_json_data):
497+
check_authorization(ROUTE, client_user_1, client_user_2, client_no_project, public_json_data)
498+
499+
500+
def test_auth_triggers(
482501
client_user_1,
483502
client_user_2,
484-
client_no_project,
485-
subject_id,
486-
license_id,
487-
brain_region_id,
503+
public_json_data,
504+
cell_morphology_protocol,
505+
public_cell_morphology_protocol,
488506
):
489-
json_data = {
490-
"location": {"x": 10, "y": 20, "z": 30},
491-
"brain_region_id": str(brain_region_id),
492-
"description": "morph description",
493-
"legacy_id": ["Test Legacy ID"],
494-
"license_id": license_id,
495-
"name": "Test Morphology Name",
496-
"subject_id": str(subject_id),
497-
}
498-
check_authorization(ROUTE, client_user_1, client_user_2, client_no_project, json_data)
507+
linked_private_u2 = create_cell_morphology_protocol_id(
508+
client_user_2,
509+
authorized_public=False,
510+
)
511+
linked_public_u2 = create_cell_morphology_protocol_id(
512+
client_user_2,
513+
authorized_public=True,
514+
)
515+
check_auth_triggers(
516+
ROUTE,
517+
client_user_1=client_user_1,
518+
json_data=public_json_data,
519+
link_key="cell_morphology_protocol_id",
520+
linked_private_u1_id=str(cell_morphology_protocol.id),
521+
linked_public_u1_id=str(public_cell_morphology_protocol.id),
522+
linked_private_u2_id=linked_private_u2,
523+
linked_public_u2_id=linked_public_u2,
524+
)
499525

500526

501527
def test_pagination(db, client, brain_region_id, person_id):

tests/test_electrical_recording_stimulus.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
check_entity_update_one,
1313
check_missing,
1414
check_pagination,
15+
create_electrical_cell_recording_id,
1516
)
17+
from tests.utils.check import check_auth_triggers
1618

1719
ROUTE = "/electrical-recording-stimulus"
1820
ADMIN_ROUTE = "/admin/electrical-recording-stimulus"
@@ -122,6 +124,32 @@ def test_authorization(
122124
check_authorization(ROUTE, client_user_1, client_user_2, client_no_project, public_json_data)
123125

124126

127+
def test_auth_triggers(
128+
client_user_1,
129+
client_user_2,
130+
public_json_data,
131+
trace_id_minimal,
132+
public_trace_id_minimal,
133+
electrical_cell_recording_json_data,
134+
):
135+
linked_private_u2 = create_electrical_cell_recording_id(
136+
client_user_2, electrical_cell_recording_json_data | {"authorized_public": False}
137+
)
138+
linked_public_u2 = create_electrical_cell_recording_id(
139+
client_user_2, electrical_cell_recording_json_data | {"authorized_public": True}
140+
)
141+
check_auth_triggers(
142+
ROUTE,
143+
client_user_1=client_user_1,
144+
json_data=public_json_data,
145+
link_key="recording_id",
146+
linked_private_u1_id=trace_id_minimal,
147+
linked_public_u1_id=public_trace_id_minimal,
148+
linked_private_u2_id=linked_private_u2,
149+
linked_public_u2_id=linked_public_u2,
150+
)
151+
152+
125153
def test_pagination(client, create_id):
126154
check_pagination(ROUTE, client, create_id)
127155

tests/test_em_cell_mesh.py

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import pytest
22

3-
from app.db.model import EMCellMesh
3+
from app.db.model import EMCellMesh, EMDenseReconstructionDataset
44
from app.db.types import EntityType
55

66
from .utils import (
77
PROJECT_ID,
8+
UNRELATED_PROJECT_ID,
89
add_all_db,
910
assert_request,
1011
check_authorization,
@@ -13,6 +14,8 @@
1314
check_missing,
1415
check_pagination,
1516
)
17+
from tests.utils.check import check_auth_triggers
18+
from tests.utils.db_create import create_entity
1619

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

2629

30+
@pytest.fixture
31+
def public_json_data(public_em_cell_mesh_json_data):
32+
return public_em_cell_mesh_json_data
33+
34+
2735
@pytest.fixture
2836
def model(em_cell_mesh):
2937
return em_cell_mesh
@@ -68,8 +76,45 @@ def test_missing(client):
6876
check_missing(ROUTE, client)
6977

7078

71-
def test_authorization(client_user_1, client_user_2, client_no_project, json_data):
72-
check_authorization(ROUTE, client_user_1, client_user_2, client_no_project, json_data)
79+
def test_authorization(client_user_1, client_user_2, client_no_project, public_json_data):
80+
check_authorization(ROUTE, client_user_1, client_user_2, client_no_project, public_json_data)
81+
82+
83+
def test_auth_triggers(
84+
client_user_1,
85+
db,
86+
person_id,
87+
public_json_data,
88+
em_dense_reconstruction_dataset,
89+
public_em_dense_reconstruction_dataset,
90+
em_dense_reconstruction_dataset_json_data,
91+
):
92+
linked_private_u2 = create_entity(
93+
db,
94+
EMDenseReconstructionDataset,
95+
person_id=person_id,
96+
authorized_public=False,
97+
authorized_project_id=UNRELATED_PROJECT_ID,
98+
json_data=em_dense_reconstruction_dataset_json_data,
99+
)
100+
linked_public_u2 = create_entity(
101+
db,
102+
EMDenseReconstructionDataset,
103+
person_id=person_id,
104+
authorized_public=True,
105+
authorized_project_id=UNRELATED_PROJECT_ID,
106+
json_data=em_dense_reconstruction_dataset_json_data,
107+
)
108+
check_auth_triggers(
109+
ROUTE,
110+
client_user_1=client_user_1,
111+
json_data=public_json_data,
112+
link_key="em_dense_reconstruction_dataset_id",
113+
linked_private_u1_id=em_dense_reconstruction_dataset.id,
114+
linked_public_u1_id=public_em_dense_reconstruction_dataset.id,
115+
linked_private_u2_id=linked_private_u2.id,
116+
linked_public_u2_id=linked_public_u2.id,
117+
)
73118

74119

75120
def test_pagination(client, create_id):
@@ -151,13 +196,13 @@ def test_filtering(client, models, brain_region_id, species_id, strain_id):
151196
}
152197

153198

154-
def test_delete_one(db, clients, json_data):
199+
def test_delete_one(db, clients, public_json_data):
155200
check_entity_delete_one(
156201
db=db,
157202
route=ROUTE,
158203
admin_route=ADMIN_ROUTE,
159204
clients=clients,
160-
json_data=json_data,
205+
json_data=public_json_data,
161206
expected_counts_before={
162207
EMCellMesh: 1,
163208
},

0 commit comments

Comments
 (0)