Skip to content

Chore/improve test event api coverage #3797

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions lego/apps/events/tests/test_events_api.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import typing
from copy import deepcopy
from datetime import timedelta
from unittest import mock, skipIf
Expand Down Expand Up @@ -226,6 +227,14 @@
},
]

_test_payload_error_messages = {
"required_field": "This field is required.",
"not_blank_field": "This field may not be blank.",
"not_null_field": "This field may not be null.",
"valid_choice": "is not valid choice.",
"wrong_format": "Datetime has wrong format.",
}

_test_registration_data = {"user": 1}

webhook_secret = getattr(settings, "STRIPE_WEBHOOK_SECRET", None)
Expand Down Expand Up @@ -509,6 +518,15 @@ class CreateEventsTestCase(BaseAPITestCase):
"test_users.yaml",
"test_events.yaml",
]
PAYLOAD_FIELDS: typing.ClassVar[list[str]] = [
"title",
"description",
"text",
"event_type",
"location",
"start_time",
"end_time",
]

def setUp(self):
self.abakus_user = User.objects.all().first()
Expand All @@ -518,6 +536,65 @@ def setUp(self):
self.assertEqual(self.event_response.status_code, status.HTTP_201_CREATED)
self.event_id = self.event_response.json().pop("id", None)

def test_fields_required_on_create(self) -> None:
response = self.client.post(_get_list_url(), {})
for field in self.PAYLOAD_FIELDS:
self.assertEqual(
response.data.get(field)[0],
_test_payload_error_messages["required_field"],
)

def test_fields_required_on_update(self) -> None:
response = self.client.put(_get_detail_url(self.event_id), {})
for field in self.PAYLOAD_FIELDS:
self.assertEqual(
response.data.get(field)[0],
_test_payload_error_messages["required_field"],
)

def test_empty_fields_in_payload(self) -> None:
test_data = [
{"method": "post", "is_detail": False},
{"method": "put", "is_detail": True},
]

_nul_fields = ["title", "text", "location"]
_date_fields = ["start_time", "end_time"]

for data in test_data:
with self.subTest():
url = (
_get_detail_url(self.event_id)
if data["is_detail"]
else _get_list_url()
)
response = getattr(self.client, data["method"])(
url,
{
"title": "",
"desciption": "",
"text": "",
"event_type": "",
"location": "",
"start_time": "",
"end_time": "",
},
)
for field in _nul_fields:
self.assertEqual(
response.data.get(field)[0],
_test_payload_error_messages["not_blank_field"],
)
for field in _date_fields:
self.assertIn(
_test_payload_error_messages["wrong_format"],
response.data.get(field)[0],
)
self.assertEqual(
response.data.get("description")[0],
_test_payload_error_messages["required_field"],
)

def test_event_creation(self):
"""Test event creation with pools"""
self.assertIsNotNone(self.event_id)
Expand Down