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