Skip to content
Snippets Groups Projects
Commit 3abefcd3 authored by sandeepsajan0's avatar sandeepsajan0 Committed by Fredrik Jonsson
Browse files

Add tests for application form admin view(error msgs) and fix linting issues

parent c09d80fc
No related branches found
No related tags found
No related merge requests found
import factory
from django.contrib.auth.models import Group from django.contrib.auth.models import Group
from django.contrib.messages import get_messages
from django.test import TestCase from django.test import TestCase
from django.urls import reverse from django.urls import reverse
from wagtail.tests.utils import WagtailTestUtils from wagtail.tests.utils import WagtailTestUtils
from hypha.apply.funds.models.forms import ApplicationForm
from hypha.apply.home.factories import ApplyHomePageFactory from hypha.apply.home.factories import ApplyHomePageFactory
from hypha.apply.users.groups import STAFF_GROUP_NAME from hypha.apply.users.groups import STAFF_GROUP_NAME
from hypha.apply.users.tests.factories import SuperUserFactory from hypha.apply.users.tests.factories import SuperUserFactory
...@@ -11,6 +14,21 @@ from .factories.models import RoundFactory ...@@ -11,6 +14,21 @@ from .factories.models import RoundFactory
from .test_admin_form import form_data 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): class TestFundCreationView(TestCase):
@classmethod @classmethod
def setUpTestData(cls): def setUpTestData(cls):
...@@ -104,3 +122,117 @@ class TestRoundIndexView(WagtailTestUtils, TestCase): ...@@ -104,3 +122,117 @@ class TestRoundIndexView(WagtailTestUtils, TestCase):
] ]
review_form_cell = f'<td class="field-review_forms title">{"".join(review_form_links)}</td>' review_form_cell = f'<td class="field-review_forms title">{"".join(review_form_links)}</td>'
self.assertContains(response, review_form_cell, html=True) 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)
...@@ -7,12 +7,7 @@ from django.utils.safestring import mark_safe ...@@ -7,12 +7,7 @@ from django.utils.safestring import mark_safe
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from pagedown.widgets import PagedownWidget from pagedown.widgets import PagedownWidget
from wagtail.admin import messages from wagtail.admin import messages
from wagtail.core.blocks import ( from wagtail.core.blocks import ListBlock, StaticBlock, StreamBlock, StreamValue
ListBlock,
StaticBlock,
StreamBlock,
StreamValue,
)
from hypha.apply.stream_forms.blocks import ( from hypha.apply.stream_forms.blocks import (
FormFieldBlock, FormFieldBlock,
...@@ -128,7 +123,7 @@ class CustomFormFieldsBlock(StreamBlock): ...@@ -128,7 +123,7 @@ class CustomFormFieldsBlock(StreamBlock):
all_errors.append( all_errors.append(
'{} cannot be empty for {}'.format(child_block.label, block.block.label) '{} 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]: for child_value in block.value[child_block_name]:
if not child_value: if not child_value:
all_errors.append( all_errors.append(
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment