Skip to content

Commit ab6ccb0

Browse files
committed
Add tests for File validation
1 parent 75dca30 commit ab6ccb0

File tree

7 files changed

+127
-1
lines changed

7 files changed

+127
-1
lines changed
Loading
Loading
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "Test JSON Document",
3+
"description": "This document will be uploaded to an API route that expects image/jpeg or image/png Content-Type, with the expectation that the API will return an error."
4+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import datetime
2+
import filecmp
3+
from pathlib import Path
4+
from typing import Type, List, Optional
5+
6+
resources = Path(__file__).parent / 'resources'
7+
8+
9+
def test_required_file(client):
10+
url = "/file/required"
11+
# Test that we receive a success response if a file is provided
12+
r = client.post(url, data={"v": (resources / "test.json").open("rb")})
13+
assert "success" in r.json
14+
assert r.json["success"]
15+
# Test that we receive an error if a file is not provided
16+
r = client.post(url)
17+
assert "error" in r.json
18+
19+
20+
def test_optional_file(client):
21+
url = "/file/optional"
22+
# Test that we receive a success response if a file is provided
23+
r = client.post(url, data={"v": (resources / "test.json").open("rb")})
24+
assert "success" in r.json
25+
assert r.json["success"]
26+
assert "file_provided" in r.json
27+
assert r.json["file_provided"] is True
28+
# Test that we receive an error if a file is not provided
29+
r = client.post(url)
30+
assert "success" in r.json
31+
assert r.json["success"]
32+
assert "file_provided" in r.json
33+
assert r.json["file_provided"] is False
34+
35+
36+
def test_file_content_types(client):
37+
url = "/file/content_types"
38+
# Test that we receive a success response if a file of correct Content-Type is provided
39+
r = client.post(url, data={"v": (resources / "test.json").open("rb")})
40+
assert "success" in r.json
41+
assert r.json["success"]
42+
# Test that we receive an error if a file of incorrect Content-Type is provided
43+
r = client.post(url, data={"v": (resources / "hubble_mars_10kB.jpg").open("rb")})
44+
assert "error" in r.json
45+
46+
47+
def test_file_min_length(client):
48+
url = "/file/min_length"
49+
# Test that we receive a success response if a file of correct Content-Length is provided
50+
load_path = resources / "hubble_mars_10kB.jpg"
51+
r = client.post(url, data={"v": load_path.open("rb")})
52+
assert "success" in r.json
53+
assert r.json["success"]
54+
assert "save_path" in r.json
55+
assert filecmp.cmp(load_path, r.json["save_path"])
56+
# Test that we receive an error if a file of incorrect Content-Length is provided
57+
r = client.post(url, data={"v": (resources / "test.json").open("rb")})
58+
assert "error" in r.json
59+
60+
def test_file_max_length(client):
61+
url = "/file/max_length"
62+
# Test that we receive a success response if a file of correct Content-Length is provided
63+
load_path = resources / "hubble_mars_10kB.jpg"
64+
r = client.post(url, data={"v": load_path.open("rb")})
65+
assert "success" in r.json
66+
assert r.json["success"]
67+
assert "save_path" in r.json
68+
assert filecmp.cmp(load_path, r.json["save_path"])
69+
# Test that we receive an error if a file of incorrect Content-Length is provided
70+
r = client.post(url, data={"v": (resources / "aldrin_47kB.jpg").open("rb")})
71+
assert "error" in r.json

flask_parameter_validation/test/testing_application.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from flask import Flask, jsonify
44

55
from flask_parameter_validation import ValidateParameters, Query, Json, Form, Route
6+
from flask_parameter_validation.test.testing_blueprints.file_blueprint import get_file_blueprint
67
from flask_parameter_validation.test.testing_blueprints.parameter_blueprint import get_parameter_blueprint
78

89

@@ -13,5 +14,5 @@ def create_app():
1314
app.register_blueprint(get_parameter_blueprint(Json, "json", "json", "post"))
1415
app.register_blueprint(get_parameter_blueprint(Form, "form", "form", "post"))
1516
app.register_blueprint(get_parameter_blueprint(Route, "route", "route", "get"))
16-
17+
app.register_blueprint(get_file_blueprint("file"))
1718
return app
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import datetime
2+
from pathlib import Path
3+
from typing import Optional
4+
from flask import Blueprint, jsonify, request
5+
from werkzeug.datastructures import FileStorage
6+
from werkzeug.utils import secure_filename
7+
8+
from flask_parameter_validation import ValidateParameters, File
9+
10+
resources = Path(__file__).parent.parent / 'uploads'
11+
12+
13+
def get_file_blueprint(bp_name: str) -> Blueprint:
14+
file_bp = Blueprint(bp_name, __name__, url_prefix="/file")
15+
16+
@file_bp.post("/required")
17+
@ValidateParameters()
18+
def required(v: FileStorage = File()):
19+
assert type(v) is FileStorage
20+
return jsonify({"success": True})
21+
22+
@file_bp.post("/optional")
23+
@ValidateParameters()
24+
def optional(v: Optional[FileStorage] = File()):
25+
return jsonify({"success": True, "file_provided": v is not None})
26+
27+
@file_bp.post("/content_types")
28+
@ValidateParameters()
29+
def content_types(v: FileStorage = File(content_types=["application/json"])):
30+
assert v.content_type == "application/json"
31+
return jsonify({"success": True})
32+
33+
@file_bp.post("/min_length")
34+
@ValidateParameters()
35+
def min_length(v: FileStorage = File(min_length=300)):
36+
save_path = resources / secure_filename(v.filename)
37+
v.save(resources / secure_filename(v.filename))
38+
v.close()
39+
return jsonify({"success": True, "save_path": str(save_path.absolute())})
40+
41+
@file_bp.post("/max_length")
42+
@ValidateParameters()
43+
def max_length(v: FileStorage = File(max_length=10000)):
44+
save_path = resources / secure_filename(v.filename)
45+
v.save(resources / secure_filename(v.filename))
46+
v.close()
47+
return jsonify({"success": True, "save_path": str(save_path.absolute())})
48+
49+
return file_bp
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*

0 commit comments

Comments
 (0)