Skip to content

Commit c9e134e

Browse files
authored
Merge pull request #254 from asherpasha/dev
Input validation - fastpheno.
2 parents 65ccdb9 + 61b979f commit c9e134e

File tree

3 files changed

+53
-5
lines changed

3 files changed

+53
-5
lines changed

api/resources/fastpheno.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
Fastpheno endpoint for retrieving tree data
55
"""
66

7+
import re
8+
79
from flask_restx import Namespace, Resource
810
from api import db
911
from api.models.fastpheno import Sites, Trees, Band, Height
@@ -26,6 +28,16 @@ def get(self, site, month, band):
2628
month = escape(month)
2729
band = escape(band)
2830

31+
# Validate input
32+
if not re.search(r"^[a-z]{1,15}$", site, re.I):
33+
return BARUtils.error_exit("Invalid site name"), 400
34+
35+
if not re.search(r"^[a-z]{1,4}$", month, re.I):
36+
return BARUtils.error_exit("Invalid month"), 400
37+
38+
if not re.search(r"^band_\d{1,8}$", band, re.I):
39+
return BARUtils.error_exit("Invalid band"), 400
40+
2941
rows = db.session.execute(
3042
db.select(Sites, Trees, Height, Band)
3143
.select_from(Sites)
@@ -67,6 +79,10 @@ def get(self, genotype_id):
6779
# Escape input data
6880
genotype_id = escape(genotype_id).capitalize()
6981

82+
# Validate input
83+
if not re.search(r"^[a-z]{1,3}$", genotype_id, re.I):
84+
return BARUtils.error_exit("Invalid genotype id"), 400
85+
7086
rows = db.session.execute(
7187
db.select(Sites, Trees)
7288
.select_from(Sites)

requirements.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
aniso8601==10.0.0
22
async-timeout==5.0.1
3-
attrs==24.3.0
3+
attrs==25.1.0
44
black==24.10.0
55
blinker==1.9.0
66
cachelib==0.9.0
77
certifi==2024.12.14
88
charset-normalizer==3.4.1
99
click==8.1.8
1010
coverage==7.6.10
11-
Deprecated==1.2.15
11+
Deprecated==1.2.18
1212
flake8==7.1.1
1313
Flask==3.1.0
1414
Flask-Caching==2.3.0
@@ -28,7 +28,7 @@ jsonschema-specifications==2024.10.1
2828
limits==4.0.1
2929
markdown-it-py==3.0.0
3030
MarkupSafe==3.0.2
31-
marshmallow==3.25.1
31+
marshmallow==3.26.0
3232
mccabe==0.7.0
3333
mdurl==0.1.2
3434
mypy-extensions==1.0.0
@@ -46,7 +46,7 @@ pytest==8.3.4
4646
python-dateutil==2.9.0.post0
4747
pytz==2024.2
4848
redis==5.2.1
49-
referencing==0.36.1
49+
referencing==0.36.2
5050
requests==2.32.3
5151
rich==13.9.4
5252
rpds-py==0.22.3

tests/resources/test_fastpheno.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,30 @@ def test_bands(self):
5252
}
5353
self.assertEqual(response.json, expected)
5454

55+
# Invalid site
56+
response = self.app_client.get("/fastpheno/get_bands/12345/feb/band_1")
57+
expected = {
58+
"wasSuccessful": False,
59+
"error": "Invalid site name",
60+
}
61+
self.assertEqual(response.json, expected)
62+
63+
# Invalid month
64+
response = self.app_client.get("/fastpheno/get_bands/pintendre/1234/band_1")
65+
expected = {
66+
"wasSuccessful": False,
67+
"error": "Invalid month",
68+
}
69+
self.assertEqual(response.json, expected)
70+
71+
# Invalid band
72+
response = self.app_client.get("/fastpheno/get_bands/NOTASITE/feb/band_x")
73+
expected = {
74+
"wasSuccessful": False,
75+
"error": "Invalid band",
76+
}
77+
self.assertEqual(response.json, expected)
78+
5579
def test_site_genotype_ids(self):
5680
"""This function checks GET request for fastpheno sites for genotype_ids
5781
:return:
@@ -92,9 +116,17 @@ def test_site_genotype_ids(self):
92116
self.assertEqual(response.json, expected)
93117

94118
# Not working version
95-
response = self.app_client.get("/fastpheno/get_trees/NOTAGENOTYPE")
119+
response = self.app_client.get("/fastpheno/get_trees/Z")
96120
expected = {
97121
"wasSuccessful": False,
98122
"error": "There are no data found for the given parameters",
99123
}
100124
self.assertEqual(response.json, expected)
125+
126+
# Invalid data
127+
response = self.app_client.get("/fastpheno/get_trees/NOTVALID")
128+
expected = {
129+
"wasSuccessful": False,
130+
"error": "Invalid genotype id",
131+
}
132+
self.assertEqual(response.json, expected)

0 commit comments

Comments
 (0)