source: products/quintagroup.plonegooglesitemaps/branches/test_refactoring/quintagroup/plonegooglesitemaps/tests/testSecurity.py

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

#206: Added security tests

  • Property svn:eol-style set to native
File size: 4.4 KB
Line 
1#
2# Tests related to general Sitemap type.
3#
4from base import *
5from zope.interface import alsoProvides
6from Products.Archetypes import atapi
7from Products.CMFPlone.utils import _createObjectByType
8
9class MixinSecurity(FunctionalTestCase):
10
11    def getview(self, vpath):
12        return self.publish("/"+self.portal.absolute_url(1)+"/"+vpath, self.auth)
13
14
15class TestSecurityConfigletManager(MixinSecurity):
16
17    def afterSetUp(self):
18        super(TestSecurityConfigletManager, self).afterSetUp()
19        self.auth = "admin:admin"
20        self.portal.portal_membership.addMember('admin', 'admin', ('Manager',), [])
21
22    def testConfigOverview(self):
23        resp = self.getview("prefs_gsm_overview")
24        self.assertEqual(resp.status / 100, 2)
25
26    def testConfigSettings(self):
27        resp = self.getview("prefs_gsm_settings")
28        self.assertEqual(resp.status / 100, 2)
29
30    def testConfigVerification(self):
31        resp = self.getview("prefs_gsm_verification")
32        self.assertEqual(resp.status / 100, 2)
33
34
35class TestSecurityConfigletNotManager(MixinSecurity):
36
37    def afterSetUp(self):
38        super(TestSecurityConfigletNotManager, self).afterSetUp()
39        self.auth = "mem:mem"
40        self.portal.portal_membership.addMember('mem', 'mem', ('Member',), [])
41
42    def testConfigOverview(self):
43        resp = self.getview("prefs_gsm_overview")
44        self.assertNotEqual(resp.status / 100, 2)
45
46    def testConfigSettings(self):
47        resp = self.getview("prefs_gsm_settings")
48        self.assertNotEqual(resp.status / 100, 2)
49
50    def testConfigVerification(self):
51        resp = self.getview("prefs_gsm_verification")
52        self.assertNotEqual(resp.status / 100, 2)
53
54
55SM_TYPES = {
56  "content": {"id": "sitemap.xml", "types": ("Document",)},
57  "news": {"id": "news-sitemap.xml", "types": ("News Item",)},
58  "mobile": {"id": "mobile-sitemap.xml", "types": ("Document",)},
59}
60from DateTime import DateTime
61
62class TestSecuritySiteMaps(MixinSecurity):
63
64    def afterSetUp(self):
65        super(TestSecuritySiteMaps, self).afterSetUp()
66        self.auth = ":"
67        self.patchMobile()
68        self.createSMaps()
69        self.createContent()
70
71    def createSMaps(self):
72        self.smaps = {}
73        for smtype, smdata in SM_TYPES.items():
74            _createObjectByType("Sitemap", self.portal, id=smdata["id"],
75                                sitemapType=smtype, portalTypes=smdata["types"])
76            sm = getattr(self.portal, smdata["id"])
77            sm.at_post_create_script()
78            self.smaps[smtype] = sm
79
80    def createContent(self):
81        self.my_doc = _createObjectByType('Document', self.portal, id='my_doc')
82        self.workflow.doActionFor(self.my_doc, 'publish')
83        self.my_news = _createObjectByType('News Item', self.portal, id='my_news')
84        self.my_news.edit(title="My News Item (test)",
85                          effectiveDate=DateTime().strftime("%Y-%m-%d"))
86        self.workflow.doActionFor(self.my_news, 'publish')
87        # mobile content must provides additional interfaces to fall into mobile sitemap
88        alsoProvides(self.my_doc, IMobileMarker)
89        self.my_doc.reindexObject()
90        self.my_news.reindexObject()
91
92    def reparse(self, data):
93        parsed = parse(data)
94        return parsed["start"], parsed["data"]
95
96    def testContentSM(self):
97        resp = self.getview("sitemap.xml")
98        self.assertEqual(resp.status / 100, 2)
99        start, data = self.reparse(resp.getBody())
100        self.assert_("loc" in start)
101        self.assert_(self.my_doc.absolute_url() in data)
102       
103    def testNewsSM(self):
104        resp = self.getview("news-sitemap.xml")
105        self.assertEqual(resp.status / 100, 2)
106        start, data = self.reparse(resp.getBody())
107        self.assert_("n:name" in start)
108        self.assert_("My News Item" in data)
109       
110    def testMobileSM(self):
111        resp = self.getview("mobile-sitemap.xml")
112        self.assertEqual(resp.status / 100, 2)
113        start, data = self.reparse(resp.getBody())
114        self.assert_("loc" in start)
115        self.assert_(self.my_doc.absolute_url() in data)
116       
117
118def test_suite():
119    from unittest import TestSuite, makeSuite
120    suite = TestSuite()
121    suite.addTest(makeSuite(TestSecurityConfigletManager))
122    suite.addTest(makeSuite(TestSecurityConfigletNotManager))
123    suite.addTest(makeSuite(TestSecuritySiteMaps))
124    return suite
125
126if __name__ == '__main__':
127    unittest.main(defaultTest='test_suite')
128#    framework()
Note: See TracBrowser for help on using the repository browser.