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

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

pyflakes fixes

  • Property svn:eol-style set to native
File size: 5.5 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', status='success',
86            errors={}, ext_action=None,)
87
88    def testGoodData(self):
89        hashkey = self.portal.getCaptcha()
90        decrypted_key = decrypt(self.captcha_key, hashkey)
91        key = getWord(int(parseKey(decrypted_key)['key']) - 1)
92        self.req.form['hashkey'] = hashkey
93        self.req.form['key'] = key + '1'
94
95        cstate = self.pfc.validate(self.dummycs, self.req,
96                                   ['captcha_validator', ])
97        error_msg = 'Please re-enter validation code.'
98        self.assertTrue(cstate.getErrors()['key'] == error_msg,
99                        "Static captcha validator validate wrong key")
100
101    def testBadKey(self):
102        hashkey = self.portal.getCaptcha()
103        self.req.form['hashkey'] = hashkey
104        self.req.form['key'] = 'bad key'
105
106        cstate = self.pfc.validate(self.dummycs, self.req,
107                                   ['captcha_validator', ])
108        error_msg = 'Please re-enter validation code.'
109        self.assertTrue(cstate.getErrors()['key'] == error_msg,
110                        "Static captcha validator validate wrong key")
111
112    def testBadDate(self):
113        # First path DateTime to get old dated hash
114        origDTTT = DateTime.timeTime
115        now = DateTime().timeTime()
116        DateTime.timeTime = lambda s: now - float(3601)
117
118        hashkey = self.portal.getCaptcha()
119
120        decrypted_key = decrypt(self.captcha_key, hashkey)
121        key = getWord(int(parseKey(decrypted_key)['key']) - 1)
122        self.req.form['hashkey'] = hashkey
123        self.req.form['key'] = key
124        # Return original Date
125        DateTime.timeTime = origDTTT
126        cstate = self.pfc.validate(self.dummycs, self.req,
127                                   ['captcha_validator', ])
128        error_msg = 'Please re-enter validation code.'
129        self.assertTrue(cstate.getErrors()['key'] == error_msg,
130                        "Static captcha validator validate wrong key")
131
132
133def test_suite():
134    suite = unittest.TestSuite()
135    suite.addTest(unittest.makeSuite(TestStatic))
136    suite.addTest(unittest.makeSuite(TestStaticValidator))
137    return suite
Note: See TracBrowser for help on using the repository browser.