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

Last change on this file since 3128 was 3128, checked in by vmaksymiv, 13 years ago

pep8 fixes

  • Property svn:eol-style set to native
File size: 5.4 KB
Line 
1import string
2from base import *
3
4from DateTime import DateTime
5from Products.CMFFormController.ControllerState import ControllerState
6
7
8class TestStatic(ptc.FunctionalTestCase):
9
10    def afterSetUp(self):
11        self.loginAsPortalOwner()
12        self.addProduct(PRODUCT_NAME)
13        self.skins = self.portal.portal_skins
14
15        self.captcha_key = self.portal.captcha_key
16        self.hashkey = self.portal.getCaptcha()
17
18    def testStaticByDefault(self):
19        # After installation static layer must present in all skin paths
20        for skin in self.skins.getSkinSelections():
21            path = self.skins.getSkinPath(skin)
22            path = map(string.strip, string.split(path, ','))
23            self.assertTrue(LAYER_STATIC_CAPTCHAS in path)
24
25    def testImagesCount(self):
26        # All images must present in static skin layer
27        static = self.skins.restrictedTraverse('captchas')
28        static_ids = static.objectIds()
29        for i in range(1, CAPTCHAS_COUNT + 1):
30            self.assertTrue("%s.jpg" % i in static_ids,
31                            "No %s.jpg in static, %s" % (i, static_ids))
32
33    def test_GetCaptcha_Date(self):
34        # *date* must present after parsing decrypted key
35        decrypted_key = decrypt(self.captcha_key, self.hashkey)
36        parsed_key = parseKey(decrypted_key)
37        self.assertTrue('date' in parsed_key.keys())
38
39    def test_GetCaptcha_Key(self):
40        decrypted_key = decrypt(self.captcha_key, self.hashkey)
41        parsed_key = parseKey(decrypted_key)
42        # *key* must present after parsing decrypted key
43        self.assertTrue('key' in parsed_key.keys())
44        # index start from 1 and lower or equals to CAPTCHAS_COUNT
45        index = int(parsed_key['key'])
46        self.assertTrue(index >= 1 and index <= CAPTCHAS_COUNT)
47        # encrypted key must be equals to title of the image
48        key = getWord(index - 1)
49        img = getattr(self.portal, '%s.jpg' % index)
50        self.assertTrue(encrypt1(key) == img.title)
51
52    def test_GetImage(self):
53        # getCaptchaImage function must return image coded in hashkey same to
54        # image get by 'key' after parsing decrypted key
55        req, resp = self.app.REQUEST, self.app.REQUEST.RESPONSE
56        decrypted_key = decrypt(self.captcha_key, self.hashkey)
57        parsed_key = parseKey(decrypted_key)
58        img = self.portal.restrictedTraverse(parsed_key['key'] + '.jpg')
59        img_html = img.index_html(req, resp)
60
61        portal = self.portal.absolute_url(1)
62        captcha_path = portal + "/getCaptchaImage/%s" % self.hashkey
63        obj_html = self.publish(captcha_path).getBody()
64        msg = "Image get by getCaptchaImage script is differ from"\
65              "image get by index (after parsing decrypted key)"
66        self.assertTrue(obj_html == img_html, msg)
67
68
69class TestStaticValidator(ptc.PloneTestCase):
70
71    def afterSetUp(self):
72        self.loginAsPortalOwner()
73        self.addProduct(PRODUCT_NAME)
74        self.captcha_key = self.portal.captcha_key
75        # Preparation for validator tests
76        self.pfc = self.portal.portal_form_controller
77        self.req = self.app.REQUEST
78        # set up a dummy state object
79        self.dummycs = ControllerState(id='prefs_captchas_setup_form',
80            context=self.portal, button='submit', status='success',
81            errors={}, ext_action=None,)
82
83    def testGoodData(self):
84        hashkey = self.portal.getCaptcha()
85        decrypted_key = decrypt(self.captcha_key, hashkey)
86        key = getWord(int(parseKey(decrypted_key)['key']) - 1)
87        self.req.form['hashkey'] = hashkey
88        self.req.form['key'] = key + '1'
89
90        cstate = self.pfc.validate(self.dummycs, self.req,
91                                   ['captcha_validator', ])
92        error_msg = 'Please re-enter validation code.'
93        self.assertTrue(cstate.getErrors()['key'] == error_msg,
94                        "Static captcha validator validate wrong key")
95
96    def testBadKey(self):
97        hashkey = self.portal.getCaptcha()
98        decrypted_key = decrypt(self.captcha_key, hashkey)
99        key = getWord(int(parseKey(decrypted_key)['key']) - 1)
100        self.req.form['hashkey'] = hashkey
101        self.req.form['key'] = 'bad key'
102
103        cstate = self.pfc.validate(self.dummycs, self.req,
104                                   ['captcha_validator', ])
105        error_msg = 'Please re-enter validation code.'
106        self.assertTrue(cstate.getErrors()['key'] == error_msg,
107                        "Static captcha validator validate wrong key")
108
109    def testBadDate(self):
110        # First path DateTime to get old dated hash
111        origDTTT = DateTime.timeTime
112        now = DateTime().timeTime()
113        DateTime.timeTime = lambda s: now - float(3601)
114
115        hashkey = self.portal.getCaptcha()
116
117        decrypted_key = decrypt(self.captcha_key, hashkey)
118        key = getWord(int(parseKey(decrypted_key)['key']) - 1)
119        self.req.form['hashkey'] = hashkey
120        self.req.form['key'] = key
121        # Return original Date
122        DateTime.timeTime = origDTTT
123        cstate = self.pfc.validate(self.dummycs, self.req,
124                                   ['captcha_validator', ])
125        error_msg = 'Please re-enter validation code.'
126        self.assertTrue(cstate.getErrors()['key'] == error_msg,
127                        "Static captcha validator validate wrong key")
128
129
130def test_suite():
131    suite = unittest.TestSuite()
132    suite.addTest(unittest.makeSuite(TestStatic))
133    suite.addTest(unittest.makeSuite(TestStaticValidator))
134    return suite
Note: See TracBrowser for help on using the repository browser.