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 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)