source: products/quintagroup.formlib.captcha/trunk/quintagroup/formlib/captcha/tests/test_unittests.py @ 3137

Last change on this file since 3137 was 3137, checked in by vmaksymiv, 11 years ago

pyflakes fixes

  • Property svn:eol-style set to native
File size: 4.7 KB
Line 
1import re
2import unittest
3
4from zope.schema.interfaces import IField
5from zope.component import queryMultiAdapter
6from zope.publisher.browser import TestRequest
7from zope.app.form.interfaces import IInputWidget
8from zope.app.form.interfaces import ConversionError
9
10from Products.Five import zcml
11from Products.Five import fiveconfigure
12from Testing import ZopeTestCase as ztc
13from Products.PloneTestCase.layer import onsetup
14from Products.PloneTestCase import PloneTestCase as ptc
15
16from quintagroup.captcha.core.utils import decrypt, parseKey, getWord
17from quintagroup.captcha.core.tests.base import testPatch
18from quintagroup.captcha.core.tests.testWidget import addTestLayer
19
20from quintagroup.formlib.captcha import Captcha
21from quintagroup.formlib.captcha import CaptchaWidget
22
23
24@onsetup
25def setup_product():
26    fiveconfigure.debug_mode = True
27    import quintagroup.captcha.core
28    import quintagroup.formlib.captcha
29    zcml.load_config('configure.zcml', quintagroup.formlib.captcha)
30    fiveconfigure.debug_mode = False
31    ztc.installPackage('quintagroup.captcha.core')
32
33setup_product()
34ptc.setupPloneSite(extension_profiles=['quintagroup.captcha.core:default', ])
35
36
37class TestRegistrations(ptc.PloneTestCase):
38
39    def testCaptchaFieldInterface(self):
40        self.assertEqual(IField.implementedBy(Captcha), True)
41
42    def testCaptchaWidgetInterface(self):
43        self.assertEqual(IInputWidget.implementedBy(CaptchaWidget), True)
44
45    def testWidgetRegistration(self):
46        cfield = Captcha()
47        request = TestRequest()
48        cwidget = queryMultiAdapter((cfield, request), IInputWidget)
49        self.assertNotEqual(cwidget, None)
50
51
52class TestCaptchaWidgetHTML(ptc.PloneTestCase):
53    def afterSetUp(self):
54        # app context
55        self.loginAsPortalOwner()
56        # get html output from widget
57        field = Captcha()
58        bound = field.bind(self.portal)
59        widget = CaptchaWidget(bound, self.app.REQUEST)
60        widget.setPrefix('')
61        self.html = widget()
62
63    def testHidden(self):
64        HIDDENTAG = '<input\s+[^>]*(?:' \
65            '(?:type="hidden"\s*)|' \
66            '(?:name="hashkey"\s*)|' \
67            '(?:value="(?P<value>[0-9a-fA-F]+)"\s*)' \
68            '){3}/>'
69        hidden = re.search(HIDDENTAG, self.html)
70        self.assertTrue(hidden and hidden.group('value'))
71
72    def testImg(self):
73        IMAGETAG = '<img\s+[^>]*src=\"' \
74            '(?P<src>[^\"]*/getCaptchaImage/[0-9a-fA-F]+)' \
75            '\"[^>]*>'
76        img = re.search(IMAGETAG, self.html)
77        self.assertTrue(img and img.group('src'))
78
79    def testTextField(self):
80        FIELDTAG = '<input\s+[^>]*type=\"text\"\s*[^>]*>'
81        self.assertEqual(re.search(FIELDTAG, self.html) is not None, True)
82
83
84class TestCaptchaWidgetToField(ptc.PloneTestCase):
85
86    def afterSetUp(self):
87        # prepare context
88        self.loginAsPortalOwner()
89        testPatch()
90        addTestLayer(self)
91        self.captcha_key = self.portal.captcha_key
92        # prepare widget
93        field = Captcha()
94        bound = field.bind(self.portal)
95        self.request = self.app.REQUEST
96        self.widget = CaptchaWidget(bound, self.request)
97        self.widget.setPrefix('')
98        # prepare captcha data
99        self.hashkey = self.portal.getCaptcha()
100        self.request.form['hashkey'] = self.hashkey
101
102    def testSubmitRightCaptcha(self):
103        decrypted = decrypt(self.captcha_key, self.hashkey)
104        key = getWord(int(parseKey(decrypted)['key']) - 1)
105        try:
106            res = self.widget._toFieldValue(key)
107        except ConversionError, e:
108            self.fail("Rised unexpected %s error on right captcha submit" % \
109                      e.doc())
110        else:
111            self.assertEqual(res, key)
112
113    def testSubmitWrongCaptcha(self):
114        try:
115            self.widget._toFieldValue("wrong key")
116        except ConversionError, e:
117            self.assertEqual(e.doc(), u'Please re-enter validation code.')
118        else:
119            self.fail("No ConversionError rised on wrong captcha key submit")
120
121    def testSubmitRightCaptchaTwice(self):
122        decrypted = decrypt(self.captcha_key, self.hashkey)
123        key = getWord(int(parseKey(decrypted)['key']) - 1)
124        self.widget._toFieldValue(key)
125        try:
126            self.widget._toFieldValue(key)
127        except ConversionError, e:
128            self.assertEqual(e.doc(), u'Please re-enter validation code.')
129        else:
130            self.fail("No ConversionError rised on right captcha key " \
131                      "submitting twice")
132
133
134def test_suite():
135    suite = unittest.TestSuite()
136    suite.addTest(unittest.makeSuite(TestRegistrations))
137    suite.addTest(unittest.makeSuite(TestCaptchaWidgetHTML))
138    suite.addTest(unittest.makeSuite(TestCaptchaWidgetToField))
139    return suite
Note: See TracBrowser for help on using the repository browser.