|
5 | 5 | import pytest |
6 | 6 | from fastapi import HTTPException |
7 | 7 | from pytest_mock import MockerFixture |
| 8 | +from sqlmodel import Session as SQLModelSession |
8 | 9 |
|
9 | 10 | from murfey.server.api.auth import ( |
| 11 | + check_user, |
10 | 12 | submit_to_auth_endpoint, |
11 | 13 | validate_frontend_session_access, |
12 | 14 | validate_token, |
13 | 15 | validate_user_instrument_access, |
14 | 16 | ) |
| 17 | +from murfey.util.db import MurfeyUser |
15 | 18 |
|
16 | 19 |
|
17 | | -def test_check_user(): |
18 | | - pass |
| 20 | +@pytest.mark.parametrize( |
| 21 | + "test_params", |
| 22 | + ( # User to check | Expected result |
| 23 | + ("murfey_user", True), |
| 24 | + ("some_dud", False), |
| 25 | + ), |
| 26 | +) |
| 27 | +def test_check_user( |
| 28 | + mocker: MockerFixture, |
| 29 | + murfey_db_session: SQLModelSession, |
| 30 | + test_params: tuple[str, bool], |
| 31 | +): |
| 32 | + # Unpack test params |
| 33 | + user_to_check, expected_result = test_params |
| 34 | + |
| 35 | + # Add the test user to the database |
| 36 | + murfey_user = MurfeyUser( |
| 37 | + username="murfey_user", |
| 38 | + hashed_password="asdfghjkl", |
| 39 | + ) |
| 40 | + murfey_db_session.add(murfey_user) |
| 41 | + murfey_db_session.commit() |
| 42 | + |
| 43 | + # Patch the Session context |
| 44 | + mock_session_context = MagicMock() |
| 45 | + mock_session_context.__enter__.return_value = murfey_db_session |
| 46 | + mock_session_context.__exit__.return_value = None |
| 47 | + mocker.patch("murfey.server.api.auth.Session", return_value=mock_session_context) |
| 48 | + |
| 49 | + # Run the function and check that the result is as expected |
| 50 | + result = check_user(user_to_check) |
| 51 | + assert result == expected_result |
19 | 52 |
|
20 | 53 |
|
21 | 54 | @pytest.mark.parametrize( |
|
0 commit comments