From f505fae3e60d7292b6bc8b21ac62c46cd7fb6bb0 Mon Sep 17 00:00:00 2001 From: Aarni Halinen Date: Mon, 15 Jun 2020 20:25:24 +0300 Subject: [PATCH] Test signup POST --- webapp/models.py | 17 ++++++------ webapp/tests/signup_fixture.py | 2 +- webapp/tests/test_signup.py | 51 ++++++++++++++++++++++++++++++++-- webapp/views.py | 2 -- 4 files changed, 59 insertions(+), 13 deletions(-) diff --git a/webapp/models.py b/webapp/models.py index f60e026..3715bb8 100644 --- a/webapp/models.py +++ b/webapp/models.py @@ -103,12 +103,7 @@ class SignupForm(models.Model): @property def schema(self): questions = self.questions - ids = list(map(lambda x: x["id"], questions)) - properties = { - "required": ids, - "minProperties": len(ids), - "maxProperties": len(ids) - } + properties = {} for q in questions: id = q["id"] @@ -143,8 +138,14 @@ class SignupForm(models.Model): } else: raise Exception("invalid question type!") - - return properties + ids = list(map(lambda x: x["id"], questions)) + return { + "type": "object", + "required": ids, + "minProperties": len(ids), + "maxProperties": len(ids), + "properties": properties + } class Meta: verbose_name = _('Signup form') diff --git a/webapp/tests/signup_fixture.py b/webapp/tests/signup_fixture.py index a036274..5a71a9a 100644 --- a/webapp/tests/signup_fixture.py +++ b/webapp/tests/signup_fixture.py @@ -10,7 +10,7 @@ ALL_QUESTION_TYPES = [ {"id": "i10d426d5", "name": "Asd3", "type": "checkbox", "options": ["A", "B", "C"]} ] -ALL_QUESTION_TYPES_ANSWER = {"j5CeRZDvl": "Testi", "RHJhSoaLD": "maybe", "i10d426d5": ["B"]} +ALL_QUESTION_TYPES_ANSWER = {"j5CeRZDvl": "Testi", "RHJhSoaLD": "maybe", "i10d426d5": ["B","C"]} def createSignupForm(name="Form1", start_time=timezone.now(), end_time=month_from_now(), questions=ALL_QUESTION_TYPES, visible=True): diff --git a/webapp/tests/test_signup.py b/webapp/tests/test_signup.py index 845aa99..025284d 100644 --- a/webapp/tests/test_signup.py +++ b/webapp/tests/test_signup.py @@ -17,6 +17,9 @@ class SignupTestCase(APITestCase): def setUp(self): self.signupForm = createSignupForm() + self.signupFormText = createSignupForm(name="Form2", questions=[ALL_QUESTION_TYPES[0]]) + self.signupFormRadio = createSignupForm(name="Form3", questions=[ALL_QUESTION_TYPES[1]]) + self.signupFormCheck = createSignupForm(name="Form4", questions=[ALL_QUESTION_TYPES[2]]) self.hiddenForm = createSignupForm(visible=False) self.signup1 = createSignupObject(self.signupForm, ALL_QUESTION_TYPES) @@ -77,11 +80,55 @@ class SignupTestCase(APITestCase): pass def test_create_malformed_answer(self): - malformed = createSignupJSON(self.signupForm.id, []) - response = self.client.post(URL, malformed, format="json") + # Empty body + response = self.client.post(URL, createSignupJSON(self.signupForm.id, {}), format="json") self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) + + # Array + response = self.client.post(URL, createSignupJSON(self.signupForm.id, []), format="json") + self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) + + # Extra ids + testInput = ALL_QUESTION_TYPES_ANSWER.copy() + testInput["newId"] = "Oon extraa" + response = self.client.post(URL, createSignupJSON(self.signupForm.id, testInput), format="json") + self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) + + # Bad id + response = self.client.post(URL, createSignupJSON(self.signupFormText.id, { "malformed": "TekstiƤ" }), format="json") + self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) + + # Wrong data type for text + response = self.client.post(URL, createSignupJSON(self.signupFormText.id, {"j5CeRZDvl": 123}), format="json") + self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) + + # Wrong data for checkbox + response = self.client.post(URL, createSignupJSON(self.signupFormCheck.id, { + "i10d426d5": ["D"] + }), format="json") + self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) + + # Wrong data type for checkbox + response = self.client.post(URL, createSignupJSON(self.signupFormCheck.id, { + "i10d426d5": {"j5CeRZDvl": { "asd": "123" }} + }), format="json") + self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) + + # Wrong data for radiobutton + response = self.client.post(URL, createSignupJSON(self.signupFormRadio.id, { + "RHJhSoaLD": [] + }), format="json") + self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) + + # Wrong data type for radiobutton + response = self.client.post(URL, createSignupJSON(self.signupFormRadio.id, { + "RHJhSoaLD": { "asd": "123" } + }), format="json") + self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) + self.assertEqual(Signup.objects.count(), 2) + # Update and Delete are available for super admin (Django Admin) # and to the user that signed up (uid token) @skip("NotImplemented") diff --git a/webapp/views.py b/webapp/views.py index 136bc82..72fcef4 100644 --- a/webapp/views.py +++ b/webapp/views.py @@ -86,11 +86,9 @@ class SignupViewSet(viewsets.ModelViewSet): if (form.visible): signup = json.loads(request.data["answer"]) # Throws error if not valid - print(form.schema) validate(instance=signup, schema=form.schema) return super().create(request, *args, **kwargs) except Exception as inst: - print(inst) return HttpResponseBadRequest() else: return HttpResponseBadRequest()