29
28
from django.test.utils import override_settings
30
29
from django.urls import reverse
31
30
from django.utils.html import escape
32
from gargoyle import gargoyle
33
31
from gargoyle.testutils import switches
34
32
from mock import Mock, patch
35
33
from pyquery import PyQuery
132
122
assert self.client.login(
133
123
username=self.data['email'], password=self.data['password'])
135
def request_when_captcha_fails(self, url, data):
136
class MockCaptcha(object):
137
def __init__(self, *args):
140
def verify(self, solution, ip_addr, email):
141
self.message = 'no-challenge'
148
with patch.object(ui, 'Captcha', MockCaptcha):
149
r = self.client.post(url, data)
154
126
@override_settings(LANGUAGE_CODE='es')
155
127
class SpanishUIViewsTestCase(BaseTestCase):
1348
1320
self.assertFalse(email.is_verified)
1351
@override_settings(CAPTCHA_PRIVATE_KEY='some-private-key')
1352
class CaptchaVerificationTestCase(BaseTestCase):
1354
success_status = 302
1357
super(CaptchaVerificationTestCase, self).setUp()
1358
mock_handler = MockHandler()
1359
mock_handler.set_next_response(200, 'false\nno-challenge')
1360
self.patch(Captcha, 'opener', new=urllib2.build_opener(mock_handler))
1362
p = switches(CAPTCHA=True)
1364
self.addCleanup(p.unpatch)
1366
def test_new_account_when_form_validation_fails(self):
1367
r = self.post_new_account()
1368
self.assertTemplateUsed(r, 'registration/new_account.html')
1369
msg = 'It appears that our captcha service was unable to load'
1370
self.assertContains(r, msg)
1372
def test_new_account_captcha_whitelist(self):
1373
email = 'canonicaltest@gmail.com'
1374
pattern = '^canonicaltest(?:\+.+)?@gmail\.com$'
1375
with self.settings(EMAIL_WHITELIST_REGEXP_LIST=[pattern]):
1376
response = self.post_new_account(email=email)
1377
self.assertEqual(response.status_code, self.success_status)
1379
def test_new_account_captcha_whitelist_with_uuid(self):
1380
email = 'canonicaltest+something@gmail.com'
1381
pattern = '^canonicaltest(?:\+.+)?@gmail\.com$'
1382
with self.settings(EMAIL_WHITELIST_REGEXP_LIST=[pattern]):
1383
response = self.post_new_account(email=email)
1384
self.assertEqual(response.status_code, self.success_status)
1386
def test_new_account_captcha_whitelist_fail(self):
1387
email = 'notcanonicaltest@gmail.com'
1388
pattern = '^canonicaltest(?:\+.+)?@gmail\.com$'
1389
with self.settings(EMAIL_WHITELIST_REGEXP_LIST=[pattern]):
1390
response = self.post_new_account(email=email)
1391
msg = 'It appears that our captcha service was unable to load'
1392
self.assertContains(response, msg)
1394
@patch.object(Captcha, '_open')
1395
def test_uses_timeline_from_request(self, mock_open):
1396
mock_open.return_value = CaptchaResponse(200, StringIO('true\na'))
1398
timeline = Timeline()
1399
request.META = {'timeline.timeline': timeline}
1400
request.POST = {'recaptcha_challenge_field': 'captcha-id'}
1401
request.environ = {'REMOTE_ADDR': '127.0.0.1'}
1402
ui._verify_captcha_response(None, request, None)
1403
self.assertEqual(1, len(timeline.actions))
1404
self.assertEqual('captcha-verify', timeline.actions[0].category)
1407
1323
class CookiesTestCase(SSOBaseTestCase):
1409
1325
def setUp(self):