Coverage for tests / test_tutorial / test_authentication_error_status_code / test_tutorial001.py: 100%
21 statements
« prev ^ index » next coverage.py v7.13.3, created at 2026-04-06 01:24 +0000
« prev ^ index » next coverage.py v7.13.3, created at 2026-04-06 01:24 +0000
1import importlib 1abcd
3import pytest 1abcd
4from fastapi.testclient import TestClient 1abcd
5from inline_snapshot import snapshot 1abcd
8@pytest.fixture( 1abcd
9 name="client",
10 params=[
11 "tutorial001_an_py310",
12 ],
13)
14def get_client(request: pytest.FixtureRequest): 1abcd
15 mod = importlib.import_module( 1efghijklmn
16 f"docs_src.authentication_error_status_code.{request.param}"
17 )
19 client = TestClient(mod.app) 1efghijklmn
20 return client 1efghijklmn
23def test_get_me(client: TestClient): 1abcd
24 response = client.get("/me", headers={"Authorization": "Bearer secrettoken"}) 1opqr
25 assert response.status_code == 200 1opqr
26 assert response.json() == { 1opqr
27 "message": "You are authenticated",
28 "token": "secrettoken",
29 }
32def test_get_me_no_credentials(client: TestClient): 1abcd
33 response = client.get("/me") 1stuv
34 assert response.status_code == 403 1stuv
35 assert response.json() == {"detail": "Not authenticated"} 1stuv
38def test_openapi_schema(client: TestClient): 1abcd
39 response = client.get("/openapi.json") 1wxyz
40 assert response.status_code == 200, response.text 1wxyz
41 assert response.json() == snapshot( 1wxyz
42 {
43 "openapi": "3.1.0",
44 "info": {"title": "FastAPI", "version": "0.1.0"},
45 "paths": {
46 "/me": {
47 "get": {
48 "summary": "Read Me",
49 "operationId": "read_me_me_get",
50 "responses": {
51 "200": {
52 "description": "Successful Response",
53 "content": {"application/json": {"schema": {}}},
54 }
55 },
56 "security": [{"HTTPBearer403": []}],
57 }
58 }
59 },
60 "components": {
61 "securitySchemes": {
62 "HTTPBearer403": {"type": "http", "scheme": "bearer"}
63 }
64 },
65 }
66 )