Skip to content
This repository was archived by the owner on Nov 19, 2024. It is now read-only.

Commit 47ea2f5

Browse files
committed
feat: Added more tests made by coderabbit
1 parent 7c24a34 commit 47ea2f5

File tree

2 files changed

+72
-19
lines changed

2 files changed

+72
-19
lines changed

flask_utils/decorators.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,9 @@ def wrapper(*args, **kwargs): # type: ignore
292292

293293
for key in data:
294294
if key not in parameters:
295-
if use_error_handlers:
296-
raise BadRequestError(f"Unexpected key: {key}.", f"Expected keys are: {parameters.keys()}")
295+
return _handle_bad_request(
296+
use_error_handlers, f"Unexpected key: {key}.", f"Expected keys are: {parameters.keys()}"
297+
)
297298

298299
for key in data:
299300
if key in parameters and not _check_type(data[key], parameters[key], allow_empty):
Lines changed: 69 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,78 @@
1+
import pytest
12
from flask import Flask
23

34
from flask_utils import validate_params
45

56

6-
def test_validate_params_without_error_handlers():
7-
app = Flask(__name__)
8-
app.testing = True
7+
class TestValidateParamsWithoutErrorHandlers:
8+
@pytest.fixture(scope="function")
9+
def setup_routes(self):
10+
app = Flask(__name__)
11+
app.testing = True
912

10-
@app.route("/example")
11-
@validate_params({"name": str})
12-
def example():
13-
return "OK", 200
13+
@app.route("/example", methods=["POST", "GET"])
14+
@validate_params({"name": str})
15+
def example():
16+
return "OK", 200
1417

15-
response = app.test_client().get("/example")
16-
assert response.status_code == 400
17-
assert (
18-
response.json["error"]
19-
== "The Content-Type header is missing or is not set to application/json, or the JSON body is missing."
20-
)
21-
assert "success" not in response.json
22-
assert "code" not in response.json
23-
assert not isinstance(response.json["error"], dict)
18+
yield app
2419

20+
@pytest.fixture(autouse=True)
21+
def client(self, setup_routes):
22+
yield setup_routes.test_client()
2523

26-
# TODO: Test all possible errors that can be raised by validate_params
24+
def test_missing_content_type(self, client):
25+
response = client.get("/example")
26+
assert response.status_code == 400
27+
assert (
28+
response.json["error"]
29+
== "The Content-Type header is missing or is not set to application/json, or the JSON body is missing."
30+
)
31+
assert "success" not in response.json
32+
assert "code" not in response.json
33+
assert not isinstance(response.json["error"], dict)
34+
35+
def test_malformed_json_body(self, client):
36+
response = client.post("/example", data="not a json", headers={"Content-Type": "application/json"})
37+
assert response.status_code == 400
38+
assert response.json["error"] == "The Json Body is malformed."
39+
assert "success" not in response.json
40+
assert "code" not in response.json
41+
assert not isinstance(response.json["error"], dict)
42+
43+
def test_json_body_not_dict(self, client):
44+
response = client.post("/example", json=["not", "a", "dict"])
45+
assert response.status_code == 400
46+
assert response.json["error"] == "JSON body must be a dict"
47+
assert "success" not in response.json
48+
assert "code" not in response.json
49+
assert not isinstance(response.json["error"], dict)
50+
51+
def test_missing_key(self, client, setup_routes):
52+
@setup_routes.route("/example2", methods=["POST"])
53+
@validate_params({"name": str, "age": int})
54+
def example2():
55+
return "OK", 200
56+
57+
response = client.post("/example2", json={"name": "John"})
58+
assert response.status_code == 400
59+
assert response.json["error"] == "Missing key: age"
60+
assert "success" not in response.json
61+
assert "code" not in response.json
62+
assert not isinstance(response.json["error"], dict)
63+
64+
def test_unexpected_key(self, client):
65+
response = client.post("/example", json={"name": "John", "extra": "value"})
66+
assert response.status_code == 400
67+
assert response.json["error"] == "Unexpected key: extra."
68+
assert "success" not in response.json
69+
assert "code" not in response.json
70+
assert not isinstance(response.json["error"], dict)
71+
72+
def test_wrong_type(self, client):
73+
response = client.post("/example", json={"name": 123})
74+
assert response.status_code == 400
75+
assert response.json["error"] == "Wrong type for key name."
76+
assert "success" not in response.json
77+
assert "code" not in response.json
78+
assert not isinstance(response.json["error"], dict)

0 commit comments

Comments
 (0)