format files with black
This commit is contained in:
@@ -3,7 +3,14 @@ from webapp.models import Event
|
||||
from webapp.utils import month_from_now
|
||||
|
||||
|
||||
def createEventObject(name="Testitapahtuma1", visible=True, start_time=timezone.now(), end_time=month_from_now(), tag_id=[], signup_id=[]):
|
||||
def createEventObject(
|
||||
name="Testitapahtuma1",
|
||||
visible=True,
|
||||
start_time=timezone.now(),
|
||||
end_time=month_from_now(),
|
||||
tag_id=[],
|
||||
signup_id=[],
|
||||
):
|
||||
return Event.objects.create(
|
||||
title_fi=name,
|
||||
title_en=f"title_en {name}",
|
||||
@@ -14,11 +21,18 @@ def createEventObject(name="Testitapahtuma1", visible=True, start_time=timezone.
|
||||
content_en=f"content_en {name}",
|
||||
start_time=start_time,
|
||||
end_time=end_time,
|
||||
location=f"loc {name}"
|
||||
location=f"loc {name}",
|
||||
)
|
||||
|
||||
|
||||
def createEventJSON(name="POST1", visible=True, start_time=timezone.now(), end_time=month_from_now(), tag_id=[], signup_id=[]):
|
||||
def createEventJSON(
|
||||
name="POST1",
|
||||
visible=True,
|
||||
start_time=timezone.now(),
|
||||
end_time=month_from_now(),
|
||||
tag_id=[],
|
||||
signup_id=[],
|
||||
):
|
||||
return {
|
||||
"tag_id": tag_id,
|
||||
"visible": visible,
|
||||
@@ -31,5 +45,5 @@ def createEventJSON(name="POST1", visible=True, start_time=timezone.now(), end_t
|
||||
"start_time": start_time,
|
||||
"end_time": end_time,
|
||||
"location": f"loc {name}",
|
||||
"signup_id": signup_id
|
||||
"signup_id": signup_id,
|
||||
}
|
||||
|
||||
@@ -2,45 +2,91 @@ from webapp.models import Signup, SignupForm
|
||||
from django.utils import timezone
|
||||
from webapp.utils import month_from_now
|
||||
|
||||
ALL_QUESTION_TYPES = [{"id": "-naY2R1-h", "name": "Nimi", "type": "text", "options": []}, {"id": "5t1oN2Qev", "name": "Testi", "type": "info", "options": "teskstii"}, {"id": "MYaaAiOiU", "name": "Email", "type": "email", "options": []}, {"id": "Wb5tmyvki", "name": "Radio", "type": "radiobutton", "options": ["Yes", "no", "maybe"]}, {"id": "U41Zp9x0L", "name": "Checkbox", "type": "checkbox", "options": ["A", "B", "C"]}, {"id": "TnDqrvXKf", "name": "Numero", "type": "integer", "options": ["1", "100"]}]
|
||||
ALL_QUESTION_TYPES = [
|
||||
{"id": "-naY2R1-h", "name": "Nimi", "type": "text", "options": []},
|
||||
{"id": "5t1oN2Qev", "name": "Testi", "type": "info", "options": "teskstii"},
|
||||
{"id": "MYaaAiOiU", "name": "Email", "type": "email", "options": []},
|
||||
{
|
||||
"id": "Wb5tmyvki",
|
||||
"name": "Radio",
|
||||
"type": "radiobutton",
|
||||
"options": ["Yes", "no", "maybe"],
|
||||
},
|
||||
{
|
||||
"id": "U41Zp9x0L",
|
||||
"name": "Checkbox",
|
||||
"type": "checkbox",
|
||||
"options": ["A", "B", "C"],
|
||||
},
|
||||
{"id": "TnDqrvXKf", "name": "Numero", "type": "integer", "options": ["1", "100"]},
|
||||
]
|
||||
|
||||
ALL_QUESTION_TYPES_ANSWER = {"-naY2R1-h": "Testi", "MYaaAiOiU": "test-spam@sahkoinsinoorikilta.fi", "Wb5tmyvki": "maybe", "U41Zp9x0L": ["B", "C"], "TnDqrvXKf": 5}
|
||||
ALL_QUESTION_TYPES_ANSWER = {
|
||||
"-naY2R1-h": "Testi",
|
||||
"MYaaAiOiU": "test-spam@sahkoinsinoorikilta.fi",
|
||||
"Wb5tmyvki": "maybe",
|
||||
"U41Zp9x0L": ["B", "C"],
|
||||
"TnDqrvXKf": 5,
|
||||
}
|
||||
|
||||
|
||||
ALL_QUESTIONS_SCHEMA = {
|
||||
"type": "object",
|
||||
"required": ["-naY2R1-h", "MYaaAiOiU", "Wb5tmyvki", "U41Zp9x0L", "TnDqrvXKf"],
|
||||
"properties": {"-naY2R1-h": {"type": "string"}, "MYaaAiOiU": {"type": "string", "format": "email", "pattern": "^[a-zA-Z0-9.!#$%&\u2019*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\\.[a-zA-Z0-9-]+)*$"}, "TnDqrvXKf": {"type": "number", "title": "Numero (1 -- 100)", "maximum": 100, "minimum": 1, "multipleOf": 1}, "U41Zp9x0L": {"type": "array", "items": {"type": "string", "pattern": "^A$|^B$|^C$"}, "maxItems": 3, "uniqueItems": True}, "Wb5tmyvki": {"type": "string", "pattern": "^Yes$|^no$|^maybe$"}},
|
||||
"minProperties": 5
|
||||
"properties": {
|
||||
"-naY2R1-h": {"type": "string"},
|
||||
"MYaaAiOiU": {
|
||||
"type": "string",
|
||||
"format": "email",
|
||||
"pattern": "^[a-zA-Z0-9.!#$%&\u2019*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\\.[a-zA-Z0-9-]+)*$",
|
||||
},
|
||||
"TnDqrvXKf": {
|
||||
"type": "number",
|
||||
"title": "Numero (1 -- 100)",
|
||||
"maximum": 100,
|
||||
"minimum": 1,
|
||||
"multipleOf": 1,
|
||||
},
|
||||
"U41Zp9x0L": {
|
||||
"type": "array",
|
||||
"items": {"type": "string", "pattern": "^A$|^B$|^C$"},
|
||||
"maxItems": 3,
|
||||
"uniqueItems": True,
|
||||
},
|
||||
"Wb5tmyvki": {"type": "string", "pattern": "^Yes$|^no$|^maybe$"},
|
||||
},
|
||||
"minProperties": 5,
|
||||
}
|
||||
|
||||
TEXT_SCHEMA = {
|
||||
"type": "object",
|
||||
"required": ["-naY2R1-h"],
|
||||
"properties": {
|
||||
"-naY2R1-h": ALL_QUESTIONS_SCHEMA["properties"]["-naY2R1-h"]
|
||||
},
|
||||
"minProperties": 1
|
||||
"properties": {"-naY2R1-h": ALL_QUESTIONS_SCHEMA["properties"]["-naY2R1-h"]},
|
||||
"minProperties": 1,
|
||||
}
|
||||
RADIO_SCHEMA = {
|
||||
"type": "object",
|
||||
"required": ["Wb5tmyvki"],
|
||||
"properties": {
|
||||
"Wb5tmyvki": ALL_QUESTIONS_SCHEMA["properties"]["Wb5tmyvki"]
|
||||
},
|
||||
"minProperties": 1
|
||||
"properties": {"Wb5tmyvki": ALL_QUESTIONS_SCHEMA["properties"]["Wb5tmyvki"]},
|
||||
"minProperties": 1,
|
||||
}
|
||||
CBOX_SCHEMA = {
|
||||
"type": "object",
|
||||
"required": ["U41Zp9x0L"],
|
||||
"properties": {
|
||||
"U41Zp9x0L": ALL_QUESTIONS_SCHEMA["properties"]["U41Zp9x0L"]
|
||||
},
|
||||
"minProperties": 1
|
||||
"properties": {"U41Zp9x0L": ALL_QUESTIONS_SCHEMA["properties"]["U41Zp9x0L"]},
|
||||
"minProperties": 1,
|
||||
}
|
||||
|
||||
|
||||
def createSignupForm(name="Form1", start_time=timezone.now(), end_time=month_from_now(), questions=ALL_QUESTION_TYPES, schema=ALL_QUESTIONS_SCHEMA, visible=True, quota=1):
|
||||
def createSignupForm(
|
||||
name="Form1",
|
||||
start_time=timezone.now(),
|
||||
end_time=month_from_now(),
|
||||
questions=ALL_QUESTION_TYPES,
|
||||
schema=ALL_QUESTIONS_SCHEMA,
|
||||
visible=True,
|
||||
quota=1,
|
||||
):
|
||||
return SignupForm.objects.create(
|
||||
title=name,
|
||||
start_time=start_time,
|
||||
@@ -48,21 +94,13 @@ def createSignupForm(name="Form1", start_time=timezone.now(), end_time=month_fro
|
||||
questions=questions,
|
||||
visible=visible,
|
||||
schema=schema,
|
||||
quota=quota
|
||||
quota=quota,
|
||||
)
|
||||
|
||||
|
||||
def createSignupObject(list_name, form, answer):
|
||||
return Signup.objects.create(
|
||||
list_name=list_name,
|
||||
signupForm=form,
|
||||
answer=answer
|
||||
)
|
||||
return Signup.objects.create(list_name=list_name, signupForm=form, answer=answer)
|
||||
|
||||
|
||||
def createSignupRequest(list_name, form_id, answer):
|
||||
return {
|
||||
"list_name": list_name,
|
||||
"signupForm_id": form_id,
|
||||
"answer": answer
|
||||
}
|
||||
return {"list_name": list_name, "signupForm_id": form_id, "answer": answer}
|
||||
|
||||
@@ -8,8 +8,5 @@ def createTagIcon():
|
||||
|
||||
def tagBuilder(slug="Tag1", icon=createTagIcon()):
|
||||
return Tag.objects.create(
|
||||
slug=slug,
|
||||
name_fi=slug + " name_fi",
|
||||
name_en=slug + " name_en",
|
||||
icon=icon
|
||||
slug=slug, name_fi=slug + " name_fi", name_en=slug + " name_en", icon=icon
|
||||
)
|
||||
|
||||
+26
-26
@@ -13,28 +13,30 @@ URL = "/api/events/"
|
||||
|
||||
|
||||
class EventTestCase(APITestCase):
|
||||
|
||||
def setUp(self):
|
||||
# Visible and relevant
|
||||
test1 = createEventObject(
|
||||
"Testitapahtuma1",
|
||||
start_time=timezone.datetime(2019, 11, 9, 12, 0, 0))
|
||||
"Testitapahtuma1", start_time=timezone.datetime(2019, 11, 9, 12, 0, 0)
|
||||
)
|
||||
# Invisible but relevant
|
||||
createEventObject(
|
||||
"Testitapahtuma2",
|
||||
visible=False,
|
||||
start_time=timezone.datetime(2018, 11, 9, 12, 0, 0))
|
||||
start_time=timezone.datetime(2018, 11, 9, 12, 0, 0),
|
||||
)
|
||||
# Visible but unrelevant
|
||||
test2 = createEventObject(
|
||||
"Testitapahtuma3",
|
||||
visible=True,
|
||||
start_time=timezone.datetime(2018, 12, 9, 12, 0, 0),
|
||||
end_time=timezone.datetime(2018, 12, 9, 13, 0, 0))
|
||||
end_time=timezone.datetime(2018, 12, 9, 13, 0, 0),
|
||||
)
|
||||
# Visible and relevant
|
||||
createEventObject(
|
||||
"Testitapahtuma4",
|
||||
visible=True,
|
||||
start_time=timezone.datetime(2018, 12, 9, 12, 0, 0))
|
||||
start_time=timezone.datetime(2018, 12, 9, 12, 0, 0),
|
||||
)
|
||||
# Add some tags
|
||||
tag1 = tagBuilder()
|
||||
tag2 = tagBuilder("testtag2")
|
||||
@@ -47,7 +49,9 @@ class EventTestCase(APITestCase):
|
||||
self.signupFormId = createSignupForm().id
|
||||
|
||||
username, password = "test_admin", "password123"
|
||||
self.authClient = User.objects.create_superuser(username, "myemail@test.com", password)
|
||||
self.authClient = User.objects.create_superuser(
|
||||
username, "myemail@test.com", password
|
||||
)
|
||||
|
||||
def test_get_current_events(self):
|
||||
# Get from API
|
||||
@@ -60,11 +64,11 @@ class EventTestCase(APITestCase):
|
||||
req = APIRequestFactory().get(r"http://testserver/api/events/")
|
||||
req.user = AnonymousUser()
|
||||
serializer = EventSerializer(
|
||||
Event.objects.filter(title_fi__in=("Testitapahtuma1", "Testitapahtuma4")).order_by("start_time"),
|
||||
Event.objects.filter(
|
||||
title_fi__in=("Testitapahtuma1", "Testitapahtuma4")
|
||||
).order_by("start_time"),
|
||||
many=True,
|
||||
context={
|
||||
"request": req
|
||||
}
|
||||
context={"request": req},
|
||||
)
|
||||
expected = serializer.data
|
||||
# TODO: Couldn't figure out how to fill filtered_signup_forms used by prefetch for the test...
|
||||
@@ -80,11 +84,11 @@ class EventTestCase(APITestCase):
|
||||
req = APIRequestFactory().get(r"http://testserver/api/events/")
|
||||
req.user = AnonymousUser()
|
||||
serializer = EventSerializer(
|
||||
Event.objects.filter(title_fi__in=("Testitapahtuma1", "Testitapahtuma3", "Testitapahtuma4")).order_by("start_time"),
|
||||
Event.objects.filter(
|
||||
title_fi__in=("Testitapahtuma1", "Testitapahtuma3", "Testitapahtuma4")
|
||||
).order_by("start_time"),
|
||||
many=True,
|
||||
context={
|
||||
"request": req
|
||||
}
|
||||
context={"request": req},
|
||||
)
|
||||
expected = serializer.data
|
||||
# TODO: Couldn't figure out how to fill filtered_signup_forms used by prefetch for the test...
|
||||
@@ -119,7 +123,7 @@ class EventTestCase(APITestCase):
|
||||
response = self.client.post(
|
||||
URL,
|
||||
createEventJSON(tag_id=[self.testTagId], signup_id=[self.signupFormId]),
|
||||
format="json"
|
||||
format="json",
|
||||
)
|
||||
|
||||
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
|
||||
@@ -129,7 +133,7 @@ class EventTestCase(APITestCase):
|
||||
response = self.client.post(
|
||||
URL,
|
||||
createEventJSON(tag_id=[self.testTagId], signup_id=[self.signupFormId]),
|
||||
format="json"
|
||||
format="json",
|
||||
)
|
||||
|
||||
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
|
||||
@@ -140,11 +144,7 @@ class EventTestCase(APITestCase):
|
||||
self.client.force_authenticate(user=self.authClient)
|
||||
event = Event.objects.get(id=self.testEventId)
|
||||
new = createEventJSON(name="Update1", signup_id=[self.signupFormId])
|
||||
response = self.client.put(
|
||||
f"{URL}{self.testEventId}/",
|
||||
new,
|
||||
format="json"
|
||||
)
|
||||
response = self.client.put(f"{URL}{self.testEventId}/", new, format="json")
|
||||
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
event = Event.objects.get(id=self.testEventId)
|
||||
@@ -153,9 +153,7 @@ class EventTestCase(APITestCase):
|
||||
|
||||
def test_update_event_unauth(self):
|
||||
response = self.client.put(
|
||||
f"{URL}{self.testEventId}/",
|
||||
createEventJSON(name="Update1"),
|
||||
format="json"
|
||||
f"{URL}{self.testEventId}/", createEventJSON(name="Update1"), format="json"
|
||||
)
|
||||
|
||||
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
|
||||
@@ -164,7 +162,9 @@ class EventTestCase(APITestCase):
|
||||
self.assertEqual(Event.objects.count(), 4)
|
||||
|
||||
def test_delete_event(self):
|
||||
response = self.client.delete(f"{URL}{self.testEventId}/",)
|
||||
response = self.client.delete(
|
||||
f"{URL}{self.testEventId}/",
|
||||
)
|
||||
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
|
||||
self.assertEqual(Event.objects.count(), 4)
|
||||
|
||||
|
||||
@@ -10,12 +10,16 @@ URL = "/api/feed/"
|
||||
|
||||
|
||||
class FeedTestCase(APITestCase):
|
||||
|
||||
def setUp(self):
|
||||
tag1 = tagBuilder()
|
||||
tag2 = tagBuilder("testtag2")
|
||||
|
||||
feed = Feed.objects.create(title="TestFeed", visible=True, description="diidadaapa", content="lorem ipsum")
|
||||
feed = Feed.objects.create(
|
||||
title="TestFeed",
|
||||
visible=True,
|
||||
description="diidadaapa",
|
||||
content="lorem ipsum",
|
||||
)
|
||||
feed.tags.add(tag1)
|
||||
feed.tags.add(tag2)
|
||||
self.assertEqual(Feed.objects.count(), 1)
|
||||
@@ -24,16 +28,22 @@ class FeedTestCase(APITestCase):
|
||||
self.feedId = feed.id
|
||||
|
||||
username, password = "test_admin", "password123"
|
||||
self.authClient = User.objects.create_superuser(username, "myemail@test.com", password)
|
||||
self.authClient = User.objects.create_superuser(
|
||||
username, "myemail@test.com", password
|
||||
)
|
||||
|
||||
def test_get_feed(self):
|
||||
response = self.client.get(URL, format="json")
|
||||
self.assertTrue(status.is_success(response.status_code))
|
||||
|
||||
feeds = Feed.objects.all()
|
||||
serializer = FeedSerializer(feeds, many=True, context={
|
||||
"request": APIRequestFactory().get(r"http://testserver/api/feed/")
|
||||
})
|
||||
serializer = FeedSerializer(
|
||||
feeds,
|
||||
many=True,
|
||||
context={
|
||||
"request": APIRequestFactory().get(r"http://testserver/api/feed/")
|
||||
},
|
||||
)
|
||||
self.assertEqual(response.data["results"], serializer.data)
|
||||
|
||||
def test_post_feed(self):
|
||||
@@ -48,7 +58,7 @@ class FeedTestCase(APITestCase):
|
||||
"description_fi": "liirumlaarum",
|
||||
"description_en": "liirumlaarum",
|
||||
"content_fi": "lorem ipsum",
|
||||
"content_en": "lorem ipsum"
|
||||
"content_en": "lorem ipsum",
|
||||
}
|
||||
# Try post without authentication
|
||||
response = self.client.post(URL, data, format="json")
|
||||
@@ -65,7 +75,9 @@ class FeedTestCase(APITestCase):
|
||||
self.assertEqual(created.tags.count(), 2)
|
||||
|
||||
def test_post_delete(self):
|
||||
response = self.client.delete(f"{URL}{self.feedId}/",)
|
||||
response = self.client.delete(
|
||||
f"{URL}{self.feedId}/",
|
||||
)
|
||||
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
|
||||
self.assertEqual(Feed.objects.count(), 1)
|
||||
|
||||
|
||||
@@ -9,7 +9,6 @@ API = "/api/jobads/"
|
||||
|
||||
|
||||
class JobAdTestCase(APITestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.prefilled_jobad = JobAd.objects.create(
|
||||
title_fi="ABB Test",
|
||||
@@ -18,11 +17,13 @@ class JobAdTestCase(APITestCase):
|
||||
description_fi="desc",
|
||||
description_en="desc",
|
||||
content_fi="lorem",
|
||||
content_en="lorem"
|
||||
content_en="lorem",
|
||||
)
|
||||
|
||||
username, password = "test_admin", "password123"
|
||||
self.authClient = User.objects.create_superuser(username, "myemail@test.com", password)
|
||||
self.authClient = User.objects.create_superuser(
|
||||
username, "myemail@test.com", password
|
||||
)
|
||||
|
||||
def test_get_jobads(self):
|
||||
response = self.client.get(API, format="json")
|
||||
@@ -39,7 +40,7 @@ class JobAdTestCase(APITestCase):
|
||||
"description_en": "liirumlaarum",
|
||||
"content_fi": "lorem ipsum",
|
||||
"content_en": "lorem ipsum",
|
||||
"autohide_enabled": "True"
|
||||
"autohide_enabled": "True",
|
||||
}
|
||||
|
||||
# Try post without authentication
|
||||
|
||||
+13
-15
@@ -4,13 +4,18 @@ from rest_framework import status
|
||||
from rest_framework.test import APITestCase
|
||||
from webapp.serializers import SignupSerializer
|
||||
from webapp.models import Signup
|
||||
from webapp.tests.signup_fixture import createSignupForm, createSignupObject, createSignupRequest, ALL_QUESTION_TYPES, ALL_QUESTION_TYPES_ANSWER
|
||||
from webapp.tests.signup_fixture import (
|
||||
createSignupForm,
|
||||
createSignupObject,
|
||||
createSignupRequest,
|
||||
ALL_QUESTION_TYPES,
|
||||
ALL_QUESTION_TYPES_ANSWER,
|
||||
)
|
||||
|
||||
URL = "/api/signup/"
|
||||
|
||||
|
||||
class SignupTestCase(APITestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.signupForm = createSignupForm()
|
||||
self.hiddenForm = createSignupForm(visible=False)
|
||||
@@ -19,13 +24,12 @@ class SignupTestCase(APITestCase):
|
||||
self.signup2 = createSignupObject("2", self.signupForm, ALL_QUESTION_TYPES)
|
||||
|
||||
username, password = "test_admin", "password123"
|
||||
self.authClient = User.objects.create_superuser(username, "myemail@test.com", password)
|
||||
self.authClient = User.objects.create_superuser(
|
||||
username, "myemail@test.com", password
|
||||
)
|
||||
|
||||
def test_get_signups(self):
|
||||
expected = SignupSerializer(
|
||||
self.signupForm.signup_set.all(),
|
||||
many=True
|
||||
)
|
||||
expected = SignupSerializer(self.signupForm.signup_set.all(), many=True)
|
||||
# Authenticate
|
||||
self.client.force_authenticate(user=self.authClient)
|
||||
response = self.client.get(URL, format="json")
|
||||
@@ -34,9 +38,7 @@ class SignupTestCase(APITestCase):
|
||||
|
||||
def test_get_single_signup(self):
|
||||
id = self.signup1.id
|
||||
expected = SignupSerializer(
|
||||
Signup.objects.get(id=id)
|
||||
)
|
||||
expected = SignupSerializer(Signup.objects.get(id=id))
|
||||
# Authenticate
|
||||
self.client.force_authenticate(user=self.authClient)
|
||||
response = self.client.get(f"{URL}{id}/", format="json")
|
||||
@@ -87,11 +89,7 @@ class SignupTestCase(APITestCase):
|
||||
def test_signupee_sendemail(self):
|
||||
form = self.signupForm
|
||||
emailURL = f"/api/signupForm/{form.id}/sendemail/"
|
||||
payload = {
|
||||
"subject": "Email subject",
|
||||
"content": "Markdown",
|
||||
"mode": "actual"
|
||||
}
|
||||
payload = {"subject": "Email subject", "content": "Markdown", "mode": "actual"}
|
||||
no_auth_response = self.client.post(emailURL, payload, format="json")
|
||||
self.assertEqual(no_auth_response.status_code, status.HTTP_401_UNAUTHORIZED)
|
||||
self.client.force_authenticate(user=self.authClient)
|
||||
|
||||
@@ -4,7 +4,16 @@ from unittest import skip
|
||||
from rest_framework import status
|
||||
from rest_framework.test import APITestCase
|
||||
from webapp.models import Signup
|
||||
from webapp.tests.signup_fixture import createSignupForm, createSignupObject, createSignupRequest, ALL_QUESTION_TYPES, ALL_QUESTION_TYPES_ANSWER, TEXT_SCHEMA, RADIO_SCHEMA, CBOX_SCHEMA
|
||||
from webapp.tests.signup_fixture import (
|
||||
createSignupForm,
|
||||
createSignupObject,
|
||||
createSignupRequest,
|
||||
ALL_QUESTION_TYPES,
|
||||
ALL_QUESTION_TYPES_ANSWER,
|
||||
TEXT_SCHEMA,
|
||||
RADIO_SCHEMA,
|
||||
CBOX_SCHEMA,
|
||||
)
|
||||
|
||||
URL = "/api/signup/"
|
||||
|
||||
@@ -12,20 +21,32 @@ URL = "/api/signup/"
|
||||
class SignupErrorTestCase(APITestCase):
|
||||
def setUp(self):
|
||||
self.signupForm = createSignupForm()
|
||||
self.signupFormText = createSignupForm(name="Form2", questions=[ALL_QUESTION_TYPES[0]], schema=TEXT_SCHEMA)
|
||||
self.signupFormRadio = createSignupForm(name="Form3", questions=[ALL_QUESTION_TYPES[1]], schema=RADIO_SCHEMA)
|
||||
self.signupFormCheck = createSignupForm(name="Form4", questions=[ALL_QUESTION_TYPES[2]], schema=CBOX_SCHEMA)
|
||||
self.signupFormText = createSignupForm(
|
||||
name="Form2", questions=[ALL_QUESTION_TYPES[0]], schema=TEXT_SCHEMA
|
||||
)
|
||||
self.signupFormRadio = createSignupForm(
|
||||
name="Form3", questions=[ALL_QUESTION_TYPES[1]], schema=RADIO_SCHEMA
|
||||
)
|
||||
self.signupFormCheck = createSignupForm(
|
||||
name="Form4", questions=[ALL_QUESTION_TYPES[2]], schema=CBOX_SCHEMA
|
||||
)
|
||||
self.hiddenForm = createSignupForm(visible=False)
|
||||
day_from_now = timezone.now() + timezone.timedelta(days=1)
|
||||
day_before_now = timezone.now() + timezone.timedelta(days=-1)
|
||||
self.signupFormNotStarted = createSignupForm(start_time=day_from_now, end_time=day_from_now)
|
||||
self.signupFormEnded = createSignupForm(start_time=day_before_now, end_time=timezone.now())
|
||||
self.signupFormNotStarted = createSignupForm(
|
||||
start_time=day_from_now, end_time=day_from_now
|
||||
)
|
||||
self.signupFormEnded = createSignupForm(
|
||||
start_time=day_before_now, end_time=timezone.now()
|
||||
)
|
||||
|
||||
self.signup1 = createSignupObject("1", self.signupForm, ALL_QUESTION_TYPES)
|
||||
self.signup2 = createSignupObject("2", self.signupForm, ALL_QUESTION_TYPES)
|
||||
|
||||
username, password = "test_admin", "password123"
|
||||
self.authClient = User.objects.create_superuser(username, "myemail@test.com", password)
|
||||
self.authClient = User.objects.create_superuser(
|
||||
username, "myemail@test.com", password
|
||||
)
|
||||
|
||||
def test_get_all_unauthorized(self):
|
||||
response = self.client.get(URL, format="json")
|
||||
@@ -43,62 +64,94 @@ class SignupErrorTestCase(APITestCase):
|
||||
self.assertEqual(Signup.objects.count(), 2)
|
||||
|
||||
def test_create_signup_not_started(self):
|
||||
new = createSignupRequest("asd", self.signupFormNotStarted.id, ALL_QUESTION_TYPES_ANSWER)
|
||||
new = createSignupRequest(
|
||||
"asd", self.signupFormNotStarted.id, ALL_QUESTION_TYPES_ANSWER
|
||||
)
|
||||
response = self.client.post(URL, new, format="json")
|
||||
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
|
||||
self.assertEqual(Signup.objects.count(), 2)
|
||||
|
||||
def test_create_signup_ended(self):
|
||||
new = createSignupRequest("asd", self.signupFormEnded.id, ALL_QUESTION_TYPES_ANSWER)
|
||||
new = createSignupRequest(
|
||||
"asd", self.signupFormEnded.id, ALL_QUESTION_TYPES_ANSWER
|
||||
)
|
||||
response = self.client.post(URL, new, format="json")
|
||||
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
|
||||
self.assertEqual(Signup.objects.count(), 2)
|
||||
|
||||
def test_create_empty_body(self):
|
||||
response = self.client.post(URL, createSignupRequest("", self.signupForm.id, {}), format="json")
|
||||
response = self.client.post(
|
||||
URL, createSignupRequest("", self.signupForm.id, {}), format="json"
|
||||
)
|
||||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
def test_create_array_body(self):
|
||||
response = self.client.post(URL, createSignupRequest("", self.signupForm.id, []), format="json")
|
||||
response = self.client.post(
|
||||
URL, createSignupRequest("", self.signupForm.id, []), format="json"
|
||||
)
|
||||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
@skip("We allow extra signup body because of info fields")
|
||||
def test_create_extra_body(self):
|
||||
testInput = ALL_QUESTION_TYPES_ANSWER.copy()
|
||||
testInput["newId"] = "Oon extraa"
|
||||
response = self.client.post(URL, createSignupRequest("", self.signupForm.id, testInput), format="json")
|
||||
response = self.client.post(
|
||||
URL, createSignupRequest("", self.signupForm.id, testInput), format="json"
|
||||
)
|
||||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
def test_create_bad_id(self):
|
||||
response = self.client.post(URL, createSignupRequest("", self.signupFormText.id, {"malformed": "Tekstiä"}), format="json")
|
||||
response = self.client.post(
|
||||
URL,
|
||||
createSignupRequest("", self.signupFormText.id, {"malformed": "Tekstiä"}),
|
||||
format="json",
|
||||
)
|
||||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
def test_create_bad_type_text(self):
|
||||
response = self.client.post(URL, createSignupRequest("", self.signupFormText.id, {"j5CeRZDvl": 123}), format="json")
|
||||
response = self.client.post(
|
||||
URL,
|
||||
createSignupRequest("", self.signupFormText.id, {"j5CeRZDvl": 123}),
|
||||
format="json",
|
||||
)
|
||||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
def test_create_bad_data_checkbox(self):
|
||||
response = self.client.post(URL, createSignupRequest("", self.signupFormCheck.id, {
|
||||
"i10d426d5": ["D"]
|
||||
}), format="json")
|
||||
response = self.client.post(
|
||||
URL,
|
||||
createSignupRequest("", self.signupFormCheck.id, {"i10d426d5": ["D"]}),
|
||||
format="json",
|
||||
)
|
||||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
def test_create_bad_type_checkbox(self):
|
||||
response = self.client.post(URL, createSignupRequest("", self.signupFormCheck.id, {
|
||||
"i10d426d5": {"j5CeRZDvl": {"asd": "123"}}
|
||||
}), format="json")
|
||||
response = self.client.post(
|
||||
URL,
|
||||
createSignupRequest(
|
||||
"",
|
||||
self.signupFormCheck.id,
|
||||
{"i10d426d5": {"j5CeRZDvl": {"asd": "123"}}},
|
||||
),
|
||||
format="json",
|
||||
)
|
||||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
def test_create_bad_radio(self):
|
||||
response = self.client.post(URL, createSignupRequest("", self.signupFormRadio.id, {
|
||||
"RHJhSoaLD": []
|
||||
}), format="json")
|
||||
response = self.client.post(
|
||||
URL,
|
||||
createSignupRequest("", self.signupFormRadio.id, {"RHJhSoaLD": []}),
|
||||
format="json",
|
||||
)
|
||||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
def test_create_bad_type_radio(self):
|
||||
response = self.client.post(URL, createSignupRequest("", self.signupFormRadio.id, {
|
||||
"RHJhSoaLD": {"asd": "123"}
|
||||
}), format="json")
|
||||
response = self.client.post(
|
||||
URL,
|
||||
createSignupRequest(
|
||||
"", self.signupFormRadio.id, {"RHJhSoaLD": {"asd": "123"}}
|
||||
),
|
||||
format="json",
|
||||
)
|
||||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
self.assertEqual(Signup.objects.count(), 2)
|
||||
|
||||
+17
-16
@@ -11,7 +11,6 @@ from webapp.tests.tag_fixture import tagBuilder, createTagIcon
|
||||
|
||||
|
||||
class TagsTestCase(APITestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.icon = createTagIcon()
|
||||
tag = tagBuilder("Party", icon=self.icon)
|
||||
@@ -20,41 +19,43 @@ class TagsTestCase(APITestCase):
|
||||
tagBuilder("Off")
|
||||
self.assertEqual(Tag.objects.count(), 2)
|
||||
|
||||
username, password = 'test_admin', 'password123'
|
||||
self.authClient = User.objects.create_superuser(username, 'myemail@test.com', password)
|
||||
username, password = "test_admin", "password123"
|
||||
self.authClient = User.objects.create_superuser(
|
||||
username, "myemail@test.com", password
|
||||
)
|
||||
|
||||
def test_get_multiple_tags(self):
|
||||
tagBuilder("Fuksi")
|
||||
tagBuilder("Inter")
|
||||
|
||||
expected = TagSerializer(
|
||||
Tag.objects.all(), many=True,
|
||||
Tag.objects.all(),
|
||||
many=True,
|
||||
context={
|
||||
"request": APIRequestFactory().get(r"http://testserver/api/events/")
|
||||
}).data
|
||||
},
|
||||
).data
|
||||
|
||||
response = self.client.get('/api/tags/', format='json')
|
||||
response = self.client.get("/api/tags/", format="json")
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
self.assertEqual(len(response.data['results']), 4)
|
||||
self.assertEqual(len(response.data["results"]), 4)
|
||||
|
||||
self.assertEqual(
|
||||
response.data['results'],
|
||||
expected
|
||||
)
|
||||
self.assertEqual(response.data["results"], expected)
|
||||
|
||||
def test_get_single_tag(self):
|
||||
response = self.client.get(f"/api/tags/{self.tag_id}/", format='json')
|
||||
response = self.client.get(f"/api/tags/{self.tag_id}/", format="json")
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
|
||||
serializer = TagSerializer(
|
||||
Tag.objects.get(id=self.tag_id),
|
||||
context={
|
||||
"request": APIRequestFactory().get(r"http://testserver/api/events/")
|
||||
})
|
||||
},
|
||||
)
|
||||
self.assertEqual(response.data, serializer.data)
|
||||
|
||||
def test_get_invalid_tag(self):
|
||||
response = self.client.get('/api/tags/15/', format='json')
|
||||
response = self.client.get("/api/tags/15/", format="json")
|
||||
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
|
||||
|
||||
# READ ONLY API! Modify result code and count
|
||||
@@ -68,9 +69,9 @@ class TagsTestCase(APITestCase):
|
||||
"name": "Testinimi",
|
||||
"name_fi": "Testinimi",
|
||||
"name_en": "Test name",
|
||||
"icon": self.icon
|
||||
"icon": self.icon,
|
||||
},
|
||||
format='json'
|
||||
format="json",
|
||||
)
|
||||
|
||||
# Method Not allowed!
|
||||
|
||||
@@ -13,31 +13,35 @@ class TemplateQuestionCase(APITestCase):
|
||||
def setUp(self):
|
||||
self.questions = [
|
||||
TemplateQuestion.objects.create(
|
||||
name="Testi1",
|
||||
questions=ALL_QUESTION_TYPES
|
||||
name="Testi1", questions=ALL_QUESTION_TYPES
|
||||
),
|
||||
TemplateQuestion.objects.create(
|
||||
name="Testi2",
|
||||
questions=ALL_QUESTION_TYPES
|
||||
)
|
||||
name="Testi2", questions=ALL_QUESTION_TYPES
|
||||
),
|
||||
]
|
||||
|
||||
username, password = "test_admin", "password123"
|
||||
self.authClient = User.objects.create_superuser(username, "myemail@test.com", password)
|
||||
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.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)
|
||||
self.assertEqual(
|
||||
response.data, SavedQuestionsSerializer(self.questions[0]).data
|
||||
)
|
||||
|
||||
def test_post(self):
|
||||
new = {
|
||||
"name": "testi3",
|
||||
"questions": json.dumps(ALL_QUESTION_TYPES)
|
||||
}
|
||||
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)
|
||||
@@ -49,23 +53,23 @@ class TemplateQuestionCase(APITestCase):
|
||||
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")
|
||||
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")
|
||||
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"
|
||||
TemplateQuestion.objects.get(id=self.questions[0].id).name, "uusi testi2"
|
||||
)
|
||||
|
||||
def test_delete(self):
|
||||
@@ -76,6 +80,8 @@ class TemplateQuestionCase(APITestCase):
|
||||
|
||||
# Authenticate
|
||||
self.client.force_authenticate(user=self.authClient)
|
||||
response = self.client.delete(f"/api/questions/{self.questions[0].id}/", format="json")
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user