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