Пример #1
0
def makeService(config):
    s = service.MultiService()

    # Create backend service with storage

    if config['backend'] == 'pgsql':
        from twisted.enterprise import adbapi
        from idavoll.pgsql_storage import Storage
        from psycopg2.extras import NamedTupleConnection
        dbpool = adbapi.ConnectionPool('psycopg2',
                                       user=config['dbuser'],
                                       password=config['dbpass'],
                                       database=config['dbname'],
                                       host=config['dbhost'],
                                       port=config['dbport'],
                                       cp_reconnect=True,
                                       client_encoding='utf-8',
                                       connection_factory=NamedTupleConnection,
                                       )
        st = Storage(dbpool)
    elif config['backend'] == 'memory':
        from idavoll.memory_storage import Storage
        st = Storage()

    bs = BackendService(st)
    bs.setName('backend')
    bs.setServiceParent(s)

    # Set up XMPP server-side component with publish-subscribe capabilities

    cs = Component(config["rhost"], int(config["rport"]),
                   config["jid"].full(), config["secret"])
    cs.setName('component')
    cs.setServiceParent(s)

    cs.factory.maxDelay = 900

    if config["verbose"]:
        cs.logTraffic = True

    FallbackHandler().setHandlerParent(cs)
    VersionHandler('Idavoll', __version__).setHandlerParent(cs)
    DiscoHandler().setHandlerParent(cs)

    resource = IPubSubResource(bs)
    resource.hideNodes = config["hide-nodes"]
    resource.serviceJID = config["jid"]

    ps = PubSubService(resource)
    ps.setHandlerParent(cs)
    resource.pubsubService = ps

    return s
Пример #2
0
def makeService(config):
    s = service.MultiService()

    # Create backend service with storage

    if config['backend'] == 'pgsql':
        from twisted.enterprise import adbapi
        from idavoll.pgsql_storage import Storage
        from psycopg2.extras import NamedTupleConnection
        dbpool = adbapi.ConnectionPool(
            'psycopg2',
            user=config['dbuser'],
            password=config['dbpass'],
            database=config['dbname'],
            host=config['dbhost'],
            port=config['dbport'],
            cp_reconnect=True,
            client_encoding='utf-8',
            connection_factory=NamedTupleConnection,
        )
        st = Storage(dbpool)
    elif config['backend'] == 'memory':
        from idavoll.memory_storage import Storage
        st = Storage()

    bs = BackendService(st)
    bs.setName('backend')
    bs.setServiceParent(s)

    # Set up XMPP server-side component with publish-subscribe capabilities

    cs = Component(config["rhost"], int(config["rport"]), config["jid"].full(),
                   config["secret"])
    cs.setName('component')
    cs.setServiceParent(s)

    cs.factory.maxDelay = 900

    if config["verbose"]:
        cs.logTraffic = True

    FallbackHandler().setHandlerParent(cs)
    VersionHandler('Idavoll', __version__).setHandlerParent(cs)
    DiscoHandler().setHandlerParent(cs)

    resource = IPubSubResource(bs)
    resource.hideNodes = config["hide-nodes"]
    resource.serviceJID = config["jid"]

    ps = PubSubService(resource)
    ps.setHandlerParent(cs)
    resource.pubsubService = ps

    return s
Пример #3
0
class CreateResourceTest(unittest.TestCase):
    """
    Tests for L{gateway.CreateResource}.
    """

    def setUp(self):
        self.backend = BackendService(Storage())
        self.resource = gateway.CreateResource(self.backend, componentJID,
                                               ownerJID)


    def test_get(self):
        """
        The method GET is not supported.
        """
        request = DummyRequest([b''])
        self.assertRaises(error.UnsupportedMethod,
                          _render, self.resource, request)


    def test_post(self):
        """
        Upon a POST, a new node is created and the URI returned.
        """
        request = DummyRequest([b''])
        request.method = 'POST'

        def gotNodes(nodeIdentifiers, uri):
            service, nodeIdentifier = gateway.getServiceAndNode(uri)
            self.assertIn(nodeIdentifier, nodeIdentifiers)

        def rendered(result):
            self.assertEqual('application/json',
                             request.outgoingHeaders['content-type'])
            payload = simplejson.loads(b''.join(request.written))
            self.assertIn('uri', payload)
            d = self.backend.getNodes()
            d.addCallback(gotNodes, payload['uri'])
            return d

        d = _render(self.resource, request)
        d.addCallback(rendered)
        return d
