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