|
1 | 1 | from pathlib import Path |
2 | 2 | from typing import Optional |
3 | | -from unittest.mock import ANY, Mock, patch |
| 3 | +from unittest.mock import ANY, MagicMock, Mock, patch |
4 | 4 | from urllib.parse import urlparse |
5 | 5 |
|
| 6 | +from fastapi import FastAPI |
| 7 | +from fastapi.testclient import TestClient |
6 | 8 | from pytest import mark |
| 9 | +from pytest_mock import MockerFixture |
7 | 10 |
|
8 | | -from murfey.instrument_server.api import ( |
9 | | - GainReference, |
10 | | - _get_murfey_url, |
11 | | - upload_gain_reference, |
12 | | -) |
| 11 | +from murfey.instrument_server.api import GainReference, _get_murfey_url |
| 12 | +from murfey.instrument_server.api import router as client_router |
| 13 | +from murfey.instrument_server.api import upload_gain_reference, validate_session_token |
13 | 14 | from murfey.util import posix_path |
14 | 15 | from murfey.util.api import url_path_for |
15 | 16 |
|
| 17 | + |
| 18 | +def set_up_test_client(session_id: Optional[int] = None): |
| 19 | + """ |
| 20 | + Helper function to set up a test client for the instrument server with validation |
| 21 | + checks disabled. |
| 22 | + """ |
| 23 | + # Set up the instrument server |
| 24 | + client_app = FastAPI() |
| 25 | + if session_id: |
| 26 | + client_app.dependency_overrides[validate_session_token] = lambda: session_id |
| 27 | + client_app.include_router(client_router) |
| 28 | + return TestClient(client_app) |
| 29 | + |
| 30 | + |
16 | 31 | test_get_murfey_url_params_matrix = ( |
17 | 32 | # Server URL to use |
18 | 33 | ("default",), |
@@ -57,6 +72,24 @@ def test_get_murfey_url( |
57 | 72 | assert parsed_server.path == parsed_original.path |
58 | 73 |
|
59 | 74 |
|
| 75 | +def test_check_multigrid_controller_exists(mocker: MockerFixture): |
| 76 | + session_id = 1 |
| 77 | + |
| 78 | + # Patch out the multigrid controllers that have been stored in memory |
| 79 | + mocker.patch("murfey.instrument_server.api.controllers", {session_id: MagicMock()}) |
| 80 | + |
| 81 | + # Set up the test client |
| 82 | + client_server = set_up_test_client(session_id=session_id) |
| 83 | + url_path = url_path_for( |
| 84 | + "api.router", "check_multigrid_controller_exists", session_id=session_id |
| 85 | + ) |
| 86 | + response = client_server.get(url_path) |
| 87 | + |
| 88 | + # Check that the result is as expected |
| 89 | + assert response.status_code == 200 |
| 90 | + assert response.json() == {"exists": True} |
| 91 | + |
| 92 | + |
60 | 93 | test_upload_gain_reference_params_matrix = ( |
61 | 94 | # Rsync URL settings |
62 | 95 | ("http://1.1.1.1",), # When rsync_url is provided |
|
0 commit comments