Skip to content

Commit

Permalink
Merge pull request #309 from SELab-2/backend_extras
Browse files Browse the repository at this point in the history
update testen
  • Loading branch information
sPAICEcake authored May 22, 2024
2 parents 163b913 + 284705d commit c5cfb65
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 43 deletions.
31 changes: 5 additions & 26 deletions api/tests/serializers/test_gebruiker.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,43 +54,22 @@ 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
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)
data["gepinde_vakken"] = [vak.vak_id]
serializer = GebruikerSerializer(
instance=self.gebruiker, data=data, partial=True
)
Expand Down
27 changes: 11 additions & 16 deletions api/tests/views/test_gebruiker.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
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


class GebruikerListViewTest(APITestCase):
Expand All @@ -16,7 +18,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)
Expand Down Expand Up @@ -50,6 +52,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})
Expand All @@ -61,28 +66,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)
Expand All @@ -103,12 +98,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):
Expand Down
2 changes: 1 addition & 1 deletion api/tests/views/test_restrictie.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit c5cfb65

Please sign in to comment.