diff --git a/core/tests.py b/core/tests.py index 69fa50aa..5b93fd71 100644 --- a/core/tests.py +++ b/core/tests.py @@ -55,6 +55,8 @@ class UserRegistrationTest(TestCase): 'date_of_birth': '12/6/1942', 'password1': 'plop', 'password2': 'plop', + 'captcha_0': 'dummy-value', + 'captcha_1': 'PASSED' }) self.assertTrue(response.status_code == 200) self.assertTrue('TEST_REGISTER_USER_FORM_OK' in str(response.content)) @@ -70,6 +72,8 @@ class UserRegistrationTest(TestCase): 'date_of_birth': '12/6/1942', 'password1': 'plop', 'password2': 'plop2', + 'captcha_0': 'dummy-value', + 'captcha_1': 'PASSED' }) self.assertTrue(response.status_code == 200) self.assertTrue('TEST_REGISTER_USER_FORM_FAIL' in str(response.content)) @@ -85,6 +89,8 @@ class UserRegistrationTest(TestCase): 'date_of_birth': '12/6/1942', 'password1': 'plop', 'password2': 'plop', + 'captcha_0': 'dummy-value', + 'captcha_1': 'PASSED' }) self.assertTrue(response.status_code == 200) self.assertTrue('TEST_REGISTER_USER_FORM_FAIL' in str(response.content)) @@ -100,6 +106,8 @@ class UserRegistrationTest(TestCase): 'date_of_birth': '12/6/1942', 'password1': 'plop', 'password2': 'plop', + 'captcha_0': 'dummy-value', + 'captcha_1': 'PASSED' }) self.assertTrue(response.status_code == 200) self.assertTrue('TEST_REGISTER_USER_FORM_FAIL' in str(response.content)) @@ -115,6 +123,8 @@ class UserRegistrationTest(TestCase): 'date_of_birth': '', 'password1': 'plop', 'password2': 'plop', + 'captcha_0': 'dummy-value', + 'captcha_1': 'PASSED' }) self.assertTrue(response.status_code == 200) self.assertTrue('TEST_REGISTER_USER_FORM_FAIL' in str(response.content)) @@ -130,6 +140,25 @@ class UserRegistrationTest(TestCase): 'date_of_birth': '12/6/1942', 'password1': 'plop', 'password2': 'plop', + 'captcha_0': 'dummy-value', + 'captcha_1': 'PASSED' + }) + self.assertTrue(response.status_code == 200) + self.assertTrue('TEST_REGISTER_USER_FORM_FAIL' in str(response.content)) + + def test_register_user_form_fail_wrong_captcha(self): + """ + Should not register a user correctly + """ + c = Client() + response = c.post(reverse('core:register'), {'first_name': 'Bibou', + 'last_name': 'Carlier', + 'email': 'bibou@git.an', + 'date_of_birth': '12/6/1942', + 'password1': 'plop', + 'password2': 'plop', + 'captcha_0': 'dummy-value', + 'captcha_1': 'WRONG_CAPTCHA' }) self.assertTrue(response.status_code == 200) self.assertTrue('TEST_REGISTER_USER_FORM_FAIL' in str(response.content)) @@ -145,6 +174,8 @@ class UserRegistrationTest(TestCase): 'date_of_birth': '12/6/1942', 'password1': 'plop', 'password2': 'plop', + 'captcha_0': 'dummy-value', + 'captcha_1': 'PASSED' }) response = c.post(reverse('core:register'), {'first_name': 'Bibou', 'last_name': 'Carlier', @@ -152,6 +183,8 @@ class UserRegistrationTest(TestCase): 'date_of_birth': '12/6/1942', 'password1': 'plop', 'password2': 'plop', + 'captcha_0': 'dummy-value', + 'captcha_1': 'PASSED' }) self.assertTrue(response.status_code == 200) self.assertTrue('TEST_REGISTER_USER_FORM_FAIL' in str(response.content)) @@ -167,6 +200,8 @@ class UserRegistrationTest(TestCase): 'date_of_birth': '12/6/1942', 'password1': 'plop', 'password2': 'plop', + 'captcha_0': 'dummy-value', + 'captcha_1': 'PASSED' }) response = c.post(reverse('core:login'), {'username': 'gcarlier', 'password': 'plop'}) self.assertTrue(response.status_code == 302) @@ -183,6 +218,8 @@ class UserRegistrationTest(TestCase): 'date_of_birth': '12/6/1942', 'password1': 'plop', 'password2': 'plop', + 'captcha_0': 'dummy-value', + 'captcha_1': 'PASSED' }) response = c.post(reverse('core:login'), {'username': 'gcarlier', 'password': 'guy'}) self.assertTrue(response.status_code == 200) diff --git a/core/views/forms.py b/core/views/forms.py index c1f64a9c..957bf498 100644 --- a/core/views/forms.py +++ b/core/views/forms.py @@ -21,7 +21,7 @@ # Place - Suite 330, Boston, MA 02111-1307, USA. # # - +from captcha.fields import CaptchaField from django.contrib.auth.forms import UserCreationForm, AuthenticationForm from django import forms from django.conf import settings @@ -148,6 +148,7 @@ class LoginForm(AuthenticationForm): class RegisteringForm(UserCreationForm): error_css_class = 'error' required_css_class = 'required' + captcha = CaptchaField() class Meta: model = User diff --git a/requirements.txt b/requirements.txt index fb8595e6..136f7362 100644 --- a/requirements.txt +++ b/requirements.txt @@ -14,5 +14,6 @@ whoosh django-debug-toolbar libsass django-ordered-model +django-simple-captcha pygraphviz # mysqlclient diff --git a/sith/settings.py b/sith/settings.py index b1531de0..0e33e7a3 100644 --- a/sith/settings.py +++ b/sith/settings.py @@ -74,6 +74,7 @@ INSTALLED_APPS = ( 'rest_framework', 'ajax_select', 'haystack', + 'captcha', 'core', 'club', 'subscription', @@ -625,3 +626,6 @@ if DEBUG: SASS_INCLUDE_FOLDERS = [ 'core/static/', ] + +if 'test' in sys.argv: + CAPTCHA_TEST_MODE = True diff --git a/sith/urls.py b/sith/urls.py index 2b744c24..af443b9c 100644 --- a/sith/urls.py +++ b/sith/urls.py @@ -72,6 +72,7 @@ urlpatterns = [ url(r'^ajax_select/', include(ajax_select_urls)), url(r'^i18n/', include('django.conf.urls.i18n')), url(r'^jsi18n/$', javascript_catalog, js_info_dict, name='javascript-catalog'), + url(r'^captcha/', include('captcha.urls')), ] if settings.DEBUG: