Skip to content

Commit 2aa6048

Browse files
committed
[release/2024-10-22] fix-forward: add character reqs to read
1 parent 12c0671 commit 2aa6048

File tree

5 files changed

+52
-7
lines changed

5 files changed

+52
-7
lines changed

src/api/tests/feature_tests/features/readQuestionnaire.failure.feature

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,27 @@ Feature: Read Questionnaire - failure scenarios
77
| version | 1 |
88
| Authorization | letmein |
99

10-
Scenario: Read an existing Questionnaire
11-
When I make a "GET" request with "default" headers to "Questionnaire/does-not-exist"
10+
Scenario Outline: Read an non-existing Questionnaire, do not fail early due to bad characters
11+
When I make a "GET" request with "default" headers to "Questionnaire/<questionnaire_name>"
1212
Then I receive a status code "404" with body
13-
| path | value |
14-
| errors.0.code | RESOURCE_NOT_FOUND |
15-
| errors.0.message | Could not find Questionnaire for key ('does-not-exist') |
13+
| path | value |
14+
| errors.0.code | RESOURCE_NOT_FOUND |
15+
| errors.0.message | Could not find Questionnaire for key ('<questionnaire_name>') |
16+
17+
Examples:
18+
| questionnaire_name |
19+
| has spaces in it |
20+
| has_underscores_and spaces |
21+
| isMixedCase |
22+
23+
Scenario Outline: Read an non-existing Questionnaire, but fail early due to bad characters
24+
When I make a "GET" request with "default" headers to "Questionnaire/<questionnaire_name>"
25+
Then I receive a status code "400" with body
26+
| path | value |
27+
| errors.0.code | VALIDATION_ERROR |
28+
| errors.0.message | QuestionnairePathParams.questionnaire_id: string does not match regex "^[a-zA-Z0-9 _]*${ dollar() }" |
29+
30+
Examples:
31+
| questionnaire_name |
32+
| has-hyphen-in-it |
33+
| has+other characters |

src/api/tests/feature_tests/features/readQuestionnaire.success.feature

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,5 @@ Feature: Read Questionnaire - success scenarios
1919
| questionnaire_name |
2020
| spine_endpoint |
2121
| spine_device |
22+
| spine_ENDpoint |
23+
| SPINE_DEVICE |

src/layers/domain/repository/questionnaire_repository/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def read_questions(path: Path):
4141
class QuestionnaireRepository:
4242

4343
def read(self, name: str) -> Questionnaire:
44-
path = get_latest_questions_by_name(name=name)
44+
path = get_latest_questions_by_name(name=name.lower())
4545
if not path:
4646
raise ItemNotFound(name, item_type=Questionnaire)
4747

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import pytest
2+
from domain.request_models.v1 import QuestionnairePathParams
3+
from pydantic import ValidationError
4+
5+
6+
@pytest.mark.parametrize(
7+
"id",
8+
["abc123", "abc 123", " 123 ABC ", " 123_ABC "],
9+
)
10+
def test_read_questionnaire_path_params_pass(id: str):
11+
params = QuestionnairePathParams(questionnaire_id=id)
12+
assert params.dict() == {"questionnaire_id": id}
13+
14+
15+
@pytest.mark.parametrize(
16+
"id",
17+
["#abc123", "abc-123", "~123_ABC "],
18+
)
19+
def test_read_questionnaire_path_params_fail(id: str):
20+
with pytest.raises(ValidationError):
21+
QuestionnairePathParams(questionnaire_id=id)

src/layers/domain/request_models/v1.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from domain.core.product_team_key import ProductTeamKey
22
from pydantic import BaseModel, Extra, Field
33

4+
ALPHANUMERIC_SPACES_AND_UNDERSCORES = r"^[a-zA-Z0-9 _]*$"
5+
46

57
class ProductTeamPathParams(BaseModel, extra=Extra.forbid):
68
product_team_id: str = Field(...)
@@ -39,4 +41,6 @@ class DeviceReferenceDataPathParams(BaseModel, extra=Extra.forbid):
3941

4042

4143
class QuestionnairePathParams(BaseModel, extra=Extra.forbid):
42-
questionnaire_id: str # NB: this maps onto the domain field Questionnaire.name
44+
45+
# NB: questionnaire_id maps onto the domain field Questionnaire.name
46+
questionnaire_id: str = Field(regex=ALPHANUMERIC_SPACES_AND_UNDERSCORES)

0 commit comments

Comments
 (0)