def test_set_question_converts_multiplication_operator_to_entity(self):
     w = MathCaptchaWidget()
     w.set_question(2, 4, '*')
     self.assertHTMLEqual(w.question_html, """
         <span class="captcha-question">
             What is 2 &times; 4?
         </span>""")
 def test_set_question(self):
     w = MathCaptchaWidget()
     w.set_question(2, 4, 'foo')
     self.assertHTMLEqual(w.question_html, """
         <span class="captcha-question">
             What is 2 foo 4?
         </span>""")
 def test_set_question_used_question_class(self):
     w = MathCaptchaWidget(question_class='question')
     w.set_question(2, 4, '+')
     self.assertHTMLEqual(w.question_html, """
         <span class="question">
             What is 2 + 4?
         </span>""")
예제 #4
0
 def test_set_question_converts_multiplication_operator_to_entity(self):
     w = MathCaptchaWidget()
     w.set_question(2, 4, '*')
     self.assertHTMLEqual(
         w.question_html, """
         <span class="captcha-question">
             What is 2 &times; 4?
         </span>""")
예제 #5
0
 def test_set_question(self):
     w = MathCaptchaWidget()
     w.set_question(2, 4, 'foo')
     self.assertHTMLEqual(
         w.question_html, """
         <span class="captcha-question">
             What is 2 foo 4?
         </span>""")
예제 #6
0
 def test_set_question_used_question_class(self):
     w = MathCaptchaWidget(question_class='question')
     w.set_question(2, 4, '+')
     self.assertHTMLEqual(
         w.question_html, """
         <span class="question">
             What is 2 + 4?
         </span>""")
 def test_render(self):
     w = MathCaptchaWidget()
     with mock.patch.object(w, 'generate_captcha') as mock_generate_captcha:
         mock_generate_captcha.return_value = 'hashed_answer'
         w.question_html = 'question_html'
         result = w.render('foo', None)
         self.assertHTMLEqual(result, """
         <span class="captcha-question">question_html</span>
         <input type="text" name="foo_0" size="5" />
         <input type="hidden" name="foo_1" value="hashed_answer"/>""")
 def test_render(self):
     w = MathCaptchaWidget()
     with mock.patch.object(w, 'generate_captcha') as mock_generate_captcha:
         mock_generate_captcha.return_value = 'hashed_answer'
         w.question_html = 'question_html'
         result = w.render('foo', None)
         self.assertHTMLEqual(
             result, """
         <span class="captcha-question">question_html</span>
         <input type="text" name="foo_0" size="5" />
         <input type="hidden" name="foo_1" value="hashed_answer"/>""")
 def test_generate_captcha(self, mock_hash_answer, mock_get_numbers,
                           mock_get_operator):
     mock_hash_answer.side_effect = lambda x: x
     mock_get_numbers.return_value = (1, 3)
     mock_get_operator.return_value = '+'
     w = MathCaptchaWidget()
     result = w.generate_captcha()
     self.assertEqual(result, 4)
     self.assertEqual(mock_hash_answer.call_count, 1)
     self.assertEqual(mock_get_numbers.call_count, 1)
     self.assertEqual(mock_get_operator.call_count, 1)
     self.assertHTMLEqual(w.question_html, "What is 1 + 3?")
 def test_generate_captcha(self, mock_hash_answer, mock_get_numbers,
                           mock_get_operator):
     mock_hash_answer.side_effect = lambda x: x
     mock_get_numbers.return_value = (1, 3)
     mock_get_operator.return_value = '+'
     w = MathCaptchaWidget()
     result = w.generate_captcha()
     self.assertEqual(result, 4)
     self.assertEqual(mock_hash_answer.call_count, 1)
     self.assertEqual(mock_get_numbers.call_count, 1)
     self.assertEqual(mock_get_operator.call_count, 1)
     self.assertHTMLEqual(w.question_html, "What is 1 + 3?")
 def test_instantiation(self):
     w = MathCaptchaWidget(1, 10)
     self.assertEqual(len(w.widgets), 2)
     self.assertEqual(w.start_int, 1)
     self.assertEqual(w.end_int, 10)
     self.assertEqual(six.text_type(w.question_tmpl),
                      'What is %(num1)i %(operator)s %(num2)i ?')
     self.assertEqual(w.question_class, 'captcha-question')
예제 #12
0
 def test_instantiation_with_widget(self, mocked):
     MathCaptchaField(widget=MathCaptchaWidget())
     self.assertEqual(mocked.call_count, 0)
 def test_decompress_always_returns_none(self):
     w = MathCaptchaWidget()
     expected = [None, None]
     self.assertEqual(w.decompress(None), expected)
     self.assertEqual(w.decompress(''), expected)
     self.assertEqual(w.decompress('something'), expected)
 def test_end_int_less_than_start_int_raises(self):
     with self.assertRaises(Warning):
         MathCaptchaWidget(10, 1)
 def test_negative_end_int_raises(self):
     with self.assertRaises(Warning):
         MathCaptchaWidget(1, -10)
 def test_negative_start_int_raises(self):
     with self.assertRaises(Warning):
         MathCaptchaWidget(-1, 10)
