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