fastapi

Форк
0
/
test_security_http_base_description.py 
60 строк · 1.9 Кб
1
from fastapi import FastAPI, Security
2
from fastapi.security.http import HTTPAuthorizationCredentials, HTTPBase
3
from fastapi.testclient import TestClient
4

5
app = FastAPI()
6

7
security = HTTPBase(scheme="Other", description="Other Security Scheme")
8

9

10
@app.get("/users/me")
11
def read_current_user(credentials: HTTPAuthorizationCredentials = Security(security)):
12
    return {"scheme": credentials.scheme, "credentials": credentials.credentials}
13

14

15
client = TestClient(app)
16

17

18
def test_security_http_base():
19
    response = client.get("/users/me", headers={"Authorization": "Other foobar"})
20
    assert response.status_code == 200, response.text
21
    assert response.json() == {"scheme": "Other", "credentials": "foobar"}
22

23

24
def test_security_http_base_no_credentials():
25
    response = client.get("/users/me")
26
    assert response.status_code == 403, response.text
27
    assert response.json() == {"detail": "Not authenticated"}
28

29

30
def test_openapi_schema():
31
    response = client.get("/openapi.json")
32
    assert response.status_code == 200, response.text
33
    assert response.json() == {
34
        "openapi": "3.1.0",
35
        "info": {"title": "FastAPI", "version": "0.1.0"},
36
        "paths": {
37
            "/users/me": {
38
                "get": {
39
                    "responses": {
40
                        "200": {
41
                            "description": "Successful Response",
42
                            "content": {"application/json": {"schema": {}}},
43
                        }
44
                    },
45
                    "summary": "Read Current User",
46
                    "operationId": "read_current_user_users_me_get",
47
                    "security": [{"HTTPBase": []}],
48
                }
49
            }
50
        },
51
        "components": {
52
            "securitySchemes": {
53
                "HTTPBase": {
54
                    "type": "http",
55
                    "scheme": "Other",
56
                    "description": "Other Security Scheme",
57
                }
58
            }
59
        },
60
    }
61

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.