Fakultas Ilmu Komputer UI

tests.py 51.7 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
        self.view = RevisiMateriView
        self.template_name = "revisi.html"

        self.contributor_credential = {
            "email": "kontributor@gov.id",
            "password": id_generator()
        }
        self.contributor = get_user_model().objects.create_user(
            **self.contributor_credential, name="Kontributor", is_contributor=True
        )
        self.cover = SimpleUploadedFile(
            "cover.jpg",
            b"Test file"
        )
        self.content = SimpleUploadedFile(
            "content.txt",
            b"Test file"
        )
        Materi(title="Materi 1", author="Agas", uploader=self.contributor,
               publisher="Kelas SC", descriptions="Deskripsi Materi 1",
               status="REVISION", cover=self.cover, content=self.content).save()

        self.materi1 = Materi.objects.first()
        self.url = "/revisi/materi/" + str(self.materi1.id) + "/"

    def test_revisi_materi_view(self):
        found = resolve(self.url)
        self.assertEqual(found.func.__name__, self.view.as_view().__name__)

    def test_revisi_materi_template(self):
        # Login
        self.client.login(**self.contributor_credential)
        # Test
        response = self.client.get(self.url)
        self.assertTemplateUsed(response, self.template_name)
        # Logout
        self.client.logout()

    def test_revisi_materi_url(self):
        # Login
        self.client.login(**self.contributor_credential)
        # Test
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 200)
        # Logout
        self.client.logout()

    def test_revisi_materi_access(self):
        # Kontributor
        # Login
        self.client.login(**self.contributor_credential)
        # Test
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 200)
        # Logout
1056
1057
1058
        self.client.logout()


1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
class GenerateDummyCommandTest(TestCase):

    def setUp(self):
        self.material_numbers = [5, 10, 25, 100]
        self.invalid_material_numbers = [-100, -10, -1, 0, 1, 2, 3, 4]
        self.stdout = StringIO()

    def test_command_output_with_given_num_of_materi(self):
        for num_of_materi in self.material_numbers:
            call_command("generatedummy", num_of_materi, stdout=self.stdout)
            self.assertIn(
                f"Successfully created {num_of_materi} materi\n",
                self.stdout.getvalue()
            )

    def test_command_should_generate_materi_objects(self):
        for num_of_materi in self.material_numbers:
            call_command("generatedummy", num_of_materi, stdout=self.stdout)
            self.assertEqual(Materi.objects.count(), num_of_materi)
            Materi.objects.all().delete()

    def test_command_should_raise_exception_if_invalid_values_are_given(self):
        with self.assertRaises(IndexError):
            for num_of_materi in self.invalid_material_numbers:
                call_command("generatedummy", num_of_materi)


class RemoveDummyCommandTest(TestCase):

    def test_calling_remove_dummy_command_should_remove_generated_dummy_objects(self):
        stdout = StringIO()
        call_command("generatedummy", 50)

        call_command("removedummy", stdout=stdout)

        self.assertEqual("Successfully remove all dummy object\n", stdout.getvalue())
        self.assertEqual(User.objects.count(), 0)
        self.assertEqual(Category.objects.count(), 0)
        self.assertEqual(Materi.objects.count(), 0)
        self.assertEqual(VerificationSetting.objects.count(), 0)
        self.assertEqual(VerificationReport.objects.count(), 0)


1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
class RatingMateriTest(TestCase):
    def setUp(self):
        self.contributor_credential = {
            "email": "kontributor@gov.id",
            "password": id_generator()
        }
        self.user_one_credential = {
            "email": "user_one@user.id",
            "password": id_generator()
        }
        self.user_two_credential = {
            "email": "user_two@user.id",
            "password": id_generator()
        }
        self.contributor = get_user_model().objects.create_user(
            **self.contributor_credential, name="Kontributor", is_contributor=True
        )
        self.user_one = get_user_model().objects.create_user(**self.user_one_credential, name="User One")
        self.user_two = get_user_model().objects.create_user(**self.user_two_credential, name="User Two")
        self.cover = SimpleUploadedFile(
            "cover.jpg",
            b"Test file"
        )
        self.content = SimpleUploadedFile(
            "content.txt",
            b"Test file"
        )
        Materi(title="Materi 1", author="Agas", uploader=self.contributor,
               publisher="Kelas SC", descriptions="Deskripsi Materi 1",
               status="PENDING", cover=self.cover, content=self.content).save()
        Materi(title="Materi Dua", author="Author", uploader=self.contributor,
               publisher="Publisher", descriptions="Deskripsi Materi Dua",
               status="APPROVE", cover=self.cover, content=self.content).save()
        self.materi1 = Materi.objects.all()[0]
        self.materi2 = Materi.objects.all()[1]
