Skip to content

Commit 9955fd6

Browse files
committed
Only allow 1 product_team_id
1 parent 5d7b9c8 commit 9955fd6

File tree

7 files changed

+49
-72
lines changed

7 files changed

+49
-72
lines changed

src/api/createProductTeam/tests/v1/test_index_v1.py

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def test_index_bad_payload(version):
124124
"1",
125125
],
126126
)
127-
def test_index(version):
127+
def test_index_duplicated_keys(version):
128128
with mock_table_cpm(table_name=TABLE_NAME), mock.patch.dict(
129129
os.environ,
130130
{
@@ -141,26 +141,18 @@ def test_index(version):
141141
"body": json.dumps(CPM_PRODUCT_TEAM_NO_ID_DUPED_KEYS),
142142
}
143143
)
144-
result_body = json_loads(result["body"])
145144
expected_body = json.dumps(
146145
{
147-
"id": result_body["id"],
148-
"name": "FOOBAR Product Team",
149-
"ods_code": "F5H1R",
150-
"status": "active",
151-
"created_on": result_body["created_on"],
152-
"updated_on": None,
153-
"deleted_on": None,
154-
"keys": [
146+
"errors": [
155147
{
156-
"key_type": "product_team_id",
157-
"key_value": "808a36db-a52a-4130-b71e-d9cbcbaed15b",
148+
"code": "VALIDATION_ERROR",
149+
"message": "CreateProductTeamIncomingParams.keys: Ensure that product_team_id only exists once within keys.",
158150
}
159-
],
151+
]
160152
}
161153
)
162154
expected = {
163-
"statusCode": 201,
155+
"statusCode": 400,
164156
"body": expected_body,
165157
"headers": {
166158
"Content-Length": str(len(expected_body)),

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Feature: Create Product Team - failure scenarios
4545
| Content-Type | application/json |
4646
| Content-Length | 175 |
4747

48-
Scenario: Cannot create a ProductTeam with an that is missing fields
48+
Scenario: Cannot create a ProductTeam that is missing fields
4949
When I make a "POST" request with "default" headers to "ProductTeam" with body:
5050
| path | value |
5151
| name | My Great Product Team |
@@ -137,3 +137,23 @@ Feature: Create Product Team - failure scenarios
137137
| name | value |
138138
| Content-Type | application/json |
139139
| Content-Length | 272 |
140+
141+
Scenario: Successfully create a ProductTeam with duplicated product_team_id keys
142+
When I make a "POST" request with "default" headers to "ProductTeam" with body:
143+
| path | value |
144+
| name | My Great Product Team |
145+
| ods_code | F5H1R |
146+
| keys.0.key_type | product_team_id |
147+
| keys.0.key_value | 0a78ee8f-5bcf-4db1-9341-ef1d67248715 |
148+
| keys.1.key_type | product_team_id |
149+
| keys.1.key_value | 0a78ee8f-5bcf-4db1-9341-ef1d67248715 |
150+
| keys.2.key_type | product_team_id |
151+
| keys.2.key_value | 0a78ee8f-5bcf-4db1-9341-ef1d67248715 |
152+
Then I receive a status code "400" with body
153+
| path | value |
154+
| errors.0.code | VALIDATION_ERROR |
155+
| errors.0.message | CreateProductTeamIncomingParams.keys: Ensure that product_team_id only exists once within keys. |
156+
And the response headers contain:
157+
| name | value |
158+
| Content-Type | application/json |
159+
| Content-Length | 152 |

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

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -43,46 +43,6 @@ Feature: Create Product Team - success scenarios
4343
| keys.0.key_type | product_team_id |
4444
| keys.0.key_value | 0a78ee8f-5bcf-4db1-9341-ef1d67248715 |
4545

46-
Scenario: Successfully create a ProductTeam by removing the duplicated keys
47-
When I make a "POST" request with "default" headers to "ProductTeam" with body:
48-
| path | value |
49-
| name | My Great Product Team |
50-
| ods_code | F5H1R |
51-
| keys.0.key_type | product_team_id |
52-
| keys.0.key_value | 0a78ee8f-5bcf-4db1-9341-ef1d67248715 |
53-
| keys.1.key_type | product_team_id |
54-
| keys.1.key_value | 0a78ee8f-5bcf-4db1-9341-ef1d67248715 |
55-
| keys.2.key_type | product_team_id |
56-
| keys.2.key_value | 0a78ee8f-5bcf-4db1-9341-ef1d67248715 |
57-
Then I receive a status code "201" with body where "keys" has a length of "1"
58-
| path | key_value |
59-
| id | << ignore >> |
60-
| name | My Great Product Team |
61-
| ods_code | F5H1R |
62-
| status | active |
63-
| created_on | << ignore >> |
64-
| updated_on | << ignore >> |
65-
| deleted_on | << ignore >> |
66-
| keys.0.key_type | product_team_id |
67-
| keys.0.key_value | 0a78ee8f-5bcf-4db1-9341-ef1d67248715 |
68-
And the response headers contain:
69-
| name | value |
70-
| Content-Type | application/json |
71-
| Content-Length | 306 |
72-
Given I note the response field "$.id" as "product_team_id"
73-
When I make a "GET" request with "default" headers to "ProductTeam/${ note(product_team_id) }"
74-
Then I receive a status code "200" with body
75-
| path | value |
76-
| id | ${ note(product_team_id) } |
77-
| name | My Great Product Team |
78-
| ods_code | F5H1R |
79-
| keys.0.key_type | product_team_id |
80-
| keys.0.key_value | 0a78ee8f-5bcf-4db1-9341-ef1d67248715 |
81-
| status | active |
82-
| created_on | << ignore >> |
83-
| updated_on | << ignore >> |
84-
| deleted_on | << ignore >> |
85-
8646
Scenario: Successfully create a ProductTeam and view by alias
8747
When I make a "POST" request with "default" headers to "ProductTeam" with body:
8848
| path | value |

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,6 @@ Feature: Delete Product Team - failure scenarios
5959
| ods_code | F5H1R |
6060
| keys.0.key_type | product_team_id |
6161
| keys.0.key_value | 0a78ee8f-5bcf-4db1-9341-ef1d67248715 |
62-
| keys.1.key_type | product_team_id |
63-
| keys.1.key_value | 7bac2c22-7fc8-452f-ab2e-08a91aa35ec6 |
6462
Given I note the response field "$.id" as "product_team_id"
6563
When I make a "DELETE" request with "default" headers to "ProductTeam/<product_team_id>"
6664
Then I receive a status code "404" with body

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ Feature: Delete Product Team - success scenarios
3737
| ods_code | F5H1R |
3838
| keys.0.key_type | product_team_id |
3939
| keys.0.key_value | 0a78ee8f-5bcf-4db1-9341-ef1d67248715 |
40-
| keys.1.key_type | product_team_id |
41-
| keys.1.key_value | 43252144-9bce-460c-8fa2-b0ab8cd74e72 |
4240
And I note the response field "$.id" as "product_team_id"
4341
And I have already made a "DELETE" request with "default" headers to "ProductTeam/<product_team_id>"
4442
When I make a "GET" request with "default" headers to "ProductTeam/<product_team_id>"
@@ -54,4 +52,3 @@ Feature: Delete Product Team - success scenarios
5452
| product_team_id |
5553
| ${ note(product_team_id) } |
5654
| 0a78ee8f-5bcf-4db1-9341-ef1d67248715 |
57-
| 43252144-9bce-460c-8fa2-b0ab8cd74e72 |

src/layers/domain/request_models/tests/test_parse_product_team_params.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,7 @@ def test_validate_product_team_raises_no_extra_keys():
4949

5050

5151
def test_product_team_duped_keys():
52-
product_team = CreateProductTeamIncomingParams(**CPM_PRODUCT_TEAM_NO_ID_DUPED_KEYS)
53-
assert isinstance(product_team, CreateProductTeamIncomingParams)
54-
assert len(product_team.keys) == 1
55-
assert product_team.keys[0] == CPM_PRODUCT_TEAM_NO_ID["keys"][0]
56-
assert product_team.ods_code == CPM_PRODUCT_TEAM_NO_ID["ods_code"]
57-
assert product_team.name == CPM_PRODUCT_TEAM_NO_ID["name"]
52+
with pytest.raises(ValidationError) as exc:
53+
CreateProductTeamIncomingParams(**CPM_PRODUCT_TEAM_NO_ID_DUPED_KEYS)
54+
55+
assert exc.value.model is CreateProductTeamIncomingParams

src/layers/domain/request_models/v1.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import List
2+
13
from domain.core.enum import Environment
24
from domain.core.product_team_key import ProductTeamKey
35
from pydantic import BaseModel, Extra, Field, root_validator, validator
@@ -64,12 +66,22 @@ def validate_name(cls, v: str) -> str:
6466
raise ValueError("ensure this value has at least 1 characters")
6567
return v
6668

67-
def __init__(self, **data):
68-
super().__init__(**data)
69-
# Deduplicate the list of keys
70-
self.keys = list(
71-
{frozenset(key.dict().items()): key for key in self.keys}.values()
72-
)
69+
@validator("keys")
70+
def validate_keys(cls, v: List[ProductTeamKey]) -> List[ProductTeamKey]:
71+
id_count = sum(1 for item in v if item.key_type == "product_team_id")
72+
if id_count > 1:
73+
raise ValueError(
74+
"Ensure that product_team_id only exists once within keys."
75+
)
76+
return v
77+
78+
#
79+
# def __init__(self, **data):
80+
# super().__init__(**data)
81+
# # Deduplicate the list of keys
82+
# self.keys = list(
83+
# {frozenset(key.dict().items()): key for key in self.keys}.values()
84+
# )
7385

7486

7587
class SearchProductQueryParams(BaseModel, extra=Extra.forbid):

0 commit comments

Comments
 (0)