Fakultas Ilmu Komputer UI

test_vacancies.py 58.8 KB
Newer Older
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
        self.assertEqual(self.vacancy.milestones.count(), 1)
        new_milestone = self.vacancy.milestones.first()
        self.assertEqual(new_milestone.name, milestone.name)
        self.assertEqual(new_milestone.detail, milestone.detail)

    def test_modify_milestone_on_a_vacancy_invalid_data(self):
        milestone = self.create_milestone_object()
        self.client.force_authenticate(user=self.company_user)
        url = '/api/vacancies/' + str(self.vacancy.pk) + '/milestones/' + str(milestone.pk) + '/'
        data = {"name": "a" * 101, "detail": "install all things", "expected_start": "2019-01-21",
                "expected_finish": "2019-01-19"}
        response = self.client.put(url, data, format='json')
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
        self.assertEqual(self.vacancy.milestones.count(), 1)
        new_milestone = self.vacancy.milestones.first()
        self.assertEqual(new_milestone.name, milestone.name)
        self.assertEqual(new_milestone.detail, milestone.detail)

    def test_modify_milestone_on_a_vacancy_unauthorized_user(self):
        milestone = self.create_milestone_object()
        self.client.force_authenticate(user=self.user)
        url = '/api/vacancies/' + str(self.vacancy.pk) + '/milestones/' + str(milestone.pk) + '/'
        data = {"name": "initiate env", "detail": "install all things", "expected_start": "2019-01-20",
                "expected_finish": "2019-01-21"}
        response = self.client.put(url, data, format='json')
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
        self.assertEqual(self.vacancy.milestones.count(), 1)
        new_milestone = self.vacancy.milestones.first()
        self.assertEqual(new_milestone.name, milestone.name)
        self.assertEqual(new_milestone.detail, milestone.detail)

    def test_delete_milestone_on_a_vacancy_success(self):
        milestone = self.create_milestone_object()
        self.client.force_authenticate(user=self.company_user)
        url = '/api/vacancies/' + str(self.vacancy.pk) + '/milestones/' + str(milestone.pk) + '/'
        response = self.client.delete(url, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(self.vacancy.milestones.count(), 0)

    def test_delete_milestone_on_a_vacancy_not_found(self):
        milestone = self.create_milestone_object()
        self.client.force_authenticate(user=self.company_user)
        url = '/api/vacancies/1000/milestones/' + str(milestone.pk) + '/'
        response = self.client.delete(url, format='json')
        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
        url2 = '/api/vacancies/' + str(self.vacancy.pk) + '/milestones/1000/'
        response2 = self.client.delete(url2, format='json')
        self.assertEqual(response2.status_code, status.HTTP_404_NOT_FOUND)
        self.assertEqual(self.vacancy.milestones.count(), 1)

    def test_delete_milestone_on_a_vacancy_unauthorized_user(self):
        milestone = self.create_milestone_object()
        self.client.force_authenticate(user=self.user)
        url = '/api/vacancies/' + str(self.vacancy.pk) + '/milestones/' + str(milestone.pk) + '/'
        response = self.client.delete(url, format='json')
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
        self.assertEqual(self.vacancy.milestones.count(), 1)

1059

1060
class AcceptOneOfferTests(APITestCase):
1061
1062
1063
1064
    def generateObject(self):
        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)
1065

1066
1067
        new_user2 = User.objects.create_user('dummy.company2', 'dummy.company2@company.com', 'lalala123')
        new_company2 = Company.objects.create(user=new_user2, description="lalala", status=Company.VERIFIED, logo=None,
1068
                                              address=None)
1069
1070

        new_vacancy = Vacancy.objects.create(company=new_company, verified=True, open_time=datetime.fromtimestamp(0),
1071
                                             description="lalala", requirements= "requirements", close_time=datetime.today(), max_accepted_applicants=3)
1072

1073
        new_vacancy2 = Vacancy.objects.create(company=new_company2, verified=True, open_time=datetime.fromtimestamp(0),
1074
                                             description="lalala", requirements= "requirements", close_time=datetime.today(), max_accepted_applicants=3)
1075
1076
1077
1078
1079
1080
1081

        new_user3 = User.objects.create_user('dummy.student', 'dummy.student@company.com', 'lalala123')
        new_student = Student.objects.create(user=new_user3, npm=1234123412)

        return new_user3, new_vacancy, new_vacancy2, new_student

    def test_number_of_content_response_object_given_id_auth(self):
1082

1083
        new_user3, new_vacancy, new_vacancy2, new_student = self.generateObject()
1084

1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
        self.client.force_authenticate(new_user3)

        Application.objects.create(student=new_student, vacancy=new_vacancy, cover_letter="asdasdasd")
        Application.objects.create(student=new_student, vacancy=new_vacancy2, cover_letter="asdasdasd")

        url = '/api/acceptoffer/' + str(new_student.pk) + '/vacancy/' + str(new_vacancy.pk) + '/'

        response = self.client.patch(url, format='json')
        body = json.loads(response.content)

        status_response = []
        for app in body:
1097
            status_response.append(app['status'])
1098
1099
1100
1101
1102
1103
1104
1105

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertTrue(len(body) >= 2)
        self.assertFalse(len(body) == 0)
        self.assertTrue('new' in status_response)
        self.assertTrue('aborted' in status_response)

    def test_student_not_exist_given_auth(self):
1106
        new_user3, new_vacancy, new_vacancy2, new_student = self.generateObject()
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118

        self.client.force_authenticate(new_user3)

        user4 = User.objects.create_user('student_user_4', 'student_user_4@company.com', 'lalala123')
        other_student = Student.objects.create(user=user4, npm=1098765432)

        Application.objects.create(student=other_student, vacancy=new_vacancy, cover_letter="asdasdasd")
        Application.objects.create(student=other_student, vacancy=new_vacancy2, cover_letter="asdasdasd")

        url = '/api/acceptoffer/' + str(new_student.pk) + '/vacancy/' + str(new_vacancy.pk) + '/'

        response = self.client.patch(url, format='json')
1119
        body = json.loads(response.content)
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertTrue(len(body) == 0)

    def test_type_error_if_input_null(self):
        new_user3, new_vacancy, new_vacancy2, new_student = self.generateObject()

        self.client.force_authenticate(new_user3)

        Application.objects.create(student=new_student, vacancy=new_vacancy, cover_letter="asdasdasd")
        Application.objects.create(student=new_student, vacancy=new_vacancy2, cover_letter="asdasdasd")

        with self.assertRaises(TypeError):
1133
            '/api/acceptoffer/' + None + '/vacancy/' + str(new_vacancy.pk) + '/'
1134

1135
        with self.assertRaises(TypeError):
1136
            '/api/acceptoffer/' + str(new_student.pk) + '/vacancy/' + None + '/'
1137

1138
1139
    def test_if_requester_is_not_authenticated(self):
        new_user3, new_vacancy, new_vacancy2, new_student = self.generateObject()
1140

1141
1142
1143
1144
1145
1146
        Application.objects.create(student=new_student, vacancy=new_vacancy, cover_letter="asdasdasd")
        Application.objects.create(student=new_student, vacancy=new_vacancy2, cover_letter="asdasdasd")

        url = '/api/acceptoffer/' + str(new_student.pk) + '/vacancy/' + str(new_vacancy.pk) + '/'

        response = self.client.patch(url, format='json')
1147
        
1148
1149

        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
For faster browsing, not all history is shown. View entire blame