1137
1138
        self.url_rate = '/materi/rate/'
        self.url_materi = '/materi/{}/'.format(self.materi1.id)
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194

    def test_rating_model_can_be_created_with_proper_parameter(self):
        Rating(materi=self.materi1, user=self.user_one, score=5).save()
        rating = Rating.objects.first()
        self.assertEqual(rating.materi, self.materi1)
        self.assertEqual(rating.user, self.user_one)
        self.assertTrue(0 < rating.score < 6)
        self.assertEqual(rating.__str__(), "Material:Materi 1 | User:User One | Rating:5")

    def test_rating_model_should_not_be_created_with_rating_more_than_5(self):
        with self.assertRaises(ValidationError) as context:
            Rating(materi=self.materi1, user=self.user_one, score=6).save()
        self.assertTrue('Rating score must be integer between 1-5' in str(context.exception))

    def test_rating_model_should_not_be_created_with_rating_less_than_1(self):
        with self.assertRaises(ValidationError) as context:
            Rating(materi=self.materi1, user=self.user_one, score=0).save()
        self.assertTrue('Rating score must be integer between 1-5' in str(context.exception))

    def test_one_materi_should_be_able_to_be_related_to_multiple_rating(self):
        Rating(materi=self.materi1, user=self.user_one, score=1).save()
        Rating(materi=self.materi1, user=self.user_two, score=2).save()
        rating_one = Rating.objects.get(user=self.user_one)
        rating_two = Rating.objects.get(user=self.user_two)
        self.assertEqual(rating_one.materi, self.materi1)
        self.assertEqual(rating_two.materi, self.materi1)
        self.assertEqual(rating_one.user, self.user_one)
        self.assertEqual(rating_two.user, self.user_two)

    def test_one_user_should_be_able_to_be_related_to_two_rating(self):
        Rating(materi=self.materi1, user=self.user_one, score=3).save()
        Rating(materi=self.materi2, user=self.user_one, score=3).save()
        rating_one = Rating.objects.filter(materi=self.materi1).first()
        rating_two = Rating.objects.filter(materi=self.materi2).first()
        self.assertEqual(rating_one.materi, self.materi1)
        self.assertEqual(rating_two.materi, self.materi2)
        self.assertEqual(rating_one.user, self.user_one)
        self.assertEqual(rating_two.user, self.user_one)

    def test_two_rating_should_not_have_same_user_and_materi(self):
        with self.assertRaises(IntegrityError) as context:
            Rating(materi=self.materi1, user=self.user_one, score=1).save()
            Rating(materi=self.materi1, user=self.user_one, score=2).save()
        self.assertTrue('already exists' in str(context.exception))

    def test_materi_in_rating_should_not_be_null(self):
        with self.assertRaises(IntegrityError):
            Rating(user=self.user_one, score=1).save()

    def test_user_in_rating_should_not_be_null(self):
        with self.assertRaises(IntegrityError):
            Rating(materi=self.materi1, score=1).save()

    def test_score_in_rating_should_not_be_null(self):
        with self.assertRaises(TypeError):
            Rating(materi=self.materi1, user=self.user_one).save()
1195

