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