[1] | 1 | # |
---|
| 2 | # Skeleton PloneTestCase |
---|
| 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('qPloneCaptchas') |
---|
| 14 | PloneTestCase.setupPloneSite() |
---|
| 15 | |
---|
| 16 | class TestCaptchaWidget(PloneTestCase.FunctionalTestCase): |
---|
| 17 | |
---|
| 18 | def afterSetUp(self): |
---|
| 19 | self.loginAsPortalOwner() |
---|
| 20 | self.addProduct('qPloneCaptchas') |
---|
| 21 | self.portal.invokeFactory('Document', 'index_html') |
---|
| 22 | self.portal['index_html'].allow_discussion = True |
---|
| 23 | self.absolute_url = self.portal['index_html'].absolute_url_path() |
---|
| 24 | |
---|
| 25 | self.basic_auth = 'portal_manager:secret' |
---|
| 26 | uf = self.app.acl_users |
---|
| 27 | uf.userFolderAddUser('portal_manager', 'secret', ['Manager'], []) |
---|
| 28 | user = uf.getUserById('portal_manager') |
---|
| 29 | if not hasattr(user, 'aq_base'): |
---|
| 30 | user = user.__of__(uf) |
---|
| 31 | newSecurityManager(None, user) |
---|
| 32 | self.captcha_key = self.portal.captcha_key |
---|
| 33 | |
---|
| 34 | def testSubmitRightCaptcha(self): |
---|
| 35 | hashkey = self.portal.getCaptcha() |
---|
| 36 | key = getWord(int(parseKey(decrypt(self.captcha_key, hashkey))['key'])) |
---|
| 37 | parameters = 'form.submitted=1&Creator=test_user&key=%s'%key |
---|
| 38 | path = '%s/discussion_reply_form?%s'%(self.absolute_url, parameters) |
---|
| 39 | extra = {'hashkey': hashkey, |
---|
| 40 | 'subject': 'testing', |
---|
| 41 | 'body_text': 'Text in Comment', |
---|
| 42 | 'discussion_reply:method': 'Save'} |
---|
| 43 | |
---|
| 44 | response = self.publish(path, self.basic_auth, extra=extra, request_method='GET').getBody() |
---|
| 45 | patt = re.compile(".*?Comment\+added") |
---|
| 46 | match_obj = patt.match(response) |
---|
| 47 | self.assert_(match_obj) |
---|
| 48 | |
---|
| 49 | |
---|
| 50 | |
---|
| 51 | def testSubmitWrongCaptcha(self): |
---|
| 52 | hashkey = self.portal.getCaptcha() |
---|
| 53 | parameters = 'form.submitted=1&Creator=test_user&key=fdfgh' |
---|
| 54 | path = '%s/discussion_reply_form?%s'%(self.absolute_url, parameters) |
---|
| 55 | extra = {'hashkey': hashkey, |
---|
| 56 | 'subject': 'testing', |
---|
| 57 | 'body_text': 'Text in Comment', |
---|
| 58 | 'discussion_reply:method': 'Save'} |
---|
| 59 | |
---|
| 60 | response = self.publish(path, self.basic_auth, extra=extra, request_method='GET').getBody() |
---|
| 61 | patt = re.compile("Please re\-enter validation code") |
---|
| 62 | match_obj = patt.search(response) |
---|
| 63 | self.assert_(match_obj) |
---|
| 64 | |
---|
| 65 | def testSubmitRightCaptchaTwice(self): |
---|
| 66 | hashkey = self.portal.getCaptcha() |
---|
| 67 | key = getWord(int(parseKey(decrypt(self.captcha_key, hashkey))['key'])) |
---|
| 68 | parameters = 'form.submitted=1&Creator=test_user&key=%s'%key |
---|
| 69 | path = '%s/discussion_reply_form?%s'%(self.absolute_url, parameters) |
---|
| 70 | extra = {'hashkey': hashkey, |
---|
| 71 | 'subject': 'testing', |
---|
| 72 | 'body_text': 'Text in Comment', |
---|
| 73 | 'discussion_reply:method': 'Save'} |
---|
| 74 | |
---|
| 75 | self.publish(path, self.basic_auth, extra=extra, request_method='GET') |
---|
| 76 | response = self.publish(path, self.basic_auth, extra=extra, request_method='GET').getBody() |
---|
| 77 | patt = re.compile(".*?Comment\+added") |
---|
| 78 | match_obj = patt.match(response) |
---|
| 79 | self.assert_(not match_obj) |
---|
| 80 | |
---|
| 81 | def testSubmitExpiredCaptcha(self): |
---|
| 82 | pass |
---|
| 83 | """ |
---|
| 84 | hashkey = self.portal.getCaptcha() |
---|
| 85 | key = getWord(int(parseKey(decrypt(self.captcha_key,hashkey))['key'])) |
---|
| 86 | parameters = 'form.submitted=1&Creator=test_user&key=%s'%key |
---|
| 87 | path = '%s/discussion_reply_form?%s'%(self.absolute_url, parameters) |
---|
| 88 | extra = {'hashkey': hashkey, |
---|
| 89 | 'subject': 'testing', |
---|
| 90 | 'body_text': 'Text in Comment', |
---|
| 91 | 'discussion_reply:method': 'Save'} |
---|
| 92 | |
---|
| 93 | from DateTime import DateTime |
---|
| 94 | last = repr(DateTime().timeTime()) |
---|
| 95 | import time |
---|
| 96 | DateTime._t = time.time() + 3601.0 |
---|
| 97 | raise repr(DateTime().timeTime())+' '+last |
---|
| 98 | |
---|
| 99 | response = self.publish(path, self.basic_auth, extra=extra, request_method='GET').getBody() |
---|
| 100 | DateTime._t = time.time() |
---|
| 101 | patt = re.compile(".*?Comment\+added") |
---|
| 102 | match_obj = patt.match(response) |
---|
| 103 | self.assert_(match_obj) |
---|
| 104 | """ |
---|
| 105 | |
---|
| 106 | class TestInstallation(PloneTestCase.FunctionalTestCase): |
---|
| 107 | |
---|
| 108 | def afterSetUp(self): |
---|
| 109 | self.loginAsPortalOwner() |
---|
| 110 | self.addProduct('qPloneCaptchas') |
---|
| 111 | |
---|
| 112 | def testCaptchaKey(self): |
---|
| 113 | ck = getattr(self.portal, 'captcha_key') |
---|
| 114 | self.assert_(ck) |
---|
| 115 | self.assertEqual(len(ck), 8) |
---|
| 116 | |
---|
| 117 | def testCaptchaTool(self): |
---|
| 118 | self.assert_('portal_captchas' in self.portal.objectIds()) |
---|
| 119 | |
---|
| 120 | |
---|
| 121 | def test_suite(): |
---|
| 122 | from unittest import TestSuite, makeSuite |
---|
| 123 | suite = TestSuite() |
---|
| 124 | suite.addTest(makeSuite(TestCaptchaWidget)) |
---|
| 125 | suite.addTest(makeSuite(TestInstallation)) |
---|
| 126 | return suite |
---|
| 127 | |
---|
| 128 | if __name__ == '__main__': |
---|
| 129 | framework() |
---|
| 130 | |
---|