diff --git a/BackEnd/profiles/migrations/0022_alter_profile_name_alter_profile_official_name.py b/BackEnd/profiles/migrations/0022_alter_profile_name_alter_profile_official_name.py new file mode 100644 index 000000000..c51fe4669 --- /dev/null +++ b/BackEnd/profiles/migrations/0022_alter_profile_name_alter_profile_official_name.py @@ -0,0 +1,22 @@ +# Generated by Django 4.2.3 on 2024-12-08 17:45 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("profiles", "0021_savedcompany_is_updated"), + ] + + operations = [ + migrations.AlterField( + model_name="profile", + name="name", + field=models.CharField(default=None, max_length=45, null=True), + ), + migrations.AlterField( + model_name="profile", + name="official_name", + field=models.CharField(default=None, max_length=255, null=True), + ), + ] diff --git a/BackEnd/profiles/models.py b/BackEnd/profiles/models.py index 6614f187c..216c516b2 100644 --- a/BackEnd/profiles/models.py +++ b/BackEnd/profiles/models.py @@ -40,7 +40,7 @@ class Profile(models.Model): id = models.AutoField(primary_key=True) - name = models.CharField(max_length=100, default=None, null=True) + name = models.CharField(max_length=45, default=None, null=True) is_registered = models.BooleanField(default=None, null=True) is_startup = models.BooleanField(default=None, null=True) is_fop = models.BooleanField(default=False) @@ -51,9 +51,7 @@ class Profile(models.Model): person = models.OneToOneField(CustomUser, on_delete=models.PROTECT) person_position = models.CharField(max_length=50, blank=True, default="") - official_name = models.CharField( - max_length=255, null=True, blank=True, default=None - ) + official_name = models.CharField(max_length=255, null=True, default=None) regions = models.ManyToManyField("Region", blank=True) common_info = models.TextField( diff --git a/BackEnd/profiles/serializers.py b/BackEnd/profiles/serializers.py index 078edaf14..d6f30e7f2 100644 --- a/BackEnd/profiles/serializers.py +++ b/BackEnd/profiles/serializers.py @@ -319,6 +319,7 @@ def validate(self, data): edrpou = data.get("edrpou", self.instance.edrpou) rnokpp = data.get("rnokpp", self.instance.rnokpp) is_fop = data.get("is_fop", self.instance.is_fop) + name = data.get("name", self.instance.name) if rnokpp and not is_fop: raise serializers.ValidationError( { @@ -331,11 +332,16 @@ def validate(self, data): "is_fop": "For the EDRPOU field filled out, FOP must be set to False" } ) + if name and len(name) > 45: + raise serializers.ValidationError( + {"name": "Ensure this field has no more than 45 characters."} + ) + return data # set optional unique fields to None if they are empty def to_internal_value(self, data): - fields_to_check = ["official_name", "edrpou", "rnokpp"] + fields_to_check = ["edrpou", "rnokpp"] for field in fields_to_check: if data.get(field) == "": data[field] = None diff --git a/BackEnd/profiles/tests/test_crud_profile.py b/BackEnd/profiles/tests/test_crud_profile.py index b0d7a63d3..9252c7be5 100644 --- a/BackEnd/profiles/tests/test_crud_profile.py +++ b/BackEnd/profiles/tests/test_crud_profile.py @@ -533,8 +533,11 @@ def test_partial_update_profile_official_name_empty_value(self): data={"official_name": ""}, format="json", ) - self.assertEqual(status.HTTP_200_OK, response.status_code) - self.assertIsNone(response.data.get("official_name")) + self.assertEqual(status.HTTP_400_BAD_REQUEST, response.status_code) + self.assertEqual( + response.json(), + {"official_name": ["This field may not be blank."]}, + ) def test_partial_update_profile_edrpou_empty_value(self): self.client.force_authenticate(self.user) @@ -563,11 +566,8 @@ def test_partial_update_profile_rnokpp_empty_value(self): self.assertIsNone(response.data.get("rnokpp")) # updating fields when another instance with empty fields already exists in db - def test_partial_update_profile_fields_with_empty_values( - self, - ): + def test_partial_update_profile_fields_with_empty_values(self): ProfileStartupFactory.create( - official_name=None, edrpou=None, ) self.client.force_authenticate(self.user) @@ -576,21 +576,16 @@ def test_partial_update_profile_fields_with_empty_values( path="/api/profiles/{profile_id}".format( profile_id=self.profile.id ), - data={"official_name": "", "edrpou": ""}, + data={"edrpou": ""}, format="json", ) self.assertEqual(status.HTTP_200_OK, response.status_code) + self.profile.refresh_from_db() + self.assertIsNone(self.profile.edrpou) + response = self.client.get(path="/api/profiles/") self.assertEqual(2, response.data["total_items"]) - self.assertTrue( - all( - [ - item.get("official_name") is None - for item in response.data["results"] - ] - ) - ) self.assertTrue( all( [ @@ -1154,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)