diff --git a/assets/js/components/ApplyModal.jsx b/assets/js/components/ApplyModal.jsx index fbd6d622165bdd08a52a8031dc28f8bc655a8342..acb63cccbe4984c0d5965b09cffb58aec91d7fda 100644 --- a/assets/js/components/ApplyModal.jsx +++ b/assets/js/components/ApplyModal.jsx @@ -1,4 +1,5 @@ import React from 'react'; +import moment from 'moment'; import { Icon, Modal, Button, TextArea, Form, Message } from 'semantic-ui-react'; import ModalAlert from './../components/ModalAlert'; import Server from './../lib/Server'; @@ -65,6 +66,7 @@ export default class ApplyModal extends React.Component { }; render() { + const isApplicationClosed = moment(this.props.data.close_time) < moment.now() return ( {this.props.buttonTitle}} @@ -104,7 +106,7 @@ export default class ApplyModal extends React.Component { )} - diff --git a/assets/js/components/Vacancy.jsx b/assets/js/components/Vacancy.jsx index 283bc462efff0e79a0d6d2c8d6a4fc3cec3d2181..3180a4cece546bf5c9a65212cadda2cb738aedf3 100644 --- a/assets/js/components/Vacancy.jsx +++ b/assets/js/components/Vacancy.jsx @@ -89,6 +89,7 @@ export default class Vacancy extends React.Component { header: this.props.data.name, description: this.props.data.description, id: this.props.data.id, + close_time: this.props.data.close_time }} resume={this.props.user.data.student.resume} buttonTitle="Detail" diff --git a/core/tests/test_vacancies.py b/core/tests/test_vacancies.py index ef5126e4342ac660dc9617680cd14254f00fa815..0d0e1c3a9764fabad515ed575f2622d37bdbbd76 100644 --- a/core/tests/test_vacancies.py +++ b/core/tests/test_vacancies.py @@ -1,6 +1,5 @@ -from datetime import datetime +from datetime import datetime, timedelta from django.utils import timezone - import json import requests_mock from django.contrib.auth.models import User @@ -74,7 +73,7 @@ class ApplicationTests(APITestCase): 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", close_time=timezone.now()) + description="lalala", 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.'}, @@ -85,6 +84,41 @@ class ApplicationTests(APITestCase): response = self.client.delete(url) self.assertEqual(response.status_code, status.HTTP_200_OK) + @requests_mock.Mocker() + def test_cannot_create_application_if_vacancy_is_closed(self, m): + m.get('https://akun.cs.ui.ac.id/oauth/token/verify/?client_id=X3zNkFmepkdA47ASNMDZRX3Z9gqSU1Lwywu5WepG', json={"username": 'dummy.mahasiswa', "role": 'mahasiswa', "identity_number": '1234567890'}, status_code=200) + m.post('https://api.cs.ui.ac.id/authentication/ldap/v2/', json={ + "username": "dummy.mahasiswa", + "nama": "Dummy Mahasiswa", + "state": 1, + "kode_org": "01.00.12.01:mahasiswa", + "kodeidentitas": "1234567890", + "nama_role": "mahasiswa" + }, status_code=200) + m.get('https://api.cs.ui.ac.id/siakngcs/mahasiswa/1234567890?client_id=X3zNkFmepkdA47ASNMDZRX3Z9gqSU1Lwywu5WepG', json={ + "kota_lahir": "kota_kota", + "tgl_lahir": "2017-12-31", + "program": [{ + "nm_org": "Ilmu Informasi", + "angkatan": "2017" + }] + }, status_code=200) + + url = '/api/login/' + + response = self.client.post(url, {'username': 'dummy.mahasiswa', 'password': 'lalala', 'login-type': 'sso-ui'}, + format='json') + student_id = response.data.get('student').get('id') + + 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(0), + description="lalala", 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) class BookmarkApplicationTests(APITestCase): @requests_mock.Mocker() diff --git a/core/views/vacancies.py b/core/views/vacancies.py index a81a1be893cbd47680abe113d674b02fb232991d..18d772792ec383f9ad0b72d1abedf34d48458ac3 100644 --- a/core/views/vacancies.py +++ b/core/views/vacancies.py @@ -1,4 +1,5 @@ import requests +from django.utils import timezone from django.conf import settings from rest_framework import viewsets, status from rest_framework.decorators import detail_route @@ -194,6 +195,9 @@ class StudentApplicationViewSet(viewsets.GenericViewSet): """ cover_letter = request.data.get('cover_letter') vacancy = get_object_or_404(Vacancy.objects.all(), pk=request.data.get('vacancy_id')) + if vacancy.close_time < timezone.now(): + return Response({"error": "vacancy is closed"}, \ + status=status.HTTP_400_BAD_REQUEST) student = get_object_or_404(Student.objects.all(), pk=student_id) if Application.objects.filter(vacancy=vacancy, student=student).exists(): raise ValidationError("You have already applied for the vacancy")