| 1 |
from datetime import datetime |
| 2 |
from typing import Annotated |
| 3 |
|
| 4 |
from fastapi import FastAPI, Security |
| 5 |
from fastapi.security import HTTPBearer |
| 6 |
from fastapi.testclient import TestClient |
| 7 |
from inline_snapshot import snapshot |
| 8 |
from pydantic import BaseModel |
| 9 |
|
| 10 |
app = FastAPI() |
| 11 |
|
| 12 |
bearer_scheme = HTTPBearer() |
| 13 |
|
| 14 |
|
| 15 |
class Subscription(BaseModel): |
| 16 |
username: str |
| 17 |
monthly_fee: float |
| 18 |
start_date: datetime |
| 19 |
|
| 20 |
|
| 21 |
@app.webhooks.post("new-subscription") |
| 22 |
def new_subscription( |
| 23 |
body: Subscription, token: Annotated[str, Security(bearer_scheme)] |
| 24 |
): |
| 25 |
""" |
| 26 |
When a new user subscribes to your service we'll send you a POST request with this |
| 27 |
data to the URL that you register for the event `new-subscription` in the dashboard. |
| 28 |
""" |
| 29 |
|
| 30 |
|
| 31 |
client = TestClient(app) |
| 32 |
|
| 33 |
|
| 34 |
def test_dummy_webhook(): |
| 35 |
# Just for coverage |
| 36 |
new_subscription( |
| 37 |
body=Subscription(username="rick", monthly_fee=9.99, start_date=datetime.now()), |
| 38 |
token="Bearer 123", |
| 39 |
) |
| 40 |
|
| 41 |
|
| 42 |
def test_openapi_schema(): |
| 43 |
response = client.get("/openapi.json") |
| 44 |
assert response.status_code == 200, response.text |
| 45 |
assert response.json() == snapshot( |
| 46 |
{ |
| 47 |
"openapi": "3.1.0", |
| 48 |
"info": {"title": "FastAPI", "version": "0.1.0"}, |
| 49 |
"paths": {}, |
| 50 |
"webhooks": { |
| 51 |
"new-subscription": { |
| 52 |
"post": { |
| 53 |
"summary": "New Subscription", |
| 54 |
"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.", |
| 55 |
"operationId": "new_subscriptionnew_subscription_post", |
| 56 |
"requestBody": { |
| 57 |
"content": { |
| 58 |
"application/json": { |
| 59 |
"schema": { |
| 60 |
"$ref": "#/components/schemas/Subscription" |
| 61 |
} |
| 62 |
} |
| 63 |
}, |
| 64 |
"required": True, |
| 65 |
}, |
| 66 |
"responses": { |
| 67 |
"200": { |
| 68 |
"description": "Successful Response", |
| 69 |
"content": {"application/json": {"schema": {}}}, |
| 70 |
}, |
| 71 |
"422": { |
| 72 |
"description": "Validation Error", |
| 73 |
"content": { |
| 74 |
"application/json": { |
| 75 |
"schema": { |
| 76 |
"$ref": "#/components/schemas/HTTPValidationError" |
| 77 |
} |
| 78 |
} |
| 79 |
}, |
| 80 |
}, |
| 81 |
}, |
| 82 |
"security": [{"HTTPBearer": []}], |
| 83 |
} |
| 84 |
} |
| 85 |
}, |
| 86 |
"components": { |
| 87 |
"schemas": { |
| 88 |
"HTTPValidationError": { |
| 89 |
"properties": { |
| 90 |
"detail": { |
| 91 |
"items": { |
| 92 |
"$ref": "#/components/schemas/ValidationError" |
| 93 |
}, |
| 94 |
"type": "array", |
| 95 |
"title": "Detail", |
| 96 |
} |
| 97 |
}, |
| 98 |
"type": "object", |
| 99 |
"title": "HTTPValidationError", |
| 100 |
}, |
| 101 |
"Subscription": { |
| 102 |
"properties": { |
| 103 |
"username": {"type": "string", "title": "Username"}, |
| 104 |
"monthly_fee": {"type": "number", "title": "Monthly Fee"}, |
| 105 |
"start_date": { |
| 106 |
"type": "string", |
| 107 |
"format": "date-time", |
| 108 |
"title": "Start Date", |
| 109 |
}, |
| 110 |
}, |
| 111 |
"type": "object", |
| 112 |
"required": ["username", "monthly_fee", "start_date"], |
| 113 |
"title": "Subscription", |
| 114 |
}, |
| 115 |
"ValidationError": { |
| 116 |
"properties": { |
| 117 |
"ctx": {"title": "Context", "type": "object"}, |
| 118 |
"input": {"title": "Input"}, |
| 119 |
"loc": { |
| 120 |
"items": { |
| 121 |
"anyOf": [{"type": "string"}, {"type": "integer"}] |
| 122 |
}, |
| 123 |
"type": "array", |
| 124 |
"title": "Location", |
| 125 |
}, |
| 126 |
"msg": {"type": "string", "title": "Message"}, |
| 127 |
"type": {"type": "string", "title": "Error Type"}, |
| 128 |
}, |
| 129 |
"type": "object", |
| 130 |
"required": ["loc", "msg", "type"], |
| 131 |
"title": "ValidationError", |
| 132 |
}, |
| 133 |
}, |
| 134 |
"securitySchemes": {"HTTPBearer": {"type": "http", "scheme": "bearer"}}, |
| 135 |
}, |
| 136 |
} |
| 137 |
) |