TemplateQuestion Tests

This commit is contained in:
Aarni Halinen
2019-11-10 20:00:13 +02:00
parent 8f0c9feecf
commit a9d6bf50d6
+81
View File
@@ -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)