def setUp(self): TestCase.setUp(self) rootresource = TestResource( None, { "file1": TestResource("/file1"), "file2": AuthAllResource("/file2"), "dir1": TestResource("/dir1/", {"subdir1": TestResource("/dir1/subdir1/", {})}), "dir2": AuthAllResource( "/dir2/", { "file1": TestResource("/dir2/file1"), "file2": TestResource("/dir2/file2"), "subdir1": TestResource( "/dir2/subdir1/", { "file1": TestResource("/dir2/subdir1/file1"), "file2": TestResource("/dir2/subdir1/file2") }) }) }) self.site = Site(rootresource)
def test_locateChild(self): resource = LeafResource() child, segments = (resource.locateChild( SimpleRequest(Site(resource), "GET", "/"), ("", "foo"), )) self.assertEquals(child, resource) self.assertEquals(segments, StopTraversal)
def setUp(self): yield super(StoreTestCase, self).setUp() yield self.buildStoreAndDirectory() self.rootResource = getRootResource(config, self.store) self.actualRoot = self.rootResource.resource.resource self.site = Site(self.actualRoot) yield self.populate()
def test_checkPreconditions_none(self): """ RenderMixin.checkPreconditions() checkPreconditions() returns None """ resource = TestResource() request = SimpleRequest(Site(resource), "SWEETHOOKUPS", "/") # Check that checkPreconditions without a raise doesn't barf self.assertEquals((yield resource.renderHTTP(request)), responsecode.NO_CONTENT)
def test_checkPreconditions_deferred(self): """ RenderMixin.checkPreconditions() checkPreconditions() returns a deferred """ resource = TestResource() request = SimpleRequest(Site(resource), "HOOKUPS", "/") # Check that checkPreconditions without a raise doesn't barf def checkResponse(response): self.assertEquals(response, responsecode.NO_CONTENT) d = resource.renderHTTP(request) d.addCallback(checkResponse)
def test_checkPreconditions_raises(self): """ RenderMixin.checkPreconditions() Exception raised in checkPreconditions() """ resource = TestResource() request = SimpleRequest(Site(resource), "BLEARGH", "/") # Check that checkPreconditions raises as expected self.assertRaises(PreconditionError, resource.checkPreconditions, request) # Check that renderHTTP calls checkPreconditions yield self.failUnlessFailure(resource.renderHTTP(request), PreconditionError)
def test_checkPrivileges(self): """ DAVResource.checkPrivileges() """ ds = [] authAllResource = AuthAllResource() requested_access = (davxml.All(), ) site = Site(authAllResource) def expectError(failure): failure.trap(AccessDeniedError) errors = failure.value.errors self.failUnless(len(errors) == 1) subpath, denials = errors[0] self.failUnless(subpath is None) self.failUnless( tuple(denials) == requested_access, "%r != %r" % (tuple(denials), requested_access)) def expectOK(result): self.failUnlessEquals(result, None) def _checkPrivileges(resource): d = resource.checkPrivileges(request, requested_access) return d # No auth; should deny request = SimpleRequest(site, "GET", "/") d = request.locateResource("/").addCallback( _checkPrivileges).addErrback(expectError) ds.append(d) # Has auth; should allow request = SimpleRequest(site, "GET", "/") request.authzUser = request.authnUser = self.rootresource.principalForUser( "gooduser") d = request.locateResource("/") d.addCallback(_checkPrivileges) d.addCallback(expectOK) ds.append(d) return DeferredList(ds)
def setUp(self): TestCase.setUp(self) gooduser = TestDAVPrincipalResource("/users/gooduser") gooduser.writeDeadProperty(TwistedPasswordProperty("goodpass")) baduser = TestDAVPrincipalResource("/users/baduser") baduser.writeDeadProperty(TwistedPasswordProperty("badpass")) rootresource = TestPrincipalsCollection( "/", { "users": TestResource("/users/", { "gooduser": gooduser, "baduser": baduser }) }) protected = TestResource("/protected", principalCollections=[rootresource]) protected.setAccessControlList( davxml.ACL( davxml.ACE(davxml.Principal(davxml.HRef("/users/gooduser")), davxml.Grant(davxml.Privilege(davxml.All())), davxml.Protected()))) rootresource.children["protected"] = protected portal = Portal(DavRealm()) portal.registerChecker(TwistedPropertyChecker()) credentialFactories = (basic.BasicCredentialFactory(""), ) loginInterfaces = (IPrincipal, ) self.rootresource = rootresource self.site = Site( AuthenticationWrapper( self.rootresource, portal, credentialFactories, credentialFactories, loginInterfaces, ))
def generateResponse(method): resource = TestResource() method = getattr(resource, "http_" + method) return method(SimpleRequest(Site(resource), method, "/"))