1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
    def test_rating_materi_url_use_add_rating_materi_function(self):
        found = resolve(self.url_rate)
        self.assertEqual(found.func, add_rating_materi)

    def test_rating_materi_get_method_should_return_403_forbidden(self):
        response = self.client.get(self.url_rate)
        response_json = json.loads(response.content)
        self.assertEqual(response_json.get("success", None), False)
        self.assertEqual(response_json.get("msg", None), "Forbidden")
        self.assertEqual(response.status_code, 403)

    def test_rating_materi_post_not_authenticated_should_return_403_forbidden(self):
        response = self.client.post(self.url_rate, {'materi_id': 1, 'rating_score': 5})
        response_json = json.loads(response.content)
        self.assertEqual(response_json.get("success", None), False)
        self.assertEqual(response_json.get("msg", None), "Forbidden")
        self.assertEqual(response.status_code, 403)

    def test_rating_materi_not_authenticated_post_wrong_param_should_return_403_forbidden(self):
        for data in [{}, {'materi_id': 1}, {'rating_score': 1}, {'rating_score': 'STRING', 'materi_id': 'STRING'}]:
            response = self.client.post(self.url_rate, data)
            response_json = json.loads(response.content)
            self.assertEqual(response_json.get("success", None), False)
            self.assertEqual(response_json.get("msg", None), "Forbidden")
            self.assertEqual(response.status_code, 403)

    def test_rating_materi_authenticated_post_missing_param(self):
        self.client.login(**self.user_one_credential)
        for data in [{'rating_score': 1}, {'materi_id': 1}, {}]:
            response = self.client.post(self.url_rate, data)
            response_json = json.loads(response.content)
            self.assertEqual(response_json.get("success", None), False)
            self.assertEqual(response_json.get("msg", None), "Missing param")
            self.assertEqual(response.status_code, 422)

    def test_rating_materi_authenticated_materi_id_doesnt_exist_should_return_422(self):
        self.client.login(**self.user_one_credential)
        response = self.client.post(self.url_rate, {'materi_id': 123456, 'rating_score': 5})
        response_json = json.loads(response.content)
        self.assertEqual(response_json.get("success", None), False)
        self.assertEqual(response_json.get("msg", None), "Materi does not exist")
        self.assertEqual(response.status_code, 422)

    def test_rating_materi_authenticated_param_wrong_data_type_should_return_422(self):
        self.client.login(**self.user_one_credential)
        response = self.client.post(self.url_rate, {'materi_id': "STRING", 'rating_score': 5})
        response_json = json.loads(response.content)
        self.assertEqual(response_json.get("success", None), False)
        self.assertEqual(response_json.get("msg", None), "materi_id must be an integer")
        self.assertEqual(response.status_code, 422)

        response = self.client.post(self.url_rate, {'materi_id': 1, 'rating_score': "STRING"})
        response_json = json.loads(response.content)
        self.assertEqual(response_json.get("success", None), False)
        self.assertEqual(response_json.get("msg", None), "rating_score must be an integer")
        self.assertEqual(response.status_code, 422)

    def test_rating_score_should_be_between_1_and_5(self):
        self.client.login(**self.user_one_credential)
        for i in range(1, 6):
            Rating.objects.all().delete()
            response = self.client.post(self.url_rate, {'materi_id': self.materi1.id, 'rating_score': i})
            response_json = json.loads(response.content)
            # self.assertEqual(response_json.get("success", None), True)
            self.assertEqual(response_json.get("msg", None), "Rating successfully created")
            self.assertEqual(response.status_code, 201)

        for i in [-100, -7, -6, -1, 0, 6, 7, 100]:
            Rating.objects.all().delete()
            response = self.client.post(self.url_rate, {'materi_id': self.materi1.id, 'rating_score': i})
            response_json = json.loads(response.content)
            # self.assertEqual(response_json.get("success", None), False)
            self.assertEqual(response_json.get("msg", None), "Rating must be an integer from 1 to 5")
            self.assertEqual(response.status_code, 422)

    def test_user_should_not_able_to_rate_materi_twice(self):
        self.client.login(**self.user_one_credential)
        Rating.objects.all().delete()
        self.client.post(self.url_rate, {'materi_id': self.materi1.id, 'rating_score': 1})
        response = self.client.post(self.url_rate, {'materi_id': self.materi1.id, 'rating_score': 2})
        response_json = json.loads(response.content)
        # self.assertEqual(response_json.get("success", None), False)
        self.assertEqual(response_json.get("msg", None), "Rating already exist")
        self.assertEqual(response.status_code, 409)

    def test_user_authenticated_visit_unrated_should_get_0_materi_rating_score_context(self):
        self.client.login(**self.user_one_credential)
        response = self.client.get(self.url_materi)
        self.assertEqual(0, response.context.get('materi_rating_score'))

    def test_user_not_authenticated_visit_unrated_should_get_0_materi_rating_score_context(self):
        response = self.client.get(self.url_materi)
        self.assertEqual(0, response.context.get('materi_rating_score'))

    def test_user_authenticated_visit_rated_should_get_correct_materi_rating_score_context(self):
        self.client.login(**self.user_one_credential)
        Rating(materi=self.materi1, user=self.user_one, score=1).save()
        response = self.client.get(self.url_materi)
        self.assertEqual(1, response.context.get('materi_rating_score'))
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321

class fileManagementUtilTest(TestCase):
    def setUp(self):
        self.filename = "image_with_exif_data.gif"
        self.file_content = open(settings.BASE_DIR + "/app/test_files/" +
                self.filename, "rb").read()

    def test_get_random_filename_isCorrect(self):
        generated_name = get_random_filename(self.filename)

        self.assertTrue(generated_name != self.filename)
        # 40 from 36 expected name length + 4 from extension
        self.assertEqual(len(generated_name), 40)
        self.assertTrue(generated_name[-4:] == ".gif")

    def test_remove_image_exifdata_isCorrect(self):
        with tempfile.TemporaryDirectory() as d:
            image_with_exif_data_path = os.path.join(d, self.filename)
            img = open(image_with_exif_data_path, "wb")
            img.write(self.file_content)
            img.close()

            remove_image_exifdata(image_with_exif_data_path)
            sanitized_img = open(image_with_exif_data_path, "rb").read()

            self.assertTrue(len(sanitized_img) < len(self.file_content))
            self.assertTrue(b'<exif:' not in sanitized_img)
For faster browsing, not all history is shown. View entire blame