From a9d6bf50d68e1ca698f0f1ecbbb30d45e5a08194 Mon Sep 17 00:00:00 2001 From: Aarni Halinen Date: Sun, 10 Nov 2019 20:00:13 +0200 Subject: [PATCH] TemplateQuestion Tests --- webapp/tests/test_templateQuestions.py | 81 ++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 webapp/tests/test_templateQuestions.py diff --git a/webapp/tests/test_templateQuestions.py b/webapp/tests/test_templateQuestions.py new file mode 100644 index 0000000..8cfa4fd --- /dev/null +++ b/webapp/tests/test_templateQuestions.py @@ -0,0 +1,81 @@ +from django.test import TestCase +from django.contrib.auth.models import User +from rest_framework import status +from rest_framework.test import APITestCase, force_authenticate + +from webapp.models import TemplateQuestion +from webapp.serializers import SavedQuestionsSerializer + +ALL_QUESTION_TYPES = [ + +] + + +class TemplateQuestionCase(APITestCase): + def setUp(self): + self.questions = [ + TemplateQuestion.objects.create( + name="Testi1", + question=ALL_QUESTION_TYPES + ), + TemplateQuestion.objects.create( + name="Testi2", + question=ALL_QUESTION_TYPES + ) + ] + + username, password = "test_admin", "password123" + self.authClient = User.objects.create_superuser(username, "myemail@test.com", password) + + def test_get(self): + response = self.client.get("/api/questions/", format="json") + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertEqual(response.data["results"], SavedQuestionsSerializer(self.questions, many=True).data) + response = self.client.get(f"/api/questions/{self.questions[0].id}/", format="json") + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertEqual(response.data, SavedQuestionsSerializer(self.questions[0]).data) + + def test_post(self): + new = { + "name": "testi3", + "questions": [] + } + response = self.client.post("/api/questions/", new, format="json") + self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) + self.assertEqual(TemplateQuestion.objects.count(), 2) + + # Authenticate + self.client.force_authenticate(user=self.authClient) + response = self.client.post("/api/questions/", new, format="json") + self.assertEqual(response.status_code, status.HTTP_201_CREATED) + self.assertEqual(TemplateQuestion.objects.count(), 3) + + def test_update(self): + new = { + "name": "uusi testi2", + "questions": [] + } + response = self.client.put(f"/api/questions/{self.questions[0].id}/", new, format="json") + self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) + self.assertEqual(TemplateQuestion.objects.count(), 2) + + # Authenticate + self.client.force_authenticate(user=self.authClient) + response = self.client.put(f"/api/questions/{self.questions[0].id}/", new, format="json") + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertEqual(TemplateQuestion.objects.count(), 2) + self.assertEqual( + TemplateQuestion.objects.get(id=self.questions[0].id).name, + "uusi testi2" + ) + + def test_delete(self): + response = self.client.delete(f"/api/questions/{self.questions[0].id}/", format="json") + self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) + self.assertEqual(TemplateQuestion.objects.count(), 2) + + # Authenticate + self.client.force_authenticate(user=self.authClient) + response = self.client.delete(f"/api/questions/{self.questions[0].id}/", format="json") + self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) + self.assertEqual(TemplateQuestion.objects.count(), 1)