[2060] | 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 | |
---|
[3124] | 16 | from quintagroup.captcha.core.utils import decrypt, parseKey, getWord |
---|
[2060] | 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 | |
---|
[3121] | 23 | |
---|
[2060] | 24 | @onsetup |
---|
| 25 | def 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 | |
---|
| 33 | setup_product() |
---|
[3121] | 34 | ptc.setupPloneSite(extension_profiles=['quintagroup.captcha.core:default', ]) |
---|
[2060] | 35 | |
---|
| 36 | |
---|
[2081] | 37 | class TestRegistrations(ptc.PloneTestCase): |
---|
[2060] | 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 | |
---|
| 52 | class 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 | |
---|
[3121] | 83 | |
---|
[2060] | 84 | class 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) |
---|
[3121] | 104 | key = getWord(int(parseKey(decrypted)['key']) - 1) |
---|
[2060] | 105 | try: |
---|
| 106 | res = self.widget._toFieldValue(key) |
---|
| 107 | except ConversionError, e: |
---|
[3121] | 108 | self.fail("Rised unexpected %s error on right captcha submit" % \ |
---|
| 109 | e.doc()) |
---|
[2060] | 110 | else: |
---|
| 111 | self.assertEqual(res, key) |
---|
[3121] | 112 | |
---|
[2060] | 113 | def testSubmitWrongCaptcha(self): |
---|
| 114 | try: |
---|
[3124] | 115 | self.widget._toFieldValue("wrong key") |
---|
[2060] | 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) |
---|
[3121] | 123 | key = getWord(int(parseKey(decrypted)['key']) - 1) |
---|
[2060] | 124 | self.widget._toFieldValue(key) |
---|
| 125 | try: |
---|
[3137] | 126 | self.widget._toFieldValue(key) |
---|
[2060] | 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 | |
---|
| 134 | def 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 |
---|