[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 |
---|
[694] | 11 | from Products.CMFCore.utils import getToolByName |
---|
[1] | 12 | from Products.qPloneCaptchas.utils import getWord, decrypt, parseKey |
---|
[694] | 13 | from Products.qPloneCaptchas.config import * |
---|
[1] | 14 | |
---|
| 15 | PloneTestCase.installProduct('PlacelessTranslationService') |
---|
| 16 | PloneTestCase.installProduct('qPloneCaptchas') |
---|
| 17 | PloneTestCase.setupPloneSite() |
---|
| 18 | |
---|
| 19 | class TestCaptchaWidget(PloneTestCase.FunctionalTestCase): |
---|
| 20 | |
---|
| 21 | def afterSetUp(self): |
---|
| 22 | self.loginAsPortalOwner() |
---|
| 23 | self.addProduct('qPloneCaptchas') |
---|
| 24 | self.portal.invokeFactory('Document', 'index_html') |
---|
[241] | 25 | self.portal['index_html'].allowDiscussion(True) |
---|
[1] | 26 | self.absolute_url = self.portal['index_html'].absolute_url_path() |
---|
| 27 | |
---|
| 28 | self.basic_auth = 'portal_manager:secret' |
---|
| 29 | uf = self.app.acl_users |
---|
| 30 | uf.userFolderAddUser('portal_manager', 'secret', ['Manager'], []) |
---|
| 31 | user = uf.getUserById('portal_manager') |
---|
| 32 | if not hasattr(user, 'aq_base'): |
---|
| 33 | user = user.__of__(uf) |
---|
| 34 | newSecurityManager(None, user) |
---|
| 35 | self.captcha_key = self.portal.captcha_key |
---|
| 36 | |
---|
| 37 | def testImage(self): |
---|
| 38 | path = '%s/discussion_reply_form'%self.absolute_url |
---|
| 39 | resp1 = self.publish(path, self.basic_auth, request_method='GET').getBody() |
---|
[241] | 40 | patt = re.compile('\s+src="%s(/getCaptchaImage/[0-9a-fA-F]+)"'%self.portal.absolute_url()) |
---|
[1] | 41 | match_obj = patt.search(resp1) |
---|
| 42 | img_url = match_obj.group(1) |
---|
| 43 | content_type = self.publish('/plone'+img_url, self.basic_auth).getHeader('content-type') |
---|
| 44 | self.assert_(content_type.startswith('image')) |
---|
| 45 | |
---|
| 46 | def testSubmitRightCaptcha(self): |
---|
| 47 | hashkey = self.portal.getCaptcha() |
---|
| 48 | key = getWord(int(parseKey(decrypt(self.captcha_key, hashkey))['key'])) |
---|
| 49 | parameters = 'form.submitted=1&Creator=test_user&key=%s'%key |
---|
| 50 | path = '%s/discussion_reply_form?%s'%(self.absolute_url, parameters) |
---|
| 51 | extra = {'hashkey': hashkey, |
---|
| 52 | 'subject': 'testing', |
---|
| 53 | 'body_text': 'Text in Comment', |
---|
| 54 | 'discussion_reply:method': 'Save'} |
---|
| 55 | |
---|
| 56 | response = self.publish(path, self.basic_auth, extra=extra, request_method='GET').getBody() |
---|
| 57 | patt = re.compile("Please re\-enter validation code") |
---|
| 58 | match_obj = patt.match(response) |
---|
| 59 | self.assert_(not match_obj) |
---|
| 60 | |
---|
| 61 | def testSubmitWrongCaptcha(self): |
---|
| 62 | hashkey = self.portal.getCaptcha() |
---|
| 63 | parameters = 'form.submitted=1&Creator=test_user&key=fdfgh' |
---|
| 64 | path = '%s/discussion_reply_form?%s'%(self.absolute_url, parameters) |
---|
| 65 | extra = {'hashkey': hashkey, |
---|
| 66 | 'subject': 'testing', |
---|
| 67 | 'body_text': 'Text in Comment', |
---|
| 68 | 'discussion_reply:method': 'Save'} |
---|
| 69 | |
---|
| 70 | response = self.publish(path, self.basic_auth, extra=extra, request_method='GET').getBody() |
---|
| 71 | patt = re.compile("Please re\-enter validation code") |
---|
| 72 | match_obj = patt.search(response) |
---|
| 73 | self.assert_(match_obj) |
---|
| 74 | |
---|
| 75 | def testSubmitRightCaptchaTwice(self): |
---|
| 76 | hashkey = self.portal.getCaptcha() |
---|
| 77 | key = getWord(int(parseKey(decrypt(self.captcha_key, hashkey))['key'])) |
---|
| 78 | parameters = 'form.submitted=1&Creator=test_user&key=%s'%key |
---|
| 79 | path = '%s/discussion_reply_form?%s'%(self.absolute_url, parameters) |
---|
| 80 | extra = {'hashkey': hashkey, |
---|
| 81 | 'subject': 'testing', |
---|
| 82 | 'body_text': 'Text in Comment', |
---|
| 83 | 'discussion_reply:method': 'Save'} |
---|
| 84 | |
---|
| 85 | self.publish(path, self.basic_auth, extra=extra, request_method='GET') |
---|
| 86 | response = self.publish(path, self.basic_auth, extra=extra, request_method='GET').getBody() |
---|
| 87 | patt = re.compile(".*?Comment\+added") |
---|
| 88 | match_obj = patt.match(response) |
---|
| 89 | self.assert_(not match_obj) |
---|
| 90 | |
---|
| 91 | class TestInstallation(PloneTestCase.FunctionalTestCase): |
---|
| 92 | |
---|
| 93 | def afterSetUp(self): |
---|
| 94 | self.loginAsPortalOwner() |
---|
[694] | 95 | self.qi = getToolByName(self.portal, 'portal_quickinstaller', None) |
---|
| 96 | self.cp = getToolByName(self.portal, 'portal_controlpanel', None) |
---|
| 97 | self.st = getToolByName(self.portal, 'portal_skins', None) |
---|
| 98 | self.qi.installProduct(PRODUCT_NAME) |
---|
[1] | 99 | |
---|
[694] | 100 | def getLayers(self): |
---|
| 101 | Layers = [] |
---|
| 102 | Layers += LAYERS |
---|
| 103 | Layers.append(LAYER_STATIC_CAPTCHAS) |
---|
| 104 | DiscussionLayer = LAYER_DISCUSSION |
---|
| 105 | |
---|
| 106 | mtool = getToolByName(self, 'portal_migration') |
---|
| 107 | plone_version = mtool.getFileSystemVersion() |
---|
| 108 | if plone_version.startswith('2.1'): |
---|
| 109 | plone_version = '2.1.2' |
---|
| 110 | elif plone_version.startswith('2.0'): |
---|
| 111 | plone_version = '2.0.5' |
---|
| 112 | elif plone_version.startswith('2.5'): |
---|
| 113 | plone_version = '2.5' |
---|
| 114 | elif plone_version.startswith('3.0'): |
---|
| 115 | plone_version = '3.0' |
---|
| 116 | elif plone_version.startswith('3.1'): |
---|
| 117 | plone_version = '3.1' |
---|
| 118 | else: |
---|
| 119 | raise Exception("Error - Unsupported version. Suported versions: Plone 2.0.5-3") |
---|
| 120 | |
---|
| 121 | if self.qi.isProductInstalled('PloneFormMailer'): |
---|
| 122 | formmailer_layer = LAYER_FORMMAILER+'/'+ plone_version |
---|
| 123 | Layers.append(formmailer_layer) |
---|
| 124 | |
---|
| 125 | discussion_layer = '/'.join([DiscussionLayer, plone_version]) |
---|
| 126 | Layers.append(discussion_layer) |
---|
| 127 | |
---|
| 128 | join_form_layer = '/'.join([LAYER_JOIN_FORM, plone_version]) |
---|
| 129 | Layers.append(join_form_layer) |
---|
| 130 | |
---|
| 131 | sendto_form_layer = '/'.join([LAYER_SENDTO_FORM, plone_version]) |
---|
| 132 | Layers.append(sendto_form_layer) |
---|
| 133 | |
---|
| 134 | return Layers |
---|
| 135 | |
---|
| 136 | def test_configlet_install(self): |
---|
| 137 | self.assert_(CONFIGLET_ID in [a.getId() for a in self.cp.listActions()], 'Configlet not found') |
---|
| 138 | |
---|
| 139 | def test_skins_install(self): |
---|
| 140 | skinstool = self.st |
---|
| 141 | Layers = self.getLayers() |
---|
| 142 | for skin in skinstool.getSkinSelections(): |
---|
| 143 | path = skinstool.getSkinPath(skin) |
---|
| 144 | path = map(str.strip, path.split(',')) |
---|
| 145 | for layer in Layers: |
---|
| 146 | self.assert_(layer.split('/')[0] in skinstool.objectIds(), '%s directory view not found in portal_skins after installation' % layer) |
---|
| 147 | self.assert_(layer in path, '%s layer not found in %s' % (PRODUCT_NAME, skin)) |
---|
| 148 | |
---|
| 149 | def test_skins_uninstall(self): |
---|
| 150 | self.qi.uninstallProducts([PRODUCT_NAME]) |
---|
| 151 | self.assertNotEqual(self.qi.isProductInstalled(PRODUCT_NAME), True,'%s is already installed' % PRODUCT_NAME) |
---|
| 152 | skinstool = self.st |
---|
| 153 | Layers = self.getLayers() |
---|
| 154 | for skin in skinstool.getSkinSelections(): |
---|
| 155 | path = skinstool.getSkinPath(skin) |
---|
| 156 | path = map(str.strip, path.split(',')) |
---|
| 157 | for layer in Layers: |
---|
| 158 | self.assert_(not layer.split('/')[0] in skinstool.objectIds(), '%s directory view found in portal_skins after uninstallation' % layer) |
---|
| 159 | self.assert_(not layer in path, '%s layer found in %s after uninstallation' % (layer, skin)) |
---|
| 160 | |
---|
| 161 | def test_configlet_uninstall(self): |
---|
| 162 | self.qi.uninstallProducts([PRODUCT_NAME]) |
---|
| 163 | self.assertNotEqual(self.qi.isProductInstalled(PRODUCT_NAME), True,'%s is already installed' % PRODUCT_NAME) |
---|
| 164 | self.assert_(not CONFIGLET_ID in [a.getId() for a in self.cp.listActions()], 'Configlet found after uninstallation') |
---|
| 165 | |
---|
[1] | 166 | def testCaptchaKey(self): |
---|
| 167 | ck = getattr(self.portal, 'captcha_key') |
---|
| 168 | self.assert_(ck) |
---|
| 169 | self.assertEqual(len(ck), 8) |
---|
| 170 | |
---|
| 171 | def testCaptchaTool(self): |
---|
| 172 | self.assert_('portal_captchas' in self.portal.objectIds()) |
---|
| 173 | |
---|
| 174 | |
---|
| 175 | def test_suite(): |
---|
| 176 | from unittest import TestSuite, makeSuite |
---|
| 177 | suite = TestSuite() |
---|
| 178 | suite.addTest(makeSuite(TestCaptchaWidget)) |
---|
| 179 | suite.addTest(makeSuite(TestInstallation)) |
---|
| 180 | return suite |
---|
| 181 | |
---|
| 182 | if __name__ == '__main__': |
---|
| 183 | framework() |
---|