Пример #4
0
class CreateResourceTest(unittest.TestCase):
    """
    Tests for L{gateway.CreateResource}.
    """
    def setUp(self):
        self.backend = BackendService(Storage())
        self.resource = gateway.CreateResource(self.backend, componentJID,
                                               ownerJID)

    def test_get(self):
        """
        The method GET is not supported.
        """
        request = DummyRequest([b''])
        self.assertRaises(error.UnsupportedMethod, _render, self.resource,
                          request)

    def test_post(self):
        """
        Upon a POST, a new node is created and the URI returned.
        """
        request = DummyRequest([b''])
        request.method = 'POST'

        def gotNodes(nodeIdentifiers, uri):
            service, nodeIdentifier = gateway.getServiceAndNode(uri)
            self.assertIn(nodeIdentifier, nodeIdentifiers)

        def rendered(result):
            self.assertEqual('application/json',
                             request.outgoingHeaders['content-type'])
            payload = simplejson.loads(b''.join(request.written))
            self.assertIn('uri', payload)
            d = self.backend.getNodes()
            d.addCallback(gotNodes, payload['uri'])
            return d

        d = _render(self.resource, request)
        d.addCallback(rendered)
        return d
Пример #5
0
 def setUp(self):
     self.backend = BackendService(Storage())
     self.resource = gateway.DeleteResource(self.backend, componentJID,
                                            ownerJID)
Пример #6
0
class DeleteResourceTest(unittest.TestCase):
    """
    Tests for L{gateway.DeleteResource}.
    """

    def setUp(self):
        self.backend = BackendService(Storage())
        self.resource = gateway.DeleteResource(self.backend, componentJID,
                                               ownerJID)


    def test_get(self):
        """
        The method GET is not supported.
        """
        request = DummyRequest([b''])
        self.assertRaises(error.UnsupportedMethod,
                          _render, self.resource, request)


    def test_post(self):
        """
        Upon a POST, a new node is created and the URI returned.
        """
        request = DummyRequest([b''])
        request.method = b'POST'

        def rendered(result):
            self.assertEqual(http.NO_CONTENT, request.responseCode)

        def nodeCreated(nodeIdentifier):
            uri = gateway.getXMPPURI(componentJID, nodeIdentifier)
            request.args[b'uri'] = [uri]
            request.content = StringIO(b'')

            return _render(self.resource, request)

        d = self.backend.createNode(u'test', ownerJID)
        d.addCallback(nodeCreated)
        d.addCallback(rendered)
        return d


    def test_postWithRedirect(self):
        """
        Upon a POST, a new node is created and the URI returned.
        """
        request = DummyRequest([b''])
        request.method = b'POST'
        otherNodeURI = b'xmpp:pubsub.example.org?node=other'

        def rendered(result):
            self.assertEqual(http.NO_CONTENT, request.responseCode)
            self.assertEqual(1, len(deletes))
            nodeIdentifier, owner, redirectURI = deletes[-1]
            self.assertEqual(otherNodeURI, redirectURI)

        def nodeCreated(nodeIdentifier):
            uri = gateway.getXMPPURI(componentJID, nodeIdentifier)
            request.args[b'uri'] = [uri]
            payload = {b'redirect_uri': otherNodeURI}
            body = simplejson.dumps(payload)
            request.content = StringIO(body)
            return _render(self.resource, request)

        def deleteNode(nodeIdentifier, owner, redirectURI):
            deletes.append((nodeIdentifier, owner, redirectURI))
            return defer.succeed(nodeIdentifier)

        deletes = []
        self.patch(self.backend, 'deleteNode', deleteNode)
        d = self.backend.createNode(u'test', ownerJID)
        d.addCallback(nodeCreated)
        d.addCallback(rendered)
        return d


    def test_postUnknownNode(self):
        """
        If the node to be deleted is unknown, 404 Not Found is returned.
        """
        request = DummyRequest([b''])
        request.method = b'POST'

        def rendered(result):
            self.assertEqual(http.NOT_FOUND, request.responseCode)

        uri = gateway.getXMPPURI(componentJID, u'unknown')
        request.args[b'uri'] = [uri]
        request.content = StringIO(b'')

        d = _render(self.resource, request)
        d.addCallback(rendered)
        return d


    def test_postMalformedXMPPURI(self):
        """
        If the XMPP URI is malformed, Bad Request is returned.
        """
        request = DummyRequest([b''])
        request.method = b'POST'

        def rendered(result):
            self.assertEqual(http.BAD_REQUEST, request.responseCode)

        uri = 'xmpp:@@@@'
        request.args[b'uri'] = [uri]
        request.content = StringIO(b'')

        d = _render(self.resource, request)
        d.addCallback(rendered)
        return d


    def test_postURIMissing(self):
        """
        If no URI is passed, 400 Bad Request is returned.
        """
        request = DummyRequest([b''])
        request.method = b'POST'

        def rendered(result):
            self.assertEqual(http.BAD_REQUEST, request.responseCode)

        request.content = StringIO(b'')

        d = _render(self.resource, request)
        d.addCallback(rendered)
        return d
