[2527] | 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) + '/google-sitemaps' |
---|
| 37 | self.gsm_props = self.portal.portal_properties['googlesitemap_properties'] |
---|
| 38 | |
---|
| 39 | # Add testing document to portal |
---|
[2534] | 40 | self.my_doc = _createObjectByType('Document', self.portal, id='my_doc') |
---|
[2527] | 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 | |
---|
| 137 | def test_suite(): |
---|
| 138 | from unittest import TestSuite, makeSuite |
---|
| 139 | suite = TestSuite() |
---|
| 140 | suite.addTest(makeSuite(TestGoogleSitemaps)) |
---|
| 141 | return suite |
---|
| 142 | |
---|
| 143 | if __name__ == '__main__': |
---|
| 144 | unittest.main(defaultTest='test_suite') |
---|
| 145 | # framework() |
---|