import string from base import * from Products.CMFCore.DirectoryView import registerDirectory from Products.CMFCore.DirectoryView import addDirectoryViews from Products.CMFCore.DirectoryView import DirectoryView NOT_VALID = re.compile("Please re\-enter validation code") class TestCaptchaWidget(ptc.FunctionalTestCase): def addTestLayer(self): # Install test_captcha skin layer registerDirectory('tests', GLOBALS) skins = self.portal.portal_skins addDirectoryViews(skins, 'tests', GLOBALS) skinName = skins.getDefaultSkin() paths = map(string.strip, skins.getSkinPath(skinName).split(',')) paths.insert(paths.index('custom')+1, 'test_captcha') skins.addSkinSelection(skinName, ','.join(paths)) self._refreshSkinData() def afterSetUp(self): self.loginAsPortalOwner() self.addProduct(PRODUCT_NAME) self.addTestLayer() self.portal.invokeFactory('Document', 'index_html') self.portal['index_html'].allowDiscussion(True) self.absolute_url = self.portal['index_html'].absolute_url_path() self.basic_auth = ':'.join((portal_owner,default_password)) self.captcha_key = self.portal.captcha_key def testImage(self): path = '%s/test_form' % self.absolute_url response = self.publish(path, self.basic_auth, request_method='GET').getBody() patt = re.compile('\s+src="%s(/getCaptchaImage/[0-9a-fA-F]+)"' % self.portal.absolute_url()) match_obj = patt.search(response) img_url = match_obj.group(1) content_type = self.publish('/plone' + img_url, self.basic_auth).getHeader('content-type') self.assert_(content_type.startswith('image')) def testSubmitRightCaptcha(self): hashkey = self.portal.getCaptcha() # index of word number starts from 1, but index of dictionary starts from 0 key = getWord(int(parseKey(decrypt(self.captcha_key, hashkey))['key'])-1 ) parameters = 'form.submitted=1&key=%s' % key path = '%s/test_form?%s' % (self.absolute_url, parameters) extra = {'hashkey': hashkey, 'form.button.Save': 'Save'} response = self.publish(path, self.basic_auth, extra=extra, request_method='GET').getBody() self.assert_(not NOT_VALID.search(response)) def testSubmitWrongCaptcha(self): hashkey = self.portal.getCaptcha() parameters = 'form.submitted=1&key=fdfgh' path = '%s/test_form?%s' % (self.absolute_url, parameters) extra = {'hashkey': hashkey, 'form.button.Save': 'Save'} response = self.publish(path, self.basic_auth, extra=extra, request_method='GET').getBody() self.assert_(NOT_VALID.search(response)) def testSubmitRightCaptchaTwice(self): hashkey = self.portal.getCaptcha() key = getWord(int(parseKey(decrypt(self.captcha_key, hashkey))['key'])-1) parameters = 'form.submitted=1&key=%s'%key path = '%s/test_form?%s'%(self.absolute_url, parameters) extra = {'hashkey': hashkey, 'form.button.Save': 'Save'} self.publish(path, self.basic_auth, extra=extra, request_method='GET') response = self.publish(path, self.basic_auth, extra=extra, request_method='GET').getBody() self.assert_(NOT_VALID.search(response)) def test_suite(): suite = unittest.TestSuite() suite.addTest(unittest.makeSuite(TestCaptchaWidget)) return suite