From 7f538f56dbfde63592c7a07651a2f7a3f0f330ab Mon Sep 17 00:00:00 2001 From: arallaer Date: Wed, 22 May 2024 23:08:57 +0200 Subject: [PATCH 1/3] update testen --- api/tests/serializers/test_gebruiker.py | 30 +++++-------------------- api/tests/views/test_gebruiker.py | 28 ++++++++++------------- api/tests/views/test_restrictie.py | 2 +- 3 files changed, 18 insertions(+), 42 deletions(-) diff --git a/api/tests/serializers/test_gebruiker.py b/api/tests/serializers/test_gebruiker.py index 666ca433b..f6f926db7 100644 --- a/api/tests/serializers/test_gebruiker.py +++ b/api/tests/serializers/test_gebruiker.py @@ -54,48 +54,28 @@ def test_create(self): self.assertEqual(gebruiker.is_lesgever, data["is_lesgever"]) def test_update(self): - data = self.serializer.data - self.assertFalse(data["is_lesgever"]) - data["is_lesgever"] = True - serializer = GebruikerSerializer( - instance=self.gebruiker, data=data, partial=True - ) - self.assertTrue(serializer.is_valid()) - self.gebruiker = serializer.save() - self.assertTrue(self.gebruiker.is_lesgever) - - def test_update_invalid_teacher(self): vak = VakFactory.create() vak.studenten.add(self.gebruiker) data = self.serializer.data - self.assertFalse(data["is_lesgever"]) - data["is_lesgever"] = True + data["gepinde_vakken"] = [vak.vak_id] serializer = GebruikerSerializer( instance=self.gebruiker, data=data, partial=True ) self.assertTrue(serializer.is_valid()) - self.assertRaises(ValidationError, serializer.save, raise_exception=True) + self.gebruiker = serializer.save() + self.assertEqual(self.gebruiker.gepinde_vakken.first(), vak) - def test_update_invalid_student(self): + def test_update_invalid(self): self.gebruiker.is_lesgever = True vak = VakFactory.create() - vak.lesgevers.add(self.gebruiker) data = self.serializer.data - data["is_lesgever"] = False + data["gepinde_vakken"] = [vak.vak_id] serializer = GebruikerSerializer( instance=self.gebruiker, data=data, partial=True ) self.assertTrue(serializer.is_valid()) self.assertRaises(ValidationError, serializer.save, raise_exception=True) - def test_update_invalid_gepinde_vak(self): - data = self.serializer.data - data["gepinde_vakken"].append(VakFactory.create().vak_id) - serializer = GebruikerSerializer( - instance=self.gebruiker, data=data, partial=True - ) - self.assertTrue(serializer.is_valid()) - self.assertRaises(ValidationError, serializer.save, raise_exception=True) def test_validation_for_blank_items(self): serializer = GebruikerSerializer(data={"user": "", "is_lesgever": []}) diff --git a/api/tests/views/test_gebruiker.py b/api/tests/views/test_gebruiker.py index f1e8775c2..9b5239d28 100644 --- a/api/tests/views/test_gebruiker.py +++ b/api/tests/views/test_gebruiker.py @@ -2,6 +2,9 @@ from api.tests.factories.gebruiker import GebruikerFactory from django.urls import reverse from rest_framework import status +from api.tests.factories.vak import VakFactory +from api.models.gebruiker import Gebruiker +from django.db import transaction class GebruikerListViewTest(APITestCase): @@ -16,7 +19,7 @@ def test_gebruiker_list_get_as_student(self): self.client.force_login(self.student.user) response = self.client.get(self.url) self.assertEqual(response.status_code, status.HTTP_200_OK) - self.assertEqual(len(response.data), 1) + self.assertEqual(len(response.data), Gebruiker.objects.count()) def test_gebruiker_list_get_lesgevers(self): self.client.force_login(self.teacher.user) @@ -50,6 +53,9 @@ def setUp(self): self.gebruiker = GebruikerFactory.create() self.gebruiker.user.is_superuser = True self.gebruiker.user.save() + self.vak = VakFactory.create() + self.vak.studenten.add(self.gebruiker) + self.vak.save() self.client = APIClient() self.client.force_login(self.gebruiker.user) self.url = reverse("gebruiker_detail", kwargs={"id": self.gebruiker.user.id}) @@ -61,28 +67,18 @@ def test_gebruiker_detail_get(self): def test_gebruiker_detail_put(self): new_data = { "user": self.gebruiker.user.id, - "is_lesgever": not self.gebruiker.is_lesgever, - "gepinde_vakken": self.gebruiker.gepinde_vakken.all(), + "is_lesgever": self.gebruiker.is_lesgever, + "gepinde_vakken": [self.vak.vak_id], } response = self.client.put(self.url, new_data, format="json") self.assertEqual(response.status_code, status.HTTP_200_OK) self.gebruiker.refresh_from_db() - self.assertEqual(self.gebruiker.is_lesgever, new_data["is_lesgever"]) + self.assertEqual(self.gebruiker.gepinde_vakken.first(), self.vak) def test_gebruiker_detail_get_non_existing_user(self): response = self.client.get(reverse("gebruiker_detail", kwargs={"id": 69})) self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) - def test_gebruiker_detail_get_unauthorized(self): - student = GebruikerFactory.create(is_lesgever=False) - self.gebruiker.user.is_superuser = False - self.gebruiker.user.save() - self.gebruiker.is_lesgever = False - self.gebruiker.save() - self.client.force_login(student.user) - response = self.client.get(self.url) - self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) - def test_gebruiker_detail_put_unauthorized(self): student = GebruikerFactory.create(is_lesgever=False) self.client.force_login(student.user) @@ -103,12 +99,12 @@ def test_gebruiker_detail_put_bad_request(self): def test_gebruiker_detail_patch(self): new_data = { - "is_lesgever": not self.gebruiker.is_lesgever, + "gepinde_vakken": [], } response = self.client.patch(self.url, new_data, format="json") self.assertEqual(response.status_code, status.HTTP_200_OK) self.gebruiker.refresh_from_db() - self.assertEqual(self.gebruiker.is_lesgever, new_data["is_lesgever"]) + self.assertEqual(self.gebruiker.gepinde_vakken.all().count(), 0) class GebruikerDetailMeViewTest(APITestCase): diff --git a/api/tests/views/test_restrictie.py b/api/tests/views/test_restrictie.py index 1cad164f5..eb7f24ef2 100644 --- a/api/tests/views/test_restrictie.py +++ b/api/tests/views/test_restrictie.py @@ -43,7 +43,7 @@ def test_restrictie_list_get_as_student(self): student = GebruikerFactory.create(is_lesgever=False) self.client.force_login(student.user) response = self.client.get(self.url) - self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) + self.assertEqual(response.status_code, status.HTTP_200_OK) def test_restrictie_list_post(self): project = ProjectFactory.create() From 269f9fde600fa9d5a1566230b3e9d95691855c7f Mon Sep 17 00:00:00 2001 From: arallaer Date: Wed, 22 May 2024 23:11:46 +0200 Subject: [PATCH 2/3] linting --- api/tests/serializers/test_gebruiker.py | 1 - 1 file changed, 1 deletion(-) diff --git a/api/tests/serializers/test_gebruiker.py b/api/tests/serializers/test_gebruiker.py index f6f926db7..4f68d28e6 100644 --- a/api/tests/serializers/test_gebruiker.py +++ b/api/tests/serializers/test_gebruiker.py @@ -76,7 +76,6 @@ def test_update_invalid(self): self.assertTrue(serializer.is_valid()) self.assertRaises(ValidationError, serializer.save, raise_exception=True) - def test_validation_for_blank_items(self): serializer = GebruikerSerializer(data={"user": "", "is_lesgever": []}) self.assertRaises(ValidationError, serializer.is_valid, raise_exception=True) From 284705d9780d352e08b9df6c997b38abb6917f44 Mon Sep 17 00:00:00 2001 From: arallaer Date: Wed, 22 May 2024 23:12:53 +0200 Subject: [PATCH 3/3] linting --- api/tests/views/test_gebruiker.py | 1 - 1 file changed, 1 deletion(-) diff --git a/api/tests/views/test_gebruiker.py b/api/tests/views/test_gebruiker.py index 9b5239d28..2c0f89ad6 100644 --- a/api/tests/views/test_gebruiker.py +++ b/api/tests/views/test_gebruiker.py @@ -4,7 +4,6 @@ from rest_framework import status from api.tests.factories.vak import VakFactory from api.models.gebruiker import Gebruiker -from django.db import transaction class GebruikerListViewTest(APITestCase):