Ejemplo n.º 1
0
 def setUp(self):
     """
     Create a resource and a wrapper to test.
     """
     self.store = Store()
     self.urlGenerator = SiteConfiguration(store=self.store,
                                           hostname=u"example.com")
     self.child = StubResource(None, None, None)
     self.childSegments = ("baz", "quux")
     self.content = "some bytes perhaps"
     self.resource = StubResource(self.child, self.childSegments,
                                  self.content)
     self.wrapper = SecuringWrapper(self.urlGenerator, self.resource)
Ejemplo n.º 2
0
 def setUp(self):
     """
     Create a resource and a wrapper to test.
     """
     self.store = Store()
     self.urlGenerator = SiteConfiguration(store=self.store,
                                           hostname=u"example.com")
     self.child = StubResource(None, None, None)
     self.childSegments = ("baz", "quux")
     self.content = "some bytes perhaps"
     self.resource = StubResource(
         self.child, self.childSegments, self.content)
     self.wrapper = SecuringWrapper(self.urlGenerator, self.resource)
Ejemplo n.º 3
0
class SecuringWrapperTests(TestCase):
    """
    L{SecuringWrapper} makes sure that any resource which is eventually
    retrieved from a wrapped resource and then rendered is rendered over HTTPS
    if possible and desired.
    """
    def setUp(self):
        """
        Create a resource and a wrapper to test.
        """
        self.store = Store()
        self.urlGenerator = SiteConfiguration(store=self.store,
                                              hostname=u"example.com")
        self.child = StubResource(None, None, None)
        self.childSegments = ("baz", "quux")
        self.content = "some bytes perhaps"
        self.resource = StubResource(self.child, self.childSegments,
                                     self.content)
        self.wrapper = SecuringWrapper(self.urlGenerator, self.resource)

    def test_locateChildHTTPS(self):
        """
        L{SecuringWrapper.locateChild} returns the wrapped resource and the
        unmodified segments if it is called with a secure request.
        """
        segments = ("foo", "bar")
        request = FakeRequest(isSecure=True)
        newResource, newSegments = self.wrapper.locateChild(request, segments)
        self.assertIdentical(newResource, self.resource)
        self.assertEqual(newSegments, segments)

    def test_locateChildHTTP(self):
        """
        L{SecuringWrapper.locateChild} returns a L{_SecureWrapper} wrapped
        around its own wrapped resource along with the unmodified segments if
        it is called with an insecure request.
        """
        segments = ("foo", "bar")
        request = FakeRequest(isSecure=False)
        newResource, newSegments = self.wrapper.locateChild(request, segments)
        self.assertTrue(isinstance(newResource, _SecureWrapper))
        self.assertIdentical(newResource.urlGenerator, self.urlGenerator)
        self.assertIdentical(newResource.wrappedResource, self.resource)
        self.assertEqual(newSegments, segments)

    def test_renderHTTPNeedsSecure(self):
        """
        L{SecuringWrapper.renderHTTP} returns a L{URL} pointing at the same
        location as the request URI but with an https scheme if the wrapped
        resource has a C{needsSecure} attribute with a true value and the
        request is over http.
        """
        SSLPort(store=self.store, factory=self.urlGenerator, portNumber=443)
        request = FakeRequest(isSecure=False,
                              uri='/bar/baz',
                              currentSegments=['bar', 'baz'])
        self.resource.needsSecure = True
        result = self.wrapper.renderHTTP(request)
        self.assertEqual(
            result, URL('https', self.urlGenerator.hostname, ['bar', 'baz']))

    def test_renderHTTP(self):
        """
        L{SecuringWrapper.renderHTTP} returns the result of the wrapped
        resource's C{renderHTTP} method if the wrapped resource does not have a
        C{needsSecure} attribute with a true value.
        """
        request = FakeRequest(isSecure=False,
                              uri='/bar/baz',
                              currentSegments=['bar', 'baz'])
        result = self.wrapper.renderHTTP(request)
        self.assertIdentical(self.resource.renderedWithContext, request)
        self.assertEqual(result, self.content)

    def test_renderHTTPS(self):
        """
        L{SecuringWrapper.renderHTTP} returns the result of the wrapped
        resource's C{renderHTTP} method if it is called with a secure request.
        """
        request = FakeRequest(isSecure=True)
        result = self.wrapper.renderHTTP(request)
        self.assertIdentical(self.resource.renderedWithContext, request)
        self.assertEqual(result, self.content)

    def test_renderHTTPCannotSecure(self):
        """
        L{SecuringWrapper.renderHTTP} returns the result of the wrapped
        resource's C{renderHTTP} method if it is invoked over http but there is
        no https location available.
        """
        request = FakeRequest(isSecure=False)
        result = self.wrapper.renderHTTP(request)
        self.assertIdentical(self.resource.renderedWithContext, request)
        self.assertEqual(result, self.content)

    def test_childLocateChild(self):
        """
        L{_SecureWrapper.locateChild} returns a L{Deferred} which is called
        back with the result of the wrapped resource's C{locateChild} method
        wrapped in another L{_SecureWrapper}.
        """
        segments = ('foo', 'bar')
        request = FakeRequest()
        wrapper = _SecureWrapper(self.urlGenerator, self.resource)
        result = wrapper.locateChild(request, segments)

        def locatedChild((resource, segments)):
            self.assertTrue(isinstance(resource, _SecureWrapper))
            self.assertIdentical(resource.wrappedResource, self.child)
            self.assertEqual(segments, self.childSegments)

        result.addCallback(locatedChild)
        return result

    def test_notFound(self):
        """
        A L{_SecureWrapper.locateChild} lets L{NotFound} results from the
        wrapped resource pass through.
        """
        segments = ('foo', 'bar')
        request = FakeRequest()
        self.resource.childResource = None
        self.resource.childSegments = ()
        wrapper = _SecureWrapper(self.urlGenerator, self.resource)
        result = wrapper.locateChild(request, segments)

        def locatedChild(result):
            self.assertIdentical(result, NotFound)

        result.addCallback(locatedChild)
        return result

    def test_adaption(self):
        """
        A L{_SecureWrapper} constructed with an object which does not provide
        L{IResource} adapts it to L{IResource} and operates on the result.
        """
        notResource = NotResource()
        wrapper = _SecureWrapper(self.urlGenerator, notResource)
        self.assertTrue(isinstance(wrapper.wrappedResource,
                                   NotResourceAdapter))
        self.assertIdentical(wrapper.wrappedResource.notResource, notResource)
