|
| 1 | +import pytest |
1 | 2 | from flask import Flask |
2 | 3 |
|
3 | 4 | from flask_utils import validate_params |
4 | 5 |
|
5 | 6 |
|
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 |
9 | 12 |
|
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 |
14 | 17 |
|
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 |
24 | 19 |
|
| 20 | + @pytest.fixture(autouse=True) |
| 21 | + def client(self, setup_routes): |
| 22 | + yield setup_routes.test_client() |
25 | 23 |
|
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