|
11 | 11 | check_user, |
12 | 12 | submit_to_auth_endpoint, |
13 | 13 | validate_frontend_session_access, |
| 14 | + validate_session_against_visit, |
14 | 15 | validate_token, |
15 | 16 | validate_user_instrument_access, |
16 | 17 | ) |
17 | | -from murfey.util.db import MurfeyUser |
| 18 | +from murfey.util.db import MurfeyUser, Session as MurfeySession |
18 | 19 |
|
19 | 20 |
|
20 | 21 | @pytest.mark.parametrize( |
@@ -264,8 +265,50 @@ async def test_validate_token( |
264 | 265 | ) |
265 | 266 |
|
266 | 267 |
|
267 | | -def test_validate_session_against_visit(): |
268 | | - pass |
| 268 | +@pytest.mark.parametrize( |
| 269 | + "test_params", |
| 270 | + ( # Session ID | Visit | Expected result |
| 271 | + (1, "test_visit", True), |
| 272 | + (1, "some_visit", False), |
| 273 | + (2, "test_visit", False), |
| 274 | + ), |
| 275 | +) |
| 276 | +def test_validate_session_against_visit( |
| 277 | + mocker: MockerFixture, |
| 278 | + murfey_db_session: SQLModelSession, |
| 279 | + test_params: tuple[int, str, bool], |
| 280 | +): |
| 281 | + # Unpack test params |
| 282 | + session_id, visit_name, expected_result = test_params |
| 283 | + |
| 284 | + # Add a test session to the database |
| 285 | + session_entry = MurfeySession( |
| 286 | + id=1, |
| 287 | + name="test_visit", |
| 288 | + visit="test_visit", |
| 289 | + started=False, |
| 290 | + current_gain_ref="/path/to/gain_ref", |
| 291 | + instrument_name="test_instrument", |
| 292 | + process=True, |
| 293 | + visit_end_time=None, |
| 294 | + ) |
| 295 | + murfey_db_session.add(session_entry) |
| 296 | + murfey_db_session.commit() |
| 297 | + |
| 298 | + # Patch the Session call with the test database |
| 299 | + mock_session_context = MagicMock() |
| 300 | + mock_session_context.__enter__.return_value = murfey_db_session |
| 301 | + mock_session_context.__exit__.return_value = None |
| 302 | + mocker.patch("murfey.server.api.auth.Session", return_value=mock_session_context) |
| 303 | + |
| 304 | + # Run the function |
| 305 | + assert ( |
| 306 | + validate_session_against_visit( |
| 307 | + session_id=session_id, |
| 308 | + visit=visit_name, |
| 309 | + ) |
| 310 | + == expected_result |
| 311 | + ) |
269 | 312 |
|
270 | 313 |
|
271 | 314 | @pytest.mark.asyncio |
|
0 commit comments