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