[server] use unicode wherever appropriate in transaction/undo related methods
Silences lgdb/sqlite warnings about using str instead of unicode.
# copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved.# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr## This file is part of CubicWeb.## CubicWeb is free software: you can redistribute it and/or modify it under the# terms of the GNU Lesser General Public License as published by the Free# Software Foundation, either version 2.1 of the License, or (at your option)# any later version.## CubicWeb is distributed in the hope that it will be useful, but WITHOUT# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more# details.## You should have received a copy of the GNU Lesser General Public License along# with CubicWeb. If not, see <http://www.gnu.org/licenses/>.fromjsonimportloadsfromos.pathimportjoinimporttempfiletry:importrequestsassert[int(n)forninrequests.__version__.split('.',2)][:2]>=[1,2]except(ImportError,AssertionError):requests=Nonefromlogilab.common.testlibimportTestCase,unittest_mainfromcubicweb.devtools.httptestimportCubicWebServerTCfromcubicweb.devtools.fakeimportFakeRequestclassAjaxReplaceUrlTC(TestCase):deftest_ajax_replace_url_1(self):self._test_arurl("fname=view&rql=Person%20P&vid=list",rql='Person P',vid='list')deftest_ajax_replace_url_2(self):self._test_arurl("age=12&fname=view&name=bar&rql=Person%20P&vid=oneline",rql='Person P',vid='oneline',name='bar',age=12)def_test_arurl(self,qs,**kwargs):req=FakeRequest()arurl=req.ajax_replace_url# NOTE: for the simplest use cases, we could use doctesturl=arurl('foo',**kwargs)self.assertTrue(url.startswith('javascript:'))self.assertTrue(url.endswith('()'))cbname=url.split()[1][:-2]self.assertMultiLineEqual('function %s() { $("#foo").loadxhtml("http://testing.fr/cubicweb/ajax?%s",''{"pageid": "%s"},"get","replace"); }'%(cbname,qs,req.pageid),req.html_headers.post_inlined_scripts[0])classFileUploadTC(CubicWebServerTC):defsetUp(self):"Skip whole test class if a suitable requests module is not available"ifrequestsisNone:self.skipTest('Python ``requests`` module is not available')super(FileUploadTC,self).setUp()@propertydef_post_url(self):withself.admin_access.web_request()asreq:returnreq.build_url('ajax',fname='fileupload')def_fobject(self,fname):returnopen(join(self.datadir,fname),'rb')def_fcontent(self,fname):returnself._fobject(fname).read()deftest_single_file_upload(self):files={'file':('schema.py',self._fobject('schema.py'))}webreq=requests.post(self._post_url,files=files)# check backward compat : a single uploaded file leads to a single# 2-uple in the request formexpect={'fname':u'fileupload','file':['schema.py',self._fcontent('schema.py')]}self.assertEqual(webreq.status_code,200)self.assertDictEqual(expect,loads(webreq.content))deftest_multiple_file_upload(self):files=[('files',('schema.py',self._fobject('schema.py'))),('files',('views.py',self._fobject('views.py')))]webreq=requests.post(self._post_url,files=files,)expect={'fname':u'fileupload','files':[['schema.py',self._fcontent('schema.py')],['views.py',self._fcontent('views.py')]],}self.assertEqual(webreq.status_code,200)self.assertDictEqual(expect,loads(webreq.content))classLanguageTC(CubicWebServerTC):deftest_language_neg(self):headers={'Accept-Language':'fr'}webreq=self.web_request(headers=headers)self.assertIn('lang="fr"',webreq.read())vary=[h.lower().strip()forhinwebreq.getheader('Vary').split(',')]self.assertIn('accept-language',vary)headers={'Accept-Language':'en'}webreq=self.web_request(headers=headers)self.assertIn('lang="en"',webreq.read())vary=[h.lower().strip()forhinwebreq.getheader('Vary').split(',')]self.assertIn('accept-language',vary)deftest_response_codes(self):withself.admin_access.client_cnx()ascnx:admin_eid=cnx.user.eid# guest can't see adminwebreq=self.web_request('/%d'%admin_eid)self.assertEqual(webreq.status,403)# but admin canself.web_login()webreq=self.web_request('/%d'%admin_eid)self.assertEqual(webreq.status,200)deftest_session_cookie_httponly(self):webreq=self.web_request()self.assertIn('HttpOnly',webreq.getheader('set-cookie'))classLogQueriesTC(CubicWebServerTC):@classmethoddefinit_config(cls,config):super(LogQueriesTC,cls).init_config(config)cls.logfile=tempfile.NamedTemporaryFile()config.global_set_option('query-log-file',cls.logfile.name)deftest_log_queries(self):self.web_request()self.assertTrue(self.logfile.read())@classmethoddeftearDownClass(cls):cls.logfile.close()if__name__=='__main__':unittest_main()