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" % LAYER_STATIC_CAPTCHAS) |
---|
40 | |
---|
41 | def test_dynamicOn(self): |
---|
42 | self.publish(self.save_url + '&static_captchas=dynamic', |
---|
43 | self.basic_auth).getBody() |
---|
44 | |
---|
45 | self.assertTrue(self.layerInSkins(LAYER_DYNAMIC_CAPTCHAS), |
---|
46 | "No '%s' skin layer in some skins" % LAYER_DYNAMIC_CAPTCHAS) |
---|
47 | |
---|
48 | def test_imageSize(self): |
---|
49 | expect = 35 |
---|
50 | self.publish(self.save_url + '&image_size=%s' % expect, |
---|
51 | self.basic_auth) |
---|
52 | |
---|
53 | imsize = self.capprops.getProperty("image_size", 0) |
---|
54 | self.assertTrue(imsize == expect, '"image_size" property ' \ |
---|
55 | 'contains: "%s", must: "%s"' % (imsize, expect)) |
---|
56 | |
---|
57 | def test_background(self): |
---|
58 | prop, expect = "background", "test-color" |
---|
59 | self.publish(self.save_url + '&%s=%s' % (prop, expect), |
---|
60 | self.basic_auth) |
---|
61 | |
---|
62 | fact = self.capprops.getProperty(prop, "") |
---|
63 | self.assertTrue(fact == expect, '"%s" property ' \ |
---|
64 | 'contains: "%s", must: "%s"' % (prop, fact, expect)) |
---|
65 | |
---|
66 | def test_fontColor(self): |
---|
67 | prop, expect = "font_color", "test-font-color" |
---|
68 | self.publish(self.save_url + '&%s=%s' % (prop, expect), |
---|
69 | self.basic_auth) |
---|
70 | |
---|
71 | fact = self.capprops.getProperty(prop, "") |
---|
72 | self.assertTrue(fact == expect, '"%s" property ' \ |
---|
73 | 'contains: "%s", must: "%s"' % (prop, fact, expect)) |
---|
74 | |
---|
75 | def test_period(self): |
---|
76 | prop, expect = "period", 22.3 |
---|
77 | self.publish(self.save_url + '&%s=%s' % (prop, expect), |
---|
78 | self.basic_auth) |
---|
79 | |
---|
80 | fact = self.capprops.getProperty(prop, 0) |
---|
81 | self.assertTrue(fact == expect, '"%s" property ' \ |
---|
82 | 'contains: "%s", must: "%s"' % (prop, fact, expect)) |
---|
83 | |
---|
84 | def test_amplitude(self): |
---|
85 | prop, expect = "amplitude", 11.2 |
---|
86 | self.publish(self.save_url + '&%s=%s' % (prop, expect), |
---|
87 | self.basic_auth) |
---|
88 | |
---|
89 | fact = self.capprops.getProperty(prop, 0) |
---|
90 | self.assertTrue(fact == expect, '"%s" property ' \ |
---|
91 | 'contains: "%s", must: "%s"' % (prop, fact, expect)) |
---|
92 | |
---|
93 | def test_random(self): |
---|
94 | prop, expect = "random_params", False |
---|
95 | self.publish(self.save_url, self.basic_auth) |
---|
96 | |
---|
97 | fact = self.capprops.getProperty(prop, None) |
---|
98 | self.assertTrue(fact == expect, '"%s" property ' \ |
---|
99 | 'contains: "%s", must: "%s"' % (prop, fact, expect)) |
---|
100 | |
---|
101 | |
---|
102 | class TestConfigletView(ptc.FunctionalTestCase): |
---|
103 | |
---|
104 | def afterSetUp(self): |
---|
105 | self.loginAsPortalOwner() |
---|
106 | self.addProduct(PRODUCT_NAME) |
---|
107 | captcha_pref_path = self.portal.id + '/prefs_captchas_setup_form' |
---|
108 | basic_auth = portal_owner + ":" + default_password |
---|
109 | self.view = self.publish(captcha_pref_path, basic_auth).getBody() |
---|
110 | |
---|
111 | def matchinput(self, name): |
---|
112 | return re.match('.*<input\s+[^\>]*name=\"%s\"[^>]*>' % name, |
---|
113 | self.view, re.I | re.S) |
---|
114 | |
---|
115 | def test_basic_form(self): |
---|
116 | reg_expr = '.*<form\s+[^\>]*action=\"[^\"]*?'\ |
---|
117 | 'prefs_captchas_setup_form\"[^>]*>' |
---|
118 | form = re.match(reg_expr, self.view, re.I | re.S) |
---|
119 | self.assertNotEqual(form, None, |
---|
120 | "No 'Plone Captchas Setup' form present on the configlet view") |
---|
121 | self.assertNotEqual(self.matchinput('form\.button\.form_submit'), None, |
---|
122 | "No submit button on the form") |
---|
123 | self.assertNotEqual(self.matchinput('static_captchas'), None, |
---|
124 | "No static/dynamic radio button present on the configlet") |
---|
125 | |
---|
126 | def test_dynamic(self): |
---|
127 | params = ["image_size", "background", "font_color", |
---|
128 | "period", "amplitude", "random_params"] |
---|
129 | for param in params: |
---|
130 | self.assertNotEqual(self.matchinput(param), None, |
---|
131 | "'%s' form element absence on the configlet form" % param) |
---|
132 | |
---|
133 | |
---|
134 | def test_suite(): |
---|
135 | from unittest import TestSuite, makeSuite |
---|
136 | suite = TestSuite() |
---|
137 | suite.addTest(makeSuite(TestConfiglet)) |
---|
138 | suite.addTest(makeSuite(TestConfigletView)) |
---|
139 | return suite |
---|