CI / lint (push) Skipped
CI / test (3.12) (push) Skipped
CI / test (3.13) (push) Skipped
254 lines
9.5 KiB
Python
254 lines
9.5 KiB
Python
from unittest.mock import MagicMock, patch
|
|
|
|
import pytest
|
|
from aarecommon.errors.exception_handler import AareDBCommunicationError
|
|
from aarecommon.models.models import DewarAddress, SampleShortInfo
|
|
from aareDB.models.sample import Sample
|
|
|
|
from aare.daq.aaredb import LoadedSampleInfo
|
|
from aare.daq.auth import TokenData
|
|
|
|
|
|
def test_installed_version(client):
|
|
response = client.get("/about/installed_version")
|
|
assert response.status_code == 200
|
|
assert isinstance(response.json(), str)
|
|
|
|
|
|
def test_login_success(client, auth_token_data):
|
|
with (
|
|
patch("aare.daq.auth.authenticate_from_proxy_header", return_value="user"),
|
|
patch(
|
|
"aare.daq.auth.authenticate_user", return_value=(auth_token_data, "fake-access-token")
|
|
),
|
|
):
|
|
response = client.post(
|
|
"/token",
|
|
data={"username": "user", "password": "pwd"},
|
|
headers={"X-Remote-User": "user"},
|
|
)
|
|
assert response.status_code == 200
|
|
assert response.json() == {
|
|
"access_token": "fake-access-token",
|
|
"token_type": "bearer",
|
|
"info": auth_token_data.model_dump(),
|
|
}
|
|
|
|
|
|
def test_login_without_proxy_header_is_401(client):
|
|
response = client.post("/token", data={"username": "user", "password": "pwd"})
|
|
assert response.status_code == 401
|
|
assert response.json()["code"] == "INVALID_TOKEN"
|
|
|
|
|
|
def test_sample_access_requires_token(client):
|
|
assert client.get("/access/sample/1").status_code == 401
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("staff", "pgroups", "sample_pgroup", "allowed"),
|
|
[
|
|
(False, ["p12345"], "p12345", True),
|
|
(False, ["p12345"], "p99999", False),
|
|
(True, ["p12345"], "p99999", True),
|
|
],
|
|
)
|
|
def test_sample_access_decision(client, mock_aaredb, staff, pgroups, sample_pgroup, allowed):
|
|
mock_aaredb.find_loaded_sample.return_value = MagicMock(id=42, pgroup=sample_pgroup)
|
|
data = TokenData(sub="u", pgroups=pgroups, session=1, staff=staff)
|
|
with patch("aare.daq.auth.parse_token", return_value=data):
|
|
response = client.get("/access/sample/42", headers={"Authorization": "Bearer t"})
|
|
assert response.status_code == 200
|
|
body = response.json()
|
|
assert body["sample_id"] == 42
|
|
assert body["allowed"] is allowed
|
|
assert body["pgroup"] == sample_pgroup
|
|
mock_aaredb.find_loaded_sample.assert_called_once_with(42)
|
|
|
|
|
|
def test_sample_access_unknown_sample(client, mock_aaredb):
|
|
mock_aaredb.find_loaded_sample.return_value = None
|
|
response = client.get("/access/sample/42", headers={"Authorization": "Bearer t"})
|
|
assert response.status_code == 200
|
|
body = response.json()
|
|
assert body["allowed"] is False
|
|
assert body["pgroup"] is None
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# /sample/mount
|
|
# ---------------------------------------------------------------------------
|
|
AUTH = {"Authorization": "Bearer t"}
|
|
|
|
|
|
def _as_user(pgroups, staff=False):
|
|
return patch(
|
|
"aare.daq.auth.parse_token",
|
|
return_value=TokenData(sub="u", pgroups=pgroups, session=1, staff=staff),
|
|
)
|
|
|
|
|
|
def _short_info(db_id, pgroup, name="lyso_01"):
|
|
return SampleShortInfo(
|
|
db_id=db_id,
|
|
puck_name="PSI-001",
|
|
dewar_name="DEWAR-A",
|
|
sample_name=name,
|
|
run_number=1,
|
|
user=pgroup,
|
|
pin=1,
|
|
location=DewarAddress(segment="A", pos=1),
|
|
)
|
|
|
|
|
|
def test_mount_requires_token(client):
|
|
assert client.post("/sample/mount?dbid=1").status_code == 401
|
|
|
|
|
|
def test_mount_own_pgroup_returns_sample_info(client, mock_samples):
|
|
mock_samples.find_sample.return_value = _short_info(1, "p12345")
|
|
with _as_user(["p12345"]):
|
|
response = client.post("/sample/mount", params={"dbid": 1}, headers=AUTH)
|
|
assert response.status_code == 200
|
|
body = response.json()
|
|
assert body["db_id"] == 1
|
|
assert body["sample_name"] == "lyso_01"
|
|
assert body["user"] == "p12345"
|
|
assert body["location"] == {"segment": "A", "pos": 1}
|
|
mock_samples.find_sample.assert_called_once_with(1, reference=False)
|
|
|
|
|
|
def test_mount_reference_flag_is_forwarded(client, mock_samples):
|
|
mock_samples.find_sample.return_value = _short_info(9001, "p11206", name="beam_location_pin")
|
|
with _as_user(["p11206"]):
|
|
response = client.post(
|
|
"/sample/mount", params={"dbid": 9001, "reference": "true"}, headers=AUTH
|
|
)
|
|
assert response.status_code == 200
|
|
assert response.json()["sample_name"] == "beam_location_pin"
|
|
mock_samples.find_sample.assert_called_once_with(9001, reference=True)
|
|
|
|
|
|
def test_mount_other_pgroup_is_403(client, mock_samples):
|
|
mock_samples.find_sample.return_value = _short_info(4, "p67890")
|
|
with _as_user(["p12345"]):
|
|
response = client.post("/sample/mount", params={"dbid": 4}, headers=AUTH)
|
|
assert response.status_code == 403
|
|
body = response.json()
|
|
assert body["code"] == "FORBIDDEN"
|
|
assert "p67890" in body["message"]
|
|
|
|
|
|
def test_mount_staff_any_pgroup(client, mock_samples):
|
|
mock_samples.find_sample.return_value = _short_info(4, "p67890")
|
|
with _as_user(["p11111"], staff=True):
|
|
response = client.post("/sample/mount", params={"dbid": 4}, headers=AUTH)
|
|
assert response.status_code == 200
|
|
assert response.json()["db_id"] == 4
|
|
|
|
|
|
def test_mount_unknown_sample_is_400(client, mock_samples):
|
|
mock_samples.find_sample.return_value = None
|
|
with _as_user(["p12345"]):
|
|
response = client.post("/sample/mount", params={"dbid": 999}, headers=AUTH)
|
|
assert response.status_code == 400
|
|
assert response.json()["exception_class"] == "SampleException"
|
|
|
|
|
|
def test_mount_against_shipped_catalogue(server_module, mock_aaredb):
|
|
"""End-to-end over the real dummy_samples.json."""
|
|
from fastapi.testclient import TestClient
|
|
|
|
with (
|
|
patch.object(server_module, "AareWrapper", return_value=mock_aaredb),
|
|
TestClient(server_module.app) as c,
|
|
):
|
|
with _as_user(["p12345"]):
|
|
# own sample, someone else's sample, nonexistent sample
|
|
assert c.post("/sample/mount", params={"dbid": 1}, headers=AUTH).status_code == 200
|
|
assert c.post("/sample/mount", params={"dbid": 4}, headers=AUTH).status_code == 403
|
|
assert c.post("/sample/mount", params={"dbid": 999}, headers=AUTH).status_code == 400
|
|
# reference tools are only found with reference=true, and belong to the beamline
|
|
assert c.post("/sample/mount", params={"dbid": 9001}, headers=AUTH).status_code == 400
|
|
ref = c.post("/sample/mount", params={"dbid": 9001, "reference": "true"}, headers=AUTH)
|
|
assert ref.status_code == 403
|
|
with _as_user([], staff=True):
|
|
ref = c.post("/sample/mount", params={"dbid": 9001, "reference": "true"}, headers=AUTH)
|
|
assert ref.status_code == 200
|
|
assert ref.json()["location"]["segment"] == "X"
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# GET /sample/{sample_id}
|
|
# ---------------------------------------------------------------------------
|
|
def _loaded(sample_id, pgroup):
|
|
return LoadedSampleInfo(
|
|
sample=Sample(
|
|
id=sample_id,
|
|
pgroup=pgroup,
|
|
sample_name="lyso_01",
|
|
position=3,
|
|
data_collection_parameters=None,
|
|
),
|
|
puck_name="PUCK-A",
|
|
puck_pgroup=pgroup,
|
|
dewar_name="DEWAR-1",
|
|
tell_position="B3",
|
|
)
|
|
|
|
|
|
def test_sample_info_requires_token(client):
|
|
assert client.get("/sample/1").status_code == 401
|
|
|
|
|
|
def test_sample_info_own_pgroup(client, mock_aaredb):
|
|
mock_aaredb.get_sample_info.return_value = _loaded(1, "p12345")
|
|
with _as_user(["p12345"]):
|
|
response = client.get("/sample/1", headers=AUTH)
|
|
assert response.status_code == 200
|
|
body = response.json()
|
|
assert body["sample"]["id"] == 1
|
|
assert body["sample"]["sample_name"] == "lyso_01"
|
|
assert body["puck_name"] == "PUCK-A"
|
|
assert body["tell_position"] == "B3"
|
|
mock_aaredb.get_sample_info.assert_called_once_with(1)
|
|
|
|
|
|
def test_sample_info_other_pgroup_is_403(client, mock_aaredb):
|
|
mock_aaredb.get_sample_info.return_value = _loaded(4, "p67890")
|
|
with _as_user(["p12345"]):
|
|
response = client.get("/sample/4", headers=AUTH)
|
|
assert response.status_code == 403
|
|
assert response.json()["code"] == "FORBIDDEN"
|
|
|
|
|
|
def test_sample_info_staff_any_pgroup(client, mock_aaredb):
|
|
mock_aaredb.get_sample_info.return_value = _loaded(4, "p67890")
|
|
with _as_user(["p11111"], staff=True):
|
|
assert client.get("/sample/4", headers=AUTH).status_code == 200
|
|
|
|
|
|
def test_sample_info_not_loaded_is_400(client, mock_aaredb):
|
|
mock_aaredb.get_sample_info.return_value = None
|
|
with _as_user(["p12345"]):
|
|
response = client.get("/sample/999", headers=AUTH)
|
|
assert response.status_code == 400
|
|
assert response.json()["exception_class"] == "SampleException"
|
|
|
|
|
|
def test_sample_info_aaredb_unreachable_is_503(client, mock_aaredb):
|
|
mock_aaredb.get_sample_info.side_effect = AareDBCommunicationError(
|
|
"Could not reach aareDB: CERTIFICATE_VERIFY_FAILED",
|
|
operation="get_pucks_with_tell_position",
|
|
base_url="https://mx-aaredb-dmz-01.psi.ch/dispatcher",
|
|
critical=True,
|
|
)
|
|
with _as_user(["p12345"]):
|
|
response = client.get("/sample/1", headers=AUTH)
|
|
assert response.status_code == 503
|
|
body = response.json()
|
|
assert body["critical"] is True
|
|
assert body["exception_class"] == "AareDBCommunicationError"
|
|
assert "CERTIFICATE_VERIFY_FAILED" in body["message"]
|
|
assert body["context"]["base_url"] == "https://mx-aaredb-dmz-01.psi.ch/dispatcher"
|