Coverage for tests / test_webhooks_security.py: 100%

22 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-02-12 18:15 +0000

1from datetime import datetime 1abcd

2from typing import Annotated 1abcd

3 

4from fastapi import FastAPI, Security 1abcd

5from fastapi.security import HTTPBearer 1abcd

6from fastapi.testclient import TestClient 1abcd

7from inline_snapshot import snapshot 1abcd

8from pydantic import BaseModel 1abcd

9 

10app = FastAPI() 1abcd

11 

12bearer_scheme = HTTPBearer() 1abcd

13 

14 

15class Subscription(BaseModel): 1abcd

16 username: str 1abcd

17 monthly_fee: float 1abcd

18 start_date: datetime 1abcd

19 

20 

21@app.webhooks.post("new-subscription") 1ahbicdj

22def new_subscription( 1abcd

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 

31client = TestClient(app) 1abcd

32 

33 

34def test_dummy_webhook(): 1abcd

35 # Just for coverage 

36 new_subscription(body={}, token="Bearer 123") 1hij

37 

38 

39def test_openapi_schema(): 1abcd

40 response = client.get("/openapi.json") 1efg

41 assert response.status_code == 200, response.text 1efg

42 assert response.json() == snapshot( 1efg

43 { 

44 "openapi": "3.1.0", 

45 "info": {"title": "FastAPI", "version": "0.1.0"}, 

46 "paths": {}, 

47 "webhooks": { 

48 "new-subscription": { 

49 "post": { 

50 "summary": "New Subscription", 

51 "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.", 

52 "operationId": "new_subscriptionnew_subscription_post", 

53 "requestBody": { 

54 "content": { 

55 "application/json": { 

56 "schema": { 

57 "$ref": "#/components/schemas/Subscription" 

58 } 

59 } 

60 }, 

61 "required": True, 

62 }, 

63 "responses": { 

64 "200": { 

65 "description": "Successful Response", 

66 "content": {"application/json": {"schema": {}}}, 

67 }, 

68 "422": { 

69 "description": "Validation Error", 

70 "content": { 

71 "application/json": { 

72 "schema": { 

73 "$ref": "#/components/schemas/HTTPValidationError" 

74 } 

75 } 

76 }, 

77 }, 

78 }, 

79 "security": [{"HTTPBearer": []}], 

80 } 

81 } 

82 }, 

83 "components": { 

84 "schemas": { 

85 "HTTPValidationError": { 

86 "properties": { 

87 "detail": { 

88 "items": { 

89 "$ref": "#/components/schemas/ValidationError" 

90 }, 

91 "type": "array", 

92 "title": "Detail", 

93 } 

94 }, 

95 "type": "object", 

96 "title": "HTTPValidationError", 

97 }, 

98 "Subscription": { 

99 "properties": { 

100 "username": {"type": "string", "title": "Username"}, 

101 "monthly_fee": {"type": "number", "title": "Monthly Fee"}, 

102 "start_date": { 

103 "type": "string", 

104 "format": "date-time", 

105 "title": "Start Date", 

106 }, 

107 }, 

108 "type": "object", 

109 "required": ["username", "monthly_fee", "start_date"], 

110 "title": "Subscription", 

111 }, 

112 "ValidationError": { 

113 "properties": { 

114 "ctx": {"title": "Context", "type": "object"}, 

115 "input": {"title": "Input"}, 

116 "loc": { 

117 "items": { 

118 "anyOf": [{"type": "string"}, {"type": "integer"}] 

119 }, 

120 "type": "array", 

121 "title": "Location", 

122 }, 

123 "msg": {"type": "string", "title": "Message"}, 

124 "type": {"type": "string", "title": "Error Type"}, 

125 }, 

126 "type": "object", 

127 "required": ["loc", "msg", "type"], 

128 "title": "ValidationError", 

129 }, 

130 }, 

131 "securitySchemes": {"HTTPBearer": {"type": "http", "scheme": "bearer"}}, 

132 }, 

133 } 

134 )