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

Last change on this file since 1992 was 1992, checked in by mylan, 9 years ago

#172: added tests for dynamic captchas

  • Property svn:eol-style set to native
File size: 3.0 KB
Line 
1import string
2from base import *
3
4from DateTime import DateTime
5from Products.CMFFormController.ControllerState import ControllerState
6
7from testStatic import TestStaticValidator
8
9class DynamicMixin:
10    def switchToDynamic(self):
11        skins = self.portal.portal_skins
12        for skin in skins.getSkinSelections():
13            path = skins.getSkinPath(skin)
14            path = map( string.strip, string.split( path,',' ))
15            try:
16                i = path.index(LAYER_STATIC_CAPTCHAS)
17                path.remove(LAYER_STATIC_CAPTCHAS)
18                path.insert(i, LAYER_DYNAMIC_CAPTCHAS)
19            except ValueError:
20                pass
21            path = string.join( path, ', ' )
22            skins.addSkinSelection( skin, path )
23            self._refreshSkinData()
24
25class TestDynamic(DynamicMixin, ptc.FunctionalTestCase):
26
27    def afterSetUp(self):
28        self.loginAsPortalOwner()
29        self.addProduct(PRODUCT_NAME)
30        self.skins = self.portal.portal_skins
31        self.switchToDynamic()
32
33        self.captcha_key = self.portal.captcha_key
34        self.hashkey = self.portal.getCaptcha()
35
36    def test_GetCaptcha_Date(self):
37        # *date* must present after parsing decrypted key
38        decrypted_key = decrypt(self.captcha_key, self.hashkey)
39        parsed_key = parseKey(decrypted_key)
40        self.assertTrue('date' in parsed_key.keys())
41
42    def test_GetCaptcha_Key(self):
43        decrypted_key = decrypt(self.captcha_key, self.hashkey)
44        parsed_key = parseKey(decrypted_key)
45        # *key* must present after parsing decrypted key
46        self.assertTrue('key' in parsed_key.keys())
47        # index start from 0 and lower or equals to captchas count
48        index = int(parsed_key['key'])
49        self.assertTrue(index >= 0 and index <= len(utils.basic_english.words.split()))
50        # encrypted key must be equals to word from the dictionary,
51        # under index position and must be not empty string
52        self.assertFalse(getWord(index) == "")
53
54    def test_GetImage(self):
55        # getCaptchaImage function must return image coded in hashkey same to
56        # image get by 'key' after parsing decrypted key
57        decrypted_key = decrypt(self.captcha_key, self.hashkey)
58        parsed_key = parseKey(decrypted_key)
59
60        img_html = self.publish(
61            self.portal.absolute_url(1)+"/getCaptchaImage/%s" % self.hashkey)
62
63        img_ctype = img_html.getHeader('content-type')
64        self.assertTrue(img_ctype == 'image/jpeg', "Wrong content type for " \
65            "generated image: %s, must be 'image/jpeg'" % img_ctype)
66        self.assertTrue(img_html.status == 200, "Wrong response status: " \
67            "'%s', must be '200'" % img_html.status)
68       
69
70class TestDynamicValidator(DynamicMixin, TestStaticValidator):
71
72    def afterSetUp(self):
73        TestStaticValidator.afterSetUp(self)
74        self.switchToDynamic()
75
76
77def test_suite():
78    suite = unittest.TestSuite()
79    suite.addTest(unittest.makeSuite(TestDynamic))
80    suite.addTest(unittest.makeSuite(TestDynamicValidator))
81    return suite
Note: See TracBrowser for help on using the repository browser.