1 | # |
---|
2 | # Tests for quintagroup.plonegooglesitemaps |
---|
3 | # |
---|
4 | |
---|
5 | from base import * |
---|
6 | from cgi import FieldStorage |
---|
7 | from tempfile import TemporaryFile, NamedTemporaryFile |
---|
8 | |
---|
9 | from OFS.Image import cookId |
---|
10 | from Products.CMFPlone.utils import _createObjectByType |
---|
11 | from ZPublisher.HTTPRequest import FileUpload |
---|
12 | |
---|
13 | |
---|
14 | def 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 | |
---|
30 | class 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) + \ |
---|
37 | '/google-sitemaps' |
---|
38 | gsm_properties = 'googlesitemap_properties' |
---|
39 | self.gsm_props = self.portal.portal_properties[gsm_properties] |
---|
40 | |
---|
41 | # Add testing document to portal |
---|
42 | self.my_doc = _createObjectByType('Document', self.portal, id='my_doc') |
---|
43 | self.my_doc.edit(text_format='plain', text='hello world') |
---|
44 | |
---|
45 | def testSitemap(self): |
---|
46 | sitemap = self.publish(self.sitemapUrl, self.auth).getBody() |
---|
47 | parsed_sitemap = parse(sitemap) |
---|
48 | start = parsed_sitemap['start'] |
---|
49 | data = parsed_sitemap['data'] |
---|
50 | self.assert_('urlset' in start.keys()) |
---|
51 | self.assertFalse(self.my_doc.absolute_url(0) in data, |
---|
52 | 'Wrong content present in the sitemap') |
---|
53 | |
---|
54 | self.workflow.doActionFor(self.my_doc, 'publish') |
---|
55 | |
---|
56 | sitemap = self.publish(self.sitemapUrl, self.auth).getBody() |
---|
57 | parsed_sitemap = parse(sitemap) |
---|
58 | start = parsed_sitemap['start'] |
---|
59 | data = parsed_sitemap['data'] |
---|
60 | self.assertEqual(len(start.keys()), 4) |
---|
61 | self.assert_('urlset' in start.keys()) |
---|
62 | self.assert_('url' in start.keys()) |
---|
63 | self.assert_('loc' in start.keys()) |
---|
64 | self.assert_('lastmod' in start.keys()) |
---|
65 | |
---|
66 | self.assertTrue(self.my_doc.absolute_url(0) in data, 'Incorect url') |
---|
67 | |
---|
68 | def testVerificationFileCreation(self): |
---|
69 | fp, fname = None, None |
---|
70 | try: |
---|
71 | fupload, fp = prepareUploadFile() |
---|
72 | fname, ftitle = cookId('', '', fupload) |
---|
73 | self.portal.REQUEST.form['verification_file'] = fupload |
---|
74 | self.portal.gsm_create_verify_file() |
---|
75 | finally: |
---|
76 | if fp: |
---|
77 | fp.close() |
---|
78 | vf_created = hasattr(self.portal, fname) |
---|
79 | self.assert_(vf_created, 'Verification file not created') |
---|
80 | |
---|
81 | def testVerificationForm(self): |
---|
82 | verifyConfigUrl = '/' + self.portal.absolute_url(1) + \ |
---|
83 | '/prefs_gsm_verification' |
---|
84 | verif_config = self.publish(verifyConfigUrl, self.auth).getBody() |
---|
85 | rexp_input_acitve = re.compile('<input\s+name="verification_file"' \ |
---|
86 | '\s+([^>]*)>', re.I | re.S) |
---|
87 | rexp_button_acitve = re.compile( |
---|
88 | '<input\s+name="form.button.CreateFile"\s+([^>]*)>', re.I | re.S) |
---|
89 | rexp_delete_button = re.compile( |
---|
90 | '<input\s+name="form.button.DeleteFile"\s+[^>]*>', re.I | re.S) |
---|
91 | |
---|
92 | input_acitve = rexp_input_acitve.search(verif_config) |
---|
93 | button_acitve = rexp_button_acitve.search(verif_config) |
---|
94 | delete_button = rexp_delete_button.match(verif_config) |
---|
95 | |
---|
96 | self.assert_(input_acitve and not 'disabled' in input_acitve.groups(1)) |
---|
97 | self.assert_(button_acitve and not 'disabled' in \ |
---|
98 | button_acitve.groups(1)) |
---|
99 | self.assert_(not delete_button) |
---|
100 | |
---|
101 | fp, fname = None, None |
---|
102 | try: |
---|
103 | fupload, fp = prepareUploadFile() |
---|
104 | fname, ftitle = cookId('', '', fupload) |
---|
105 | self.portal.REQUEST.form['verification_file'] = fupload |
---|
106 | self.portal.gsm_create_verify_file() |
---|
107 | finally: |
---|
108 | if fp: |
---|
109 | fp.close() |
---|
110 | |
---|
111 | input_acitve = rexp_input_acitve.search(verif_config) |
---|
112 | button_acitve = rexp_button_acitve.search(verif_config) |
---|
113 | delete_button = rexp_delete_button.match(verif_config) |
---|
114 | |
---|
115 | verif_config = self.publish(verifyConfigUrl, self.auth).getBody() |
---|
116 | self.assert_(input_acitve and not 'disabled' in input_acitve.groups(1)) |
---|
117 | self.assert_(not delete_button) |
---|
118 | |
---|
119 | def testMultiplyVerificationFiles(self): |
---|
120 | verifyConfigUrl = '/' + self.portal.absolute_url(1) + \ |
---|
121 | '/prefs_gsm_verification' |
---|
122 | fnames = [] |
---|
123 | for i in [1, 2]: |
---|
124 | fp, fname, response = None, None, None |
---|
125 | try: |
---|
126 | fupload, fp = prepareUploadFile(prefix=str(i)) |
---|
127 | fname, ftitle = cookId('', '', fupload) |
---|
128 | form = {'form.button.CreateFile': 'Create verification file', |
---|
129 | 'form.submitted': 1} |
---|
130 | extra_update = {'verification_file': fupload} |
---|
131 | response = self.publish(verifyConfigUrl, request_method='POST', |
---|
132 | stdin=StringIO(urlencode(form)), |
---|
133 | basic=self.auth, extra=extra_update) |
---|
134 | finally: |
---|
135 | if fp: |
---|
136 | fp.close() |
---|
137 | |
---|
138 | self.assertEqual(response.getStatus(), 200) |
---|
139 | self.assert_(fname in \ |
---|
140 | self.gsm_props.getProperty('verification_filenames', []), |
---|
141 | self.gsm_props.getProperty('verification_filenames', [])) |
---|
142 | fnames.append(fname) |
---|
143 | |
---|
144 | self.assertEqual(len([1 for vf in fnames \ |
---|
145 | if vf in \ |
---|
146 | self.gsm_props.getProperty('verification_filenames', [])]), 2, |
---|
147 | self.gsm_props.getProperty('verification_filenames', [])) |
---|
148 | |
---|
149 | |
---|
150 | def test_suite(): |
---|
151 | from unittest import TestSuite, makeSuite |
---|
152 | suite = TestSuite() |
---|
153 | suite.addTest(makeSuite(TestGoogleSitemaps)) |
---|
154 | return suite |
---|
155 | |
---|
156 | if __name__ == '__main__': |
---|
157 | unittest.main(defaultTest='test_suite') |
---|
158 | # framework() |
---|