From cedb80f215464c9dd4b4c7c97ee50ba16c963dd7 Mon Sep 17 00:00:00 2001 From: NICHOLAS PRIAMBODO <nicholas.priambodo@ui.ac.id> Date: Sun, 6 Oct 2019 23:54:10 +0700 Subject: [PATCH] Merge branch 'master' into '1606879905-25' # Conflicts: # core/tests/test_vacancies.py --- core/serializers/vacancies.py | 6 +++ core/tests/test_vacancies.py | 90 +++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+) diff --git a/core/serializers/vacancies.py b/core/serializers/vacancies.py index 5938e5f9..d8b10827 100644 --- a/core/serializers/vacancies.py +++ b/core/serializers/vacancies.py @@ -35,9 +35,15 @@ class VacancySerializer(serializers.ModelSerializer): fields = ['company', 'verified', 'open_time', 'description', 'close_time', 'created', 'updated', 'name', \ 'status', 'bookmarked', 'id'] +def name_position_validator(names): + for name in names.split(" "): + if not name.isalpha(): + raise serializers.ValidationError("Name must alphabets only") + return name class PostVacancySerializer(serializers.ModelSerializer): company = serializers.PrimaryKeyRelatedField(queryset=Company.objects.all()) + name = serializers.CharField(validators=[name_position_validator], max_length=100, allow_null=False) class Meta: model = Vacancy diff --git a/core/tests/test_vacancies.py b/core/tests/test_vacancies.py index 5b382216..ef5126e4 100644 --- a/core/tests/test_vacancies.py +++ b/core/tests/test_vacancies.py @@ -523,6 +523,96 @@ class SupervisorApprovalTests(APITestCase): self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) self.assertEqual(new_vacancy.verified, False) +class ValidationPositionNameinCreateLowonganKP(APITestCase): + def setUp(self): + user = User.objects.create_user('dummy', 'dummy@dummy.com', 'dummy') + self.client.force_authenticate(user=user) + self.company = Company.objects.create( + user=user, + description="dummy", + status=Company.VERIFIED, + logo=None, + address=None + ) + self.url = '/api/vacancies/' + self.payload = { + "company" : self.company.id, + "open_time" : datetime.fromtimestamp(0), + "close_time" : datetime.today() + } + self.message = "Name must alphabets only" + + def test_name_contains_only_alphabets(self): + self.payload["name"] = "Engineer" + + response = self.client.post( + self.url, + self.payload, + format="json" + ) + + response_status_code = response.status_code + + self.assertEqual(response_status_code, 201) + + def test_name_contains_only_alphabets_and_spaces(self): + self.payload["name"] = "Software Engineer " + + response = self.client.post( + self.url, + self.payload, + format="json" + ) + + response_status_code = response.status_code + + self.assertEqual(response_status_code, 201) + + def test_name_contains_alphabets_and_numerics(self): + self.payload["name"] = "Software18 Engineer" + + response = self.client.post( + self.url, + self.payload, + format="json" + ) + + response_status_code = response.status_code + self.assertEqual(response_status_code, 400) + + response_message = response.data["name"][0] + self.assertEqual(response_message, self.message) + + def test_name_contains_only_numerics(self): + self.payload["name"] = "123654789" + + response = self.client.post( + self.url, + self.payload, + format="json" + ) + + response_status_code = response.status_code + self.assertEqual(response_status_code, 400) + + response_message = response.data["name"][0] + self.assertEqual(response_message, self.message) + + def test_name_contains_characters_besides_alphabets(self): + self.payload["name"] = "!@#$%^&*()" + + response = self.client.post( + self.url, + self.payload, + format="json" + ) + + response_status_code = response.status_code + self.assertEqual(response_status_code, 400) + + response_message = response.data["name"][0] + self.assertEqual(response_message, self.message) + class AcceptOneOfferTests(APITestCase): def generateObject(self): -- GitLab