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

Last change on this file since 3600 was 3600, checked in by vmaksymiv, 12 years ago

PPP fixes

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