Fakultas Ilmu Komputer UI

Commit b4fac668 authored by NICHOLAS PRIAMBODO's avatar NICHOLAS PRIAMBODO
Browse files

1606879905-232 "Membuat Validasi Minimum Panjang Field cover_letter pada Backend"

parent 2094103d
......@@ -13,7 +13,7 @@ from core.models.vacancies import Vacancy, Application, VacancyMilestone
from core.serializers.vacancies import VacancySerializer
from core.tests.mocks import mock_csui_oauth_verify, mock_csui_ldap_student, mock_csui_siak_student
from core.views.vacancies import date_validator
from core.views import views_constants
class ApplicationTests(APITestCase):
def get_student_id(self, mock_obj):
......@@ -55,7 +55,7 @@ class ApplicationTests(APITestCase):
close_time=(timezone.now() + timedelta(days=1)))
url = '/api/students/' + str(student_id) + '/applied-vacancies/'
response = self.client.post(url, {'vacancy_id': new_vacancy.pk, 'cover_letter': 'this is a cover letter.'},
response = self.client.post(url, {'vacancy_id': new_vacancy.pk, 'cover_letter': 'this is a cover letter. ' * 50},
format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK)
......@@ -63,6 +63,51 @@ class ApplicationTests(APITestCase):
response = self.client.delete(url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
@requests_mock.Mocker()
def test_application_cover_letter_validation_error(self, m):
student_id = self.get_student_id(m)
new_user = User.objects.create_user(
'dummy.company',
'dummy.company@company.com',
'lalala123'
)
new_company = Company.objects.create(
user=new_user,
description="lalala",
status=Company.VERIFIED,
logo=None,
address=None
)
new_vacancy = Vacancy.objects.create(
company=new_company,
verified=True,
open_time=datetime.fromtimestamp(1541319300.0),
description="lalala",
max_accepted_applicants=3,
working_period="3 Bulan",
requirements="requirements",
close_time=(timezone.now() + timedelta(days=1))
)
url = '/api/students/' + str(student_id) + '/applied-vacancies/'
response = self.client.post(
url,
{
'vacancy_id': new_vacancy.pk,
'cover_letter': 'this is a cover letter.'
},
format='json'
)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
message_error = response.data[0]
self.assertEqual(message_error, views_constants.ERROR_WORDS_COUNT)
def test_count_application_a_person_is_already_register(self):
new_user = User.objects.create_user('dummy.company', 'dummy.company@company.com', 'lalala123')
new_user2 = User.objects.create_user('dummy.student', 'dummy.student@student.com', 'lalala123')
......
......@@ -325,6 +325,10 @@ class StudentApplicationViewSet(viewsets.GenericViewSet):
paramType: body
"""
cover_letter = request.data.get(views_constants.COVER_LETTER)
words_count = len(cover_letter.split())
if words_count < 250:
raise ValidationError(views_constants.ERROR_WORDS_COUNT)
vacancy = get_object_or_404(Vacancy.objects.all(), pk=request.data.get(views_constants.VACANCY_ID))
if vacancy.close_time < timezone.now():
return Response({views_constants.ERROR: views_constants.ERROR_VACANCY_CLOSED},
......
......@@ -14,6 +14,7 @@ ERROR_VACANCY_CLOSED = 'vacancy is closed'
ERROR_INVALID_STATUS_CODE = 'status must be an integer between 0 and 4'
ERROR_FORBIDDEN = 'forbidden'
ERROR_BAD_REQUEST = 'bad request'
ERROR_WORDS_COUNT = 'Cover letter must minimum 250 words!'
ACTION_LIST = 'list'
ACTION_PARTIAL_UPDATE = 'partial_update'
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment