[CalendarServer-changes] [10576] CalendarServer/branches/users/glyph/unshare-when-access-revoked
source_changes at macosforge.org
source_changes at macosforge.org
Mon Jan 28 19:28:58 PST 2013
Revision: 10576
http://trac.calendarserver.org//changeset/10576
Author: glyph at apple.com
Date: 2013-01-28 19:28:58 -0800 (Mon, 28 Jan 2013)
Log Message:
-----------
Move test helpers further up the hierarchy.
Modified Paths:
--------------
CalendarServer/branches/users/glyph/unshare-when-access-revoked/twext/web2/dav/test/util.py
CalendarServer/branches/users/glyph/unshare-when-access-revoked/twext/web2/test/test_server.py
CalendarServer/branches/users/glyph/unshare-when-access-revoked/twistedcaldav/test/test_sharing.py
Property Changed:
----------------
CalendarServer/branches/users/glyph/unshare-when-access-revoked/
Modified: CalendarServer/branches/users/glyph/unshare-when-access-revoked/twext/web2/dav/test/util.py
===================================================================
--- CalendarServer/branches/users/glyph/unshare-when-access-revoked/twext/web2/dav/test/util.py 2013-01-29 03:28:56 UTC (rev 10575)
+++ CalendarServer/branches/users/glyph/unshare-when-access-revoked/twext/web2/dav/test/util.py 2013-01-29 03:28:58 UTC (rev 10576)
@@ -29,20 +29,62 @@
from shutil import copy
from twisted.trial import unittest
+from twisted.internet import address
+
from twisted.internet.defer import Deferred
from twext.python.log import Logger
from twext.web2.http import HTTPError, StatusResponse
-from twext.web2 import responsecode
+from twext.web2 import responsecode, server
+from twext.web2 import http_headers
+from twext.web2 import stream
+
from twext.web2.dav.resource import TwistedACLInheritable
from twext.web2.dav.static import DAVFile
from twext.web2.dav.util import joinURL
from txdav.xml import element
from txdav.xml.base import encodeXMLName
+from twext.web2.http_headers import MimeType
+from twext.web2.dav.util import allDataFromStream
log = Logger()
+
+class SimpleRequest(server.Request):
+ """
+ A L{SimpleRequest} can be used in cases where a L{server.Request} object is
+ necessary but it is beneficial to bypass the concrete transport (and
+ associated logic with the C{chanRequest} attribute).
+ """
+
+ clientproto = (1,1)
+
+ def __init__(self, site, method, uri, headers=None, content=None):
+ if not headers:
+ headers = http_headers.Headers(headers)
+
+ super(SimpleRequest, self).__init__(
+ site=site,
+ chanRequest=None,
+ command=method,
+ path=uri,
+ version=self.clientproto,
+ contentLength=len(content or ''),
+ headers=headers)
+
+ self.stream = stream.MemoryStream(content or '')
+
+ self.remoteAddr = address.IPv4Address('TCP', '127.0.0.1', 0)
+ self._parseURL()
+ self.host = 'localhost'
+ self.port = 8080
+
+ def writeResponse(self, response):
+ return response
+
+
+
class InMemoryPropertyStore (object):
"""
A dead property store for keeping properties in memory
@@ -202,6 +244,17 @@
def send(self, request, callback=None):
+ """
+ Invoke the logic involved in traversing a given L{server.Request} as if
+ a client had sent it; call C{locateResource} to look up the resource to
+ be rendered, and render it by calling its C{renderHTTP} method.
+
+ @param request: A L{server.Request} (generally, to avoid real I/O, a
+ L{SimpleRequest}) already associated with a site.
+
+ @return: asynchronously return a response object or L{None}
+ @rtype: L{Deferred} firing L{Response} or L{None}
+ """
log.msg("Sending %s request for URI %s" % (request.method, request.uri))
d = request.locateResource(request.uri)
@@ -216,6 +269,57 @@
return d
+
+ def simpleSend(self, method, path="/", body="", mimetype="text",
+ subtype="xml", resultcode=responsecode.OK, headers=()):
+ """
+ Assemble and send a simple request using L{SimpleRequest}. This
+ L{SimpleRequest} is associated with this L{TestCase}'s C{site}
+ attribute.
+
+ @param method: the HTTP method
+ @type method: L{bytes}
+
+ @param path: the absolute path portion of the HTTP URI
+ @type path: L{bytes}
+
+ @param body: the content body of the request
+ @type body: L{bytes}
+
+ @param mimetype: the main type of the mime type of the body of the
+ request
+ @type mimetype: L{bytes}
+
+ @param subtype: the subtype of the mimetype of the body of the request
+ @type subtype: L{bytes}
+
+ @param resultcode: The expected result code for the response to the
+ request.
+ @type resultcode: L{int}
+
+ @param headers: An iterable of 2-tuples of C{(header, value)}; headers
+ to set on the outgoing request.
+
+ @return: a L{Deferred} which fires with a L{bytes} if the request was
+ successfully processed and fails with an L{HTTPError} if not; or,
+ if the resultcode does not match the response's code, fails with
+ L{FailTest}.
+ """
+ request = SimpleRequest(self.site, method, path, content=body)
+ if headers is not None:
+ for k, v in headers:
+ request.headers.setHeader(k, v)
+ request.headers.setHeader("content-type", MimeType(mimetype, subtype))
+ def checkResult(response):
+ self.assertEqual(response.code, resultcode)
+ if response.stream is None:
+ return None
+ return allDataFromStream(response.stream)
+ return self.send(request, None).addCallback(checkResult)
+
+
+
+
class Site:
# FIXME: There is no ISite interface; there should be.
# implements(ISite)
Modified: CalendarServer/branches/users/glyph/unshare-when-access-revoked/twext/web2/test/test_server.py
===================================================================
--- CalendarServer/branches/users/glyph/unshare-when-access-revoked/twext/web2/test/test_server.py 2013-01-29 03:28:56 UTC (rev 10575)
+++ CalendarServer/branches/users/glyph/unshare-when-access-revoked/twext/web2/test/test_server.py 2013-01-29 03:28:58 UTC (rev 10576)
@@ -10,6 +10,8 @@
from twisted.python import components
from twext.web2 import http, http_headers, iweb, server
from twext.web2 import resource, stream
+from twext.web2.dav.test.util import SimpleRequest
+
from twisted.trial import unittest
from twisted.internet import reactor, defer, address
@@ -114,37 +116,6 @@
-class SimpleRequest(server.Request):
- """I can be used in cases where a Request object is necessary
- but it is beneficial to bypass the chanRequest
- """
-
- clientproto = (1,1)
-
- def __init__(self, site, method, uri, headers=None, content=None):
- if not headers:
- headers = http_headers.Headers(headers)
-
- super(SimpleRequest, self).__init__(
- site=site,
- chanRequest=None,
- command=method,
- path=uri,
- version=self.clientproto,
- contentLength=len(content or ''),
- headers=headers)
-
- self.stream = stream.MemoryStream(content or '')
-
- self.remoteAddr = address.IPv4Address('TCP', '127.0.0.1', 0)
- self._parseURL()
- self.host = 'localhost'
- self.port = 8080
-
- def writeResponse(self, response):
- return response
-
-
class TestChanRequest:
implements(iweb.IChanRequest)
Modified: CalendarServer/branches/users/glyph/unshare-when-access-revoked/twistedcaldav/test/test_sharing.py
===================================================================
--- CalendarServer/branches/users/glyph/unshare-when-access-revoked/twistedcaldav/test/test_sharing.py 2013-01-29 03:28:56 UTC (rev 10575)
+++ CalendarServer/branches/users/glyph/unshare-when-access-revoked/twistedcaldav/test/test_sharing.py 2013-01-29 03:28:58 UTC (rev 10576)
@@ -22,9 +22,7 @@
from txdav.xml.parser import WebDAVDocument
from twext.web2 import responsecode
-from twext.web2.http_headers import MimeType
from twext.web2.iweb import IResource
-from twext.web2.stream import MemoryStream
from twext.web2.test.test_server import SimpleRequest
from twisted.internet.defer import inlineCallbacks, returnValue, succeed
@@ -36,7 +34,6 @@
from twistedcaldav.resource import CalDAVResource
from txdav.common.datastore.test.util import buildStore, StubNotifierFactory
-from twext.web2.dav.util import allDataFromStream
from txdav.caldav.icalendarstore import BIND_DIRECT
from twistedcaldav.directory.aggregate import AggregateDirectoryService
from twistedcaldav.directory.principal import DirectoryPrincipalProvisioningResource
@@ -243,7 +240,7 @@
if "bogus" in cuaddr:
return None
else:
- return SharingTests.FakePrincipal(cuaddr, self)
+ return FakePrincipal(cuaddr, self)
@patched
def validUserIDForShare(resourceSelf, userid, request):
@@ -276,54 +273,9 @@
returnValue(result)
- @inlineCallbacks
- def do(self, method, path="/", body="", mimetype="text", subtype="xml",
- resultcode=responsecode.OK, headers=()):
- """
- Do a simple request.
-
- @param method: the HTTP method
- @type method: L{bytes}
-
- @param path: the absolute path portion of the HTTP URI
- @type path: L{bytes}
-
- @param body: the content body of the request
- @type body: L{bytes}
-
- @param mimetype: the main type of the mime type of the body of the
- request
- @type mimetype: L{bytes}
-
- @param subtype: the subtype of the mimetype of the body of the request
- @type subtype: L{bytes}
-
- @param resultcode: The expected result code for the response to the
- request.
- @type resultcode: L{int}
-
- @param headers: An iterable of 2-tuples of C{(header, value)}; headers
- to set on the outgoing request.
-
- @return: a L{Deferred} which fires with an L{IResponse} if the request
- was successfully processed and fails with an L{HTTPError} if not;
- or, if the resultcode does not match the response's code, fails
- with L{FailTest}.
- """
- request = SimpleRequest(self.site, method, path)
- if headers is not None:
- for k, v in headers:
- request.headers.setHeader(k, v)
- request.headers.setHeader("content-type", MimeType(mimetype, subtype))
- request.stream = MemoryStream(body)
-
- response = (yield self.send(request, None))
- self.assertEqual(response.code, resultcode)
- returnValue(response)
-
-
def _doPOST(self, body, resultcode=responsecode.OK):
- return self.do("POST", "/calendar/", body, resultcode=resultcode)
+ return self.simpleSend("POST", "/calendar/", body,
+ resultcode=resultcode)
def _clearUIDElementValue(self, xml):
@@ -723,7 +675,7 @@
def test_POSTaddInvalidInvitee(self):
self.resource.upgradeToShare()
- response = (yield self._doPOST(
+ data = (yield self._doPOST(
"""<?xml version="1.0" encoding="utf-8" ?>
<CS:share xmlns:D="DAV:" xmlns:CS="http://calendarserver.org/ns/">
<CS:set>
@@ -736,7 +688,7 @@
responsecode.MULTI_STATUS
))
self.assertXMLEquals(
- str(response.stream.read()).replace("\r\n", "\n"),
+ data,
"""<?xml version='1.0' encoding='UTF-8'?>
<multistatus xmlns='DAV:'>
<response>
@@ -901,10 +853,10 @@
self.patch(sharing, "getWikiAccess", stubWikiAccessMethod)
@inlineCallbacks
def listChildrenViaPropfind():
- response = yield self.do("PROPFIND", "/",
- resultcode=responsecode.MULTI_STATUS,
- headers=[('Depth', '1')])
- data = yield allDataFromStream(response.stream)
+ data = yield self.simpleSend(
+ "PROPFIND", "/", resultcode=responsecode.MULTI_STATUS,
+ headers=[('Depth', '1')]
+ )
tree = XML(data)
seq = [e.text for e in tree.findall("{DAV:}response/{DAV:}href")]
shortest = min(seq, key=len)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20130128/bd96d904/attachment-0001.html>
More information about the calendarserver-changes
mailing list