示例#1
0
class AMPConfigurationTests(TestCase):
    """
    Tests for L{xmantissa.ampserver.AMPConfiguration} which defines how to
    create an L{AMP} server.
    """
    def setUp(self):
        """
        Create an in-memory L{Store} with an L{AMPConfiguration} in it.
        """
        self.store = Store()
        self.conf = AMPConfiguration(store=self.store)
        installOn(self.conf, self.store)

    def test_interfaces(self):
        """
        L{AMPConfiguration} implements L{IProtocolFactoryFactory}.
        """
        self.assertTrue(verifyObject(IProtocolFactoryFactory, self.conf))

    def test_powerup(self):
        """
        L{ionstallOn} powers up the target for L{IProtocolFactoryFactory} with
        L{AMPConfiguration}.
        """
        self.assertIn(self.conf,
                      list(self.store.powerupsFor(IProtocolFactoryFactory)))

    def test_getFactory(self):
        """
        L{AMPConfiguration.getFactory} returns a L{ServerFactory} instance
        which returns L{CredReceiver} instances from its C{buildProtocol}
        method.
        """
        factory = self.conf.getFactory()
        self.assertTrue(isinstance(factory, ServerFactory))
        protocol = factory.buildProtocol(None)
        self.assertTrue(isinstance(protocol, CredReceiver))

    def test_generateOneTimePad(self):
        """
        L{AMPConfiguration.generateOneTimePad} returns a one-time pad.
        """
        object.__setattr__(self.conf, 'callLater', lambda x, y: None)
        pad = self.conf.generateOneTimePad(self.store)
        self.assertNotEqual(pad, self.conf.generateOneTimePad(self.store))

    def test_oneTimePadExpires(self):
        """
        L{AMPConfiguration.generateOneTimePad} should expire its pad.
        """
        def callLater(seconds, f):
            self.assertEqual(seconds, self.conf.ONE_TIME_PAD_DURATION)
            f()

        object.__setattr__(self.conf, 'callLater', callLater)
        pad = self.conf.generateOneTimePad(self.store)
        self.assertFalse(pad in self.conf._oneTimePads)
示例#2
0
class AMPConfigurationTests(TestCase):
    """
    Tests for L{xmantissa.ampserver.AMPConfiguration} which defines how to
    create an L{AMP} server.
    """
    def setUp(self):
        """
        Create an in-memory L{Store} with an L{AMPConfiguration} in it.
        """
        self.store = Store()
        self.conf = AMPConfiguration(store=self.store)
        installOn(self.conf, self.store)


    def test_interfaces(self):
        """
        L{AMPConfiguration} implements L{IProtocolFactoryFactory}.
        """
        self.assertTrue(verifyObject(IProtocolFactoryFactory, self.conf))


    def test_powerup(self):
        """
        L{ionstallOn} powers up the target for L{IProtocolFactoryFactory} with
        L{AMPConfiguration}.
        """
        self.assertIn(
            self.conf, list(self.store.powerupsFor(IProtocolFactoryFactory)))


    def test_getFactory(self):
        """
        L{AMPConfiguration.getFactory} returns a L{ServerFactory} instance
        which returns L{CredReceiver} instances from its C{buildProtocol}
        method.
        """
        factory = self.conf.getFactory()
        self.assertTrue(isinstance(factory, ServerFactory))
        protocol = factory.buildProtocol(None)
        self.assertTrue(isinstance(protocol, CredReceiver))


    def test_generateOneTimePad(self):
        """
        L{AMPConfiguration.generateOneTimePad} returns a one-time pad.
        """
        object.__setattr__(self.conf, 'callLater', lambda x, y: None)
        pad = self.conf.generateOneTimePad(self.store)
        self.assertNotEqual(
            pad, self.conf.generateOneTimePad(self.store))


    def test_oneTimePadExpires(self):
        """
        L{AMPConfiguration.generateOneTimePad} should expire its pad.
        """
        def callLater(seconds, f):
            self.assertEqual(
                seconds, self.conf.ONE_TIME_PAD_DURATION)
            f()
        object.__setattr__(self.conf, 'callLater', callLater)
        pad = self.conf.generateOneTimePad(self.store)
        self.assertFalse(pad in self.conf._oneTimePads)
