-
Notifications
You must be signed in to change notification settings - Fork 324
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
marsh_
committed
Jul 8, 2015
1 parent
b69d07e
commit b1bf212
Showing
7 changed files
with
124 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,11 +2,15 @@ | |
from captcha.conf import settings | ||
from captcha.fields import CaptchaField, CaptchaTextInput | ||
from captcha.models import CaptchaStore, get_safe_now | ||
from captcha.backends.db import DBStore | ||
from captcha.backends.session import SessionStore | ||
from django.conf import settings as django_settings | ||
from django.core.exceptions import ImproperlyConfigured | ||
from django.core.urlresolvers import reverse | ||
from django.test import TestCase | ||
from django.utils.translation import ugettext_lazy | ||
from django.test.utils import override_settings | ||
import django | ||
import datetime | ||
import json | ||
import re | ||
|
@@ -29,7 +33,6 @@ class CaptchaCase(TestCase): | |
urls = 'captcha.tests.urls' | ||
|
||
def setUp(self): | ||
|
||
self.stores = {} | ||
self.__current_settings_output_format = settings.CAPTCHA_OUTPUT_FORMAT | ||
self.__current_settings_dictionary = settings.CAPTCHA_WORDS_DICTIONARY | ||
|
@@ -332,5 +335,101 @@ def test_image_size(self): | |
settings.CAPTCHA_IMAGE_SIZE = __current_test_mode_setting | ||
|
||
|
||
@override_settings(CAPTCHA_STORE='SESSION') | ||
class CaptchaCase2(TestCase): | ||
urls = 'captcha.tests.urls' | ||
|
||
# store tests | ||
def test_db_store(self): | ||
store = DBStore() | ||
key = store.generate_key() | ||
record = store.get(hashkey=key) | ||
self.assertEqual(record['hashkey'], key) | ||
self.assertNotEqual(record._captcha.get('challenge'), None) | ||
self.assertNotEqual(record._captcha.get('response'), None) | ||
record.delete() | ||
try: | ||
store.get(hashkey=key) | ||
self.fail('Record deletion error') | ||
except: | ||
pass | ||
|
||
key = store.generate_key() | ||
cap = CaptchaStore.objects.get(hashkey=key) | ||
cap.expiration = get_safe_now() - datetime.timedelta(minutes=1) | ||
cap.save() | ||
try: | ||
store.get(hashkey=key, allow_expired=False) | ||
self.fail() | ||
except: | ||
pass | ||
|
||
store.remove_expired() | ||
try: | ||
store.get(hashkey=key) | ||
self.fail('remove_expired failed') | ||
except: | ||
pass | ||
|
||
def test_session_store(self): | ||
store = SessionStore() | ||
key = store.generate_key() | ||
record = store.get(hashkey=key) | ||
self.assertEqual(record.session_key, key) | ||
self.assertNotEqual(record.get('challenge'), None) | ||
self.assertNotEqual(record.get('response'), None) | ||
record.delete() | ||
try: | ||
store.get(hashkey=key) | ||
self.fail('Record deletion error') | ||
except: | ||
pass | ||
|
||
key = store.generate_key() | ||
cap = store.get(hashkey=key) | ||
cap.set_expiry(-1 * 60) | ||
cap.save() | ||
try: | ||
store.get(hashkey=key, allow_expired=False) | ||
self.fail() | ||
except: | ||
pass | ||
|
||
if not django.get_version() < '1.5': | ||
# django lower than 1.5 can't remove expired sessions | ||
store.remove_expired() | ||
try: | ||
store.get(hashkey=key) | ||
self.fail('remove_expired failed') | ||
except: | ||
pass | ||
|
||
# view tests | ||
@override_settings(CAPTCHA_STORE='SESSION') | ||
def testFormSubmit2(self): | ||
|
||
r = self.client.get(reverse('captcha-test')) | ||
self.assertEqual(r.status_code, 200) | ||
|
||
store = SessionStore() | ||
hash_ = re.findall(r'value="([0-9a-f]+)"', str(r.content))[0] | ||
response = store.get(hashkey=hash_)['response'] | ||
|
||
r = self.client.post(reverse('captcha-test'), dict(captcha_0=hash_, captcha_1=response, subject='xxx', sender='[email protected]')) | ||
self.assertEqual(r.status_code, 200) | ||
self.assertTrue(str(r.content).find('Form validated') > 0) | ||
|
||
r = self.client.post(reverse('captcha-test'), dict(captcha_0=hash_, captcha_1=response, subject='xxx', sender='[email protected]')) | ||
self.assertEqual(r.status_code, 200) | ||
self.assertFalse(str(r.content).find('Form validated') > 0) | ||
|
||
def testWrongSubmit2(self): | ||
for urlname in ('captcha-test', 'captcha-test-model-form'): | ||
r = self.client.get(reverse(urlname)) | ||
self.assertEqual(r.status_code, 200) | ||
r = self.client.post(reverse(urlname), dict(captcha_0='abc', captcha_1='wrong response', subject='xxx', sender='[email protected]')) | ||
self.assertFormError(r, 'form', 'captcha', ugettext_lazy('Invalid CAPTCHA')) | ||
|
||
|
||
def trivial_challenge(): | ||
return 'trivial', 'trivial' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,7 +33,7 @@ | |
'captcha', | ||
] | ||
|
||
LANGUAGE_CODE = "en" | ||
LANGUAGE_CODE = "en-us" | ||
|
||
LANGUAGES = ( | ||
('en', 'English'), | ||
|