def test_recaptcha_field_added(self, mock_stub): mock_stub.return_value = True fb = FormBuilder(self.form.get_form_fields(), add_recaptcha=True) form_class = fb.get_form_class() field_names = form_class.base_fields.keys() self.assertIn('recaptcha', field_names) self.assertIsInstance(form_class.base_fields['recaptcha'], ReCaptchaField)
def test_regex_field_with_validator_has_correct_options_set(self): validator = RegexFieldValidator.objects.create( name='Positive Number', regex='/^\d+$/', error_message='Please enter a positive number.' ) self.field.regex_validator = validator self.field.save() fb = FormBuilder(self.form.get_form_fields(), add_recaptcha=False) form_class = fb.get_form_class() self.assertEqual(form_class.base_fields['my-regex'].regex.pattern, validator.regex) self.assertEqual(form_class.base_fields['my-regex'].error_messages['invalid'], validator.error_message)
def test_formfields__missing_label_in_value(self): fields = [{'type': 'singleline', 'value': {}}] with self.assertRaises(AttributeError) as ex: FormBuilder(fields).formfields self.assertEqual( ex.exception.args[0], 'The block for singleline must contain a label of type blocks.CharBlock(required=True)' )
def test_formfields__missing_label_in_value(self): fields = [{"type": "singleline", "value": {}}] with self.assertRaises(AttributeError) as ex: FormBuilder(fields).formfields self.assertEqual( ex.exception.args[0], "No label value can be determined for an instance of SingleLineTextField. " "Add a 'label' CharBlock() in your field's get_form_block() method to allow " "this to be specified by form editors. Or, override get_formfield_label() " "to return a different value.", )
def test_get_form_class(self): fields = self.form.get_form_fields() form_class = FormBuilder(fields).get_form_class() self.assertEqual(len(form_class().fields), 16) formfields = form_class().fields for name, field in get_fields().items(): self.assertIn(name, formfields) self.assertIsInstance(formfields[name], field().field_class) self.assertIsInstance(formfields["form_id"], forms.CharField) self.assertIsInstance(formfields["form_reference"], forms.CharField)
def test_formfields__invalid_type(self): fields = [{"type": "foo", "value": {}}] with self.assertRaises(AttributeError) as ex: FormBuilder(fields).formfields self.assertEqual(ex.exception.args[0], "Could not find a registered field of type foo")
def test_formfields(self): fields = self.form.get_form_fields() formfields = FormBuilder(fields).formfields for field in fields: self.assertIn(field["type"], formfields)
def get_form_class(self): """ Returns the form class. """ return FormBuilder(self.get_form_fields()).get_form_class()
def get_form_class(self): fb = FormBuilder(self.get_form_fields(), add_recaptcha=self.add_recaptcha) return fb.get_form_class()
def test_recaptcha_field_not_added_when_not_enabled(self, mock_stub): mock_stub.return_value = False fb = FormBuilder(self.form.get_form_fields(), add_recaptcha=False) form_class = fb.get_form_class() field_names = form_class.base_fields.keys() self.assertNotIn('recaptcha', field_names)
def test_regex_field_default_options(self): fb = FormBuilder(self.form.get_form_fields(), add_recaptcha=False) form_class = fb.get_form_class() self.assertEqual(form_class.base_fields['my-regex'].regex.pattern, '(.*?)')
def test_regex_field_exists(self): fb = FormBuilder(self.form.get_form_fields(), add_recaptcha=False) form_class = fb.get_form_class() field_names = form_class.base_fields.keys() self.assertIn('my-regex', field_names) self.assertIsInstance(form_class.base_fields['my-regex'], forms.RegexField)