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