|
| 1 | +from fastapi import FastAPI |
| 2 | +from httpx import ASGITransport |
| 3 | +from httpx import AsyncClient |
| 4 | + |
| 5 | +from fractal_server.app.routes.auth import current_user_act |
| 6 | +from fractal_server.app.routes.auth import current_user_act_ver_prof |
| 7 | +from fractal_server.app.security import _create_first_user |
| 8 | + |
| 9 | + |
| 10 | +_PWD = "12345" |
| 11 | + |
| 12 | + |
| 13 | +async def test_current_user_act_ver_prof(app: FastAPI, client): |
| 14 | + await _create_first_user( |
| 15 | + email=_EMAIL, |
| 16 | + password=_PWD, |
| 17 | + is_superuser=False, |
| 18 | + project_dir="/fake", |
| 19 | + ) |
| 20 | + async with AsyncClient( |
| 21 | + base_url="http://test", |
| 22 | + transport=ASGITransport(app=app), |
| 23 | + ) as client: |
| 24 | + # Get token |
| 25 | + data_login = dict(username=_EMAIL, password=_PWD) |
| 26 | + res = await client.post("auth/token/login/", data=data_login) |
| 27 | + token = res.json()["access_token"] |
| 28 | + |
| 29 | + # Set Authorization header, so that the client impersonates this user |
| 30 | + client.headers["Authorization"] = f"Bearer {token}" |
| 31 | + |
| 32 | + # Success in GET-current-user (which depends on `current_user_act`) |
| 33 | + res = await client.get("/auth/current-user/") |
| 34 | + assert res.status_code == 200 |
| 35 | + assert res.json()["profile_id"] is None |
| 36 | + |
| 37 | + # Failure in GET-current-user, if it provisionally depends on |
| 38 | + # `current_user_act_ver_prof` |
| 39 | + assert app.dependency_overrides == {} |
| 40 | + app.dependency_overrides[current_user_act] = current_user_act_ver_prof |
| 41 | + res = await client.get("/auth/current-user/") |
| 42 | + assert res.status_code == 403 |
| 43 | + app.dependency_overrides = {} |
0 commit comments