Skip to content

Commit

Permalink
added tests for name validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Chelakhovl committed Dec 9, 2024
1 parent f31bd6a commit 72625d7
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion BackEnd/profiles/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ def validate(self, data):
)
if name and len(name) > 45:
raise serializers.ValidationError(
{"name": "The company name must not exceed 45 characters."}
{"name": "Ensure this field has no more than 45 characters."}
)

return data
Expand Down
38 changes: 38 additions & 0 deletions BackEnd/profiles/tests/test_crud_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -1149,3 +1149,41 @@ def test_create_profile_wrong_data(self):
},
)
self.assertEqual(400, response.status_code)

def test_partial_update_profile_name_exceeds_character_limit(self):
self.client.force_authenticate(self.user)

long_name = "a" * 46
response = self.client.patch(
path="/api/profiles/{profile_id}".format(
profile_id=self.profile.id
),
data={"name": long_name},
format="json",
)

self.assertEqual(status.HTTP_400_BAD_REQUEST, response.status_code)

self.assertEqual(
response.json(),
{"name": ["Ensure this field has no more than 45 characters."]},
)

def test_partial_update_profile_name_within_limit(self):
self.client.force_authenticate(self.user)

valid_name = "Valid Company Name"
response = self.client.patch(
path="/api/profiles/{profile_id}".format(
profile_id=self.profile.id
),
data={"name": valid_name},
format="json",
)

self.assertEqual(status.HTTP_200_OK, response.status_code)

self.assertEqual(response.data.get("name"), valid_name)

self.profile.refresh_from_db()
self.assertEqual(self.profile.name, valid_name)

0 comments on commit 72625d7

Please sign in to comment.