|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import pytest |
| 4 | +from sqlalchemy.exc import IntegrityError |
| 5 | +from uuid_utils import uuid7 |
| 6 | + |
| 7 | +from diracx.db.exceptions import DBInBadStateError |
| 8 | +from diracx.db.sql.auth.db import AuthDB |
| 9 | +from diracx.db.sql.utils import DBStateAssertion |
| 10 | + |
| 11 | + |
| 12 | +@pytest.fixture |
| 13 | +async def auth_db(tmp_path): |
| 14 | + auth_db = AuthDB("sqlite+aiosqlite:///:memory:") |
| 15 | + async with auth_db.engine_context(): |
| 16 | + async with auth_db.engine.begin() as conn: |
| 17 | + await conn.run_sync(auth_db.metadata.create_all) |
| 18 | + yield auth_db |
| 19 | + |
| 20 | + |
| 21 | +async def test_context_manager(auth_db: AuthDB): |
| 22 | + """We will test with refresh tokens the context manager. |
| 23 | +
|
| 24 | + 1. Insert a refresh token in the DB with a proper jti |
| 25 | + 2. Insert a refresh token with another jti |
| 26 | + 3. Insert a refresh token with the first jti, except the right error /!\ |
| 27 | + 4. Insert a refresh token with the first jti, except the wrong error /!\ |
| 28 | +
|
| 29 | + 1. and 2. should pass |
| 30 | + 3. and 4. should raise DBInBadStateError |
| 31 | +
|
| 32 | + 3. Saying that the expected error was raised. |
| 33 | + 4. Saying that an unexpected error was raised. |
| 34 | + """ |
| 35 | + # Insert a refresh token details |
| 36 | + async with auth_db as auth_db: |
| 37 | + jti = uuid7() |
| 38 | + await auth_db.insert_refresh_token( |
| 39 | + jti, |
| 40 | + "subject", |
| 41 | + "scope", |
| 42 | + ) |
| 43 | + |
| 44 | + # Revoke the token |
| 45 | + async with auth_db as auth_db: |
| 46 | + |
| 47 | + # No error should be raised |
| 48 | + async with DBStateAssertion(auth_db.conn, [IntegrityError]): |
| 49 | + await auth_db.insert_refresh_token( |
| 50 | + uuid7(), |
| 51 | + "subject", |
| 52 | + "scope", |
| 53 | + ) |
| 54 | + |
| 55 | + # Await the right error (IntegrityError) |
| 56 | + with pytest.raises(DBInBadStateError) as exc_info: |
| 57 | + # DBInBadStateError because we say that IntegrityError should not be raised |
| 58 | + async with DBStateAssertion(auth_db.conn, [IntegrityError]): |
| 59 | + await auth_db.insert_refresh_token( |
| 60 | + jti, |
| 61 | + "subject", |
| 62 | + "scope", |
| 63 | + ) |
| 64 | + |
| 65 | + assert ( |
| 66 | + str(exc_info.value) |
| 67 | + == "This error may NOT have been raised. Please report this at https://github.com/DIRACGrid/diracx/issues" |
| 68 | + ) |
| 69 | + |
| 70 | + # Await the wrong error |
| 71 | + with pytest.raises(DBInBadStateError) as exc_info: |
| 72 | + # DBInBadStateError because we say that IntegrityError should not be raised |
| 73 | + async with DBStateAssertion(auth_db.conn, [ValueError]): |
| 74 | + await auth_db.insert_refresh_token( |
| 75 | + jti, |
| 76 | + "subject", |
| 77 | + "scope", |
| 78 | + ) |
| 79 | + |
| 80 | + # (We use "in" because the error details can change depending on the engine) |
| 81 | + assert str(exc_info.value).startswith("Unexpected error (IntegrityError):") |
| 82 | + assert "IntegrityError" in str(exc_info) |
0 commit comments