Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_invalid_dict():
with pytest.raises(AssertionError):
app = FastAPI()
class Item(BaseModel):
title: str
@app.get("/items/")
def read_items(q: Dict[str, Item] = Query(None)):
pass # pragma: no cover
from typing import Optional
from fastapi import FastAPI
from pydantic import BaseModel
from starlette.testclient import TestClient
app = FastAPI()
class SubModel(BaseModel):
a: Optional[str] = "foo"
class Model(BaseModel):
x: Optional[int]
sub: SubModel
class ModelSubclass(Model):
y: int
@app.get("/", response_model=Model, response_model_exclude_unset=True)
from typing import Optional
from fastapi import FastAPI, Security
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
from starlette.testclient import TestClient
app = FastAPI()
security = HTTPBearer(auto_error=False)
@app.get("/users/me")
def read_current_user(
credentials: Optional[HTTPAuthorizationCredentials] = Security(security),
):
if credentials is None:
return {"msg": "Create an account first"}
return {"scheme": credentials.scheme, "credentials": credentials.credentials}
client = TestClient(app)
openapi_schema = {
def read_items(q: Dict[str, Item] = Query(None)):
pass # pragma: no cover
def test_no_recycling_in_bretagne_poi():
"""
Check that no trash info is provided for a POI in bretagne
"""
client = TestClient(app)
response = client.get(url=f"http://localhost/v1/pois/osm:node:36153811")
assert response.status_code == 200
resp = response.json()
assert resp["id"] == "osm:node:36153811"
assert resp["name"] == "Multiplexe Liberté"
assert len([x for x in resp["blocks"] if x["type"] == "recycling"]) == 0
def test_direction_car(mock_directions_car):
client = TestClient(app)
response = client.get(
"http://localhost/v1/directions/2.3402355%2C48.8900732%3B2.3688579%2C48.8529869",
params={"language": "fr", "type": "driving", "exclude": "ferry"},
)
assert response.status_code == 200
response_data = response.json()
assert response_data["status"] == "success"
assert len(response_data["data"]["routes"]) == 3
assert all(r["geometry"] for r in response_data["data"]["routes"])
assert response_data["data"]["routes"][0]["duration"] == 1819
assert len(response_data["data"]["routes"][0]["legs"]) == 1
assert len(response_data["data"]["routes"][0]["legs"][0]["steps"]) == 10
assert response_data["data"]["routes"][0]["legs"][0]["mode"] == "CAR"
assert "exclude=ferry" in mock_directions_car.calls[0].request.url
def test_contact_phone():
"""
The louvre museum has the tag 'contact:phone'
We test this tag is correct here
"""
client = TestClient(app)
response = client.get(url=f"http://localhost/v1/pois/osm:relation:7515426")
assert response.status_code == 200
resp = response.json()
assert resp["id"] == "osm:relation:7515426"
assert resp["name"] == "Louvre Museum"
assert resp["local_name"] == "Musée du Louvre"
assert resp["class_name"] == "museum"
assert resp["subclass_name"] == "museum"
assert resp["blocks"][1]["type"] == "phone"
assert resp["blocks"][1]["url"] == "tel:+33140205229"
assert resp["blocks"][1]["international_format"] == "+33 1 40 20 52 29"
assert resp["blocks"][1]["local_format"] == "01 40 20 52 29"
def test_redirect_obsolete_address_with_lat_lon():
client = TestClient(app)
response = client.get(
url=f"http://localhost/v1/places/addr:-1.12;45.6?lang=fr", allow_redirects=False
)
assert response.status_code == 303
assert response.headers["location"] == "/v1/places/latlon:45.60000:-1.12000?lang=fr"
def test_directions_public_transport_restricted_areas():
client = TestClient(app)
# Paris - South Africa
response = client.get(
"http://localhost/v1/directions/2.3211757,48.8604893;22.1741215,-33.1565800",
params={"language": "fr", "type": "publictransport"},
)
assert response.status_code == 422
# Pekin
response = client.get(
"http://localhost/v1/directions/116.2945000,39.9148800;116.4998847,39.9091405",
params={"language": "fr", "type": "publictransport"},
)
assert response.status_code == 422
# Washington - New York
def test_full(mock_external_requests):
"""
Exhaustive test that checks all possible blocks
"""
client = TestClient(app)
response = client.get(url=f"http://localhost/v1/pois/osm:way:7777778?lang=es")
assert response.status_code == 200
resp = response.json()
assert resp == {
"type": "poi",
"id": "osm:way:7777778",
"name": "Fako Allo",
"local_name": "Fake All",
"class_name": "museum",
"subclass_name": "museum",
"geometry": {
"coordinates": [2.3250037768187326, 48.86618482685007],
"type": "Point",