|
| 1 | +from pathlib import Path |
| 2 | +from unittest import mock |
| 3 | +from unittest.mock import MagicMock |
| 4 | + |
| 5 | +from fastapi import FastAPI |
| 6 | +from fastapi.testclient import TestClient |
| 7 | +from pytest_mock import MockerFixture |
| 8 | + |
| 9 | +from murfey.server.api.auth import ( |
| 10 | + validate_instrument_server_session_access, |
| 11 | + validate_instrument_token, |
| 12 | +) |
| 13 | +from murfey.server.api.session_control import spa_router |
| 14 | +from murfey.server.murfey_db import murfey_db_session |
| 15 | +from murfey.util.api import url_path_for |
| 16 | + |
| 17 | + |
| 18 | +def test_make_atlas_jpg(mocker: MockerFixture, tmp_path: Path): |
| 19 | + # Set up the objects to mock |
| 20 | + instrument_name = "test" |
| 21 | + visit_name = "test_visit" |
| 22 | + session_id = 1 |
| 23 | + |
| 24 | + # Override the database session generator |
| 25 | + mock_session = MagicMock() |
| 26 | + mock_session.instrument_name = instrument_name |
| 27 | + mock_session.visit = visit_name |
| 28 | + mock_query_result = MagicMock() |
| 29 | + mock_query_result.one.return_value = mock_session |
| 30 | + mock_db_session = MagicMock() |
| 31 | + mock_db_session.exec.return_value = mock_query_result |
| 32 | + |
| 33 | + def mock_get_db_session(): |
| 34 | + yield mock_db_session |
| 35 | + |
| 36 | + # Mock the instrument server tokens dictionary |
| 37 | + mock_tokens = mocker.patch( |
| 38 | + "murfey.server.api.instrument.instrument_server_tokens", |
| 39 | + {session_id: {"access_token": mock.sentinel}}, |
| 40 | + ) |
| 41 | + |
| 42 | + # Mock the called workflow function |
| 43 | + mock_atlas_jpg = mocker.patch( |
| 44 | + "murfey.server.api.session_control.atlas_jpg_from_mrc", |
| 45 | + return_value=None, |
| 46 | + ) |
| 47 | + |
| 48 | + # Set up the test file |
| 49 | + image_dir = tmp_path / instrument_name / "data" / visit_name / "Atlas" |
| 50 | + image_dir.mkdir(parents=True, exist_ok=True) |
| 51 | + test_file = image_dir / "Atlas1.mrc" |
| 52 | + |
| 53 | + # Set up the backend server |
| 54 | + backend_app = FastAPI() |
| 55 | + |
| 56 | + # Override validation and database dependencies |
| 57 | + backend_app.dependency_overrides[validate_instrument_token] = lambda: None |
| 58 | + backend_app.dependency_overrides[validate_instrument_server_session_access] = ( |
| 59 | + lambda: session_id |
| 60 | + ) |
| 61 | + backend_app.dependency_overrides[murfey_db_session] = mock_get_db_session |
| 62 | + backend_app.include_router(spa_router) |
| 63 | + backend_server = TestClient(backend_app) |
| 64 | + |
| 65 | + atlas_jpg_url = url_path_for( |
| 66 | + "api.session_control.spa_router", "make_atlas_jpg", session_id=session_id |
| 67 | + ) |
| 68 | + response = backend_server.post( |
| 69 | + atlas_jpg_url, |
| 70 | + json={"path": str(test_file)}, |
| 71 | + headers={"Authorization": f"Bearer {mock_tokens[session_id]['access_token']}"}, |
| 72 | + ) |
| 73 | + |
| 74 | + # Check that the expected calls were made |
| 75 | + mock_atlas_jpg.assert_called_once_with(instrument_name, visit_name, test_file) |
| 76 | + assert response.status_code == 200 |
0 commit comments