Debian Codegraph @

tests/test_tutorial/test_security/test_tutorial007.py

raw
1 import importlib
2 from base64 import b64encode
3
4 import pytest
5 from fastapi.testclient import TestClient
6 from inline_snapshot import snapshot
7
8
9 @pytest.fixture(
10 name="client",
11 params=[
12 pytest.param("tutorial007_py310"),
13 pytest.param("tutorial007_an_py310"),
14 ],
15 )
16 def get_client(request: pytest.FixtureRequest):
17 mod = importlib.import_module(f"docs_src.security.{request.param}")
18 return TestClient(mod.app)
19
20
21 def test_security_http_basic(client: TestClient):
22 response = client.get("/users/me", auth=("stanleyjobson", "swordfish"))
23 assert response.status_code == 200, response.text
24 assert response.json() == {"username": "stanleyjobson"}
25
26
27 def test_security_http_basic_no_credentials(client: TestClient):
28 response = client.get("/users/me")
29 assert response.json() == {"detail": "Not authenticated"}
30 assert response.status_code == 401, response.text
31 assert response.headers["WWW-Authenticate"] == "Basic"
32
33
34 def test_security_http_basic_invalid_credentials(client: TestClient):
35 response = client.get(
36 "/users/me", headers={"Authorization": "Basic notabase64token"}
37 )
38 assert response.status_code == 401, response.text
39 assert response.headers["WWW-Authenticate"] == "Basic"
40 assert response.json() == {"detail": "Not authenticated"}
41
42
43 def test_security_http_basic_non_basic_credentials(client: TestClient):
44 payload = b64encode(b"johnsecret").decode("ascii")
45 auth_header = f"Basic {payload}"
46 response = client.get("/users/me", headers={"Authorization": auth_header})
47 assert response.status_code == 401, response.text
48 assert response.headers["WWW-Authenticate"] == "Basic"
49 assert response.json() == {"detail": "Not authenticated"}
50
51
52 def test_security_http_basic_invalid_username(client: TestClient):
53 response = client.get("/users/me", auth=("alice", "swordfish"))
54 assert response.status_code == 401, response.text
55 assert response.json() == {"detail": "Incorrect username or password"}
56 assert response.headers["WWW-Authenticate"] == "Basic"
57
58
59 def test_security_http_basic_invalid_password(client: TestClient):
60 response = client.get("/users/me", auth=("stanleyjobson", "wrongpassword"))
61 assert response.status_code == 401, response.text
62 assert response.json() == {"detail": "Incorrect username or password"}
63 assert response.headers["WWW-Authenticate"] == "Basic"
64
65
66 def test_openapi_schema(client: TestClient):
67 response = client.get("/openapi.json")
68 assert response.status_code == 200, response.text
69 assert response.json() == snapshot(
70 {
71 "openapi": "3.1.0",
72 "info": {"title": "FastAPI", "version": "0.1.0"},
73 "paths": {
74 "/users/me": {
75 "get": {
76 "responses": {
77 "200": {
78 "description": "Successful Response",
79 "content": {"application/json": {"schema": {}}},
80 }
81 },
82 "summary": "Read Current User",
83 "operationId": "read_current_user_users_me_get",
84 "security": [{"HTTPBasic": []}],
85 }
86 }
87 },
88 "components": {
89 "securitySchemes": {"HTTPBasic": {"type": "http", "scheme": "basic"}}
90 },
91 }
92 )