Coverage for tests/test_webhooks_security.py: 100%
21 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-08-08 03:53 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2024-08-08 03:53 +0000
1from datetime import datetime 1abcde
3from fastapi import FastAPI, Security 1abcde
4from fastapi.security import HTTPBearer 1abcde
5from fastapi.testclient import TestClient 1abcde
6from pydantic import BaseModel 1abcde
7from typing_extensions import Annotated 1abcde
9app = FastAPI() 1abcde
11bearer_scheme = HTTPBearer() 1abcde
14class Subscription(BaseModel): 1abcde
15 username: str 1abcde
16 monthly_fee: float 1abcde
17 start_date: datetime 1abcde
20@app.webhooks.post("new-subscription") 1abcde
21def new_subscription( 1abcde
22 body: Subscription, token: Annotated[str, Security(bearer_scheme)]
23):
24 """
25 When a new user subscribes to your service we'll send you a POST request with this
26 data to the URL that you register for the event `new-subscription` in the dashboard.
27 """
30client = TestClient(app) 1abcde
33def test_dummy_webhook(): 1abcde
34 # Just for coverage
35 new_subscription(body={}, token="Bearer 123") 1abcde
38def test_openapi_schema(): 1abcde
39 response = client.get("/openapi.json") 1abcde
40 assert response.status_code == 200, response.text 1abcde
41 # insert_assert(response.json())
42 assert response.json() == { 1abcde
43 "openapi": "3.1.0",
44 "info": {"title": "FastAPI", "version": "0.1.0"},
45 "paths": {},
46 "webhooks": {
47 "new-subscription": {
48 "post": {
49 "summary": "New Subscription",
50 "description": "When a new user subscribes to your service we'll send you a POST request with this\ndata to the URL that you register for the event `new-subscription` in the dashboard.",
51 "operationId": "new_subscriptionnew_subscription_post",
52 "requestBody": {
53 "content": {
54 "application/json": {
55 "schema": {"$ref": "#/components/schemas/Subscription"}
56 }
57 },
58 "required": True,
59 },
60 "responses": {
61 "200": {
62 "description": "Successful Response",
63 "content": {"application/json": {"schema": {}}},
64 },
65 "422": {
66 "description": "Validation Error",
67 "content": {
68 "application/json": {
69 "schema": {
70 "$ref": "#/components/schemas/HTTPValidationError"
71 }
72 }
73 },
74 },
75 },
76 "security": [{"HTTPBearer": []}],
77 }
78 }
79 },
80 "components": {
81 "schemas": {
82 "HTTPValidationError": {
83 "properties": {
84 "detail": {
85 "items": {"$ref": "#/components/schemas/ValidationError"},
86 "type": "array",
87 "title": "Detail",
88 }
89 },
90 "type": "object",
91 "title": "HTTPValidationError",
92 },
93 "Subscription": {
94 "properties": {
95 "username": {"type": "string", "title": "Username"},
96 "monthly_fee": {"type": "number", "title": "Monthly Fee"},
97 "start_date": {
98 "type": "string",
99 "format": "date-time",
100 "title": "Start Date",
101 },
102 },
103 "type": "object",
104 "required": ["username", "monthly_fee", "start_date"],
105 "title": "Subscription",
106 },
107 "ValidationError": {
108 "properties": {
109 "loc": {
110 "items": {
111 "anyOf": [{"type": "string"}, {"type": "integer"}]
112 },
113 "type": "array",
114 "title": "Location",
115 },
116 "msg": {"type": "string", "title": "Message"},
117 "type": {"type": "string", "title": "Error Type"},
118 },
119 "type": "object",
120 "required": ["loc", "msg", "type"],
121 "title": "ValidationError",
122 },
123 },
124 "securitySchemes": {"HTTPBearer": {"type": "http", "scheme": "bearer"}},
125 },
126 }