Skip to content

Commit 783e6e8

Browse files
committed
test_current_user_act_ver_prof.py
1 parent 1db37e9 commit 783e6e8

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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

Comments
 (0)