コード例 #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)
コード例 #2
0
 def setUp(self):
     self.domain = u"example.com"
     self.store = Store()
     self.site = SiteConfiguration(store=self.store, hostname=self.domain)
コード例 #3
0
class SiteConfigurationTests(TestCase):
    """
    L{xmantissa.web.Site} defines how to create an HTTP server.
    """
    def setUp(self):
        self.domain = u"example.com"
        self.store = Store()
        self.site = SiteConfiguration(store=self.store, hostname=self.domain)

    def test_interfaces(self):
        """
        L{SiteConfiguration} implements L{IProtocolFactoryFactory} and
        L{ISiteURLGenerator}.
        """
        self.assertTrue(verifyObject(ISiteURLGenerator, self.site))
        self.assertTrue(verifyObject(IProtocolFactoryFactory, self.site))

    def _baseTest(self, portType, scheme, portNumber, method):
        portType(store=self.store, portNumber=portNumber, factory=self.site)
        self.assertEquals(
            getattr(self.site, method)(), URL(scheme, self.domain))

    def test_cleartextRoot(self):
        """
        L{SiteConfiguration.cleartextRoot} method returns the proper URL for
        HTTP communication with this site.
        """
        self._baseTest(TCPPort, 'http', 80, 'cleartextRoot')

    def test_encryptedRoot(self):
        """
        L{SiteConfiguration.encryptedRoot} method returns the proper URL for
        HTTPS communication with this site.
        """
        self._baseTest(SSLPort, 'https', 443, 'encryptedRoot')

    def _nonstandardPortTest(self, portType, scheme, portNumber, method):
        portType(store=self.store, portNumber=portNumber, factory=self.site)
        self.assertEquals(
            getattr(self.site, method)(),
            URL(scheme, '%s:%s' % (self.domain, portNumber)))

    def test_cleartextRootNonstandardPort(self):
        """
        L{SiteConfiguration.cleartextRoot} method returns the proper URL for
        HTTP communication with this site even if the server is listening on a
        non-standard port number.
        """
        self._nonstandardPortTest(TCPPort, 'http', 8000, 'cleartextRoot')

    def test_encryptedRootNonstandardPort(self):
        """
        L{SiteConfiguration.encryptedRoot} method returns the proper URL for
        HTTPS communication with this site even if the server is listening on a
        non-standard port number.
        """
        self._nonstandardPortTest(SSLPort, 'https', 8443, 'encryptedRoot')

    def _unavailableTest(self, method):
        self.assertEquals(getattr(self.site, method)(), None)

    def test_cleartextRootUnavailable(self):
        """
        L{SiteConfiguration.cleartextRoot} method returns None if there is no
        HTTP server listening.
        """
        self._unavailableTest('cleartextRoot')

    def test_encryptedRootUnavailable(self):
        """
        L{SiteConfiguration.encryptedRoot} method returns None if there is no
        HTTPS server listening.
        """
        self._unavailableTest('encryptedRoot')

    def _hostOverrideTest(self, portType, scheme, portNumber, method):
        portType(store=self.store, portNumber=portNumber, factory=self.site)
        self.assertEquals(
            getattr(self.site, method)(u'example.net'),
            URL(scheme, 'example.net'))

    def test_cleartextRootHostOverride(self):
        """
        A hostname passed to L{SiteConfiguration.cleartextRoot} overrides the
        configured hostname in the result.
        """
        self._hostOverrideTest(TCPPort, 'http', 80, 'cleartextRoot')

    def test_encryptedRootHostOverride(self):
        """
        A hostname passed to L{SiteConfiguration.encryptedRoot} overrides the
        configured hostname in the result.
        """
        self._hostOverrideTest(SSLPort, 'https', 443, 'encryptedRoot')

    def _portZero(self, portType, scheme, method):
        randomPort = 7777

        class FakePort(object):
            def getHost(self):
                return IPv4Address('TCP', u'example.com', randomPort)

        port = portType(store=self.store, portNumber=0, factory=self.site)
        port.listeningPort = FakePort()
        self.assertEquals(
            getattr(self.site, method)(),
            URL(scheme, '%s:%s' % (self.domain, randomPort)))

    def test_cleartextRootPortZero(self):
        """
        When the C{portNumber} of a started L{TCPPort} which refers to the
        C{SiteConfiguration} is C{0}, L{SiteConfiguration.cleartextRoot}
        returns an URL with the port number which was actually bound in the
        netloc.
        """
        self._portZero(TCPPort, 'http', 'cleartextRoot')

    def test_encryptedRootPortZero(self):
        """
        When the C{portNumber} of a started L{SSLPort} which refers to the
        C{SiteConfiguration} is C{0}, L{SiteConfiguration.encryptedRoot}
        returns an URL with the port number which was actually bound in the
        netloc.
        """
        self._portZero(SSLPort, 'https', 'encryptedRoot')

    def _portZeroDisconnected(self, portType, method):
        portType(store=self.store, portNumber=0, factory=self.site)
        self.assertEquals(None, getattr(self.site, method)())

    def test_cleartextRootPortZeroDisconnected(self):
        """
        When the C{portNumber} of an unstarted L{TCPPort} which refers to the
        C{SiteConfiguration} is C{0}, L{SiteConfiguration.cleartextRoot}
        returns C{None}.
        """
        self._portZeroDisconnected(TCPPort, 'cleartextRoot')

    def test_encryptedRootPortZeroDisconnected(self):
        """
        When the C{portNumber} of an unstarted L{SSLPort} which refers to the
        C{SiteConfiguration} is C{0}, L{SiteConfiguration.encryptedRoot}
        returns C{None}.
        """
        self._portZeroDisconnected(SSLPort, 'encryptedRoot')

    def test_rootURL(self):
        """
        L{SiteConfiguration.rootURL} returns C{/} for a request made onto the
        hostname with which the L{SiteConfiguration} is configured.
        """
        request = FakeRequest(headers={'host': self.domain.encode('ascii')})
        self.assertEqual(self.site.rootURL(request), URL('', ''))

    def test_rootURLWithoutHost(self):
        """
        L{SiteConfiguration.rootURL} returns C{/} for a request made without a
        I{Host} header.
        """
        request = FakeRequest()
        self.assertEqual(self.site.rootURL(request), URL('', ''))

    def test_rootURLWWWSubdomain(self):
        """
        L{SiteConfiguration.rootURL} returns C{/} for a request made onto the
        I{www} subdomain of the hostname of the L{SiteConfiguration}.
        """
        request = FakeRequest(
            headers={'host': 'www.' + self.domain.encode('ascii')})
        self.assertEqual(self.site.rootURL(request), URL('', ''))

    def _differentHostnameTest(self, portType, portNumber, isSecure, scheme):
        request = FakeRequest(
            isSecure=isSecure,
            headers={'host': 'alice.' + self.domain.encode('ascii')})
        portType(store=self.store, factory=self.site, portNumber=portNumber)
        self.assertEqual(self.site.rootURL(request), URL(scheme, self.domain))

    def test_cleartextRootURLDifferentHostname(self):
        """
        L{SiteConfiguration.rootURL} returns an absolute URL with the HTTP
        scheme and its hostname as the netloc and with a path of C{/} for a
        request made over HTTP onto a hostname different from the hostname of the
        L{SiteConfiguration}.

        """
        self._differentHostnameTest(TCPPort, 80, False, 'http')

    def test_encryptedRootURLDifferentHostname(self):
        """
        L{SiteConfiguration.rootURL} returns an absolute URL with its hostname
        as the netloc and with a path of C{/} for a request made over HTTPS onto a
        hostname different from the hostname of the L{SiteConfiguration}.
        """
        self._differentHostnameTest(SSLPort, 443, True, 'https')

    def _differentHostnameNonstandardPort(self, portType, isSecure, scheme):
        portNumber = 12345
        request = FakeRequest(
            isSecure=isSecure,
            headers={'host': 'alice.' + self.domain.encode('ascii')})
        portType(store=self.store, factory=self.site, portNumber=portNumber)
        self.assertEqual(
            self.site.rootURL(request),
            URL(scheme, '%s:%s' % (self.domain.encode('ascii'), portNumber)))

    def test_cleartextRootURLDifferentHostnameNonstandardPort(self):
        """
        L{SiteConfiguration.rootURL} returns an absolute URL with an HTTP
        scheme and an explicit port number in the netloc for a request made
        over HTTP onto a hostname different from the hostname of the
        L{SiteConfiguration} if the L{SiteConfiguration} has an HTTP server on
        a non-standard port.
        """
        self._differentHostnameNonstandardPort(TCPPort, False, 'http')

    def test_encryptedRootURLDifferentHostnameNonstandardPort(self):
        """
        L{SiteConfiguration.rootURL} returns an absolute URL with an HTTPS
        scheme and an explicit port number in the netloc for a request made
        over HTTPS onto a hostname different from the hostname of the
        L{SiteConfiguration} if the L{SiteConfiguration} has an HTTPS server on
        a non-standard port.
        """
        self._differentHostnameNonstandardPort(SSLPort, True, 'https')

    def test_rootURLNonstandardRequestPort(self):
        """
        L{SiteConfiguration.rootURL} returns C{/} for a request made onto a
        non-standard port which is one on which the L{SiteConfiguration} is
        configured to listen.
        """
        request = FakeRequest(
            headers={'host': '%s:%s' % (self.domain.encode('ascii'), 54321)})
        TCPPort(store=self.store, factory=self.site, portNumber=54321)
        self.assertEqual(self.site.rootURL(request), URL('', ''))
