[3144] | 1 | import re |
---|
[1957] | 2 | import string |
---|
[3144] | 3 | import unittest |
---|
| 4 | from quintagroup.captcha.core.config import PRODUCT_NAME, GLOBALS |
---|
| 5 | from base import testPatch |
---|
| 6 | from quintagroup.captcha.core.utils import getWord, decrypt, parseKey |
---|
[1957] | 7 | |
---|
[3144] | 8 | from Products.PloneTestCase import PloneTestCase as ptc |
---|
| 9 | from Products.PloneTestCase.PloneTestCase import portal_owner |
---|
| 10 | from Products.PloneTestCase.PloneTestCase import default_password |
---|
| 11 | |
---|
[1957] | 12 | from Products.CMFCore.DirectoryView import registerDirectory |
---|
| 13 | from Products.CMFCore.DirectoryView import addDirectoryViews |
---|
| 14 | |
---|
| 15 | NOT_VALID = re.compile("Please re\-enter validation code") |
---|
[2019] | 16 | IMAGE_PATT = '\s+src="%s(/getCaptchaImage/[0-9a-fA-F]+)"' |
---|
[1957] | 17 | |
---|
[1991] | 18 | # patch to use test images and dictionary |
---|
| 19 | testPatch() |
---|
| 20 | |
---|
[3128] | 21 | |
---|
[2019] | 22 | def addTestLayer(self): |
---|
| 23 | # Install test_captcha skin layer |
---|
| 24 | registerDirectory('tests', GLOBALS) |
---|
| 25 | skins = self.portal.portal_skins |
---|
| 26 | addDirectoryViews(skins, 'tests', GLOBALS) |
---|
| 27 | skinName = skins.getDefaultSkin() |
---|
| 28 | paths = map(string.strip, skins.getSkinPath(skinName).split(',')) |
---|
[3128] | 29 | paths.insert(paths.index('custom') + 1, 'test_captcha') |
---|
[2019] | 30 | skins.addSkinSelection(skinName, ','.join(paths)) |
---|
| 31 | self._refreshSkinData() |
---|
| 32 | |
---|
| 33 | |
---|
[1957] | 34 | class TestCaptchaWidget(ptc.FunctionalTestCase): |
---|
| 35 | |
---|
| 36 | def afterSetUp(self): |
---|
| 37 | self.loginAsPortalOwner() |
---|
| 38 | self.addProduct(PRODUCT_NAME) |
---|
[2019] | 39 | addTestLayer(self) |
---|
[1957] | 40 | self.portal.invokeFactory('Document', 'index_html') |
---|
| 41 | self.portal['index_html'].allowDiscussion(True) |
---|
| 42 | self.absolute_url = self.portal['index_html'].absolute_url_path() |
---|
| 43 | |
---|
[3128] | 44 | self.basic_auth = ':'.join((portal_owner, default_password)) |
---|
[1957] | 45 | self.captcha_key = self.portal.captcha_key |
---|
| 46 | |
---|
| 47 | def testImage(self): |
---|
| 48 | path = '%s/test_form' % self.absolute_url |
---|
[3128] | 49 | response = self.publish(path, self.basic_auth, |
---|
| 50 | request_method='GET').getBody() |
---|
[2019] | 51 | patt = re.compile(IMAGE_PATT % self.portal.absolute_url()) |
---|
[1957] | 52 | match_obj = patt.search(response) |
---|
| 53 | |
---|
| 54 | img_url = match_obj.group(1) |
---|
[3128] | 55 | res = self.publish('/plone' + img_url, self.basic_auth) |
---|
| 56 | content_type = res.getHeader('content-type') |
---|
[1957] | 57 | self.assert_(content_type.startswith('image')) |
---|
| 58 | |
---|
| 59 | def testSubmitRightCaptcha(self): |
---|
| 60 | hashkey = self.portal.getCaptcha() |
---|
[3128] | 61 | # index of word number starts from 1, |
---|
| 62 | # but index of dictionary starts from 0 |
---|
| 63 | decrypted_key = decrypt(self.captcha_key, hashkey) |
---|
| 64 | key = getWord(int(parseKey(decrypted_key)['key']) - 1) |
---|
[1957] | 65 | parameters = 'form.submitted=1&key=%s' % key |
---|
| 66 | path = '%s/test_form?%s' % (self.absolute_url, parameters) |
---|
| 67 | extra = {'hashkey': hashkey, |
---|
| 68 | 'form.button.Save': 'Save'} |
---|
[3128] | 69 | response = self.publish(path, self.basic_auth, extra=extra, |
---|
| 70 | request_method='GET').getBody() |
---|
[1957] | 71 | self.assert_(not NOT_VALID.search(response)) |
---|
| 72 | |
---|
| 73 | def testSubmitWrongCaptcha(self): |
---|
| 74 | hashkey = self.portal.getCaptcha() |
---|
| 75 | parameters = 'form.submitted=1&key=fdfgh' |
---|
| 76 | path = '%s/test_form?%s' % (self.absolute_url, parameters) |
---|
| 77 | extra = {'hashkey': hashkey, |
---|
| 78 | 'form.button.Save': 'Save'} |
---|
[3128] | 79 | response = self.publish(path, self.basic_auth, extra=extra, |
---|
| 80 | request_method='GET').getBody() |
---|
[1957] | 81 | self.assert_(NOT_VALID.search(response)) |
---|
| 82 | |
---|
| 83 | def testSubmitRightCaptchaTwice(self): |
---|
| 84 | hashkey = self.portal.getCaptcha() |
---|
[3128] | 85 | decrypted_key = decrypt(self.captcha_key, hashkey) |
---|
| 86 | key = getWord(int(parseKey(decrypted_key)['key']) - 1) |
---|
| 87 | parameters = 'form.submitted=1&key=%s' % key |
---|
| 88 | path = '%s/test_form?%s' % (self.absolute_url, parameters) |
---|
[1957] | 89 | extra = {'hashkey': hashkey, |
---|
| 90 | 'form.button.Save': 'Save'} |
---|
| 91 | self.publish(path, self.basic_auth, extra=extra, request_method='GET') |
---|
[3128] | 92 | response = self.publish(path, self.basic_auth, extra=extra, |
---|
| 93 | request_method='GET').getBody() |
---|
[1957] | 94 | |
---|
| 95 | self.assert_(NOT_VALID.search(response)) |
---|
| 96 | |
---|
[2542] | 97 | def testCaptchaWidget(self): |
---|
| 98 | # captcha core related issue, described in |
---|
| 99 | # in http://plone.org/products/plone-comments/issues/5 |
---|
[3128] | 100 | resp = self.publish(self.portal.absolute_url(1) + "/captcha_widget") |
---|
[2542] | 101 | self.assertEqual(resp.status / 100, 2) |
---|
[1957] | 102 | |
---|
[3128] | 103 | |
---|
[1957] | 104 | def test_suite(): |
---|
| 105 | suite = unittest.TestSuite() |
---|
| 106 | suite.addTest(unittest.makeSuite(TestCaptchaWidget)) |
---|
| 107 | return suite |
---|