Fix webapp tests

This commit is contained in:
Aarni Halinen
2019-11-11 00:18:48 +02:00
parent d17e6710e6
commit f7cf54cab2
4 changed files with 36 additions and 16 deletions
+8 -2
View File
@@ -2,10 +2,16 @@ from webapp.models import Signup, SignupForm
from django.utils import timezone
from webapp.utils import month_from_now
import json
ALL_QUESTION_TYPES = [
{"id": "j5CeRZDvl", "name": "Asd", "type": "text", "options": []}, {"id": "RHJhSoaLD", "name": "Asd2", "type": "radiobutton", "options": ["Yes", "no", "maybe"]}, {"id": "i10d426d5", "name": "Asd3", "type": "checkbox", "options": ["A", "B", "C"]}
{"id": "j5CeRZDvl", "name": "Asd", "type": "text", "options": []},
{"id": "RHJhSoaLD", "name": "Asd2", "type": "radiobutton", "options": ["Yes", "no", "maybe"]},
{"id": "i10d426d5", "name": "Asd3", "type": "checkbox", "options": ["A", "B", "C"]}
]
ALL_QUESTION_TYPES_ANSWER = {"j5CeRZDvl": "Testi", "RHJhSoaLD": "maybe", "i10d426d5": ["B"]}
def createSignupForm(name="Form1", start_time=timezone.now(), end_time=month_from_now(), questions=ALL_QUESTION_TYPES, visible=True):
return SignupForm.objects.create(
@@ -27,5 +33,5 @@ def createSignupObject(form, answer):
def createSignupJSON(form_id, answer):
return {
"signupForm_id": form_id,
"answer": answer
"answer": json.dumps(answer)
}
+4 -3
View File
@@ -44,17 +44,18 @@ class FeedTestCase(APITestCase):
"title_fi": "testtitle",
"title_en": "testtitle",
"visible": "True",
"description_fi": "liirumlaarum", "description_en": "liirumlaarum",
"description_fi": "liirumlaarum",
"description_en": "liirumlaarum",
"content_fi": "lorem ipsum",
"content_en": "lorem ipsum"
}
# Try post without authentication
response = self.client.post("/api/feed/", data, format="multipart")
response = self.client.post("/api/feed/", data, format="json")
self.assertTrue(response.status_code, status.HTTP_401_UNAUTHORIZED)
self.assertEqual(Feed.objects.count(), 1)
# Authenticate
self.client.force_authenticate(user=self.authClient)
response = self.client.post("/api/feed/", data, format="multipart")
response = self.client.post("/api/feed/", data, format="json")
# Return success and check object was created
self.assertTrue(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(Feed.objects.count(), 2)
+20 -9
View File
@@ -1,4 +1,5 @@
from django.test import TestCase
from unittest import skip
from django.contrib.auth.models import User
from rest_framework import status
from rest_framework.test import APITestCase, force_authenticate
@@ -6,7 +7,8 @@ from rest_framework.test import APITestCase, force_authenticate
from webapp.serializers import SignupSerializer, SignupFormSerializer
from webapp.models import Signup
from webapp.tests.event_fixture import createEventObject
from webapp.tests.signup_fixture import createSignupForm, createSignupObject, createSignupJSON, ALL_QUESTION_TYPES
from webapp.tests.signup_fixture import createSignupForm, createSignupObject, createSignupJSON, ALL_QUESTION_TYPES, ALL_QUESTION_TYPES_ANSWER
URL = "/api/signup/"
@@ -17,7 +19,7 @@ class SignupTestCase(APITestCase):
self.signupForm = createSignupForm()
self.hiddenForm = createSignupForm(visible=False)
self.signup1 = createSignupObject(self.signupForm, [])
self.signup1 = createSignupObject(self.signupForm, ALL_QUESTION_TYPES)
self.signup2 = createSignupObject(self.signupForm, [])
username, password = "test_admin", "password123"
@@ -54,27 +56,36 @@ class SignupTestCase(APITestCase):
self.assertEqual(response.data, expected.data)
def test_create_signup(self):
new = createSignupJSON(self.signup1.id, [])
new = createSignupJSON(self.signupForm.id, ALL_QUESTION_TYPES_ANSWER)
response = self.client.post(URL, new, format="json")
print(response.data)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(Signup.objects.count(), 3)
def test_create_signup_404_or_hidden(self):
new = createSignupJSON(3001, [])
new = createSignupJSON(3001, ALL_QUESTION_TYPES_ANSWER)
response = self.client.post(URL, new, format="json")
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(response.data["signupForm_id"][0].code, "does_not_exist")
self.assertEqual(Signup.objects.count(), 2)
new = createSignupJSON(self.hiddenForm.id, ALL_QUESTION_TYPES_ANSWER)
response = self.client.post(URL, new, format="json")
print(response.data)
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
self.assertEqual(Signup.objects.count(), 2)
new = createSignupJSON(self.hiddenForm, [])
response = self.client.post(URL, new, format="json")
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
self.assertEqual(Signup.objects.count(), 2)
@skip("NotImplemented")
def test_create_malformed_answer(self):
response = self.client.post(URL, createSignupJSON(self.signupForm.id, []), format="json")
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
# Update and Delete are available for super admin (Django Admin)
# and to the user that signed up (uid token)
@skip("NotImplemented")
def test_update_signup_token(self):
pass
@skip("NotImplemented")
def test_delete_signup_token(self):
pass
+4 -2
View File
@@ -6,6 +6,7 @@ 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):
@@ -35,7 +36,7 @@ class TemplateQuestionCase(APITestCase):
def test_post(self):
new = {
"name": "testi3",
"questions": []
"question": json.dumps(ALL_QUESTION_TYPES)
}
response = self.client.post("/api/questions/", new, format="json")
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
@@ -50,7 +51,7 @@ class TemplateQuestionCase(APITestCase):
def test_update(self):
new = {
"name": "uusi testi2",
"questions": []
"question": 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)
@@ -59,6 +60,7 @@ class TemplateQuestionCase(APITestCase):
# 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(