示例#3
0
class AMPConfigurationSubStoreTests(TestCase):
    """
    Tests for L{AMPConfiguration} which require a substore.
    """
    def setUp(self):
        """
        Create an in-memory L{Store} with an L{AMPConfiguration} in it, and a
        substore.
        """
        self.store = Store()
        self.conf = AMPConfiguration(store=self.store)
        installOn(self.conf, self.store)

        self.localpart = u'alice'
        self.domain = u'example.org'
        self.password = u'foobar'

        loginSystem = self.store.findUnique(LoginSystem)
        account = loginSystem.addAccount(
            self.localpart, self.domain, self.password, internal=True)
        self.subStore = account.avatars.open()


    def _testPortalLogin(self, credentials):
        factory = self.conf.getFactory()
        protocol = factory.buildProtocol(None)
        portal = protocol.portal

        class IDummy(Interface):
            pass

        avatar = object()
        self.subStore.inMemoryPowerUp(avatar, IDummy)
        login = portal.login(credentials, None, IDummy)
        def cbLoggedIn(result):
            self.assertIdentical(IDummy, result[0])
            self.assertIdentical(avatar, result[1])
        login.addCallback(cbLoggedIn)
        return login


    def test_portal(self):
        """
        L{AMPConfiguration.getFactory} returns a factory which creates
        protocols which have a C{portal} attribute which is a L{Portal} which
        authenticates and authorizes using L{axiom.userbase}.
        """
        return self._testPortalLogin(
            UsernamePassword(
                '%s@%s' % (self.localpart.encode('ascii'),
                           self.domain.encode('ascii')),
                self.password),)


    def test_portalOneTimePad(self):
        """
        L{AMPConfiguration.getFactory} returns a factory which creates
        protocols which have a C{portal} attribute which is a L{Portal} which
        can authenticate using one-time pads.
        """
        object.__setattr__(self.conf, 'callLater', lambda x, y: None)
        PAD = self.conf.generateOneTimePad(self.subStore)
        class OTP:
            implements(IOneTimePad)
            padValue = PAD

        return self._testPortalLogin(OTP())
示例#4
0
class AMPConfigurationSubStoreTests(TestCase):
    """
    Tests for L{AMPConfiguration} which require a substore.
    """
    def setUp(self):
        """
        Create an in-memory L{Store} with an L{AMPConfiguration} in it, and a
        substore.
        """
        self.store = Store()
        self.conf = AMPConfiguration(store=self.store)
        installOn(self.conf, self.store)

        self.localpart = u'alice'
        self.domain = u'example.org'
        self.password = u'foobar'

        loginSystem = self.store.findUnique(LoginSystem)
        account = loginSystem.addAccount(self.localpart,
                                         self.domain,
                                         self.password,
                                         internal=True)
        self.subStore = account.avatars.open()

    def _testPortalLogin(self, credentials):
        factory = self.conf.getFactory()
        protocol = factory.buildProtocol(None)
        portal = protocol.portal

        class IDummy(Interface):
            pass

        avatar = object()
        self.subStore.inMemoryPowerUp(avatar, IDummy)
        login = portal.login(credentials, None, IDummy)

        def cbLoggedIn(result):
            self.assertIdentical(IDummy, result[0])
            self.assertIdentical(avatar, result[1])

        login.addCallback(cbLoggedIn)
        return login

    def test_portal(self):
        """
        L{AMPConfiguration.getFactory} returns a factory which creates
        protocols which have a C{portal} attribute which is a L{Portal} which
        authenticates and authorizes using L{axiom.userbase}.
        """
        return self._testPortalLogin(
            UsernamePassword(
                '%s@%s' %
                (self.localpart.encode('ascii'), self.domain.encode('ascii')),
                self.password), )

    def test_portalOneTimePad(self):
        """
        L{AMPConfiguration.getFactory} returns a factory which creates
        protocols which have a C{portal} attribute which is a L{Portal} which
        can authenticate using one-time pads.
        """
        object.__setattr__(self.conf, 'callLater', lambda x, y: None)
        PAD = self.conf.generateOneTimePad(self.subStore)

        class OTP:
            implements(IOneTimePad)
            padValue = PAD

        return self._testPortalLogin(OTP())