[797] | 1 | import unittest |
---|
| 2 | import os |
---|
| 3 | import sys |
---|
| 4 | import re |
---|
| 5 | |
---|
| 6 | from Products.Five import zcml |
---|
| 7 | from Products.Five import fiveconfigure |
---|
| 8 | |
---|
| 9 | from Testing import ZopeTestCase as ztc |
---|
| 10 | from Products.PloneTestCase import PloneTestCase as ptc |
---|
| 11 | from Products.PloneTestCase.layer import onsetup |
---|
| 12 | |
---|
| 13 | from AccessControl.SecurityManagement import newSecurityManager |
---|
| 14 | from Products.CMFCore.utils import getToolByName |
---|
| 15 | |
---|
| 16 | from quintagroup.plonecaptchas.utils import getWord, decrypt, parseKey |
---|
| 17 | from quintagroup.plonecaptchas.config import * |
---|
| 18 | |
---|
| 19 | @onsetup |
---|
| 20 | def setup_product(): |
---|
| 21 | fiveconfigure.debug_mode = True |
---|
| 22 | import quintagroup.plonecaptchas |
---|
| 23 | zcml.load_config('configure.zcml', quintagroup.plonecaptchas) |
---|
| 24 | fiveconfigure.debug_mode = False |
---|
| 25 | ztc.installPackage('quintagroup.plonecaptchas') |
---|
| 26 | |
---|
| 27 | setup_product() |
---|
| 28 | ptc.setupPloneSite() |
---|
| 29 | |
---|
| 30 | class TestCaptchaWidget(ptc.FunctionalTestCase): |
---|
| 31 | |
---|
| 32 | def afterSetUp(self): |
---|
| 33 | self.loginAsPortalOwner() |
---|
[963] | 34 | self.addProduct(PRODUCT_NAME) |
---|
[797] | 35 | self.portal.invokeFactory('Document', 'index_html') |
---|
| 36 | self.portal['index_html'].allowDiscussion(True) |
---|
| 37 | self.absolute_url = self.portal['index_html'].absolute_url_path() |
---|
| 38 | |
---|
| 39 | self.basic_auth = 'portal_manager:secret' |
---|
| 40 | uf = self.app.acl_users |
---|
| 41 | uf.userFolderAddUser('portal_manager', 'secret', ['Manager'], []) |
---|
| 42 | user = uf.getUserById('portal_manager') |
---|
| 43 | if not hasattr(user, 'aq_base'): |
---|
| 44 | user = user.__of__(uf) |
---|
| 45 | newSecurityManager(None, user) |
---|
| 46 | self.captcha_key = self.portal.captcha_key |
---|
| 47 | |
---|
| 48 | def testImage(self): |
---|
[963] | 49 | path = '%s/discussion_reply_form' % self.absolute_url |
---|
| 50 | response = self.publish(path, self.basic_auth, request_method='GET').getBody() |
---|
| 51 | patt = re.compile('\s+src="%s(/getCaptchaImage/[0-9a-fA-F]+)"' % self.portal.absolute_url()) |
---|
| 52 | match_obj = patt.search(response) |
---|
[797] | 53 | img_url = match_obj.group(1) |
---|
[963] | 54 | content_type = self.publish('/plone' + img_url, self.basic_auth).getHeader('content-type') |
---|
[797] | 55 | self.assert_(content_type.startswith('image')) |
---|
| 56 | |
---|
| 57 | def testSubmitRightCaptcha(self): |
---|
| 58 | hashkey = self.portal.getCaptcha() |
---|
| 59 | key = getWord(int(parseKey(decrypt(self.captcha_key, hashkey))['key'])) |
---|
[963] | 60 | parameters = 'form.submitted=1&Creator=test_user&key=%s' % key |
---|
| 61 | path = '%s/discussion_reply_form?%s' % (self.absolute_url, parameters) |
---|
[797] | 62 | extra = {'hashkey': hashkey, |
---|
| 63 | 'subject': 'testing', |
---|
| 64 | 'body_text': 'Text in Comment', |
---|
| 65 | 'discussion_reply:method': 'Save'} |
---|
| 66 | response = self.publish(path, self.basic_auth, extra=extra, request_method='GET').getBody() |
---|
| 67 | patt = re.compile("Please re\-enter validation code") |
---|
| 68 | match_obj = patt.match(response) |
---|
| 69 | self.assert_(not match_obj) |
---|
| 70 | |
---|
| 71 | def testSubmitWrongCaptcha(self): |
---|
| 72 | hashkey = self.portal.getCaptcha() |
---|
| 73 | parameters = 'form.submitted=1&Creator=test_user&key=fdfgh' |
---|
[963] | 74 | path = '%s/discussion_reply_form?%s' % (self.absolute_url, parameters) |
---|
[797] | 75 | extra = {'hashkey': hashkey, |
---|
| 76 | 'subject': 'testing', |
---|
| 77 | 'body_text': 'Text in Comment', |
---|
| 78 | 'discussion_reply:method': 'Save'} |
---|
| 79 | response = self.publish(path, self.basic_auth, extra=extra, request_method='GET').getBody() |
---|
| 80 | patt = re.compile("Please re\-enter validation code") |
---|
| 81 | match_obj = patt.search(response) |
---|
| 82 | self.assert_(match_obj) |
---|
| 83 | |
---|
| 84 | def testSubmitRightCaptchaTwice(self): |
---|
| 85 | hashkey = self.portal.getCaptcha() |
---|
| 86 | key = getWord(int(parseKey(decrypt(self.captcha_key, hashkey))['key'])) |
---|
| 87 | parameters = 'form.submitted=1&Creator=test_user&key=%s'%key |
---|
| 88 | path = '%s/discussion_reply_form?%s'%(self.absolute_url, parameters) |
---|
| 89 | extra = {'hashkey': hashkey, |
---|
| 90 | 'subject': 'testing', |
---|
| 91 | 'body_text': 'Text in Comment', |
---|
| 92 | 'discussion_reply:method': 'Save'} |
---|
| 93 | self.publish(path, self.basic_auth, extra=extra, request_method='GET') |
---|
| 94 | response = self.publish(path, self.basic_auth, extra=extra, request_method='GET').getBody() |
---|
| 95 | patt = re.compile(".*?Comment\+added") |
---|
| 96 | match_obj = patt.match(response) |
---|
| 97 | self.assert_(not match_obj) |
---|
| 98 | |
---|
| 99 | class TestInstallation(ptc.FunctionalTestCase): |
---|
| 100 | |
---|
| 101 | def afterSetUp(self): |
---|
| 102 | self.loginAsPortalOwner() |
---|
| 103 | self.qi = getToolByName(self.portal, 'portal_quickinstaller', None) |
---|
| 104 | self.cp = getToolByName(self.portal, 'portal_controlpanel', None) |
---|
| 105 | self.st = getToolByName(self.portal, 'portal_skins', None) |
---|
| 106 | self.qi.installProduct(PRODUCT_NAME) |
---|
| 107 | |
---|
| 108 | def getLayers(self): |
---|
[817] | 109 | return LAYERS + [LAYER_STATIC_CAPTCHAS] |
---|
[797] | 110 | |
---|
[963] | 111 | def testPropertysheetInstall(self): |
---|
| 112 | pp = getToolByName(self.portal, 'portal_properties') |
---|
| 113 | self.assert_(PROPERTY_SHEET in pp.objectIds(), 'Property sheet isn\'t found') |
---|
| 114 | |
---|
| 115 | def testPropertysheetUninstall(self): |
---|
| 116 | self.qi.uninstallProducts([PRODUCT_NAME]) |
---|
| 117 | pp = getToolByName(self.portal, 'portal_properties') |
---|
| 118 | self.assert_(not PROPERTY_SHEET in pp.objectIds(), |
---|
| 119 | 'Property sheet found after uninstallation') |
---|
| 120 | |
---|
| 121 | def testConfigletInstall(self): |
---|
[797] | 122 | self.assert_(CONFIGLET_ID in [a.getId() for a in self.cp.listActions()], 'Configlet not found') |
---|
| 123 | |
---|
[963] | 124 | def testConfigletUninstall(self): |
---|
| 125 | self.qi.uninstallProducts([PRODUCT_NAME]) |
---|
| 126 | self.assertNotEqual(self.qi.isProductInstalled(PRODUCT_NAME), True,'%s is already installed' % PRODUCT_NAME) |
---|
| 127 | self.assert_(not CONFIGLET_ID in [a.getId() for a in self.cp.listActions()], 'Configlet found after uninstallation') |
---|
| 128 | |
---|
| 129 | def testSkinsInstall(self): |
---|
[797] | 130 | skinstool = self.st |
---|
[963] | 131 | layers = self.getLayers() |
---|
[797] | 132 | for skin in skinstool.getSkinSelections(): |
---|
| 133 | path = skinstool.getSkinPath(skin) |
---|
| 134 | path = map(str.strip, path.split(',')) |
---|
[963] | 135 | for layer in layers: |
---|
[797] | 136 | self.assert_(layer.split('/')[0] in skinstool.objectIds(), '%s directory view not found in portal_skins after installation' % layer) |
---|
| 137 | self.assert_(layer in path, '%s layer not found in %s' % (PRODUCT_NAME, skin)) |
---|
| 138 | |
---|
[963] | 139 | def testSkinsUninstall(self): |
---|
[797] | 140 | self.qi.uninstallProducts([PRODUCT_NAME]) |
---|
| 141 | self.assertNotEqual(self.qi.isProductInstalled(PRODUCT_NAME), True,'%s is already installed' % PRODUCT_NAME) |
---|
| 142 | skinstool = self.st |
---|
[963] | 143 | layers = self.getLayers() |
---|
[797] | 144 | for skin in skinstool.getSkinSelections(): |
---|
| 145 | path = skinstool.getSkinPath(skin) |
---|
| 146 | path = map(str.strip, path.split(',')) |
---|
[963] | 147 | for layer in layers: |
---|
[797] | 148 | self.assert_(not layer.split('/')[0] in skinstool.objectIds(), '%s directory view found in portal_skins after uninstallation' % layer) |
---|
| 149 | self.assert_(not layer in path, '%s layer found in %s after uninstallation' % (layer, skin)) |
---|
| 150 | |
---|
[963] | 151 | def testToolInstall(self): |
---|
| 152 | self.assert_(TOOL_ID in self.portal.objectIds()) |
---|
| 153 | |
---|
| 154 | def testToolUninstall(self): |
---|
[797] | 155 | self.qi.uninstallProducts([PRODUCT_NAME]) |
---|
[963] | 156 | self.assertNotEqual(self.qi.isProductInstalled(PRODUCT_NAME), True, |
---|
| 157 | '%s is already installed' % PRODUCT_NAME) |
---|
| 158 | self.assert_(not TOOL_ID in self.portal.objectIds()) |
---|
[797] | 159 | |
---|
| 160 | def testCaptchaKey(self): |
---|
| 161 | ck = getattr(self.portal, 'captcha_key') |
---|
| 162 | self.assert_(ck) |
---|
| 163 | self.assertEqual(len(ck), 8) |
---|
| 164 | |
---|
| 165 | def test_suite(): |
---|
| 166 | suite = unittest.TestSuite() |
---|
| 167 | suite.addTest(unittest.makeSuite(TestCaptchaWidget)) |
---|
| 168 | suite.addTest(unittest.makeSuite(TestInstallation)) |
---|
| 169 | return suite |
---|