Пример #7
0
 def setUp(self):
     self.backend = BackendService(Storage())
     self.resource = gateway.DeleteResource(self.backend, componentJID,
                                            ownerJID)
Пример #8
0
class DeleteResourceTest(unittest.TestCase):
    """
    Tests for L{gateway.DeleteResource}.
    """
    def setUp(self):
        self.backend = BackendService(Storage())
        self.resource = gateway.DeleteResource(self.backend, componentJID,
                                               ownerJID)

    def test_get(self):
        """
        The method GET is not supported.
        """
        request = DummyRequest([b''])
        self.assertRaises(error.UnsupportedMethod, _render, self.resource,
                          request)

    def test_post(self):
        """
        Upon a POST, a new node is created and the URI returned.
        """
        request = DummyRequest([b''])
        request.method = b'POST'

        def rendered(result):
            self.assertEqual(http.NO_CONTENT, request.responseCode)

        def nodeCreated(nodeIdentifier):
            uri = gateway.getXMPPURI(componentJID, nodeIdentifier)
            request.args[b'uri'] = [uri]
            request.content = StringIO(b'')

            return _render(self.resource, request)

        d = self.backend.createNode(u'test', ownerJID)
        d.addCallback(nodeCreated)
        d.addCallback(rendered)
        return d

    def test_postWithRedirect(self):
        """
        Upon a POST, a new node is created and the URI returned.
        """
        request = DummyRequest([b''])
        request.method = b'POST'
        otherNodeURI = b'xmpp:pubsub.example.org?node=other'

        def rendered(result):
            self.assertEqual(http.NO_CONTENT, request.responseCode)
            self.assertEqual(1, len(deletes))
            nodeIdentifier, owner, redirectURI = deletes[-1]
            self.assertEqual(otherNodeURI, redirectURI)

        def nodeCreated(nodeIdentifier):
            uri = gateway.getXMPPURI(componentJID, nodeIdentifier)
            request.args[b'uri'] = [uri]
            payload = {b'redirect_uri': otherNodeURI}
            body = simplejson.dumps(payload)
            request.content = StringIO(body)
            return _render(self.resource, request)

        def deleteNode(nodeIdentifier, owner, redirectURI):
            deletes.append((nodeIdentifier, owner, redirectURI))
            return defer.succeed(nodeIdentifier)

        deletes = []
        self.patch(self.backend, 'deleteNode', deleteNode)
        d = self.backend.createNode(u'test', ownerJID)
        d.addCallback(nodeCreated)
        d.addCallback(rendered)
        return d

    def test_postUnknownNode(self):
        """
        If the node to be deleted is unknown, 404 Not Found is returned.
        """
        request = DummyRequest([b''])
        request.method = b'POST'

        def rendered(result):
            self.assertEqual(http.NOT_FOUND, request.responseCode)

        uri = gateway.getXMPPURI(componentJID, u'unknown')
        request.args[b'uri'] = [uri]
        request.content = StringIO(b'')

        d = _render(self.resource, request)
        d.addCallback(rendered)
        return d

    def test_postMalformedXMPPURI(self):
        """
        If the XMPP URI is malformed, Bad Request is returned.
        """
        request = DummyRequest([b''])
        request.method = b'POST'

        def rendered(result):
            self.assertEqual(http.BAD_REQUEST, request.responseCode)

        uri = 'xmpp:@@@@'
        request.args[b'uri'] = [uri]
        request.content = StringIO(b'')

        d = _render(self.resource, request)
        d.addCallback(rendered)
        return d

    def test_postURIMissing(self):
        """
        If no URI is passed, 400 Bad Request is returned.
        """
        request = DummyRequest([b''])
        request.method = b'POST'

        def rendered(result):
            self.assertEqual(http.BAD_REQUEST, request.responseCode)

        request.content = StringIO(b'')

        d = _render(self.resource, request)
        d.addCallback(rendered)
        return d