source: products/quintagroup.gauth/trunk/quintagroup/gauth/tests.py @ 2625

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

Fixed action icon uninstallation (removed by QI)

  • Property svn:eol-style set to native
File size: 7.4 KB
Line 
1import re
2import sys
3import unittest
4
5from zope.component import queryUtility, queryAdapter
6from zope.component import getSiteManager, getGlobalSiteManager
7from Testing import ZopeTestCase as ztc
8
9from Products.Five import zcml
10from Products.Five import fiveconfigure
11from Products.PloneTestCase import PloneTestCase as ptc
12from Products.PloneTestCase.layer import PloneSite
13from Products.PloneTestCase.PloneTestCase import portal_owner
14from Products.PloneTestCase.PloneTestCase import default_password
15
16ptc.setupPloneSite()
17
18import quintagroup.gauth
19from quintagroup.gauth.utility import SafeQuery
20from quintagroup.gauth.interfaces import IGAuthUtility
21from quintagroup.gauth.browser.configlet import IGAuthConfigletSchema
22
23class GauthLayer(PloneSite):
24    @classmethod
25    def setUp(cls):
26        fiveconfigure.debug_mode = True
27        import quintagroup.gauth
28        zcml.load_config('configure.zcml', quintagroup.gauth)
29        fiveconfigure.debug_mode = False
30        ztc.installPackage("quintagroup.gauth")
31
32    @classmethod
33    def tearDown(cls):
34        pass
35
36
37class TestCase(ptc.PloneTestCase):
38    layer = GauthLayer
39
40
41class FunctionalTestCase(ptc.FunctionalTestCase):
42    layer = GauthLayer
43
44    def _getauth(self):
45        # Fix authenticator for the form
46        try:
47            authenticator = self.portal.restrictedTraverse("@@authenticator")
48        except:
49            handle  = ""
50        else:
51            html = authenticator.authenticator()
52            handle = re.search('value="(.*)"', html).groups()[0]
53        return handle
54
55
56class TestInstall(TestCase):
57
58    def afterSetUp(self):
59        self.loginAsPortalOwner()
60        self.addProduct("quintagroup.gauth")
61       
62    def testProperties(self):
63        pp = self.portal.portal_properties
64        self.assert_("gauth_properties" in pp.objectIds())
65        self.assert_(bool(pp.gauth_properties.hasProperty("gauth_email")))
66
67    def testConfiglet(self):
68        cp = self.portal.portal_controlpanel
69        aifs = [ai['id'] for ai in cp.listActionInfos(
70                check_visibility=0, check_permissions=0, check_condition=0)]
71        self.assert_("quintagroup.gauth" in aifs, aifs)
72
73    def testUtility(self):
74        lsm = getSiteManager(self.portal)
75        gsm = getGlobalSiteManager()
76        lgauth = lsm.queryUtility(IGAuthUtility)
77        ggauth = gsm.queryUtility(IGAuthUtility)
78        self.assertEqual(ggauth, None)
79        self.assertNotEqual(lgauth, None)
80
81    def testActionIcons(self):
82        ait = self.portal.portal_actionicons
83        ai = ait.getActionInfo("controlpanel", "quintagroup.gauth")
84        self.assertNotEqual(ai, None)
85
86
87class TestUninstall(TestCase):
88
89    def afterSetUp(self):
90        self.loginAsPortalOwner()
91        self.addProduct("quintagroup.gauth")
92        qi = self.portal.portal_quickinstaller
93        # qi.installProducts(products=["quintagroup.gauth",])
94        qi.uninstallProducts(products=["quintagroup.gauth",])
95
96    def testProperties(self):
97        pp = self.portal.portal_properties
98        self.assert_(not "gauth_properties" in pp.objectIds())
99
100    def testConfiglet(self):
101        cp = self.portal.portal_controlpanel
102        aifs = [ai['id'] for ai in cp.listActionInfos(
103                check_visibility=0, check_permissions=0, check_condition=0)]
104        self.assert_(not "quintagroup.gauth" in aifs)
105
106    def testUtility(self):
107        lsm = getSiteManager(self.portal)
108        gsm = getGlobalSiteManager()
109        lgauth = lsm.queryUtility(IGAuthUtility)
110        ggauth = gsm.queryUtility(IGAuthUtility)
111        self.assertEqual(ggauth, None)
112        self.assertEqual(lgauth, None)
113
114    def testActionIcons(self):
115        ait = self.portal.portal_actionicons
116        ai = ait.queryActionInfo("controlpanel", "quintagroup.gauth", default=None)
117        self.assert_(ai == None)
118
119
120class TestConfiglet(FunctionalTestCase):
121
122    def afterSetUp(self):
123        self.loginAsPortalOwner()
124        self.addProduct("quintagroup.gauth")
125        self.basic_auth = portal_owner + ":" + default_password
126        self.get_url = self.portal.id+'/@@gauth-controlpanel'
127        self.save_url = self.portal.id+'/@@gauth-controlpanel?form.actions.save=1' \
128            '&_authenticator=%s' % self._getauth()
129
130    def test_presentEmail(self):
131        res = self.publish(self.get_url, self.basic_auth).getBody()
132        self.assert_(re.match(".*<input\s[^>]*name=\"form.gauth_email\"[^>]*>", res, re.I|re.S))
133
134    def test_presentPassword(self):
135        res = self.publish(self.get_url, self.basic_auth).getBody()
136        self.assert_(re.match(".*<input\s[^>]*name=\"form.gauth_pass\"[^>]*>", res, re.I|re.S))
137
138    def test_update(self):
139        temail, tpass = "tester@test.com", "secret"
140        gauth_util = queryUtility(IGAuthUtility)
141        url = self.save_url + '&form.gauth_email='+temail + '&form.gauth_pass='+tpass
142        self.publish(url, self.basic_auth)
143        self.assert_(gauth_util.email == temail)
144        self.assert_(gauth_util.password == tpass)
145
146
147class TestUtility(FunctionalTestCase):
148
149    def afterSetUp(self):
150        self.loginAsPortalOwner()
151        self.addProduct("quintagroup.gauth")
152        sm = getSiteManager(self.portal)
153        self.gauthutil = sm.queryUtility(IGAuthUtility)
154        self.gauthconfiglet = queryAdapter(self.portal, IGAuthConfigletSchema)
155
156    def testEmail(self):
157        self.assertEqual(bool(self.gauthutil.email), False)
158        self.gauthconfiglet.gauth_email = "tester@test.com"
159        self.assertEqual(self.gauthutil.email, "tester@test.com")
160
161    def testPassword(self):
162        self.assertEqual(bool(self.gauthutil.password), False)
163        self.gauthconfiglet.gauth_pass = u"secret"
164        self.assertEqual(self.gauthutil.password, "secret")
165
166
167import gdata.service
168
169out = ""
170class DummyService(object):
171    doraise = False
172
173    def ProgrammaticLogin(self):
174        global out
175        out += "\nCall ProgrammaticLogin"
176        self.doraise = False
177
178    def Action(self, *args, **kwargs):
179        global out
180        out += "\nCall Action with: args='%s', kwargs='%s'" % (str(args), str(kwargs))
181        if self.doraise:
182            raise gdata.service.RequestError("Token is expired")
183
184class TestSafeQuery(unittest.TestCase):
185
186    def setUp(self):
187        global out
188        self.serv = DummyService()
189        self.args = "test_arg",
190        self.kwargs = {"kw1_key": "kw1_val"}
191        self.sq = SafeQuery()
192        out = ""
193
194    def testMethodCall(self):
195        self.sq.safeQuery(self.serv, self.serv.Action, *self.args, **self.kwargs)
196        res = filter(None, out.split("\n"))
197        self.assertEqual(res[0],
198            "Call Action with: args='%s', kwargs='%s'" % (str(self.args), str(self.kwargs)))
199
200    def testProgrammaticLogin(self):
201        self.serv.doraise = True
202        self.sq.safeQuery(self.serv, self.serv.Action, *self.args, **self.kwargs)
203        res = filter(None, out.split("\n"))
204        self.assertEqual(res[0], "Call Action with: args='%s', kwargs='%s'" % (
205            str(self.args), str(self.kwargs)))
206        self.assertEqual(res[1], "Call ProgrammaticLogin")
207        self.assertEqual(res[2], "Call Action with: args='%s', kwargs='%s'" % (
208            str(self.args), str(self.kwargs)))
209
210
211def test_suite():
212    from unittest import TestSuite, makeSuite
213    suite = TestSuite()
214    suite.addTest(makeSuite(TestInstall))
215    suite.addTest(makeSuite(TestUninstall))
216    suite.addTest(makeSuite(TestConfiglet))
217    suite.addTest(makeSuite(TestUtility))
218    suite.addTest(makeSuite(TestSafeQuery))
219    return suite
220
221
222if __name__ == '__main__':
223    unittest.main(defaultTest='test_suite')
Note: See TracBrowser for help on using the repository browser.