[test] simplify setup of sobjects/test/unittest_supervising.py
fromlogilab.commonimporttempattrfromlogilab.common.testlibimporttag,Tagsfromcubicweb.devtools.testlibimportCubicWebTCimportosimportos.pathasospimportglobfromcubicweb.utilsimportHTMLHeadfromcubicweb.web.views.staticcontrollersimportConcatFilesHandlerclassStaticControllerCacheTC(CubicWebTC):tags=CubicWebTC.tags|Tags('static_controller','cache','http')def_publish_static_files(self,url,header={}):req=self.request(headers=header)req._url=urlreturnself.app_handle_request(req,url),reqdeftest_static_file_are_cached(self):_,req=self._publish_static_files('data/cubicweb.css')self.assertEqual(200,req.status_out)self.assertIn('last-modified',req.headers_out)next_headers={'if-modified-since':req.get_response_header('last-modified',raw=True),}_,req=self._publish_static_files('data/cubicweb.css',next_headers)self.assertEqual(304,req.status_out)classDataControllerTC(CubicWebTC):tags=CubicWebTC.tags|Tags('static_controller','data','http')def_publish_static_files(self,url,header={}):req=self.request(headers=header)req._url=urlreturnself.app_handle_request(req,url),reqdef_check_datafile_ok(self,fname):_,req=self._publish_static_files(fname)self.assertEqual(200,req.status_out)self.assertIn('last-modified',req.headers_out)next_headers={'if-modified-since':req.get_response_header('last-modified',raw=True),}_,req=self._publish_static_files(fname,next_headers)self.assertEqual(304,req.status_out)def_check_no_datafile(self,fname):_,req=self._publish_static_files(fname)self.assertEqual(404,req.status_out)deftest_static_data_mode(self):hash=self.vreg.config.instance_md5_version()self.assertEqual(32,len(hash))withtempattr(self.vreg.config,'mode','test'):self._check_datafile_ok('data/cubicweb.css')self._check_no_datafile('data/does/not/exist')self._check_no_datafile('data/%s/cubicweb.css'%('0'*len(hash)))withtempattr(self.vreg.config,'mode','notest'):self._check_datafile_ok('data/cubicweb.css')self._check_datafile_ok('data/%s/cubicweb.css'%hash)self._check_no_datafile('data/does/not/exist')self._check_no_datafile('data/%s/cubicweb.css'%('0'*len(hash)))classConcatFilesTC(CubicWebTC):tags=CubicWebTC.tags|Tags('static_controller','concat')deftearDown(self):super(ConcatFilesTC,self).tearDown()self._cleanup_concat_cache()def_cleanup_concat_cache(self):uicachedir=osp.join(self.config.apphome,'uicache')forfnameinglob.glob(osp.join(uicachedir,'cache_concat_*')):os.unlink(osp.join(uicachedir,fname))def_publish_js_files(self,js_files):req=self.request()head=HTMLHead(req)url=head.concat_urls([req.data_url(js_file)forjs_fileinjs_files])[len(req.base_url()):]req._url=urlreturnself.app_handle_request(req,url),reqdefexpected_content(self,js_files):content=u''forjs_fileinjs_files:dirpath,rid=self.config.locate_resource(js_file)ifdirpathisnotNone:# ignore resources not foundwithopen(osp.join(dirpath,rid))asf:content+=f.read()+'\n'returncontentdeftest_cache(self):js_files=('cubicweb.ajax.js','jquery.js')result,req=self._publish_js_files(js_files)self.assertNotEqual(404,req.status_out)# check result contentself.assertEqual(result,self.expected_content(js_files))# make sure we kept a cached version on filesystemconcat_hander=ConcatFilesHandler(self.config)filepath=concat_hander.build_filepath(js_files)self.assertTrue(osp.isfile(filepath))deftest_invalid_file_in_debug_mode(self):js_files=('cubicweb.ajax.js','dummy.js')# in debug mode, an error is raisedself.config.debugmode=Truetry:result,req=self._publish_js_files(js_files)#print resultself.assertEqual(404,req.status_out)finally:self.config.debugmode=Falsedeftest_invalid_file_in_production_mode(self):js_files=('cubicweb.ajax.js','dummy.js')result,req=self._publish_js_files(js_files)self.assertNotEqual(404,req.status_out)# check result contentself.assertEqual(result,self.expected_content(js_files))if__name__=='__main__':fromlogilab.common.testlibimportunittest_mainunittest_main()