1 | import sys |
---|
2 | import re |
---|
3 | import unittest |
---|
4 | from StringIO import StringIO |
---|
5 | |
---|
6 | #from zope.testing import doctestunit |
---|
7 | #from zope.component import testing |
---|
8 | from zope.component import queryUtility, queryAdapter |
---|
9 | from zope.component import getSiteManager, getGlobalSiteManager |
---|
10 | from Testing import ZopeTestCase as ztc |
---|
11 | |
---|
12 | from Products.Five import fiveconfigure |
---|
13 | from Products.PloneTestCase import PloneTestCase as ptc |
---|
14 | from Products.PloneTestCase.layer import PloneSite |
---|
15 | from Products.PloneTestCase.PloneTestCase import portal_owner |
---|
16 | from Products.PloneTestCase.PloneTestCase import default_password |
---|
17 | |
---|
18 | ptc.setupPloneSite() |
---|
19 | |
---|
20 | import quintagroup.gauth |
---|
21 | from quintagroup.gauth.utility import SafeQuery |
---|
22 | from quintagroup.gauth.interfaces import IGAuthUtility |
---|
23 | from quintagroup.gauth.browser.configlet import IGAuthConfigletSchema |
---|
24 | |
---|
25 | class GauthLayer(PloneSite): |
---|
26 | @classmethod |
---|
27 | def setUp(cls): |
---|
28 | fiveconfigure.debug_mode = True |
---|
29 | ztc.installPackage(quintagroup.gauth) |
---|
30 | fiveconfigure.debug_mode = False |
---|
31 | |
---|
32 | @classmethod |
---|
33 | def tearDown(cls): |
---|
34 | pass |
---|
35 | |
---|
36 | |
---|
37 | class TestCase(ptc.PloneTestCase): |
---|
38 | layer = GauthLayer |
---|
39 | |
---|
40 | |
---|
41 | class 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 | |
---|
56 | class 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 | |
---|
82 | class TestConfiglet(FunctionalTestCase): |
---|
83 | |
---|
84 | def afterSetUp(self): |
---|
85 | self.loginAsPortalOwner() |
---|
86 | self.addProduct("quintagroup.gauth") |
---|
87 | self.basic_auth = portal_owner + ":" + default_password |
---|
88 | self.get_url = self.portal.id+'/@@gauth-controlpanel' |
---|
89 | self.save_url = self.portal.id+'/@@gauth-controlpanel?form.actions.save=1' \ |
---|
90 | '&_authenticator=%s' % self._getauth() |
---|
91 | |
---|
92 | def test_presentEmail(self): |
---|
93 | res = self.publish(self.get_url, self.basic_auth).getBody() |
---|
94 | self.assert_(re.match(".*<input\s[^>]*name=\"form.gauth_email\"[^>]*>", res, re.I|re.S)) |
---|
95 | |
---|
96 | def test_presentPassword(self): |
---|
97 | res = self.publish(self.get_url, self.basic_auth).getBody() |
---|
98 | self.assert_(re.match(".*<input\s[^>]*name=\"form.gauth_pass\"[^>]*>", res, re.I|re.S)) |
---|
99 | |
---|
100 | def test_update(self): |
---|
101 | temail, tpass = "tester@test.com", "secret" |
---|
102 | gauth_util = queryUtility(IGAuthUtility) |
---|
103 | url = self.save_url + '&form.gauth_email='+temail + '&form.gauth_pass='+tpass |
---|
104 | self.publish(url, self.basic_auth) |
---|
105 | self.assert_(gauth_util.email == temail) |
---|
106 | self.assert_(gauth_util.password == tpass) |
---|
107 | |
---|
108 | |
---|
109 | class TestUtility(FunctionalTestCase): |
---|
110 | |
---|
111 | def afterSetUp(self): |
---|
112 | self.loginAsPortalOwner() |
---|
113 | self.addProduct("quintagroup.gauth") |
---|
114 | sm = getSiteManager(self.portal) |
---|
115 | self.gauthutil = sm.queryUtility(IGAuthUtility) |
---|
116 | self.gauthconfiglet = queryAdapter(self.portal, IGAuthConfigletSchema) |
---|
117 | |
---|
118 | def testEmail(self): |
---|
119 | self.assertEqual(bool(self.gauthutil.email), False) |
---|
120 | self.gauthconfiglet.gauth_email = "tester@test.com" |
---|
121 | self.assertEqual(self.gauthutil.email, "tester@test.com") |
---|
122 | |
---|
123 | def testPassword(self): |
---|
124 | self.assertEqual(bool(self.gauthutil.password), False) |
---|
125 | self.gauthconfiglet.gauth_pass = u"secret" |
---|
126 | self.assertEqual(self.gauthutil.password, "secret") |
---|
127 | |
---|
128 | |
---|
129 | import gdata.service |
---|
130 | |
---|
131 | out = "" |
---|
132 | class DummyService(object): |
---|
133 | doraise = False |
---|
134 | |
---|
135 | def ProgrammaticLogin(self): |
---|
136 | global out |
---|
137 | out += "\nCall ProgrammaticLogin" |
---|
138 | self.doraise = False |
---|
139 | |
---|
140 | def Action(self, *args, **kwargs): |
---|
141 | global out |
---|
142 | out += "\nCall Action with: args='%s', kwargs='%s'" % (str(args), str(kwargs)) |
---|
143 | if self.doraise: |
---|
144 | raise gdata.service.RequestError("Token is expired") |
---|
145 | |
---|
146 | class TestSafeQuery(unittest.TestCase): |
---|
147 | |
---|
148 | def setUp(self): |
---|
149 | global out |
---|
150 | self.serv = DummyService() |
---|
151 | self.args = "test_arg", |
---|
152 | self.kwargs = {"kw1_key": "kw1_val"} |
---|
153 | self.sq = SafeQuery() |
---|
154 | out = "" |
---|
155 | |
---|
156 | def testMethodCall(self): |
---|
157 | self.sq.safeQuery(self.serv, self.serv.Action, *self.args, **self.kwargs) |
---|
158 | res = filter(None, out.split("\n")) |
---|
159 | self.assertEqual(res[0], |
---|
160 | "Call Action with: args='%s', kwargs='%s'" % (str(self.args), str(self.kwargs))) |
---|
161 | |
---|
162 | def testProgrammaticLogin(self): |
---|
163 | self.serv.doraise = True |
---|
164 | self.sq.safeQuery(self.serv, self.serv.Action, *self.args, **self.kwargs) |
---|
165 | res = filter(None, out.split("\n")) |
---|
166 | self.assertEqual(res[0], "Call Action with: args='%s', kwargs='%s'" % ( |
---|
167 | str(self.args), str(self.kwargs))) |
---|
168 | self.assertEqual(res[1], "Call ProgrammaticLogin") |
---|
169 | self.assertEqual(res[2], "Call Action with: args='%s', kwargs='%s'" % ( |
---|
170 | str(self.args), str(self.kwargs))) |
---|
171 | |
---|
172 | |
---|
173 | def test_suite(): |
---|
174 | from unittest import TestSuite, makeSuite |
---|
175 | suite = TestSuite() |
---|
176 | suite.addTest(makeSuite(TestInstall)) |
---|
177 | suite.addTest(makeSuite(TestConfiglet)) |
---|
178 | suite.addTest(makeSuite(TestUtility)) |
---|
179 | suite.addTest(makeSuite(TestSafeQuery)) |
---|
180 | return suite |
---|
181 | |
---|
182 | |
---|
183 | if __name__ == '__main__': |
---|
184 | unittest.main(defaultTest='test_suite') |
---|