Ejemplo n.º 4
0
class SecuringWrapperTests(TestCase):
    """
    L{SecuringWrapper} makes sure that any resource which is eventually
    retrieved from a wrapped resource and then rendered is rendered over HTTPS
    if possible and desired.
    """
    def setUp(self):
        """
        Create a resource and a wrapper to test.
        """
        self.store = Store()
        self.urlGenerator = SiteConfiguration(store=self.store,
                                              hostname=u"example.com")
        self.child = StubResource(None, None, None)
        self.childSegments = ("baz", "quux")
        self.content = "some bytes perhaps"
        self.resource = StubResource(
            self.child, self.childSegments, self.content)
        self.wrapper = SecuringWrapper(self.urlGenerator, self.resource)


    def test_locateChildHTTPS(self):
        """
        L{SecuringWrapper.locateChild} returns the wrapped resource and the
        unmodified segments if it is called with a secure request.
        """
        segments = ("foo", "bar")
        request = FakeRequest(isSecure=True)
        newResource, newSegments = self.wrapper.locateChild(request, segments)
        self.assertIdentical(newResource, self.resource)
        self.assertEqual(newSegments, segments)


    def test_locateChildHTTP(self):
        """
        L{SecuringWrapper.locateChild} returns a L{_SecureWrapper} wrapped
        around its own wrapped resource along with the unmodified segments if
        it is called with an insecure request.
        """
        segments = ("foo", "bar")
        request = FakeRequest(isSecure=False)
        newResource, newSegments = self.wrapper.locateChild(request, segments)
        self.assertTrue(isinstance(newResource, _SecureWrapper))
        self.assertIdentical(newResource.urlGenerator, self.urlGenerator)
        self.assertIdentical(newResource.wrappedResource, self.resource)
        self.assertEqual(newSegments, segments)


    def test_renderHTTPNeedsSecure(self):
        """
        L{SecuringWrapper.renderHTTP} returns a L{URL} pointing at the same
        location as the request URI but with an https scheme if the wrapped
        resource has a C{needsSecure} attribute with a true value and the
        request is over http.
        """
        SSLPort(store=self.store, factory=self.urlGenerator, portNumber=443)
        request = FakeRequest(
            isSecure=False, uri='/bar/baz', currentSegments=['bar', 'baz'])
        self.resource.needsSecure = True
        result = self.wrapper.renderHTTP(request)
        self.assertEqual(
            result, URL('https', self.urlGenerator.hostname, ['bar', 'baz']))


    def test_renderHTTP(self):
        """
        L{SecuringWrapper.renderHTTP} returns the result of the wrapped
        resource's C{renderHTTP} method if the wrapped resource does not have a
        C{needsSecure} attribute with a true value.
        """
        request = FakeRequest(
            isSecure=False, uri='/bar/baz', currentSegments=['bar', 'baz'])
        result = self.wrapper.renderHTTP(request)
        self.assertIdentical(self.resource.renderedWithContext, request)
        self.assertEqual(result, self.content)


    def test_renderHTTPS(self):
        """
        L{SecuringWrapper.renderHTTP} returns the result of the wrapped
        resource's C{renderHTTP} method if it is called with a secure request.
        """
        request = FakeRequest(isSecure=True)
        result = self.wrapper.renderHTTP(request)
        self.assertIdentical(self.resource.renderedWithContext, request)
        self.assertEqual(result, self.content)


    def test_renderHTTPCannotSecure(self):
        """
        L{SecuringWrapper.renderHTTP} returns the result of the wrapped
        resource's C{renderHTTP} method if it is invoked over http but there is
        no https location available.
        """
        request = FakeRequest(isSecure=False)
        result = self.wrapper.renderHTTP(request)
        self.assertIdentical(self.resource.renderedWithContext, request)
        self.assertEqual(result, self.content)


    def test_childLocateChild(self):
        """
        L{_SecureWrapper.locateChild} returns a L{Deferred} which is called
        back with the result of the wrapped resource's C{locateChild} method
        wrapped in another L{_SecureWrapper}.
        """
        segments = ('foo', 'bar')
        request = FakeRequest()
        wrapper = _SecureWrapper(self.urlGenerator, self.resource)
        result = wrapper.locateChild(request, segments)
        def locatedChild((resource, segments)):
            self.assertTrue(isinstance(resource, _SecureWrapper))
            self.assertIdentical(resource.wrappedResource, self.child)
            self.assertEqual(segments, self.childSegments)
        result.addCallback(locatedChild)
        return result


    def test_notFound(self):
        """
        A L{_SecureWrapper.locateChild} lets L{NotFound} results from the
        wrapped resource pass through.
        """
        segments = ('foo', 'bar')
        request = FakeRequest()
        self.resource.childResource = None
        self.resource.childSegments = ()
        wrapper = _SecureWrapper(self.urlGenerator, self.resource)
        result = wrapper.locateChild(request, segments)
        def locatedChild(result):
            self.assertIdentical(result, NotFound)
        result.addCallback(locatedChild)
        return result


    def test_adaption(self):
        """
        A L{_SecureWrapper} constructed with an object which does not provide
        L{IResource} adapts it to L{IResource} and operates on the result.
        """
        notResource = NotResource()
        wrapper = _SecureWrapper(self.urlGenerator, notResource)
        self.assertTrue(isinstance(wrapper.wrappedResource, NotResourceAdapter))
        self.assertIdentical(wrapper.wrappedResource.notResource, notResource)