コード例 #4
0
ファイル: test_website.py プロジェクト: twisted/mantissa
 def setUp(self):
     self.domain = u"example.com"
     self.store = Store()
     self.site = SiteConfiguration(store=self.store, hostname=self.domain)
コード例 #5
0
ファイル: test_website.py プロジェクト: twisted/mantissa
class SiteConfigurationTests(TestCase):
    """
    L{xmantissa.web.Site} defines how to create an HTTP server.
    """
    def setUp(self):
        self.domain = u"example.com"
        self.store = Store()
        self.site = SiteConfiguration(store=self.store, hostname=self.domain)


    def test_interfaces(self):
        """
        L{SiteConfiguration} implements L{IProtocolFactoryFactory} and
        L{ISiteURLGenerator}.
        """
        self.assertTrue(verifyObject(ISiteURLGenerator, self.site))
        self.assertTrue(verifyObject(IProtocolFactoryFactory, self.site))


    def _baseTest(self, portType, scheme, portNumber, method):
        portType(store=self.store, portNumber=portNumber, factory=self.site)
        self.assertEquals(
            getattr(self.site, method)(), URL(scheme, self.domain))


    def test_cleartextRoot(self):
        """
        L{SiteConfiguration.cleartextRoot} method returns the proper URL for
        HTTP communication with this site.
        """
        self._baseTest(TCPPort, 'http', 80, 'cleartextRoot')


    def test_encryptedRoot(self):
        """
        L{SiteConfiguration.encryptedRoot} method returns the proper URL for
        HTTPS communication with this site.
        """
        self._baseTest(SSLPort, 'https', 443, 'encryptedRoot')


    def _nonstandardPortTest(self, portType, scheme, portNumber, method):
        portType(store=self.store, portNumber=portNumber, factory=self.site)
        self.assertEquals(
            getattr(self.site, method)(),
            URL(scheme, '%s:%s' % (self.domain, portNumber)))


    def test_cleartextRootNonstandardPort(self):
        """
        L{SiteConfiguration.cleartextRoot} method returns the proper URL for
        HTTP communication with this site even if the server is listening on a
        non-standard port number.
        """
        self._nonstandardPortTest(TCPPort, 'http', 8000, 'cleartextRoot')


    def test_encryptedRootNonstandardPort(self):
        """
        L{SiteConfiguration.encryptedRoot} method returns the proper URL for
        HTTPS communication with this site even if the server is listening on a
        non-standard port number.
        """
        self._nonstandardPortTest(SSLPort, 'https', 8443, 'encryptedRoot')


    def _unavailableTest(self, method):
        self.assertEquals(getattr(self.site, method)(), None)


    def test_cleartextRootUnavailable(self):
        """
        L{SiteConfiguration.cleartextRoot} method returns None if there is no
        HTTP server listening.
        """
        self._unavailableTest('cleartextRoot')


    def test_encryptedRootUnavailable(self):
        """
        L{SiteConfiguration.encryptedRoot} method returns None if there is no
        HTTPS server listening.
        """
        self._unavailableTest('encryptedRoot')


    def _hostOverrideTest(self, portType, scheme, portNumber, method):
        portType(store=self.store, portNumber=portNumber, factory=self.site)
        self.assertEquals(
            getattr(self.site, method)(u'example.net'),
            URL(scheme, 'example.net'))


    def test_cleartextRootHostOverride(self):
        """
        A hostname passed to L{SiteConfiguration.cleartextRoot} overrides the
        configured hostname in the result.
        """
        self._hostOverrideTest(TCPPort, 'http', 80, 'cleartextRoot')


    def test_encryptedRootHostOverride(self):
        """
        A hostname passed to L{SiteConfiguration.encryptedRoot} overrides the
        configured hostname in the result.
        """
        self._hostOverrideTest(SSLPort, 'https', 443, 'encryptedRoot')


    def _portZero(self, portType, scheme, method):
        randomPort = 7777

        class FakePort(object):
            def getHost(self):
                return IPv4Address('TCP', u'example.com', randomPort)

        port = portType(store=self.store, portNumber=0, factory=self.site)
        port.listeningPort = FakePort()
        self.assertEquals(
            getattr(self.site, method)(),
            URL(scheme, '%s:%s' % (self.domain, randomPort)))


    def test_cleartextRootPortZero(self):
        """
        When the C{portNumber} of a started L{TCPPort} which refers to the
        C{SiteConfiguration} is C{0}, L{SiteConfiguration.cleartextRoot}
        returns an URL with the port number which was actually bound in the
        netloc.
        """
        self._portZero(TCPPort, 'http', 'cleartextRoot')


    def test_encryptedRootPortZero(self):
        """
        When the C{portNumber} of a started L{SSLPort} which refers to the
        C{SiteConfiguration} is C{0}, L{SiteConfiguration.encryptedRoot}
        returns an URL with the port number which was actually bound in the
        netloc.
        """
        self._portZero(SSLPort, 'https', 'encryptedRoot')


    def _portZeroDisconnected(self, portType, method):
        portType(store=self.store, portNumber=0, factory=self.site)
        self.assertEquals(None, getattr(self.site, method)())


    def test_cleartextRootPortZeroDisconnected(self):
        """
        When the C{portNumber} of an unstarted L{TCPPort} which refers to the
        C{SiteConfiguration} is C{0}, L{SiteConfiguration.cleartextRoot}
        returns C{None}.
        """
        self._portZeroDisconnected(TCPPort, 'cleartextRoot')


    def test_encryptedRootPortZeroDisconnected(self):
        """
        When the C{portNumber} of an unstarted L{SSLPort} which refers to the
        C{SiteConfiguration} is C{0}, L{SiteConfiguration.encryptedRoot}
        returns C{None}.
        """
        self._portZeroDisconnected(SSLPort, 'encryptedRoot')


    def test_rootURL(self):
        """
        L{SiteConfiguration.rootURL} returns C{/} for a request made onto the
        hostname with which the L{SiteConfiguration} is configured.
        """
        request = FakeRequest(headers={
            'host': self.domain.encode('ascii')})
        self.assertEqual(self.site.rootURL(request), URL('', ''))


    def test_rootURLWithoutHost(self):
        """
        L{SiteConfiguration.rootURL} returns C{/} for a request made without a
        I{Host} header.
        """
        request = FakeRequest()
        self.assertEqual(self.site.rootURL(request), URL('', ''))


    def test_rootURLWWWSubdomain(self):
        """
        L{SiteConfiguration.rootURL} returns C{/} for a request made onto the
        I{www} subdomain of the hostname of the L{SiteConfiguration}.
        """
        request = FakeRequest(headers={
            'host': 'www.' + self.domain.encode('ascii')})
        self.assertEqual(self.site.rootURL(request), URL('', ''))


    def test_rootURLAlternateSubdomain(self):
        """
        L{SiteConfiguration.rootURL} returns C{/} for a request made onto a
        subdomain known as an internal domain.
        """
        userbase.LoginMethod(
            store=self.store, localpart=u'username', domain=u'example.org',
            internal=True, protocol=u'*', verified=True, account=self.store)
        request = FakeRequest(headers={
            'host': 'example.org'})
        self.assertEqual(self.site.rootURL(request), URL('', ''))


    def _differentHostnameTest(self, portType, portNumber, isSecure, scheme):
        request = FakeRequest(isSecure=isSecure, headers={
            'host': 'alice.' + self.domain.encode('ascii')})
        portType(store=self.store, factory=self.site, portNumber=portNumber)
        self.assertEqual(self.site.rootURL(request), URL(scheme, self.domain))


    def test_cleartextRootURLDifferentHostname(self):
        """
        L{SiteConfiguration.rootURL} returns an absolute URL with the HTTP
        scheme and its hostname as the netloc and with a path of C{/} for a
        request made over HTTP onto a hostname different from the hostname of the
        L{SiteConfiguration}.

        """
        self._differentHostnameTest(TCPPort, 80, False, 'http')


    def test_encryptedRootURLDifferentHostname(self):
        """
        L{SiteConfiguration.rootURL} returns an absolute URL with its hostname
        as the netloc and with a path of C{/} for a request made over HTTPS onto a
        hostname different from the hostname of the L{SiteConfiguration}.
        """
        self._differentHostnameTest(SSLPort, 443, True, 'https')


    def _differentHostnameNonstandardPort(self, portType, isSecure, scheme):
        portNumber = 12345
        request = FakeRequest(isSecure=isSecure, headers={
            'host': 'alice.' + self.domain.encode('ascii')})
        portType(store=self.store, factory=self.site, portNumber=portNumber)
        self.assertEqual(
            self.site.rootURL(request),
            URL(scheme, '%s:%s' % (self.domain.encode('ascii'), portNumber)))


    def test_cleartextRootURLDifferentHostnameNonstandardPort(self):
        """
        L{SiteConfiguration.rootURL} returns an absolute URL with an HTTP
        scheme and an explicit port number in the netloc for a request made
        over HTTP onto a hostname different from the hostname of the
        L{SiteConfiguration} if the L{SiteConfiguration} has an HTTP server on
        a non-standard port.
        """
        self._differentHostnameNonstandardPort(TCPPort, False, 'http')


    def test_encryptedRootURLDifferentHostnameNonstandardPort(self):
        """
        L{SiteConfiguration.rootURL} returns an absolute URL with an HTTPS
        scheme and an explicit port number in the netloc for a request made
        over HTTPS onto a hostname different from the hostname of the
        L{SiteConfiguration} if the L{SiteConfiguration} has an HTTPS server on
        a non-standard port.
        """
        self._differentHostnameNonstandardPort(SSLPort, True, 'https')


    def test_rootURLNonstandardRequestPort(self):
        """
        L{SiteConfiguration.rootURL} returns C{/} for a request made onto a
        non-standard port which is one on which the L{SiteConfiguration} is
        configured to listen.
        """
        request = FakeRequest(headers={
            'host': '%s:%s' % (self.domain.encode('ascii'), 54321)})
        TCPPort(store=self.store, factory=self.site, portNumber=54321)
        self.assertEqual(self.site.rootURL(request), URL('', ''))
