Skip to content

Commit f04752d

Browse files
authored
Merge pull request #6001 from hotosm/fix/5946-empty-interest-name
Don't allow creating/updating interest with empty name
2 parents 306575c + 254ea53 commit f04752d

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

backend/models/dtos/interests_dto.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class InterestDTO(Model):
77
"""DTO for a interest."""
88

99
id = IntType()
10-
name = StringType()
10+
name = StringType(required=True, min_length=1)
1111
user_selected = BooleanType(
1212
serialized_name="userSelected", serialize_when_none=False
1313
)

tests/backend/integration/api/interests/test_resources.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,21 @@ def test_create_a_new_interest_by_organisation_admin_passes(self):
9595
self.assertEqual(response.status_code, 200)
9696
self.assertEqual(response_body["name"], NEW_INTEREST_NAME)
9797

98+
def test_create_a_new_interest_with_empty_name_fails(self):
99+
"""
100+
Test that endpoint returns 400 when admin creates a new interest with empty name
101+
"""
102+
# setup: make test user organisation admin
103+
add_manager_to_organisation(self.test_organisation, self.test_user)
104+
response = self.client.post(
105+
self.endpoint_url,
106+
headers={"Authorization": self.session_token},
107+
json={"name": ""},
108+
)
109+
response_body = response.get_json()
110+
self.assertEqual(response.status_code, 400)
111+
self.assertEqual(response_body["SubCode"], "InvalidData")
112+
98113
# get
99114
def test_get_all_interest_passes(self):
100115
"""
@@ -230,6 +245,21 @@ def test_update_an_existent_interest_with_invalid_data_fails(self):
230245
self.assertEqual(response.status_code, 400)
231246
self.assertEqual(response_body["SubCode"], "InvalidData")
232247

248+
def test_update_an_interest_with_empty_name_fails(self):
249+
"""
250+
Test that endpoint returns 400 when updating an interest with empty name
251+
"""
252+
# setup: make test user organisation admin
253+
add_manager_to_organisation(self.test_organisation, self.test_user)
254+
response = self.client.patch(
255+
self.endpoint_url,
256+
headers={"Authorization": self.session_token},
257+
json={"name": ""},
258+
)
259+
response_body = response.get_json()
260+
self.assertEqual(response.status_code, 400)
261+
self.assertEqual(response_body["SubCode"], "InvalidData")
262+
233263
def test_update_an_existing_interest_by_organisation_admin_passes(self):
234264
"""
235265
Test that endpoint returns 200 when updating an existing interest by admin

0 commit comments

Comments
 (0)