-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c68fd3a
commit de06a51
Showing
13 changed files
with
277 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import factory | ||
from factory.django import DjangoModelFactory | ||
from api.models.restrictie import Restrictie | ||
from api.tests.factories.project import ProjectFactory | ||
|
||
|
||
class RestrictieFactory(DjangoModelFactory): | ||
class Meta: | ||
model = Restrictie | ||
|
||
project = factory.SubFactory(ProjectFactory) | ||
script = factory.django.FileField(filename="test_script.sh") | ||
moet_slagen = factory.Faker("boolean") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from django.test import TestCase | ||
from api.tests.factories.restrictie import RestrictieFactory | ||
from api.models.restrictie import upload_to | ||
|
||
|
||
class RestrictieModelTest(TestCase): | ||
def setUp(self): | ||
self.restrictie = RestrictieFactory.create() | ||
|
||
def test_project(self): | ||
self.assertIsNotNone(self.restrictie.project) | ||
|
||
def test_script(self): | ||
self.assertIsNotNone(self.restrictie.script) | ||
|
||
def test_moet_slagen(self): | ||
self.assertIsNotNone(self.restrictie.moet_slagen) | ||
|
||
def test_str_method(self): | ||
expected_str = ( | ||
self.restrictie.project.titel | ||
+ ", restrictie: " | ||
+ str(self.restrictie.script) | ||
) | ||
self.assertEqual(str(self.restrictie), expected_str) | ||
|
||
def test_upload_to(self): | ||
project = self.restrictie.project | ||
filename = "test_script.txt" | ||
expected_path = f"data/restricties/project_{project.project_id}/{filename}" | ||
self.assertEqual(upload_to(self.restrictie, filename), expected_path) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
from django.test import TestCase | ||
from rest_framework.exceptions import ValidationError | ||
from api.tests.factories.restrictie import RestrictieFactory | ||
from api.serializers.restrictie import RestrictieSerializer | ||
from django.core.files.uploadedfile import SimpleUploadedFile | ||
|
||
|
||
class RestrictieSerializerTest(TestCase): | ||
def setUp(self): | ||
self.restrictie = RestrictieFactory.create() | ||
self.serializer = RestrictieSerializer(instance=self.restrictie) | ||
|
||
def test_contains_expected_fields(self): | ||
data = self.serializer.data | ||
self.assertCountEqual( | ||
data.keys(), ["restrictie_id", "project", "script", "moet_slagen"] | ||
) | ||
|
||
def test_restrictie_id_field_content(self): | ||
data = self.serializer.data | ||
self.assertEqual(data["restrictie_id"], self.restrictie.restrictie_id) | ||
|
||
def test_project_field_content(self): | ||
data = self.serializer.data | ||
self.assertEqual(data["project"], self.restrictie.project.project_id) | ||
|
||
def test_script_field_content(self): | ||
data = self.serializer.data | ||
path = self.restrictie.script.path.split("/") | ||
self.assertEqual(data["script"], "/" + "/".join(path[-4:])) | ||
|
||
def test_moet_slagen_field_content(self): | ||
data = self.serializer.data | ||
self.assertEqual(data["moet_slagen"], self.restrictie.moet_slagen) | ||
|
||
def test_create(self): | ||
data = { | ||
"project": self.restrictie.project.project_id, | ||
"script": SimpleUploadedFile("script.sh", b"file_content"), | ||
"moet_slagen": False, | ||
} | ||
serializer = RestrictieSerializer(data=data) | ||
self.assertTrue(serializer.is_valid()) | ||
restrictie = serializer.save() | ||
self.assertEqual(restrictie.project.project_id, data["project"]) | ||
path = restrictie.script.path.split("/") | ||
filename = path[-1] | ||
filename = filename.split("_")[0] | ||
filename = filename.split(".")[0] + ".sh" | ||
self.assertEqual( | ||
"/".join(path[-4:-1]) + "/" + filename, | ||
f"data/restricties/project_{restrictie.project.project_id}/" | ||
+ str(data["script"]), | ||
) | ||
self.assertEqual(restrictie.moet_slagen, data["moet_slagen"]) | ||
|
||
def test_update(self): | ||
data = self.serializer.data | ||
data["script"] = SimpleUploadedFile(data["script"], b"file_content") | ||
data["moet_slagen"] = not data["moet_slagen"] | ||
serializer = RestrictieSerializer( | ||
instance=self.restrictie, data=data, partial=True | ||
) | ||
self.assertTrue(serializer.is_valid()) | ||
self.restrictie = serializer.save() | ||
self.assertEqual(self.restrictie.moet_slagen, data["moet_slagen"]) | ||
|
||
def test_validation_for_blank_items(self): | ||
serializer = RestrictieSerializer( | ||
data={"project": "", "script": "", "moet_slagen": ""} | ||
) | ||
self.assertRaises(ValidationError, serializer.is_valid, raise_exception=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
from django.urls import reverse | ||
from rest_framework.test import APITestCase, APIClient | ||
from api.tests.factories.restrictie import RestrictieFactory | ||
from api.tests.factories.project import ProjectFactory | ||
from django.core.files.uploadedfile import SimpleUploadedFile | ||
from rest_framework import status | ||
from api.tests.factories.gebruiker import GebruikerFactory | ||
|
||
|
||
class RestrictieListViewTest(APITestCase): | ||
def setUp(self): | ||
self.client = APIClient() | ||
self.restrictie = RestrictieFactory.create(moet_slagen=True) | ||
self.url = reverse("restrictie_list") | ||
self.teacher = GebruikerFactory.create(is_lesgever=True) | ||
self.client.force_login(self.teacher.user) | ||
|
||
def test_restrictie_list_get(self): | ||
response = self.client.get(self.url) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(len(response.data), 1) | ||
|
||
def test_restrictie_list_get_project(self): | ||
response = self.client.get( | ||
self.url, {"project": self.restrictie.project.project_id} | ||
) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(len(response.data), 1) | ||
|
||
def test_restrictie_list_get_moet_slagen(self): | ||
response = self.client.get(self.url, {"moet_slagen": "true"}) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(len(response.data), 1) | ||
response = self.client.get(self.url, {"moet_slagen": "false"}) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(len(response.data), 0) | ||
|
||
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) | ||
|
||
def test_restrictie_list_post(self): | ||
project = ProjectFactory.create() | ||
data = { | ||
"project": project.project_id, | ||
"script": SimpleUploadedFile("nieuw_script.sh", b"file_content"), | ||
"moet_slagen": False, | ||
} | ||
response = self.client.post(self.url, data, format="multipart") | ||
self.assertEqual(response.status_code, status.HTTP_201_CREATED) | ||
|
||
|
||
class RestrictieDetailViewTest(APITestCase): | ||
def setUp(self): | ||
self.client = APIClient() | ||
self.restrictie = RestrictieFactory.create() | ||
self.url = reverse( | ||
"restrictie_detail", kwargs={"id": self.restrictie.restrictie_id} | ||
) | ||
self.teacher = GebruikerFactory.create(is_lesgever=True) | ||
self.client.force_login(self.teacher.user) | ||
|
||
def test_restrictie_detail_get(self): | ||
response = self.client.get(self.url) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(response.data["restrictie_id"], self.restrictie.restrictie_id) | ||
|
||
def test_restrictie_detail_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) | ||
|
||
def test_restrictie_put(self): | ||
new_data = { | ||
"restrictie_id": self.restrictie.restrictie_id, | ||
"project": self.restrictie.project.project_id, | ||
"script": SimpleUploadedFile(self.restrictie.script.name, b"file_content"), | ||
"moet_slagen": not self.restrictie.moet_slagen, | ||
} | ||
response = self.client.put(self.url, new_data, format="multipart") | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(response.data["moet_slagen"], not self.restrictie.moet_slagen) | ||
|
||
def test_restrictie_put_as_student(self): | ||
student = GebruikerFactory.create(is_lesgever=False) | ||
self.client.force_login(student.user) | ||
new_data = { | ||
"restrictie_id": self.restrictie.restrictie_id, | ||
"project": self.restrictie.project.project_id, | ||
"script": SimpleUploadedFile(self.restrictie.script.name, b"file_content"), | ||
"moet_slagen": not self.restrictie.moet_slagen, | ||
} | ||
response = self.client.put(self.url, new_data, format="multipart") | ||
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) | ||
|
||
def test_restrictie_delete(self): | ||
response = self.client.delete(self.url) | ||
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) | ||
|
||
def test_restrictie_delete_as_student(self): | ||
student = GebruikerFactory.create(is_lesgever=False) | ||
self.client.force_login(student.user) | ||
response = self.client.delete(self.url) | ||
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) |
Oops, something went wrong.