[ldapfeed] Add support for LDAP groups (closes #2528116)
Groups from the LDAP server are imported as CWGourp entities, and in_group relationships are created between existing CWUsers and CWGroups
Unit tests have been refactored a bit, especially ti add helper methods to manage LDAP database.
fromlogilab.common.testlibimportTestCase,unittest_main,tag,Tagsfromcubicweb.webimportStatusResponsefromcubicweb.devtools.fakeimportFakeRequestdef_test_cache(hin,hout,method='GET'):"""forge and process a request return status code and the request object status is None is no cache is involved """# forge requestreq=FakeRequest(method=method)forkey,valueinhin:req._headers_in.addRawHeader(key,str(value))forkey,valueinhout:req.headers_out.addRawHeader(key,str(value))# processstatus=Nonetry:req.validate_cache()exceptStatusResponseasex:status=ex.statusreturnstatus,reqclassHTTPCache(TestCase):"""Check that the http cache logiac work as expected (as far as we understood the RFC) """tags=TestCase.tags|Tags('http','cache')defassertCache(self,expected,status,situation=''):"""simple assert for nicer message"""ifexpected!=status:ifexpectedisNone:expected="MODIFIED"ifstatusisNone:status="MODIFIED"msg='expected %r got %r'%(expected,status)ifsituation:msg="%s - when: %s"%(msg,situation)self.fail(msg)deftest_IN_none_OUT_none(self):#: test that no caching is requested when not data is available#: on any sidestatus,req=_test_cache((),())self.assertIsNone(status)deftest_IN_Some_OUT_none(self):#: test that no caching is requested when no data is available#: server (origin) sidehin=[('if-modified-since','Sat, 14 Apr 2012 14:39:32 GM'),]status,req=_test_cache(hin,())self.assertIsNone(status)hin=[('if-none-match','babar/huitre'),]status,req=_test_cache(hin,())self.assertIsNone(status)hin=[('if-modified-since','Sat, 14 Apr 2012 14:39:32 GM'),('if-none-match','babar/huitre'),]status,req=_test_cache(hin,())self.assertIsNone(status)deftest_IN_none_OUT_Some(self):#: test that no caching is requested when no data is provided#: by the clienthout=[('last-modified','Sat, 14 Apr 2012 14:39:32 GM'),]status,req=_test_cache((),hout)self.assertIsNone(status)hout=[('etag','babar/huitre'),]status,req=_test_cache((),hout)self.assertIsNone(status)hout=[('last-modified','Sat, 14 Apr 2012 14:39:32 GM'),('etag','babar/huitre'),]status,req=_test_cache((),hout)self.assertIsNone(status)@tag('last_modified')deftest_last_modified_newer(self):#: test the proper behavior of modification date only# newerhin=[('if-modified-since','Sat, 13 Apr 2012 14:39:32 GM'),]hout=[('last-modified','Sat, 14 Apr 2012 14:39:32 GM'),]status,req=_test_cache(hin,hout)self.assertCache(None,status,'origin is newer than client')@tag('last_modified')deftest_last_modified_older(self):# olderhin=[('if-modified-since','Sat, 15 Apr 2012 14:39:32 GM'),]hout=[('last-modified','Sat, 14 Apr 2012 14:39:32 GM'),]status,req=_test_cache(hin,hout)self.assertCache(304,status,'origin is older than client')@tag('last_modified')deftest_last_modified_same(self):# samehin=[('if-modified-since','Sat, 14 Apr 2012 14:39:32 GM'),]hout=[('last-modified','Sat, 14 Apr 2012 14:39:32 GM'),]status,req=_test_cache(hin,hout)self.assertCache(304,status,'origin is equal to client')@tag('etag')deftest_etag_mismatch(self):#: test the proper behavior of etag only# etag mismatchhin=[('if-none-match','babar'),]hout=[('etag','celestine'),]status,req=_test_cache(hin,hout)self.assertCache(None,status,'etag mismatch')@tag('etag')deftest_etag_match(self):# etag matchhin=[('if-none-match','babar'),]hout=[('etag','babar'),]status,req=_test_cache(hin,hout)self.assertCache(304,status,'etag match')# etag match in multiplehin=[('if-none-match','loutre'),('if-none-match','babar'),]hout=[('etag','babar'),]status,req=_test_cache(hin,hout)self.assertCache(304,status,'etag match in multiple')# client use "*" as etaghin=[('if-none-match','*'),]hout=[('etag','babar'),]status,req=_test_cache(hin,hout)self.assertCache(304,status,'client use "*" as etag')@tag('etag','last_modified')deftest_both(self):#: test the proper behavior of etag only# both wronghin=[('if-none-match','babar'),('if-modified-since','Sat, 14 Apr 2012 14:39:32 GM'),]hout=[('etag','loutre'),('last-modified','Sat, 15 Apr 2012 14:39:32 GM'),]status,req=_test_cache(hin,hout)self.assertCache(None,status,'both wrong')@tag('etag','last_modified')deftest_both_etag_mismatch(self):# both etag mismatchhin=[('if-none-match','babar'),('if-modified-since','Sat, 14 Apr 2012 14:39:32 GM'),]hout=[('etag','loutre'),('last-modified','Sat, 13 Apr 2012 14:39:32 GM'),]status,req=_test_cache(hin,hout)self.assertCache(None,status,'both but etag mismatch')@tag('etag','last_modified')deftest_both_but_modified(self):# both but modifiedhin=[('if-none-match','babar'),('if-modified-since','Sat, 14 Apr 2012 14:39:32 GM'),]hout=[('etag','babar'),('last-modified','Sat, 15 Apr 2012 14:39:32 GM'),]status,req=_test_cache(hin,hout)self.assertCache(None,status,'both but modified')@tag('etag','last_modified')deftest_both_ok(self):# both okhin=[('if-none-match','babar'),('if-modified-since','Sat, 14 Apr 2012 14:39:32 GM'),]hout=[('etag','babar'),('last-modified','Sat, 13 Apr 2012 14:39:32 GM'),]status,req=_test_cache(hin,hout)self.assertCache(304,status,'both ok')@tag('etag','HEAD')deftest_head_verb(self):#: check than FOUND 200 is properly raise without content on HEAD request#: This logic does not really belong here :-/# modifiedhin=[('if-none-match','babar'),]hout=[('etag','rhino/really-not-babar'),]status,req=_test_cache(hin,hout,method='HEAD')self.assertCache(200,status,'modifier HEAD verb')# not modifiedhin=[('if-none-match','babar'),]hout=[('etag','babar'),]status,req=_test_cache(hin,hout,method='HEAD')self.assertCache(304,status,'not modifier HEAD verb')@tag('etag','POST')deftest_post_verb(self):# modifiedhin=[('if-none-match','babar'),]hout=[('etag','rhino/really-not-babar'),]status,req=_test_cache(hin,hout,method='POST')self.assertCache(None,status,'modifier HEAD verb')# not modifiedhin=[('if-none-match','babar'),]hout=[('etag','babar'),]status,req=_test_cache(hin,hout,method='POST')self.assertCache(412,status,'not modifier HEAD verb')@tag('expires')deftest_expires_added(self):#: Check that Expires header is added:#: - when the page is modified#: - when none was already presenthin=[('if-none-match','babar'),]hout=[('etag','rhino/really-not-babar'),]status,req=_test_cache(hin,hout)self.assertCache(None,status,'modifier HEAD verb')value=req.headers_out.getHeader('expires')self.assertIsNotNone(value)@tag('expires')deftest_expires_not_added(self):#: Check that Expires header is not added if NOT-MODIFIEDhin=[('if-none-match','babar'),]hout=[('etag','babar'),]status,req=_test_cache(hin,hout)self.assertCache(304,status,'not modifier HEAD verb')value=req.headers_out.getHeader('expires')self.assertIsNone(value)@tag('expires')deftest_expires_no_overwrite(self):#: Check that cache does not overwrite existing Expires headerhin=[('if-none-match','babar'),]DATE='Sat, 13 Apr 2012 14:39:32 GM'hout=[('etag','rhino/really-not-babar'),('expires',DATE),]status,req=_test_cache(hin,hout)self.assertCache(None,status,'not modifier HEAD verb')value=req.headers_out.getRawHeaders('expires')self.assertEqual(value,[DATE])if__name__=='__main__':unittest_main()