28 changed files with 696 additions and 76 deletions
@ -0,0 +1,25 @@ |
|||||
|
from fastapi import FastAPI |
||||
|
from fastapi.params import Param |
||||
|
from starlette.testclient import TestClient |
||||
|
|
||||
|
app = FastAPI() |
||||
|
|
||||
|
|
||||
|
@app.get("/items/") |
||||
|
def read_items(q: str = Param(None)): |
||||
|
return {"q": q} |
||||
|
|
||||
|
|
||||
|
client = TestClient(app) |
||||
|
|
||||
|
|
||||
|
def test_default_param_query_none(): |
||||
|
response = client.get("/items/") |
||||
|
assert response.status_code == 200 |
||||
|
assert response.json() == {"q": None} |
||||
|
|
||||
|
|
||||
|
def test_default_param_query(): |
||||
|
response = client.get("/items/?q=foo") |
||||
|
assert response.status_code == 200 |
||||
|
assert response.json() == {"q": "foo"} |
@ -0,0 +1,144 @@ |
|||||
|
import pytest |
||||
|
from starlette.testclient import TestClient |
||||
|
|
||||
|
from dependencies.tutorial004 import app |
||||
|
|
||||
|
client = TestClient(app) |
||||
|
|
||||
|
openapi_schema = { |
||||
|
"openapi": "3.0.2", |
||||
|
"info": {"title": "Fast API", "version": "0.1.0"}, |
||||
|
"paths": { |
||||
|
"/items/": { |
||||
|
"get": { |
||||
|
"responses": { |
||||
|
"200": { |
||||
|
"description": "Successful Response", |
||||
|
"content": {"application/json": {"schema": {}}}, |
||||
|
}, |
||||
|
"422": { |
||||
|
"description": "Validation Error", |
||||
|
"content": { |
||||
|
"application/json": { |
||||
|
"schema": { |
||||
|
"$ref": "#/components/schemas/HTTPValidationError" |
||||
|
} |
||||
|
} |
||||
|
}, |
||||
|
}, |
||||
|
}, |
||||
|
"summary": "Read Items Get", |
||||
|
"operationId": "read_items_items__get", |
||||
|
"parameters": [ |
||||
|
{ |
||||
|
"required": False, |
||||
|
"schema": {"title": "Q", "type": "string"}, |
||||
|
"name": "q", |
||||
|
"in": "query", |
||||
|
}, |
||||
|
{ |
||||
|
"required": False, |
||||
|
"schema": {"title": "Skip", "type": "integer", "default": 0}, |
||||
|
"name": "skip", |
||||
|
"in": "query", |
||||
|
}, |
||||
|
{ |
||||
|
"required": False, |
||||
|
"schema": {"title": "Limit", "type": "integer", "default": 100}, |
||||
|
"name": "limit", |
||||
|
"in": "query", |
||||
|
}, |
||||
|
], |
||||
|
} |
||||
|
} |
||||
|
}, |
||||
|
"components": { |
||||
|
"schemas": { |
||||
|
"ValidationError": { |
||||
|
"title": "ValidationError", |
||||
|
"required": ["loc", "msg", "type"], |
||||
|
"type": "object", |
||||
|
"properties": { |
||||
|
"loc": { |
||||
|
"title": "Location", |
||||
|
"type": "array", |
||||
|
"items": {"type": "string"}, |
||||
|
}, |
||||
|
"msg": {"title": "Message", "type": "string"}, |
||||
|
"type": {"title": "Error Type", "type": "string"}, |
||||
|
}, |
||||
|
}, |
||||
|
"HTTPValidationError": { |
||||
|
"title": "HTTPValidationError", |
||||
|
"type": "object", |
||||
|
"properties": { |
||||
|
"detail": { |
||||
|
"title": "Detail", |
||||
|
"type": "array", |
||||
|
"items": {"$ref": "#/components/schemas/ValidationError"}, |
||||
|
} |
||||
|
}, |
||||
|
}, |
||||
|
} |
||||
|
}, |
||||
|
} |
||||
|
|
||||
|
|
||||
|
def test_openapi_schema(): |
||||
|
response = client.get("/openapi.json") |
||||
|
assert response.status_code == 200 |
||||
|
assert response.json() == openapi_schema |
||||
|
|
||||
|
|
||||
|
@pytest.mark.parametrize( |
||||
|
"path,expected_status,expected_response", |
||||
|
[ |
||||
|
( |
||||
|
"/items", |
||||
|
200, |
||||
|
{ |
||||
|
"items": [ |
||||
|
{"item_name": "Foo"}, |
||||
|
{"item_name": "Bar"}, |
||||
|
{"item_name": "Baz"}, |
||||
|
] |
||||
|
}, |
||||
|
), |
||||
|
( |
||||
|
"/items?q=foo", |
||||
|
200, |
||||
|
{ |
||||
|
"items": [ |
||||
|
{"item_name": "Foo"}, |
||||
|
{"item_name": "Bar"}, |
||||
|
{"item_name": "Baz"}, |
||||
|
], |
||||
|
"q": "foo", |
||||
|
}, |
||||
|
), |
||||
|
( |
||||
|
"/items?q=foo&skip=1", |
||||
|
200, |
||||
|
{"items": [{"item_name": "Bar"}, {"item_name": "Baz"}], "q": "foo"}, |
||||
|
), |
||||
|
( |
||||
|
"/items?q=bar&limit=2", |
||||
|
200, |
||||
|
{"items": [{"item_name": "Foo"}, {"item_name": "Bar"}], "q": "bar"}, |
||||
|
), |
||||
|
( |
||||
|
"/items?q=bar&skip=1&limit=1", |
||||
|
200, |
||||
|
{"items": [{"item_name": "Bar"}], "q": "bar"}, |
||||
|
), |
||||
|
( |
||||
|
"/items?limit=1&q=bar&skip=1", |
||||
|
200, |
||||
|
{"items": [{"item_name": "Bar"}], "q": "bar"}, |
||||
|
), |
||||
|
], |
||||
|
) |
||||
|
def test_get(path, expected_status, expected_response): |
||||
|
response = client.get(path) |
||||
|
assert response.status_code == expected_status |
||||
|
assert response.json() == expected_response |
@ -0,0 +1,36 @@ |
|||||
|
from starlette.testclient import TestClient |
||||
|
|
||||
|
from path_operation_advanced_configuration.tutorial001 import app |
||||
|
|
||||
|
client = TestClient(app) |
||||
|
|
||||
|
openapi_schema = { |
||||
|
"openapi": "3.0.2", |
||||
|
"info": {"title": "Fast API", "version": "0.1.0"}, |
||||
|
"paths": { |
||||
|
"/items/": { |
||||
|
"get": { |
||||
|
"responses": { |
||||
|
"200": { |
||||
|
"description": "Successful Response", |
||||
|
"content": {"application/json": {"schema": {}}}, |
||||
|
} |
||||
|
}, |
||||
|
"summary": "Read Items Get", |
||||
|
"operationId": "some_specific_id_you_define", |
||||
|
} |
||||
|
} |
||||
|
}, |
||||
|
} |
||||
|
|
||||
|
|
||||
|
def test_openapi_schema(): |
||||
|
response = client.get("/openapi.json") |
||||
|
assert response.status_code == 200 |
||||
|
assert response.json() == openapi_schema |
||||
|
|
||||
|
|
||||
|
def test_get(): |
||||
|
response = client.get("/items/") |
||||
|
assert response.status_code == 200 |
||||
|
assert response.json() == [{"item_id": "Foo"}] |
@ -0,0 +1,23 @@ |
|||||
|
from starlette.testclient import TestClient |
||||
|
|
||||
|
from path_operation_advanced_configuration.tutorial002 import app |
||||
|
|
||||
|
client = TestClient(app) |
||||
|
|
||||
|
openapi_schema = { |
||||
|
"openapi": "3.0.2", |
||||
|
"info": {"title": "Fast API", "version": "0.1.0"}, |
||||
|
"paths": {}, |
||||
|
} |
||||
|
|
||||
|
|
||||
|
def test_openapi_schema(): |
||||
|
response = client.get("/openapi.json") |
||||
|
assert response.status_code == 200 |
||||
|
assert response.json() == openapi_schema |
||||
|
|
||||
|
|
||||
|
def test_get(): |
||||
|
response = client.get("/items/") |
||||
|
assert response.status_code == 200 |
||||
|
assert response.json() == [{"item_id": "Foo"}] |
@ -0,0 +1,112 @@ |
|||||
|
from starlette.testclient import TestClient |
||||
|
|
||||
|
from path_operation_configuration.tutorial005 import app |
||||
|
|
||||
|
client = TestClient(app) |
||||
|
|
||||
|
openapi_schema = { |
||||
|
"openapi": "3.0.2", |
||||
|
"info": {"title": "Fast API", "version": "0.1.0"}, |
||||
|
"paths": { |
||||
|
"/items/": { |
||||
|
"post": { |
||||
|
"responses": { |
||||
|
"200": { |
||||
|
"description": "The created item", |
||||
|
"content": { |
||||
|
"application/json": { |
||||
|
"schema": {"$ref": "#/components/schemas/Item"} |
||||
|
} |
||||
|
}, |
||||
|
}, |
||||
|
"422": { |
||||
|
"description": "Validation Error", |
||||
|
"content": { |
||||
|
"application/json": { |
||||
|
"schema": { |
||||
|
"$ref": "#/components/schemas/HTTPValidationError" |
||||
|
} |
||||
|
} |
||||
|
}, |
||||
|
}, |
||||
|
}, |
||||
|
"summary": "Create an item", |
||||
|
"description": "\n Create an item with all the information:\n \n * name: each item must have a name\n * description: a long description\n * price: required\n * tax: if the item doesn't have tax, you can omit this\n * tags: a set of unique tag strings for this item\n ", |
||||
|
"operationId": "create_item_items__post", |
||||
|
"requestBody": { |
||||
|
"content": { |
||||
|
"application/json": { |
||||
|
"schema": {"$ref": "#/components/schemas/Item"} |
||||
|
} |
||||
|
}, |
||||
|
"required": True, |
||||
|
}, |
||||
|
} |
||||
|
} |
||||
|
}, |
||||
|
"components": { |
||||
|
"schemas": { |
||||
|
"Item": { |
||||
|
"title": "Item", |
||||
|
"required": ["name", "price"], |
||||
|
"type": "object", |
||||
|
"properties": { |
||||
|
"name": {"title": "Name", "type": "string"}, |
||||
|
"price": {"title": "Price", "type": "number"}, |
||||
|
"description": {"title": "Description", "type": "string"}, |
||||
|
"tax": {"title": "Tax", "type": "number"}, |
||||
|
"tags": { |
||||
|
"title": "Tags", |
||||
|
"uniqueItems": True, |
||||
|
"type": "array", |
||||
|
"items": {"type": "string"}, |
||||
|
"default": [], |
||||
|
}, |
||||
|
}, |
||||
|
}, |
||||
|
"ValidationError": { |
||||
|
"title": "ValidationError", |
||||
|
"required": ["loc", "msg", "type"], |
||||
|
"type": "object", |
||||
|
"properties": { |
||||
|
"loc": { |
||||
|
"title": "Location", |
||||
|
"type": "array", |
||||
|
"items": {"type": "string"}, |
||||
|
}, |
||||
|
"msg": {"title": "Message", "type": "string"}, |
||||
|
"type": {"title": "Error Type", "type": "string"}, |
||||
|
}, |
||||
|
}, |
||||
|
"HTTPValidationError": { |
||||
|
"title": "HTTPValidationError", |
||||
|
"type": "object", |
||||
|
"properties": { |
||||
|
"detail": { |
||||
|
"title": "Detail", |
||||
|
"type": "array", |
||||
|
"items": {"$ref": "#/components/schemas/ValidationError"}, |
||||
|
} |
||||
|
}, |
||||
|
}, |
||||
|
} |
||||
|
}, |
||||
|
} |
||||
|
|
||||
|
|
||||
|
def test_openapi_schema(): |
||||
|
response = client.get("/openapi.json") |
||||
|
assert response.status_code == 200 |
||||
|
assert response.json() == openapi_schema |
||||
|
|
||||
|
|
||||
|
def test_query_params_str_validations(): |
||||
|
response = client.post("/items/", json={"name": "Foo", "price": 42}) |
||||
|
assert response.status_code == 200 |
||||
|
assert response.json() == { |
||||
|
"name": "Foo", |
||||
|
"price": 42, |
||||
|
"description": None, |
||||
|
"tax": None, |
||||
|
"tags": [], |
||||
|
} |
@ -0,0 +1,73 @@ |
|||||
|
import pytest |
||||
|
from starlette.testclient import TestClient |
||||
|
|
||||
|
from path_operation_configuration.tutorial006 import app |
||||
|
|
||||
|
client = TestClient(app) |
||||
|
|
||||
|
openapi_schema = { |
||||
|
"openapi": "3.0.2", |
||||
|
"info": {"title": "Fast API", "version": "0.1.0"}, |
||||
|
"paths": { |
||||
|
"/items/": { |
||||
|
"get": { |
||||
|
"responses": { |
||||
|
"200": { |
||||
|
"description": "Successful Response", |
||||
|
"content": {"application/json": {"schema": {}}}, |
||||
|
} |
||||
|
}, |
||||
|
"tags": ["items"], |
||||
|
"summary": "Read Items Get", |
||||
|
"operationId": "read_items_items__get", |
||||
|
} |
||||
|
}, |
||||
|
"/users/": { |
||||
|
"get": { |
||||
|
"responses": { |
||||
|
"200": { |
||||
|
"description": "Successful Response", |
||||
|
"content": {"application/json": {"schema": {}}}, |
||||
|
} |
||||
|
}, |
||||
|
"tags": ["users"], |
||||
|
"summary": "Read Users Get", |
||||
|
"operationId": "read_users_users__get", |
||||
|
} |
||||
|
}, |
||||
|
"/elements/": { |
||||
|
"get": { |
||||
|
"responses": { |
||||
|
"200": { |
||||
|
"description": "Successful Response", |
||||
|
"content": {"application/json": {"schema": {}}}, |
||||
|
} |
||||
|
}, |
||||
|
"tags": ["items"], |
||||
|
"summary": "Read Elements Get", |
||||
|
"operationId": "read_elements_elements__get", |
||||
|
"deprecated": True, |
||||
|
} |
||||
|
}, |
||||
|
}, |
||||
|
} |
||||
|
|
||||
|
|
||||
|
def test_openapi_schema(): |
||||
|
response = client.get("/openapi.json") |
||||
|
assert response.status_code == 200 |
||||
|
assert response.json() == openapi_schema |
||||
|
|
||||
|
|
||||
|
@pytest.mark.parametrize( |
||||
|
"path,expected_status,expected_response", |
||||
|
[ |
||||
|
("/items/", 200, [{"name": "Foo", "price": 42}]), |
||||
|
("/users/", 200, [{"username": "johndoe"}]), |
||||
|
("/elements/", 200, [{"item_id": "Foo"}]), |
||||
|
], |
||||
|
) |
||||
|
def test_query_params_str_validations(path, expected_status, expected_response): |
||||
|
response = client.get(path) |
||||
|
assert response.status_code == expected_status |
||||
|
assert response.json() == expected_response |
@ -0,0 +1,167 @@ |
|||||
|
from starlette.testclient import TestClient |
||||
|
|
||||
|
from security.tutorial003 import app |
||||
|
|
||||
|
client = TestClient(app) |
||||
|
|
||||
|
openapi_schema = { |
||||
|
"openapi": "3.0.2", |
||||
|
"info": {"title": "Fast API", "version": "0.1.0"}, |
||||
|
"paths": { |
||||
|
"/token": { |
||||
|
"post": { |
||||
|
"responses": { |
||||
|
"200": { |
||||
|
"description": "Successful Response", |
||||
|
"content": {"application/json": {"schema": {}}}, |
||||
|
}, |
||||
|
"422": { |
||||
|
"description": "Validation Error", |
||||
|
"content": { |
||||
|
"application/json": { |
||||
|
"schema": { |
||||
|
"$ref": "#/components/schemas/HTTPValidationError" |
||||
|
} |
||||
|
} |
||||
|
}, |
||||
|
}, |
||||
|
}, |
||||
|
"summary": "Login Post", |
||||
|
"operationId": "login_token_post", |
||||
|
"requestBody": { |
||||
|
"content": { |
||||
|
"application/x-www-form-urlencoded": { |
||||
|
"schema": {"$ref": "#/components/schemas/Body_login"} |
||||
|
} |
||||
|
}, |
||||
|
"required": True, |
||||
|
}, |
||||
|
} |
||||
|
}, |
||||
|
"/users/me": { |
||||
|
"get": { |
||||
|
"responses": { |
||||
|
"200": { |
||||
|
"description": "Successful Response", |
||||
|
"content": {"application/json": {"schema": {}}}, |
||||
|
} |
||||
|
}, |
||||
|
"summary": "Read Users Me Get", |
||||
|
"operationId": "read_users_me_users_me_get", |
||||
|
"security": [{"OAuth2PasswordBearer": []}], |
||||
|
} |
||||
|
}, |
||||
|
}, |
||||
|
"components": { |
||||
|
"schemas": { |
||||
|
"Body_login": { |
||||
|
"title": "Body_login", |
||||
|
"required": ["username", "password"], |
||||
|
"type": "object", |
||||
|
"properties": { |
||||
|
"grant_type": { |
||||
|
"title": "Grant_Type", |
||||
|
"pattern": "password", |
||||
|
"type": "string", |
||||
|
}, |
||||
|
"username": {"title": "Username", "type": "string"}, |
||||
|
"password": {"title": "Password", "type": "string"}, |
||||
|
"scope": {"title": "Scope", "type": "string", "default": ""}, |
||||
|
"client_id": {"title": "Client_Id", "type": "string"}, |
||||
|
"client_secret": {"title": "Client_Secret", "type": "string"}, |
||||
|
}, |
||||
|
}, |
||||
|
"ValidationError": { |
||||
|
"title": "ValidationError", |
||||
|
"required": ["loc", "msg", "type"], |
||||
|
"type": "object", |
||||
|
"properties": { |
||||
|
"loc": { |
||||
|
"title": "Location", |
||||
|
"type": "array", |
||||
|
"items": {"type": "string"}, |
||||
|
}, |
||||
|
"msg": {"title": "Message", "type": "string"}, |
||||
|
"type": {"title": "Error Type", "type": "string"}, |
||||
|
}, |
||||
|
}, |
||||
|
"HTTPValidationError": { |
||||
|
"title": "HTTPValidationError", |
||||
|
"type": "object", |
||||
|
"properties": { |
||||
|
"detail": { |
||||
|
"title": "Detail", |
||||
|
"type": "array", |
||||
|
"items": {"$ref": "#/components/schemas/ValidationError"}, |
||||
|
} |
||||
|
}, |
||||
|
}, |
||||
|
}, |
||||
|
"securitySchemes": { |
||||
|
"OAuth2PasswordBearer": { |
||||
|
"type": "oauth2", |
||||
|
"flows": {"password": {"scopes": {}, "tokenUrl": "/token"}}, |
||||
|
} |
||||
|
}, |
||||
|
}, |
||||
|
} |
||||
|
|
||||
|
|
||||
|
def test_openapi_schema(): |
||||
|
response = client.get("/openapi.json") |
||||
|
assert response.status_code == 200 |
||||
|
assert response.json() == openapi_schema |
||||
|
|
||||
|
|
||||
|
def test_login(): |
||||
|
response = client.post("/token", data={"username": "johndoe", "password": "secret"}) |
||||
|
assert response.status_code == 200 |
||||
|
assert response.json() == {"access_token": "johndoe", "token_type": "bearer"} |
||||
|
|
||||
|
|
||||
|
def test_login_incorrect_password(): |
||||
|
response = client.post("/token", data={"username": "johndoe", "password": "incorrect"}) |
||||
|
assert response.status_code == 400 |
||||
|
assert response.json() == {"detail": "Incorrect username or password"} |
||||
|
|
||||
|
|
||||
|
def test_login_incorrect_username(): |
||||
|
response = client.post("/token", data={"username": "foo", "password": "secret"}) |
||||
|
assert response.status_code == 400 |
||||
|
assert response.json() == {"detail": "Incorrect username or password"} |
||||
|
|
||||
|
|
||||
|
def test_no_token(): |
||||
|
response = client.get("/users/me") |
||||
|
assert response.status_code == 403 |
||||
|
assert response.json() == {"detail": "Not authenticated"} |
||||
|
|
||||
|
|
||||
|
def test_token(): |
||||
|
response = client.get("/users/me", headers={"Authorization": "Bearer johndoe"}) |
||||
|
assert response.status_code == 200 |
||||
|
assert response.json() == { |
||||
|
"username": "johndoe", |
||||
|
"full_name": "John Doe", |
||||
|
"email": "[email protected]", |
||||
|
"hashed_password": "fakehashedsecret", |
||||
|
"disabled": False, |
||||
|
} |
||||
|
|
||||
|
|
||||
|
def test_incorrect_token(): |
||||
|
response = client.get("/users/me", headers={"Authorization": "Bearer nonexistent"}) |
||||
|
assert response.status_code == 400 |
||||
|
assert response.json() == {"detail": "Invalid authentication credentials"} |
||||
|
|
||||
|
def test_incorrect_token_type(): |
||||
|
response = client.get( |
||||
|
"/users/me", headers={"Authorization": "Notexistent testtoken"} |
||||
|
) |
||||
|
assert response.status_code == 403 |
||||
|
assert response.json() == {"detail": "Not authenticated"} |
||||
|
|
||||
|
def test_inactive_user(): |
||||
|
response = client.get("/users/me", headers={"Authorization": "Bearer alice"}) |
||||
|
assert response.status_code == 400 |
||||
|
assert response.json() == {"detail": "Inactive user"} |
Loading…
Reference in new issue