예제 #17
0
class RegisterForm(forms.Form):
    """Form used for to register new users."""

    email = forms.EmailField(label=u'Adresse email')
    username = forms.CharField(label=u'Nom d’utilisateur', max_length=30)
    password = forms.CharField(label=u'Mot de passe',
                               max_length=76,
                               widget=forms.PasswordInput)
    password_confirm = forms.CharField(label=u'Confirmation',
                                       max_length=76,
                                       widget=forms.PasswordInput)
    captcha = MathCaptchaField(error_messages={
        'invalid': u'Vérifiez votre réponse et réessayez.',
        'invalid_number': u'Entrez un nombre entier.',
    },
                               widget=MathCaptchaWidget(
                                   question_tmpl=u'Quel est le résultat de '
                                   u'%(num1)i %(operator)s %(num2)i ?',
                                   question_class='captcha'))

    def __init__(self, *args, **kwargs):
        self.helper = FormHelper()
        self.helper.form_method = 'post'

        self.helper.layout = Layout(
            Fieldset(
                u'Identifiants',
                Field('username'),
                Field('password'),
                Field('password_confirm'),
                Field('email'),
            ), Fieldset(
                u'Captcha',
                Field('captcha'),
            ),
            Div(Submit('submit', u'Valider mon inscription'),
                HTML(u'<a href="/" class="button secondary">Annuler</a>'),
                css_class='button-group'))
        super().__init__(*args, **kwargs)

    def clean(self):
        cleaned_data = super().clean()

        password = cleaned_data.get('password')
        password_confirm = cleaned_data.get('password_confirm')

        # Check that the password and its confirmation match
        if not password_confirm == password:
            msg = u'Les mots de passe sont différents.'
            self.add_error('password', msg)
            self.add_error('password_confirm', msg)

            # Remove the wrong passwords from the form
            if 'password' in cleaned_data:
                del cleaned_data['password']
            if 'password_confirm' in cleaned_data:
                del cleaned_data['password_confirm']

        # Check that the user doesn't exist yet
        username = cleaned_data.get('username')
        if User.objects.filter(username=username).count() > 0:
            msg = u'Ce nom d’utilisateur est déjà utilisé.'
            self.add_error('username', msg)

        return cleaned_data
 def test_set_question(self):
     w = MathCaptchaWidget()
     w.set_question(2, 4, 'foo')
     self.assertHTMLEqual(w.question_html, "What is 2 foo 4?")
 def test_set_question_converts_multiplication_operator_to_entity(self):
     w = MathCaptchaWidget()
     w.set_question(2, 4, '*')
     self.assertHTMLEqual(w.question_html, "What is 2 &times; 4?")
 def test_set_question(self):
     w = MathCaptchaWidget()
     w.set_question(2, 4, 'foo')
     self.assertHTMLEqual(w.question_html, "What is 2 foo 4?")
 def test_format_output(self):
     w = MathCaptchaWidget()
     w.question_html = 'abc'
     result = w.format_output(['def', 'hij'])
     self.assertEqual(result, 'abcdefhij')
 def test_render_is_different_each_time_called(self):
     w = MathCaptchaWidget()
     result1 = w.render('foo', None)
     result2 = w.render('foo', None)
     self.assertHTMLNotEqual(result1, result2)
예제 #23
0
 def test_instantiation_with_widget_and_values_is_error(self):
     with self.assertRaises(TypeError):
         MathCaptchaField(start_int=5,
                          end_int=10,
                          widget=MathCaptchaWidget())
 def test_decompress_always_returns_none(self):
     w = MathCaptchaWidget()
     expected = [None, None]
     self.assertEqual(w.decompress(None), expected)
     self.assertEqual(w.decompress(''), expected)
     self.assertEqual(w.decompress('something'), expected)
 def test_render_is_different_each_time_called(self):
     w = MathCaptchaWidget()
     result1 = w.render('foo', None)
     result2 = w.render('foo', None)
     self.assertHTMLNotEqual(result1, result2)
 def test_default_question_tmpl(self):
     w = MathCaptchaWidget(question_tmpl='foo')
     self.assertEqual(w.question_tmpl, 'foo')
 def test_default_question_class(self):
     w = MathCaptchaWidget(question_class='foo')
     self.assertEqual(w.question_class, 'foo')
 def test_set_question_converts_multiplication_operator_to_entity(self):
     w = MathCaptchaWidget()
     w.set_question(2, 4, '*')
     self.assertHTMLEqual(w.question_html, "What is 2 &times; 4?")
예제 #29
0
 def test_format_output(self):
     w = MathCaptchaWidget()
     w.question_html = 'abc'
     result = w.format_output(['def', 'hij'])
     self.assertEqual(result, 'abcdefhij')