コード例 #6
0
ファイル: website.py プロジェクト: jonathanj/mantissa
def _makeSiteConfiguration(currentVersion, oldSite, couldHavePorts):
    from xmantissa.publicweb import AnonymousSite

    newSite = oldSite.upgradeVersion(
        'mantissa_web_powerup', currentVersion, 6,
        hitCount=oldSite.hitCount)

    if newSite.store.parent is not None:
        return newSite

    # SiteConfiguration dependsOn LoginSystem.  LoginSystem was probably
    # installed by the mantissa axiomatic command.  During the dependency
    # system conversion, that command was changed to use installOn on the
    # LoginSystem.  However, no upgrader was supplied to create the new
    # dependency state.  Consequently, there may be none.  Consequently, a new
    # LoginSystem will be created if an item which dependsOn LoginSystem is
    # installed.  This would be bad.  So, set up the necessary dependency state
    # here, before instantiating SiteConfiguration. -exarkun

    # Addendum: it is almost certainly the case that there are not legitimate
    # configurations which lack a LoginSystem.  However, a large number of
    # database upgrade tests construct unrealistic databases.  One aspect of
    # the unrealism is that they lack a LoginSystem.  Therefore, rather than
    # changing all the bogus stubs and regenerating the stubs, I will just
    # support the case where LoginSystem is missing.  However, note that a
    # LoginSystem upgrader may invalidate this check and result in a duplicate
    # being created anyway. -exarkun
    loginSystem = oldSite.store.findUnique(LoginSystem, default=None)
    if loginSystem is not None and installedOn(loginSystem) is None:
        installOn(loginSystem, oldSite.store)

    uninstallFrom(oldSite.store, oldSite)

    site = SiteConfiguration(
        store=oldSite.store,
        httpLog=oldSite.store.filesdir.child('httpd.log'),
        hostname=getattr(oldSite, "hostname", None) or u"localhost")
    installOn(site, site.store)

    anonymousAvatar = AnonymousSite(store=oldSite.store)
    installOn(anonymousAvatar, oldSite.store)

    if couldHavePorts:
        for tcp in site.store.query(TCPPort, TCPPort.factory == oldSite):
            tcp.factory = site
        for ssl in site.store.query(SSLPort, SSLPort.factory == oldSite):
            ssl.factory = site
    else:
        if oldSite.portNumber is not None:
            port = TCPPort(
                store=oldSite.store,
                portNumber=oldSite.portNumber,
                factory=site)
            installOn(port, oldSite.store)

        securePortNumber = oldSite.securePortNumber
        certificateFile = oldSite.certificateFile
        if securePortNumber is not None and certificateFile:
            oldCertPath = site.store.dbdir.preauthChild(certificateFile)
            newCertPath = site.store.newFilePath('server.pem')
            oldCertPath.moveTo(newCertPath)
            port = SSLPort(
                store=site.store,
                portNumber=oldSite.securePortNumber,
                certificatePath=newCertPath,
                factory=site)
            installOn(port, site.store)

    newSite.deleteFromStore()