88 lines
3.4 KiB
Python
88 lines
3.4 KiB
Python
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
|
|
from webapp.tests.signup_fixture import ALL_QUESTION_TYPES
|
|
import json
|
|
|
|
|
|
class TemplateQuestionCase(APITestCase):
|
|
def setUp(self):
|
|
self.questions = [
|
|
TemplateQuestion.objects.create(
|
|
name="Testi1", questions=ALL_QUESTION_TYPES
|
|
),
|
|
TemplateQuestion.objects.create(
|
|
name="Testi2", questions=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": json.dumps(ALL_QUESTION_TYPES)}
|
|
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": json.dumps({})}
|
|
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):
|
|
id = self.questions[0].id
|
|
response = self.client.delete(f"/api/questions/{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_200_OK)
|
|
self.assertEqual(TemplateQuestion.objects.get(id=id).deleted, True)
|