[1] | 1 | # |
---|
| 2 | # Tests for qPloneCaptchas |
---|
| 3 | # |
---|
| 4 | |
---|
| 5 | import os, sys, re |
---|
| 6 | if __name__ == '__main__': |
---|
| 7 | execfile(os.path.join(sys.path[0], 'framework.py')) |
---|
| 8 | |
---|
| 9 | from Products.PloneTestCase import PloneTestCase |
---|
| 10 | from AccessControl.SecurityManagement import newSecurityManager |
---|
| 11 | from Products.qPloneCaptchas.utils import getWord, decrypt, parseKey |
---|
| 12 | |
---|
| 13 | PloneTestCase.installProduct('PlacelessTranslationService') |
---|
| 14 | PloneTestCase.installProduct('qPloneCaptchas') |
---|
| 15 | PloneTestCase.setupPloneSite() |
---|
| 16 | |
---|
| 17 | class TestCaptchaWidget(PloneTestCase.FunctionalTestCase): |
---|
| 18 | |
---|
| 19 | def afterSetUp(self): |
---|
| 20 | self.loginAsPortalOwner() |
---|
| 21 | self.addProduct('qPloneCaptchas') |
---|
| 22 | self.portal.invokeFactory('Document', 'index_html') |
---|
| 23 | self.portal['index_html'].allow_discussion = True |
---|
| 24 | self.absolute_url = self.portal['index_html'].absolute_url_path() |
---|
| 25 | |
---|
| 26 | self.basic_auth = 'portal_manager:secret' |
---|
| 27 | uf = self.app.acl_users |
---|
| 28 | uf.userFolderAddUser('portal_manager', 'secret', ['Manager'], []) |
---|
| 29 | user = uf.getUserById('portal_manager') |
---|
| 30 | if not hasattr(user, 'aq_base'): |
---|
| 31 | user = user.__of__(uf) |
---|
| 32 | newSecurityManager(None, user) |
---|
| 33 | self.captcha_key = self.portal.captcha_key |
---|
| 34 | |
---|
| 35 | def testImage(self): |
---|
| 36 | path = '%s/discussion_reply_form'%self.absolute_url |
---|
| 37 | resp1 = self.publish(path, self.basic_auth, request_method='GET').getBody() |
---|
| 38 | patt = re.compile('<img\s+src="%s(/getCaptchaImage/[0-9a-fA-F]+)"'%self.portal.absolute_url()) |
---|
| 39 | match_obj = patt.search(resp1) |
---|
| 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&Creator=test_user&key=%s'%key |
---|
| 48 | path = '%s/discussion_reply_form?%s'%(self.absolute_url, parameters) |
---|
| 49 | extra = {'hashkey': hashkey, |
---|
| 50 | 'subject': 'testing', |
---|
| 51 | 'body_text': 'Text in Comment', |
---|
| 52 | 'discussion_reply:method': 'Save'} |
---|
| 53 | |
---|
| 54 | response = self.publish(path, self.basic_auth, extra=extra, request_method='GET').getBody() |
---|
| 55 | patt = re.compile("Please re\-enter validation code") |
---|
| 56 | match_obj = patt.match(response) |
---|
| 57 | self.assert_(not match_obj) |
---|
| 58 | |
---|
| 59 | def testSubmitWrongCaptcha(self): |
---|
| 60 | hashkey = self.portal.getCaptcha() |
---|
| 61 | parameters = 'form.submitted=1&Creator=test_user&key=fdfgh' |
---|
| 62 | path = '%s/discussion_reply_form?%s'%(self.absolute_url, parameters) |
---|
| 63 | extra = {'hashkey': hashkey, |
---|
| 64 | 'subject': 'testing', |
---|
| 65 | 'body_text': 'Text in Comment', |
---|
| 66 | 'discussion_reply:method': 'Save'} |
---|
| 67 | |
---|
| 68 | response = self.publish(path, self.basic_auth, extra=extra, request_method='GET').getBody() |
---|
| 69 | patt = re.compile("Please re\-enter validation code") |
---|
| 70 | match_obj = patt.search(response) |
---|
| 71 | self.assert_(match_obj) |
---|
| 72 | |
---|
| 73 | def testSubmitRightCaptchaTwice(self): |
---|
| 74 | hashkey = self.portal.getCaptcha() |
---|
| 75 | key = getWord(int(parseKey(decrypt(self.captcha_key, hashkey))['key'])) |
---|
| 76 | parameters = 'form.submitted=1&Creator=test_user&key=%s'%key |
---|
| 77 | path = '%s/discussion_reply_form?%s'%(self.absolute_url, parameters) |
---|
| 78 | extra = {'hashkey': hashkey, |
---|
| 79 | 'subject': 'testing', |
---|
| 80 | 'body_text': 'Text in Comment', |
---|
| 81 | 'discussion_reply:method': 'Save'} |
---|
| 82 | |
---|
| 83 | self.publish(path, self.basic_auth, extra=extra, request_method='GET') |
---|
| 84 | response = self.publish(path, self.basic_auth, extra=extra, request_method='GET').getBody() |
---|
| 85 | patt = re.compile(".*?Comment\+added") |
---|
| 86 | match_obj = patt.match(response) |
---|
| 87 | self.assert_(not match_obj) |
---|
| 88 | |
---|
| 89 | class TestInstallation(PloneTestCase.FunctionalTestCase): |
---|
| 90 | |
---|
| 91 | def afterSetUp(self): |
---|
| 92 | self.loginAsPortalOwner() |
---|
| 93 | self.addProduct('qPloneCaptchas') |
---|
| 94 | |
---|
| 95 | def testCaptchaKey(self): |
---|
| 96 | ck = getattr(self.portal, 'captcha_key') |
---|
| 97 | self.assert_(ck) |
---|
| 98 | self.assertEqual(len(ck), 8) |
---|
| 99 | |
---|
| 100 | def testCaptchaTool(self): |
---|
| 101 | self.assert_('portal_captchas' in self.portal.objectIds()) |
---|
| 102 | |
---|
| 103 | |
---|
| 104 | def test_suite(): |
---|
| 105 | from unittest import TestSuite, makeSuite |
---|
| 106 | suite = TestSuite() |
---|
| 107 | suite.addTest(makeSuite(TestCaptchaWidget)) |
---|
| 108 | suite.addTest(makeSuite(TestInstallation)) |
---|
| 109 | return suite |
---|
| 110 | |
---|
| 111 | if __name__ == '__main__': |
---|
| 112 | framework() |
---|