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