From 3731230be8330769290b7fa588c625fca77f3a9a Mon Sep 17 00:00:00 2001 From: Morgen Sagen Date: Tue, 18 Mar 2014 17:55:31 +0000 Subject: [PATCH] Fix more tests git-svn-id: https://svn.calendarserver.org/repository/calendarserver/CalendarServer/branches/users/sagen/move2who-4@12935 e27351fd-9f3e-4f54-a53b-843176b1656c --- calendarserver/accesslog.py | 5 +- calendarserver/provision/root.py | 2 +- calendarserver/provision/test/test_root.py | 209 ++++++++------------- calendarserver/tools/calverify.py | 8 +- calendarserver/tools/export.py | 15 +- calendarserver/tools/shell/directory.py | 4 +- calendarserver/tools/shell/vfs.py | 2 +- twistedcaldav/cache.py | 25 ++- twistedcaldav/directory/calendar.py | 2 +- twistedcaldav/directory/test/util.py | 4 +- twistedcaldav/extensions.py | 2 +- twistedcaldav/test/util.py | 12 +- 12 files changed, 126 insertions(+), 164 deletions(-) diff --git a/calendarserver/accesslog.py b/calendarserver/accesslog.py index ffaf5b3b0..a661cb5e3 100644 --- a/calendarserver/accesslog.py +++ b/calendarserver/accesslog.py @@ -155,8 +155,9 @@ def emit(self, eventDict): format += ' i=%(serverInstance)s' formatArgs["serverInstance"] = config.LogID if config.LogID else "0" - format += ' or=%(outstandingRequests)s' - formatArgs["outstandingRequests"] = request.chanRequest.channel.factory.outstandingRequests + if request.chanRequest: # This can be None during tests + format += ' or=%(outstandingRequests)s' + formatArgs["outstandingRequests"] = request.chanRequest.channel.factory.outstandingRequests # Tags for time stamps collected along the way - the first one in the list is the initial # time for request creation - we use that to track the entire request/response time diff --git a/calendarserver/provision/root.py b/calendarserver/provision/root.py index 5f6bd6e74..a2ca3c9d9 100644 --- a/calendarserver/provision/root.py +++ b/calendarserver/provision/root.py @@ -160,7 +160,7 @@ def checkSacl(self, request): request.checkingSACL = True for collection in self.principalCollections(): - principal = collection._principalForURI(authzUser.children[0].children[0].data) + principal = yield collection._principalForURI(authzUser.children[0].children[0].data) if principal is None: response = (yield UnauthorizedResponse.makeResponse( request.credentialFactories, diff --git a/calendarserver/provision/test/test_root.py b/calendarserver/provision/test/test_root.py index 19cf228a4..d883bca75 100644 --- a/calendarserver/provision/test/test_root.py +++ b/calendarserver/provision/test/test_root.py @@ -14,28 +14,21 @@ # limitations under the License. ## -import os -from twisted.cred.portal import Portal from twisted.internet.defer import inlineCallbacks, maybeDeferred, returnValue +from twext.who.idirectory import RecordType from txweb2 import http_headers from txweb2 import responsecode -from txweb2 import server -from txweb2.auth import basic -from txweb2.dav import auth from txdav.xml import element as davxml from txweb2.http import HTTPError from txweb2.iweb import IResponse -from txweb2.test.test_server import SimpleRequest -from twistedcaldav.test.util import TestCase +from twistedcaldav.test.util import StoreTestCase, SimpleStoreRequest from twistedcaldav.directory.principal import DirectoryPrincipalProvisioningResource -from twistedcaldav.directory.xmlfile import XMLDirectoryService -from twistedcaldav.directory.test.test_xmlfile import xmlFile, augmentsFile from calendarserver.provision.root import RootResource -from twistedcaldav.directory import augment + class FakeCheckSACL(object): def __init__(self, sacls=None): @@ -53,46 +46,14 @@ def __call__(self, username, service): -class RootTests(TestCase): +class RootTests(StoreTestCase): + @inlineCallbacks def setUp(self): - super(RootTests, self).setUp() - - self.docroot = self.mktemp() - os.mkdir(self.docroot) + yield super(RootTests, self).setUp() RootResource.CheckSACL = FakeCheckSACL(sacls={"calendar": ["dreid"]}) - directory = XMLDirectoryService( - { - "xmlFile" : xmlFile, - "augmentService" : - augment.AugmentXMLDB(xmlFiles=(augmentsFile.path,)) - } - ) - - principals = DirectoryPrincipalProvisioningResource( - "/principals/", - directory - ) - - root = RootResource(self.docroot, principalCollections=[principals]) - - root.putChild("principals", - principals) - - portal = Portal(auth.DavRealm()) - portal.registerChecker(directory) - - self.root = auth.AuthenticationWrapper( - root, - portal, - (basic.BasicCredentialFactory("Test realm"),), - (basic.BasicCredentialFactory("Test realm"),), - loginInterfaces=(auth.IPrincipal,)) - - self.site = server.Site(self.root) - class ComplianceTests(RootTests): @@ -107,8 +68,8 @@ def issueRequest(self, segments, method="GET"): Deferred which will fire with (something adaptable to) an HTTP response object. """ - request = SimpleRequest(self.site, method, ("/".join([""] + segments))) - rsrc = self.root + request = SimpleStoreRequest(self, method, ("/".join([""] + segments))) + rsrc = self.actualRoot while segments: rsrc, segments = (yield maybeDeferred( rsrc.locateChild, request, segments @@ -138,18 +99,12 @@ def test_noSacls(self): should return a valid resource """ - self.root.resource.useSacls = False - - request = SimpleRequest(self.site, - "GET", - "/principals/") + self.actualRoot.useSacls = False - resrc, _ignore_segments = (yield maybeDeferred( - self.root.locateChild, request, ["principals"] - )) + request = SimpleStoreRequest(self, "GET", "/principals/") resrc, segments = (yield maybeDeferred( - resrc.locateChild, request, ["principals"] + self.actualRoot.locateChild, request, ["principals"] )) self.failUnless( @@ -169,26 +124,21 @@ def test_inSacls(self): should return a valid resource """ - self.root.resource.useSacls = True + self.actualRoot.useSacls = True - request = SimpleRequest( - self.site, + record = yield self.directory.recordWithShortName( + RecordType.user, + u"dreid" + ) + request = SimpleStoreRequest( + self, "GET", "/principals/", - headers=http_headers.Headers({ - "Authorization": [ - "basic", - "%s" % ("dreid:dierd".encode("base64"),) - ] - }) + authRecord=record ) - resrc, _ignore_segments = (yield maybeDeferred( - self.root.locateChild, request, ["principals"] - )) - resrc, segments = (yield maybeDeferred( - resrc.locateChild, request, ["principals"] + self.actualRoot.locateChild, request, ["principals"] )) self.failUnless( @@ -218,28 +168,27 @@ def test_notInSacls(self): should return a 403 forbidden response """ - self.root.resource.useSacls = True + self.actualRoot.useSacls = True + + record = yield self.directory.recordWithShortName( + RecordType.user, + u"wsanchez" + ) - request = SimpleRequest( - self.site, + request = SimpleStoreRequest( + self, "GET", "/principals/", - headers=http_headers.Headers({ - "Authorization": [ - "basic", - "%s" % ("wsanchez:zehcnasw".encode("base64"),) - ] - }) + authRecord=record ) - resrc, _ignore_segments = (yield maybeDeferred( - self.root.locateChild, request, ["principals"] - )) - try: resrc, _ignore_segments = (yield maybeDeferred( - resrc.locateChild, request, ["principals"] + self.actualRoot.locateChild, request, ["principals"] )) + raise AssertionError( + "RootResource.locateChild did not return an error" + ) except HTTPError, e: self.assertEquals(e.response.code, 403) @@ -253,20 +202,16 @@ def test_unauthenticated(self): should return a 401 UnauthorizedResponse """ - self.root.resource.useSacls = True - request = SimpleRequest( - self.site, + self.actualRoot.useSacls = True + request = SimpleStoreRequest( + self, "GET", "/principals/" ) - resrc, _ignore_segments = (yield maybeDeferred( - self.root.locateChild, request, ["principals"] - )) - try: resrc, _ignore_segments = (yield maybeDeferred( - resrc.locateChild, request, ["principals"] + self.actualRoot.locateChild, request, ["principals"] )) raise AssertionError( "RootResource.locateChild did not return an error" @@ -283,24 +228,28 @@ def test_badCredentials(self): should return a 401 UnauthorizedResponse """ - self.root.resource.useSacls = True + self.actualRoot.useSacls = True - request = SimpleRequest( - self.site, + request = SimpleStoreRequest( + self, "GET", "/principals/", - headers=http_headers.Headers({ - "Authorization": ["basic", "%s" % ( - "dreid:dreid".encode("base64"),)]})) - - resrc, _ignore_segments = (yield maybeDeferred( - self.root.locateChild, request, ["principals"] - )) + headers=http_headers.Headers( + { + "Authorization": [ + "basic", "%s" % ("dreid:dreid".encode("base64"),) + ] + } + ) + ) try: resrc, _ignore_segments = (yield maybeDeferred( - resrc.locateChild, request, ["principals"] + self.actualRoot.locateChild, request, ["principals"] )) + raise AssertionError( + "RootResource.locateChild did not return an error" + ) except HTTPError, e: self.assertEquals(e.response.code, 401) @@ -313,7 +262,7 @@ def do_test(response): self.fail("Incorrect response for DELETE /: %s" % (response.code,)) - request = SimpleRequest(self.site, "DELETE", "/") + request = SimpleStoreRequest(self, "DELETE", "/") return self.send(request, do_test) @@ -325,8 +274,8 @@ def do_test(response): self.fail("Incorrect response for COPY /: %s" % (response.code,)) - request = SimpleRequest( - self.site, + request = SimpleStoreRequest( + self, "COPY", "/", headers=http_headers.Headers({"Destination": "/copy/"}) @@ -342,8 +291,8 @@ def do_test(response): self.fail("Incorrect response for MOVE /: %s" % (response.code,)) - request = SimpleRequest( - self.site, + request = SimpleStoreRequest( + self, "MOVE", "/", headers=http_headers.Headers({"Destination": "/copy/"}) @@ -371,13 +320,14 @@ def cacheResponseForRequest(self, request, response): return response + @inlineCallbacks def setUp(self): - super(SACLCacheTests, self).setUp() - self.root.resource.responseCache = SACLCacheTests.StubResponseCacheResource() + yield super(SACLCacheTests, self).setUp() + self.actualRoot.responseCache = SACLCacheTests.StubResponseCacheResource() def test_PROPFIND(self): - self.root.resource.useSacls = True + self.actualRoot.useSacls = True body = """ @@ -388,15 +338,19 @@ def test_PROPFIND(self): """ - request = SimpleRequest( - self.site, + request = SimpleStoreRequest( + self, "PROPFIND", "/principals/users/dreid/", - headers=http_headers.Headers({ - 'Authorization': ['basic', '%s' % ('dreid:dierd'.encode('base64'),)], + headers=http_headers.Headers( + { + 'Authorization': [ + 'basic', '%s' % ('dreid:dierd'.encode('base64'),) + ], 'Content-Type': 'application/xml; charset="utf-8"', 'Depth': '1', - }), + } + ), content=body ) @@ -404,15 +358,19 @@ def gotResponse1(response): if response.code != responsecode.MULTI_STATUS: self.fail("Incorrect response for PROPFIND /principals/: %s" % (response.code,)) - request = SimpleRequest( - self.site, + request = SimpleStoreRequest( + self, "PROPFIND", "/principals/users/dreid/", - headers=http_headers.Headers({ - 'Authorization': ['basic', '%s' % ('dreid:dierd'.encode('base64'),)], + headers=http_headers.Headers( + { + 'Authorization': [ + 'basic', '%s' % ('dreid:dierd'.encode('base64'),) + ], 'Content-Type': 'application/xml; charset="utf-8"', 'Depth': '1', - }), + } + ), content=body ) @@ -422,7 +380,7 @@ def gotResponse1(response): def gotResponse2(response): if response.code != responsecode.MULTI_STATUS: self.fail("Incorrect response for PROPFIND /principals/: %s" % (response.code,)) - self.assertEqual(self.root.resource.responseCache.cacheHitCount, 1) + self.assertEqual(self.actualRoot.responseCache.cacheHitCount, 1) d = self.send(request, gotResponse1) return d @@ -438,12 +396,9 @@ def test_oneTime(self): request.checkedWiki will be set to True """ - request = SimpleRequest(self.site, "GET", "/principals/") + request = SimpleStoreRequest(self, "GET", "/principals/") resrc, _ignore_segments = (yield maybeDeferred( - self.root.locateChild, request, ["principals"] - )) - resrc, _ignore_segments = (yield maybeDeferred( - resrc.locateChild, request, ["principals"] + self.actualRoot.locateChild, request, ["principals"] )) self.assertTrue(request.checkedWiki) diff --git a/calendarserver/tools/calverify.py b/calendarserver/tools/calverify.py index 44bc061c1..cd5160b5c 100755 --- a/calendarserver/tools/calverify.py +++ b/calendarserver/tools/calverify.py @@ -973,7 +973,7 @@ def doAction(self): elif not record.thisServer(): contents = yield self.countHomeContents(uid) wrong_server.append((uid, contents,)) - elif not record.enabledForCalendaring: + elif not record.hasCalendars: contents = yield self.countHomeContents(uid) disabled.append((uid, contents,)) @@ -1542,7 +1542,7 @@ def testForCalendaringUUID(self, uuid): if uuid not in self.validForCalendaringUUIDs: record = self.directoryService().recordWithGUID(uuid) - self.validForCalendaringUUIDs[uuid] = record is not None and record.enabledForCalendaring and record.thisServer() + self.validForCalendaringUUIDs[uuid] = record is not None and record.hasCalendars and record.thisServer() return self.validForCalendaringUUIDs[uuid] @@ -2171,7 +2171,7 @@ def doAction(self): record = self.directoryService().recordWithGUID(uuid) if record is None: continue - if not record.thisServer() or not record.enabledForCalendaring: + if not record.thisServer() or not record.hasCalendars: continue rname = record.fullName @@ -2482,7 +2482,7 @@ def doAction(self): record = self.directoryService().recordWithGUID(uuid) if record is None: continue - if not record.thisServer() or not record.enabledForCalendaring: + if not record.thisServer() or not record.hasCalendars: continue rname = record.fullName diff --git a/calendarserver/tools/export.py b/calendarserver/tools/export.py index 2f60067e7..d33b3d3a5 100755 --- a/calendarserver/tools/export.py +++ b/calendarserver/tools/export.py @@ -41,7 +41,7 @@ from twisted.python.text import wordWrap from twisted.python.usage import Options, UsageError -from twisted.internet.defer import inlineCallbacks, returnValue +from twisted.internet.defer import inlineCallbacks, returnValue, succeed from twext.python.log import Logger from twistedcaldav.ical import Component @@ -76,6 +76,7 @@ def usage(e=None): ) ) + class ExportOptions(Options): """ Command-line options for 'calendarserver_export' @@ -188,7 +189,7 @@ def listCalendars(self, txn, exportService): Enumerate all calendars based on the directory record and/or calendars for this calendar home. """ - uid = self.getHomeUID(exportService) + uid = yield self.getHomeUID(exportService) home = yield txn.calendarHomeWithUID(uid, True) result = [] if self.collections: @@ -218,7 +219,7 @@ def __init__(self, uid): def getHomeUID(self, exportService): - return self.uid + return succeed(self.uid) @@ -244,13 +245,17 @@ def __init__(self, recordType, shortName): self.shortName = shortName + @inlineCallbacks def getHomeUID(self, exportService): """ Retrieve the home UID. """ directory = exportService.directoryService() - record = directory.recordWithShortName(self.recordType, self.shortName) - return record.uid + record = yield directory.recordWithShortName( + directory.oldNameToRecordType(self.recordType), + self.shortName + ) + returnValue(record.uid) diff --git a/calendarserver/tools/shell/directory.py b/calendarserver/tools/shell/directory.py index e26a54f29..135ef0419 100644 --- a/calendarserver/tools/shell/directory.py +++ b/calendarserver/tools/shell/directory.py @@ -97,8 +97,8 @@ def add(name, value): add("Server ID" , record.serverID) add("Enabled" , record.enabled) - add("Enabled for Calendar", record.enabledForCalendaring) - add("Enabled for Contacts", record.enabledForAddressBooks) + add("Enabled for Calendar", record.hasCalendars) + add("Enabled for Contacts", record.hasContacts) return succeed(table.toString()) diff --git a/calendarserver/tools/shell/vfs.py b/calendarserver/tools/shell/vfs.py index a36072b4a..32f453d57 100644 --- a/calendarserver/tools/shell/vfs.py +++ b/calendarserver/tools/shell/vfs.py @@ -411,7 +411,7 @@ def _initChildren(self): if ( self.record is not None and self.service.config.EnableCalDAV and - self.record.enabledForCalendaring + self.record.hasCalendars ): create = True else: diff --git a/twistedcaldav/cache.py b/twistedcaldav/cache.py index 6da916a88..ade58e16f 100644 --- a/twistedcaldav/cache.py +++ b/twistedcaldav/cache.py @@ -69,6 +69,7 @@ """ + class DisabledCacheNotifier(object): def __init__(self, *args, **kwargs): pass @@ -164,6 +165,7 @@ def _getRecord(resrc): raise URINotFoundException(uri) + @inlineCallbacks def _canonicalizeURIForRequest(self, uri, request): """ Always use canonicalized forms of the URIs for caching (i.e. __uids__ paths). @@ -174,21 +176,24 @@ def _canonicalizeURIForRequest(self, uri, request): uribits = uri.split("/") if len(uribits) > 1 and uribits[1] in ("principals", "calendars", "addressbooks"): if uribits[2] == "__uids__": - return succeed(uri) + returnValue(uri) else: recordType = uribits[2] recordName = uribits[3] directory = request.site.resource.getDirectory() - record = directory.recordWithShortName(recordType, recordName) + record = yield directory.recordWithShortName( + directory.oldNameToRecordType(recordType), + recordName + ) if record is not None: uribits[2] = "__uids__" - uribits[3] = record.uid - return succeed("/".join(uribits)) + uribits[3] = record.uid.encode("utf-8") + returnValue("/".join(uribits)) # Fall back to the locateResource approach try: - return request.locateResource(uri).addCallback( - lambda resrc: resrc.url()).addErrback(self._uriNotFound, uri) + resrc = yield request.locateResource(uri) + returnValue(resrc.url()) except AssertionError: raise URINotFoundException(uri) @@ -252,7 +257,8 @@ def _tokenForURI(self, uri, cachePoolHandle=None): """ Get the current token for a particular URI. """ - + if isinstance(uri, unicode): + uri = uri.encode("utf-8") if cachePoolHandle: result = (yield defaultCachePool(cachePoolHandle).get('cacheToken:%s' % (uri,))) else: @@ -436,8 +442,9 @@ def cacheResponseForRequest(self, request, response): cTokens, ) ) - yield self.getCachePool().set(key, cacheEntry, - expireTime=config.ResponseCacheTimeout * 60) + yield self.getCachePool().set( + key, cacheEntry, expireTime=config.ResponseCacheTimeout * 60 + ) except URINotFoundException, e: self.log.debug("Could not locate URI: {e!r}", e=e) diff --git a/twistedcaldav/directory/calendar.py b/twistedcaldav/directory/calendar.py index 281b49ede..1ae2805f2 100644 --- a/twistedcaldav/directory/calendar.py +++ b/twistedcaldav/directory/calendar.py @@ -192,7 +192,7 @@ def listChildren(self): if config.EnablePrincipalListings: children = [] for record in (yield self.directory.listRecords(self.recordType)): - if record.enabledForCalendaring: + if record.hasCalendars: for shortName in record.shortNames: children.append(shortName) returnValue(children) diff --git a/twistedcaldav/directory/test/util.py b/twistedcaldav/directory/test/util.py index 48aace3f7..32ead4c9c 100644 --- a/twistedcaldav/directory/test/util.py +++ b/twistedcaldav/directory/test/util.py @@ -212,9 +212,9 @@ def value(key): guid = record.guid addresses = set(value("addresses")) - if record.enabledForCalendaring: + if record.hasCalendars: addresses.add("urn:uuid:%s" % (record.guid,)) - addresses.add("/principals/__uids__/%s/" % (record.guid,)) + addresses.add("/principals/__uids__/%s/" % (record.uid,)) addresses.add("/principals/%s/%s/" % (record.recordType, record.shortNames[0],)) if hasattr(record.service, "recordTypePrefix"): diff --git a/twistedcaldav/extensions.py b/twistedcaldav/extensions.py index 9b6ba5d88..aa59d490b 100644 --- a/twistedcaldav/extensions.py +++ b/twistedcaldav/extensions.py @@ -1004,7 +1004,7 @@ def extractCalendarServerPrincipalSearchData(doc): applyTo = True elif child.qname() == (calendarserver_namespace, "search-token"): - tokens.append(str(child)) + tokens.append(child.toString()) elif child.qname() == (calendarserver_namespace, "limit"): try: diff --git a/twistedcaldav/test/util.py b/twistedcaldav/test/util.py index 3acf48c3b..f76202363 100644 --- a/twistedcaldav/test/util.py +++ b/twistedcaldav/test/util.py @@ -32,8 +32,6 @@ from twistedcaldav.directory.calendar import ( DirectoryCalendarHomeProvisioningResource ) -from twistedcaldav.directory.principal import ( - DirectoryPrincipalProvisioningResource) from twistedcaldav.directory.util import transactionFromRequest from twistedcaldav.memcacheclient import ClientFactory from twistedcaldav.stdconfig import config @@ -46,6 +44,7 @@ import txweb2.dav.test.util from txweb2.http import HTTPError, StatusResponse import xattr +from txweb2.server import Site log = Logger() @@ -129,13 +128,8 @@ def setUp(self): self._sqlCalendarStore.setDirectoryService(self.directory) self.rootResource = getRootResource(config, self._sqlCalendarStore) - - self.principalsResource = DirectoryPrincipalProvisioningResource("/principals/", self.directory) - self.site.resource.putChild("principals", self.principalsResource) - self.calendarCollection = DirectoryCalendarHomeProvisioningResource(self.directory, "/calendars/", self._sqlCalendarStore) - self.site.resource.putChild("calendars", self.calendarCollection) - self.addressbookCollection = DirectoryAddressBookHomeProvisioningResource(self.directory, "/addressbooks/", self._sqlCalendarStore) - self.site.resource.putChild("addressbooks", self.addressbookCollection) + self.actualRoot = self.rootResource.resource.resource + self.site = Site(self.rootResource) yield self.populate()