source: products/quintagroup.plonegooglesitemaps/trunk/quintagroup/plonegooglesitemaps/tests/testGoogleSitemaps.py @ 2538

Last change on this file since 2538 was 2538, checked in by mylan, 9 years ago

Merged revisions 3566-3575 via svnmerge from
http://svn.quintagroup.com/products/quintagroup.plonegooglesitemaps/branches/test_refactoring

........

r3566 | mylan | 2010-06-14 12:24:52 +0300 (Mon, 14 Jun 2010) | 1 line


#206: Split single testqPloneGoogleSitemaps module into several specific one

........

r3567 | mylan | 2010-06-14 16:14:34 +0300 (Mon, 14 Jun 2010) | 1 line


#206: Improve code coverage - remove useless BBB code from interfaces.

........

r3568 | mylan | 2010-06-14 20:24:25 +0300 (Mon, 14 Jun 2010) | 1 line


#206: Improve code coverage - added test for MobileSitemap?, MobileSitemapView?.

........

r3569 | mylan | 2010-06-14 22:39:39 +0300 (Mon, 14 Jun 2010) | 1 line


#206: reorganize sitemap tests

........

r3570 | mylan | 2010-06-14 22:40:12 +0300 (Mon, 14 Jun 2010) | 1 line


#206: Added configlet tests

........

r3571 | mylan | 2010-06-14 23:05:19 +0300 (Mon, 14 Jun 2010) | 1 line


#206: remove BBB code for plone<3.0

........

r3572 | mylan | 2010-06-14 23:12:00 +0300 (Mon, 14 Jun 2010) | 1 line


#206: added workflow vocabularies tests for SitemapTypes?

........

r3573 | mylan | 2010-06-15 21:36:40 +0300 (Tue, 15 Jun 2010) | 1 line


#206: some cleanup, simplify tests

........

r3574 | mylan | 2010-06-16 16:35:31 +0300 (Wed, 16 Jun 2010) | 1 line


#206: move mobile sitemap code preparation into base module

........

r3575 | mylan | 2010-06-16 16:37:14 +0300 (Wed, 16 Jun 2010) | 1 line


#206: Added security tests

........

  • Property svn:eol-style set to native
