[3128] | 1 | import re |
---|
| 2 | import string |
---|
[3144] | 3 | from quintagroup.captcha.core.config import LAYER_STATIC_CAPTCHAS, \ |
---|
[3132] | 4 | PROPERTY_SHEET, LAYER_DYNAMIC_CAPTCHAS, PRODUCT_NAME |
---|
[1979] | 5 | |
---|
[3144] | 6 | from Products.PloneTestCase import PloneTestCase as ptc |
---|
| 7 | from Products.PloneTestCase.PloneTestCase import portal_owner |
---|
| 8 | from Products.PloneTestCase.PloneTestCase import default_password |
---|
[3133] | 9 | |
---|
[3144] | 10 | |
---|
[1979] | 11 | class TestConfiglet(ptc.FunctionalTestCase): |
---|
| 12 | |
---|
| 13 | def afterSetUp(self): |
---|
| 14 | self.sp = self.portal.portal_properties.site_properties |
---|
[3128] | 15 | self.basic_auth = ':'.join((portal_owner, default_password)) |
---|
[1979] | 16 | self.loginAsPortalOwner() |
---|
| 17 | self.addProduct(PRODUCT_NAME) |
---|
[3128] | 18 | |
---|
[1979] | 19 | self.capprops = self.portal.portal_properties[PROPERTY_SHEET] |
---|
| 20 | self.save_url = self.portal.id + \ |
---|
| 21 | '/prefs_captchas_setup_form?form.submitted=1' + \ |
---|
| 22 | '&form.button.form_submit=Save' |
---|
| 23 | |
---|
| 24 | def layerInSkins(self, layer): |
---|
| 25 | skins = self.portal.portal_skins |
---|
| 26 | for skin in skins.getSkinSelections(): |
---|
| 27 | path = skins.getSkinPath(skin) |
---|
[3128] | 28 | path = map(string.strip, string.split(path, ',')) |
---|
[1979] | 29 | if not layer in path: |
---|
| 30 | return False |
---|
| 31 | |
---|
| 32 | return True |
---|
| 33 | |
---|
| 34 | def test_staticOn(self): |
---|
| 35 | self.publish(self.save_url + '&static_captchas=static', |
---|
| 36 | self.basic_auth) |
---|
[3128] | 37 | |
---|
[1979] | 38 | self.assertTrue(self.layerInSkins(LAYER_STATIC_CAPTCHAS), |
---|
[3600] | 39 | "No '%s' skin layer in some skins" % |
---|
| 40 | LAYER_STATIC_CAPTCHAS) |
---|
[1979] | 41 | |
---|
| 42 | def test_dynamicOn(self): |
---|
[3132] | 43 | self.publish(self.save_url + '&static_captchas=dynamic', |
---|
[1979] | 44 | self.basic_auth).getBody() |
---|
[3128] | 45 | |
---|
[1979] | 46 | self.assertTrue(self.layerInSkins(LAYER_DYNAMIC_CAPTCHAS), |
---|
[3600] | 47 | "No '%s' skin layer in some skins" % |
---|
| 48 | LAYER_DYNAMIC_CAPTCHAS) |
---|
[1979] | 49 | |
---|
| 50 | def test_imageSize(self): |
---|
| 51 | expect = 35 |
---|
| 52 | self.publish(self.save_url + '&image_size=%s' % expect, |
---|
[3600] | 53 | self.basic_auth) |
---|
[1979] | 54 | |
---|
| 55 | imsize = self.capprops.getProperty("image_size", 0) |
---|
[3600] | 56 | self.assertTrue(imsize == expect, '"image_size" property ' |
---|
| 57 | 'contains: "%s", must: "%s"' % (imsize, expect)) |
---|
[1979] | 58 | |
---|
| 59 | def test_background(self): |
---|
| 60 | prop, expect = "background", "test-color" |
---|
| 61 | self.publish(self.save_url + '&%s=%s' % (prop, expect), |
---|
[3600] | 62 | self.basic_auth) |
---|
[1979] | 63 | |
---|
| 64 | fact = self.capprops.getProperty(prop, "") |
---|
[3600] | 65 | self.assertTrue(fact == expect, '"%s" property ' |
---|
| 66 | 'contains: "%s", must: "%s"' % (prop, fact, expect)) |
---|
[1979] | 67 | |
---|
| 68 | def test_fontColor(self): |
---|
| 69 | prop, expect = "font_color", "test-font-color" |
---|
| 70 | self.publish(self.save_url + '&%s=%s' % (prop, expect), |
---|
[3600] | 71 | self.basic_auth) |
---|
[1979] | 72 | |
---|
| 73 | fact = self.capprops.getProperty(prop, "") |
---|
[3600] | 74 | self.assertTrue(fact == expect, '"%s" property ' |
---|
| 75 | 'contains: "%s", must: "%s"' % (prop, fact, expect)) |
---|
[1979] | 76 | |
---|
| 77 | def test_period(self): |
---|
| 78 | prop, expect = "period", 22.3 |
---|
| 79 | self.publish(self.save_url + '&%s=%s' % (prop, expect), |
---|
[3600] | 80 | self.basic_auth) |
---|
[1979] | 81 | |
---|
| 82 | fact = self.capprops.getProperty(prop, 0) |
---|
[3600] | 83 | self.assertTrue(fact == expect, '"%s" property ' |
---|
| 84 | 'contains: "%s", must: "%s"' % (prop, fact, expect)) |
---|
[1979] | 85 | |
---|
| 86 | def test_amplitude(self): |
---|
| 87 | prop, expect = "amplitude", 11.2 |
---|
| 88 | self.publish(self.save_url + '&%s=%s' % (prop, expect), |
---|
[3600] | 89 | self.basic_auth) |
---|
[1979] | 90 | |
---|
| 91 | fact = self.capprops.getProperty(prop, 0) |
---|
[3600] | 92 | self.assertTrue(fact == expect, '"%s" property ' |
---|
| 93 | 'contains: "%s", must: "%s"' % (prop, fact, expect)) |
---|
[1979] | 94 | |
---|
| 95 | def test_random(self): |
---|
| 96 | prop, expect = "random_params", False |
---|
| 97 | self.publish(self.save_url, self.basic_auth) |
---|
| 98 | |
---|
| 99 | fact = self.capprops.getProperty(prop, None) |
---|
[3600] | 100 | self.assertTrue(fact == expect, '"%s" property ' |
---|
| 101 | 'contains: "%s", must: "%s"' % (prop, fact, expect)) |
---|
[1979] | 102 | |
---|
| 103 | |
---|
[2447] | 104 | class TestConfigletView(ptc.FunctionalTestCase): |
---|
| 105 | |
---|
| 106 | def afterSetUp(self): |
---|
| 107 | self.loginAsPortalOwner() |
---|
| 108 | self.addProduct(PRODUCT_NAME) |
---|
[3128] | 109 | captcha_pref_path = self.portal.id + '/prefs_captchas_setup_form' |
---|
| 110 | basic_auth = portal_owner + ":" + default_password |
---|
| 111 | self.view = self.publish(captcha_pref_path, basic_auth).getBody() |
---|
| 112 | |
---|
[2447] | 113 | def matchinput(self, name): |
---|
| 114 | return re.match('.*<input\s+[^\>]*name=\"%s\"[^>]*>' % name, |
---|
[3128] | 115 | self.view, re.I | re.S) |
---|
[2447] | 116 | |
---|
| 117 | def test_basic_form(self): |
---|
[3128] | 118 | reg_expr = '.*<form\s+[^\>]*action=\"[^\"]*?'\ |
---|
| 119 | 'prefs_captchas_setup_form\"[^>]*>' |
---|
| 120 | form = re.match(reg_expr, self.view, re.I | re.S) |
---|
[2447] | 121 | self.assertNotEqual(form, None, |
---|
[3604] | 122 | "No 'Plone Captchas Setup' form present " |
---|
| 123 | "on the configlet view") |
---|
[2447] | 124 | self.assertNotEqual(self.matchinput('form\.button\.form_submit'), None, |
---|
[3600] | 125 | "No submit button on the form") |
---|
[2447] | 126 | self.assertNotEqual(self.matchinput('static_captchas'), None, |
---|
[3604] | 127 | "No static/dynamic radio button present on the " |
---|
| 128 | "configlet") |
---|
[2447] | 129 | |
---|
| 130 | def test_dynamic(self): |
---|
| 131 | params = ["image_size", "background", "font_color", |
---|
| 132 | "period", "amplitude", "random_params"] |
---|
| 133 | for param in params: |
---|
| 134 | self.assertNotEqual(self.matchinput(param), None, |
---|
[3604] | 135 | "'%s' form element absence on the configlet " |
---|
| 136 | "form" % param) |
---|
[2447] | 137 | |
---|
[3128] | 138 | |
---|
[1979] | 139 | def test_suite(): |
---|
| 140 | from unittest import TestSuite, makeSuite |
---|
| 141 | suite = TestSuite() |
---|
| 142 | suite.addTest(makeSuite(TestConfiglet)) |
---|
[2447] | 143 | suite.addTest(makeSuite(TestConfigletView)) |
---|
[1979] | 144 | return suite |
---|