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