File size: 5.8 KB
Line 
1#
2# Tests for quintagroup.plonegooglesitemaps
3#
4
5from base import *
6from cgi import FieldStorage
7from tempfile import TemporaryFile, NamedTemporaryFile
8
9from OFS.Image import cookId
10from Products.CMFPlone.utils import _createObjectByType
11from ZPublisher.HTTPRequest import FileUpload
12
13
14def prepareUploadFile(prefix=""):
15    """ Helper function for prerare file to uploading """
16    fp = NamedTemporaryFile(mode='w+', prefix=prefix)
17    fp.write("google-site-verification: " + fp.name)
18    fp.seek(0,2)
19    fsize = fp.tell()
20    fp.seek(0)
21
22    env = {'REQUEST_METHOD':'PUT'}
23    headers = {'content-type':'text/plain',
24               'content-length': fsize,
25               'content-disposition':'attachment; filename=%s' % fp.name}
26    fs = FieldStorage(fp=fp, environ=env, headers=headers)
27    return FileUpload(fs), fp
28
29
30class TestGoogleSitemaps(FunctionalTestCase):
31
32    def afterSetUp(self):
33        super(TestGoogleSitemaps, self).afterSetUp()
34
35        _createObjectByType('Sitemap', self.portal, id='google-sitemaps')
36        self.sitemapUrl = '/'+self.portal.absolute_url(1) + '/google-sitemaps'
37        self.gsm_props = self.portal.portal_properties['googlesitemap_properties']
38
39        # Add testing document to portal
40        self.my_doc = _createObjectByType('Document', self.portal, id='my_doc')
41        self.my_doc.edit(text_format='plain', text='hello world')
42
43    def testSitemap(self):
44        sitemap = self.publish(self.sitemapUrl, self.auth).getBody()
45        parsed_sitemap = parse(sitemap)
46        start = parsed_sitemap['start']
47        data = parsed_sitemap['data']
48        self.assert_('urlset' in start.keys())
49        self.assertFalse(self.my_doc.absolute_url(0) in data,
50                         'Wrong content present in the sitemap')
51
52        self.workflow.doActionFor(self.my_doc, 'publish')
53
54        sitemap = self.publish(self.sitemapUrl, self.auth).getBody()
55        parsed_sitemap = parse(sitemap)
56        start = parsed_sitemap['start']
57        data = parsed_sitemap['data']
58        self.assertEqual(len(start.keys()), 4)
59        self.assert_('urlset' in start.keys())
60        self.assert_('url' in start.keys())
61        self.assert_('loc' in start.keys())
62        self.assert_('lastmod' in start.keys())
63
64        self.assertTrue(self.my_doc.absolute_url(0) in data, 'Incorect url')
65
66    def testVerificationFileCreation(self):
67        fp, fname = None, None
68        try:
69            fupload, fp = prepareUploadFile()
70            fname, ftitle = cookId('', '', fupload)
71            self.portal.REQUEST.form['verification_file'] = fupload
72            self.portal.gsm_create_verify_file()
73        finally:
74            if fp: fp.close()
75        vf_created = hasattr(self.portal, fname)
76        self.assert_(vf_created, 'Verification file not created')
77
78    def testVerificationForm(self):
79        verifyConfigUrl = '/'+self.portal.absolute_url(1) + '/prefs_gsm_verification'
80        verif_config = self.publish(verifyConfigUrl, self.auth).getBody()
81        rexp_input_acitve = re.compile('<input\s+name="verification_file"\s+([^>]*)>', re.I|re.S)
82        rexp_button_acitve = re.compile('<input\s+name="form.button.CreateFile"\s+([^>]*)>', re.I|re.S)
83        rexp_delete_button = re.compile('<input\s+name="form.button.DeleteFile"\s+[^>]*>', re.I|re.S)
84
85        input_acitve = rexp_input_acitve.search(verif_config)
86        button_acitve = rexp_button_acitve.search(verif_config)
87        delete_button = rexp_delete_button.match(verif_config)
88
89        self.assert_(input_acitve and not 'disabled' in input_acitve.groups(1))
90        self.assert_(button_acitve and not 'disabled' in button_acitve.groups(1))
91        self.assert_(not delete_button)
92
93        fp, fname = None, None
94        try:
95            fupload, fp = prepareUploadFile()
96            fname, ftitle = cookId('', '', fupload)
97            self.portal.REQUEST.form['verification_file'] = fupload
98            self.portal.gsm_create_verify_file()
99        finally:
100            if fp: fp.close()
101
102        input_acitve = rexp_input_acitve.search(verif_config)
103        button_acitve = rexp_button_acitve.search(verif_config)
104        delete_button = rexp_delete_button.match(verif_config)
105
106        verif_config = self.publish(verifyConfigUrl, self.auth).getBody()
107        self.assert_(input_acitve and not 'disabled' in input_acitve.groups(1))
108        self.assert_(not delete_button)
109
110    def testMultiplyVerificationFiles(self):
111        verifyConfigUrl = '/'+self.portal.absolute_url(1) + '/prefs_gsm_verification'
112        fnames = []
113        for i in [1,2]:
114            fp, fname, response = None, None, None
115            try:
116                fupload, fp = prepareUploadFile(prefix=str(i))
117                fname, ftitle = cookId('', '', fupload)
118                form = {'form.button.CreateFile': 'Create verification file',
119                        'form.submitted': 1}
120                extra_update = {'verification_file': fupload}
121                response = self.publish(verifyConfigUrl, request_method='POST',
122                                        stdin=StringIO(urlencode(form)),
123                                        basic=self.auth, extra=extra_update)
124            finally:
125                if fp: fp.close()
126           
127            self.assertEqual(response.getStatus(), 200)
128            self.assert_(fname in self.gsm_props.getProperty('verification_filenames',[]),
129                             self.gsm_props.getProperty('verification_filenames',[]))
130            fnames.append(fname)
131
132        self.assertEqual(len([1 for vf in fnames \
133            if vf in self.gsm_props.getProperty('verification_filenames',[])]), 2,
134            self.gsm_props.getProperty('verification_filenames',[]))
135
136
137def test_suite():
138    from unittest import TestSuite, makeSuite
139    suite = TestSuite()
140    suite.addTest(makeSuite(TestGoogleSitemaps))
141    return suite
142
143if __name__ == '__main__':
144    unittest.main(defaultTest='test_suite')
145#    framework()
Note: See TracBrowser for help on using the repository browser.