Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restrict amount of data beiging posted Issue #172 #176

Merged
merged 4 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions pv_site_api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ def get_sites(
# post_pv_actual: sends data to us, and we save to database
@app.post("/sites/{site_uuid}/pv_actual", tags=["Generation"])
def post_pv_actual(
request: Request,
site_uuid: str,
pv_actual: MultiplePVActual,
session: Session = Depends(get_session),
Expand All @@ -247,6 +248,13 @@ def post_pv_actual(

"""

# limit payload size to 1 MB, raise 413 if exceeded
content_length = int(request.headers.get("Content-Length", 0))
max_payload_size = 1024 * 1024

if content_length > max_payload_size:
raise HTTPException(status_code=413, detail="Payload too large")

if is_fake():
print(f"Got {pv_actual.model_dump()} for site {site_uuid}")
print("Not doing anything with it (yet!)")
Expand Down
22 changes: 22 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,30 @@ def test_post_pv_actual(client, fake):
assert response.status_code == 200


def test_post_too_large_pv_actual(client, fake):
pv_actual_value = PVActualValue(
datetime_utc=datetime.now(timezone.utc), actual_generation_kw=73.3
)

# make fake iteration of pv values that is bigger than 1 MB
fake_pv_actual_iteration = MultiplePVActual(
# 30000 {key:value} pairs ~ 1 MB
site_uuid="fff-fff",
pv_actual_values=[pv_actual_value] * 30000,
)

# this makes sure the datetimes are iso strings
obj = json.loads(fake_pv_actual_iteration.json())

response = client.post("/sites/fff-fff-fff/pv_actual", json=obj)

assert response.status_code == 413
assert response.json() == {"detail": "Payload too large"}


def test_delete_site(client, fake):
response = client.delete("/sites/delete/fff-fff-fff")

assert response.json()["message"] == "Site deleted successfully"
assert response.status_code == 200

Loading