diff --git a/hypha/apply/funds/tests/test_admin_views.py b/hypha/apply/funds/tests/test_admin_views.py index 4269584540ec5194eabc08e3b0b35db17ea63331..9459995dc419ceb1e1c45b15c78f2832da444393 100644 --- a/hypha/apply/funds/tests/test_admin_views.py +++ b/hypha/apply/funds/tests/test_admin_views.py @@ -1,8 +1,11 @@ +import factory from django.contrib.auth.models import Group +from django.contrib.messages import get_messages from django.test import TestCase from django.urls import reverse from wagtail.tests.utils import WagtailTestUtils +from hypha.apply.funds.models.forms import ApplicationForm from hypha.apply.home.factories import ApplyHomePageFactory from hypha.apply.users.groups import STAFF_GROUP_NAME from hypha.apply.users.tests.factories import SuperUserFactory @@ -11,6 +14,21 @@ from .factories.models import RoundFactory from .test_admin_form import form_data +def create_form_fields_data(blocks): + parent_field = 'form_fields' + form_fields_dict = dict() + form_fields_dict[f'{parent_field}-count'] = [str(len(blocks))] + for index, block_name in enumerate(blocks): + form_fields_dict[f'{parent_field}-{index}-deleted'] = [''] + form_fields_dict[f'{parent_field}-{index}-order'] = [str(index)] + form_fields_dict[f'{parent_field}-{index}-type'] = [str(block_name)] + + for field_name, field_value in blocks[block_name].items(): + form_fields_dict[f'{parent_field}-{index}-value-{field_name}'] = field_value + + return form_fields_dict + + class TestFundCreationView(TestCase): @classmethod def setUpTestData(cls): @@ -104,3 +122,117 @@ class TestRoundIndexView(WagtailTestUtils, TestCase): ] review_form_cell = f'<td class="field-review_forms title">{"".join(review_form_links)}</td>' self.assertContains(response, review_form_cell, html=True) + + +class TestCreateApplicationFormView(TestCase): + @classmethod + def setUpTestData(cls): + cls.user = SuperUserFactory() + cls.label_help_text_data = { + 'field_label': factory.Faker('sentence').evaluate(None, None, {'locale': None}), + 'help_text': factory.Faker('sentence').evaluate(None, None, {'locale': None}) + } + cls.name = factory.Faker('name').evaluate(None, None, {'locale': None}) + + def create_page(self, data): + self.client.force_login(self.user) + url = reverse('funds_applicationform_modeladmin_create') + response = self.client.post(url, data=data, secure=True, follow=True) + return response + + def test_name_field_required(self): + data = {'name': ['']} + form_field_data = create_form_fields_data( + { + 'title': self.label_help_text_data, + 'email': self.label_help_text_data, + 'full_name': self.label_help_text_data, + } + ) + data.update(form_field_data) + response = self.create_page(data=data) + + expected_message = 'This field is required.' + for message in get_messages(response.context['request']): + self.assertEqual(expected_message, str(message.message).strip()) + self.assertEqual(ApplicationForm.objects.count(), 0) + + def test_title_is_required(self): + data = {'name': [self.name]} + form_field_data = create_form_fields_data( + { + 'email': self.label_help_text_data, + 'full_name': self.label_help_text_data, + } + ) + data.update(form_field_data) + response = self.create_page(data=data) + + expected_message = 'You are missing the following required fields: Title' + for message in get_messages(response.context['request']): + self.assertEqual(expected_message, str(message.message).strip()) + self.assertEqual(ApplicationForm.objects.count(), 0) + + def test_email_is_required(self): + data = {'name': [self.name]} + form_field_data = create_form_fields_data( + { + 'title': self.label_help_text_data, + 'full_name': self.label_help_text_data, + } + ) + data.update(form_field_data) + response = self.create_page(data=data) + + expected_message = 'You are missing the following required fields: Email' + for message in get_messages(response.context['request']): + self.assertEqual(expected_message, str(message.message).strip()) + self.assertEqual(ApplicationForm.objects.count(), 0) + + def test_full_name_is_required(self): + data = {'name': [self.name]} + form_field_data = create_form_fields_data( + { + 'title': self.label_help_text_data, + 'email': self.label_help_text_data, + } + ) + data.update(form_field_data) + response = self.create_page(data=data) + + expected_message = 'You are missing the following required fields: Full Name' + for message in get_messages(response.context['request']): + self.assertEqual(expected_message, str(message.message).strip()) + self.assertEqual(ApplicationForm.objects.count(), 0) + + def test_field_label_required(self): + data = {'name': [self.name]} + form_field_data = create_form_fields_data( + { + 'title': {}, + 'email': {}, + 'full_name': {}, + } + ) + data.update(form_field_data) + response = self.create_page(data=data) + + expected_messages_list = ['Label cannot be empty for Application title', 'Label cannot be empty for Email', 'Label cannot be empty for Full name'] + for message in get_messages(response.context['request']): + self.assertIn(str(message.message).strip(), expected_messages_list) + self.assertEqual(ApplicationForm.objects.count(), 0) + + def test_form_creation(self): + data = {'name': [self.name]} + form_field_data = create_form_fields_data( + { + 'title': self.label_help_text_data, + 'email': self.label_help_text_data, + 'full_name': self.label_help_text_data, + } + ) + data.update(form_field_data) + response = self.create_page(data=data) + + self.assertEqual(response.status_code, 200) + self.assertEqual(ApplicationForm.objects.count(), 1) diff --git a/hypha/apply/utils/blocks.py b/hypha/apply/utils/blocks.py index 1f380f510e62b3f484783dcd148fc876d849cdfe..5778b273f82ae04e9ec3d2934f6cbff887bad188 100644 --- a/hypha/apply/utils/blocks.py +++ b/hypha/apply/utils/blocks.py @@ -7,12 +7,7 @@ from django.utils.safestring import mark_safe from django.utils.translation import gettext_lazy as _ from pagedown.widgets import PagedownWidget from wagtail.admin import messages -from wagtail.core.blocks import ( - ListBlock, - StaticBlock, - StreamBlock, - StreamValue, -) +from wagtail.core.blocks import ListBlock, StaticBlock, StreamBlock, StreamValue from hypha.apply.stream_forms.blocks import ( FormFieldBlock, @@ -128,7 +123,7 @@ class CustomFormFieldsBlock(StreamBlock): all_errors.append( '{} cannot be empty for {}'.format(child_block.label, block.block.label) ) - if isinstance(child_block,ListBlock) and child_block.child_block.required: + if isinstance(child_block, ListBlock) and child_block.child_block.required: for child_value in block.value[child_block_name]: if not child_value: all_errors.append(