source: products/quintagroup.captcha.core/trunk/quintagroup/captcha/core/tests.py @ 1956

Last change on this file since 1956 was 1956, checked in by mylan, 14 years ago

#172: Tests review, little correction

File size: 7.3 KB
Line 
1import unittest
2import os
3import sys
4import re
5
6from Products.Five import zcml
7from Products.Five import fiveconfigure
8
9from Testing import ZopeTestCase as ztc
10from Products.PloneTestCase import PloneTestCase as ptc
11from Products.PloneTestCase.layer import onsetup
12from Products.PloneTestCase.PloneTestCase import portal_owner
13from Products.PloneTestCase.PloneTestCase import default_password
14
15# from AccessControl.SecurityManagement import newSecurityManager
16from Products.CMFCore.utils import getToolByName
17
18from quintagroup.captcha.core.config import *
19from quintagroup.captcha.core.utils import getWord, decrypt, parseKey
20
21@onsetup
22def setup_product():
23    fiveconfigure.debug_mode = True
24    import quintagroup.captcha.core
25    zcml.load_config('configure.zcml', quintagroup.captcha.core)
26    fiveconfigure.debug_mode = False
27    ztc.installPackage('quintagroup.captcha.core')
28
29setup_product()
30ptc.setupPloneSite()
31
32class TestCaptchaWidget(ptc.FunctionalTestCase):
33
34    def afterSetUp(self):
35        self.loginAsPortalOwner()
36        self.addProduct(PRODUCT_NAME)
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
41        self.basic_auth = ':'.join((portal_owner,default_password))
42        self.captcha_key = self.portal.captcha_key
43
44    def testImage(self):
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)
49        img_url = match_obj.group(1)
50        content_type = self.publish('/plone' + img_url, self.basic_auth).getHeader('content-type')
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']))
56        parameters = 'form.submitted=1&Creator=test_user&key=%s' % key
57        path = '%s/discussion_reply_form?%s' % (self.absolute_url, parameters)
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'
70        path = '%s/discussion_reply_form?%s' % (self.absolute_url, parameters)
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
95class 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):
105        return LAYERS + [LAYER_STATIC_CAPTCHAS]
106
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):
118        self.assert_(CONFIGLET_ID in [a.getId() for a in self.cp.listActions()], 'Configlet not found')
119
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):
126        skinstool = self.st
127        layers = self.getLayers()
128        for skin in skinstool.getSkinSelections():
129            path = skinstool.getSkinPath(skin)
130            path = map(str.strip, path.split(','))
131            for layer in layers:
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
135    def testSkinsUninstall(self):
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
139        layers = self.getLayers()
140        for skin in skinstool.getSkinSelections():
141            path = skinstool.getSkinPath(skin)
142            path = map(str.strip, path.split(','))
143            for layer in layers:
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
147    def testToolInstall(self):
148        self.assert_(TOOL_ID in self.portal.objectIds())
149
150    def testToolUninstall(self):
151        self.qi.uninstallProducts([PRODUCT_NAME])
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())
155
156    def testCaptchaKey(self):
157        ck = getattr(self.portal, CAPTCHA_KEY)
158        self.assert_(ck)
159        self.assertEqual(len(ck), 8)
160        self.qi.uninstallProducts([PRODUCT_NAME])
161        self.assert_(not self.portal.hasProperty(CAPTCHA_KEY))
162
163def test_suite():
164    suite = unittest.TestSuite()
165    suite.addTest(unittest.makeSuite(TestCaptchaWidget))
166    suite.addTest(unittest.makeSuite(TestInstallation))
167    return suite
Note: See TracBrowser for help on using the repository browser.