source: products/quintagroup.pfg.captcha/trunk/quintagroup/pfg/captcha/tests.py @ 2099

Last change on this file since 2099 was 2099, checked in by mylan, 14 years ago

#174: added tests for CaptchaWidget?

  • Property svn:eol-style set to native
File size: 5.0 KB
Line 
1import re
2import string
3import unittest
4
5from Products.Five import zcml
6from Products.Five import fiveconfigure
7from Testing import ZopeTestCase as ztc
8from Products.PloneTestCase.layer import onsetup
9from Products.PloneTestCase import PloneTestCase as ptc
10from Products.CMFCore.permissions import View
11from Products.Archetypes.atapi import StringField
12from Products.Archetypes.Registry import availableWidgets
13
14from quintagroup.pfg.captcha import CaptchaField
15from quintagroup.pfg.captcha import CaptchaWidget
16from quintagroup.pfg.captcha import CaptchaValidator
17from quintagroup.pfg.captcha.widget import CAPTCHA_MACRO
18from quintagroup.pfg.captcha.field import CAPTCHA_ID, HIDDEN_FIELDS
19
20_marker = object()
21
22PRODUCTS = [
23    'quintagroup.captcha.core',
24    'quintagroup.pfg.captcha',
25]
26PROFILES = [p+':default' for p in PRODUCTS]
27
28@onsetup
29def setup_product():
30    fiveconfigure.debug_mode = True
31    import quintagroup.pfg.captcha
32    zcml.load_config('configure.zcml', quintagroup.pfg.captcha)
33    fiveconfigure.debug_mode = False
34    ztc.installProduct('PloneFormGen')
35    ztc.installPackage('quintagroup.pfg.captcha')
36    ztc.installPackage('quintagroup.captcha.core')
37
38setup_product()
39ptc.setupPloneSite(products=['PloneFormGen',], extension_profiles=PROFILES)
40
41
42class TestInstallations(ptc.PloneTestCase):
43
44    def testInstalledProducts(self):
45        qi = self.portal.portal_quickinstaller
46        installed = [p['id'] for p in qi.listInstalledProducts()]
47        for p in PRODUCTS:
48            if p.startswith('Products'):
49                p = p[9:]
50            self.assertEqual(p in installed, True,
51                '"%s" product not installed' % p)
52
53    def testType(self):
54        pt = self.portal.portal_types
55        self.assertEqual("CaptchaField" in pt.objectIds(), True)
56
57    def testPortalFactory(self):
58        pf = self.portal.portal_factory
59        self.assertEqual("CaptchaField" in pf.getFactoryTypes(), True)
60
61    def testWorkflow(self):
62        pw = self.portal.portal_workflow
63        default_chain = pw.getDefaultChain()
64        cf_chain = pw.getChainForPortalType('CaptchaField')
65        self.assertNotEqual(cf_chain == default_chain , True)
66
67    def testNotToList(self):
68        navtree = self.portal.portal_properties.navtree_properties
69        mtNotToList = navtree.getProperty("metaTypesNotToList")
70        self.assertEqual('CaptchaField' in mtNotToList, True)
71
72    def testSkins(self):
73        ps = self.portal.portal_skins
74        self.assertEqual("qplonecaptchafield" in ps.objectIds(), True)
75        for sname, spath in ps.getSkinPaths():
76            paths = filter(None, map(string.strip, spath.split(',')))
77            self.assertEqual("qplonecaptchafield" in paths, True,
78                '"qplonecaptchafield" layer not present in "%s" skin' % sname)
79
80
81class TestCaptchaField(ptc.PloneTestCase):
82
83    def afterSetUp(self):
84        self.folder.invokeFactory('FormFolder', 'ff1')
85        self.ff1 = getattr(self.folder, 'ff1')
86        self.ff1.invokeFactory('CaptchaField', 'captcha_field')
87
88    def testId(self):
89        """CaptchaField has always CAPTCHA_ID id."""
90        self.assertEqual(CAPTCHA_ID in self.ff1, True)
91        self.assertNotEqual('captcha_field' in self.ff1, True)
92
93    def testSchema(self):
94        cf = getattr(self.ff1, CAPTCHA_ID)
95        schema = cf.Schema()
96        for field in HIDDEN_FIELDS:
97            visibility = schema[field].widget.visible
98            self.assertEqual(visibility, {'view':'invisible','edit':'invisible'},
99                '"%s" field is not hidden, but %s:' % (field, visibility))
100
101    def testFGField(self):
102        cf = getattr(self.ff1, CAPTCHA_ID)
103        fgField = getattr(cf, 'fgField', _marker)
104        self.assertNotEqual(fgField, _marker)
105        # Test fgField properties
106        self.assertEqual(type(fgField), StringField)
107        self.assertEqual(bool(fgField.searchable), False )
108        self.assertEqual(fgField.write_permission, View)
109        self.assertEqual(type(fgField.widget), CaptchaWidget)
110        validators = [v.__class__ for v in fgField.validators._chain]
111        self.assertEqual(CaptchaValidator in validators, True)
112
113
114class TestCaptchaWidget(ptc.PloneTestCase):
115
116    CF = CaptchaField.__module__ + '.CaptchaField'
117    CW = CaptchaWidget.__module__ + '.CaptchaWidget'
118
119    def afterSetUp(self):
120        self.widgets = dict(availableWidgets())
121
122    def testRegistration(self):
123        self.assertEqual(self.CW in self.widgets, True)
124        cw = self.widgets[self.CW]
125        self.assertEqual(self.CF in cw.used_for, True)
126
127    def testWidgetMacro(self):
128        widget_macro = self.widgets[self.CW].klass._properties['macro']
129        self.assertEqual(widget_macro, CAPTCHA_MACRO)
130
131    def testWidgetMacroAccessable(self):
132        macro = self.portal.restrictedTraverse(CAPTCHA_MACRO)
133        self.assertNotEqual(macro, None)
134   
135
136def test_suite():
137    suite = unittest.TestSuite()
138    suite.addTest(unittest.makeSuite(TestInstallations))
139    suite.addTest(unittest.makeSuite(TestCaptchaField))
140    suite.addTest(unittest.makeSuite(TestCaptchaWidget))
141    return suite
Note: See TracBrowser for help on using the repository browser.