~deadlight/canonical-identity-provider/logout

« back to all changes in this revision

Viewing changes to src/webui/tests/test_views_ui.py

  • Committer: Ubuntu One Auto Copilot
  • Author(s): Maximiliano Bertacchini
  • Date: 2018-10-25 18:05:01 UTC
  • mfrom: (1664.4.12 drop-recaptcha-v1)
  • Revision ID: otto-copilot@canonical.com-20181025180501-jdf7qwlrpef93aac
Drop the last bits of reCaptcha V1 code.

Merged from https://code.launchpad.net/~maxiberta/canonical-identity-provider/drop-recaptcha-v1/+merge/356937

Show diffs side-by-side

added added

removed removed

Lines of Context:
10
10
import urllib2
11
11
from datetime import date
12
12
from functools import partial
13
 
from StringIO import StringIO
14
13
from urlparse import urlsplit
15
14
 
16
15
from django.conf import settings
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
50
48
    OpenIDRPConfig,
51
49
    twofactor,
52
50
)
53
 
from identityprovider.models.captcha import (
54
 
    Captcha,
55
 
    CaptchaResponse,
56
 
    CaptchaV2,
57
 
)
 
51
from identityprovider.models.captcha import CaptchaV2
58
52
from identityprovider.models.const import (
59
53
    AccountStatus,
60
54
    AuthLogType,
65
59
from identityprovider.tests import DEFAULT_USER_PASSWORD
66
60
from identityprovider.tests.test_auth import AuthLogTestCaseMixin
67
61
from identityprovider.tests.utils import (
68
 
    MockHandler,
69
62
    SSOBaseTestCase,
70
63
    TimelineActionMixin,
71
64
)
115
108
            'passwordconfirm': 'Testing123',
116
109
            'accept_tos': True
117
110
        }
118
 
        if gargoyle.is_active('CAPTCHA'):
119
 
            data['recaptcha_challenge_field'] = 'ignored'
120
 
            data['recaptcha_response_field'] = 'ignored'
121
111
 
122
112
        return self.client.post(url, data, follow=follow)
123
113
 
132
122
        assert self.client.login(
133
123
            username=self.data['email'], password=self.data['password'])
134
124
 
135
 
    def request_when_captcha_fails(self, url, data):
136
 
        class MockCaptcha(object):
137
 
            def __init__(self, *args):
138
 
                pass
139
 
 
140
 
            def verify(self, solution, ip_addr, email):
141
 
                self.message = 'no-challenge'
142
 
                return False
143
 
 
144
 
            @classmethod
145
 
            def new(cls, env):
146
 
                return cls()
147
 
 
148
 
        with patch.object(ui, 'Captcha', MockCaptcha):
149
 
            r = self.client.post(url, data)
150
 
 
151
 
        return r
152
 
 
153
125
 
154
126
@override_settings(LANGUAGE_CODE='es')
155
127
class SpanishUIViewsTestCase(BaseTestCase):
1348
1320
        self.assertFalse(email.is_verified)
1349
1321
 
1350
1322
 
1351
 
@override_settings(CAPTCHA_PRIVATE_KEY='some-private-key')
1352
 
class CaptchaVerificationTestCase(BaseTestCase):
1353
 
 
1354
 
    success_status = 302
1355
 
 
1356
 
    def setUp(self):
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))
1361
 
 
1362
 
        p = switches(CAPTCHA=True)
1363
 
        p.patch()
1364
 
        self.addCleanup(p.unpatch)
1365
 
 
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)
1371
 
 
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)
1378
 
 
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)
1385
 
 
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)
1393
 
 
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'))
1397
 
        request = Mock()
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)
1405
 
 
1406
 
 
1407
1323
class CookiesTestCase(SSOBaseTestCase):
1408
1324
 
1409
1325
    def setUp(self):