source: ralphm-patches/pubsub_resource.patch @ 7:0fbac5c2e97d

Last change on this file since 7:0fbac5c2e97d was 7:0fbac5c2e97d, checked in by Ralph Meijer <ralphm@…>, 12 years ago

Save current state.

File size: 59.7 KB
  • wokkel/iwokkel.py

    diff -r cf770fa6bcb7 wokkel/iwokkel.py
    a b  
    297297        """
    298298
    299299
    300     def publish(request):
     300class IPubSubResource(Interface):
     301
     302    def locateResource(request):
    301303        """
    302         Called when a publish request has been received.
     304        Locate a resource that will handle the request.
    303305
    304306        @param request: The publish-subscribe request.
    305307        @type request: L{wokkel.pubsub.PubSubRequest}
    306         @return: deferred that fires on success.
     308        """
     309
     310
     311    def getInfo(requestor, service, nodeIdentifier):
     312        """
     313        Get node type and meta data.
     314
     315        @param requestor: The entity the request originated from.
     316        @type requestor: L{jid.JID}
     317        @param service: The publish-subscribe service entity.
     318        @type service: L{jid.JID}
     319        @param nodeIdentifier: Identifier of the node to request the info for.
     320        @type nodeIdentifier: L{unicode}
     321        @return: A deferred that fires with a dictionary. If not empty,
     322                 it must have the keys C{'type'} and C{'meta-data'} to keep
     323                 respectively the node type and a dictionary with the meta
     324                 data for that node.
    307325        @rtype: L{defer.Deferred}
    308326        """
    309327
    310328
    311     def subscribe(request):
     329    def getNodes(requestor, service, nodeIdentifier):
    312330        """
    313         Called when a subscribe request has been received.
     331        Get all nodes contained by this node.
    314332
    315         @param request: The publish-subscribe request.
    316         @type request: L{wokkel.pubsub.PubSubRequest}
    317         @return: A deferred that fires with a
    318                  L{Subscription<wokkel.pubsub.Subscription>}.
    319         @rtype: L{defer.Deferred}
    320         """
    321 
    322 
    323     def unsubscribe(request):
    324         """
    325         Called when a subscribe request has been received.
    326 
    327         @param request: The publish-subscribe request.
    328         @type request: L{wokkel.pubsub.PubSubRequest}
    329         @return: A deferred that fires with C{None} when unsubscription has
    330                  succeeded.
    331         @rtype: L{defer.Deferred}
    332         """
    333 
    334 
    335     def subscriptions(request):
    336         """
    337         Called when a subscriptions retrieval request has been received.
    338 
    339         @param request: The publish-subscribe request.
    340         @type request: L{wokkel.pubsub.PubSubRequest}
    341         @return: A deferred that fires with a C{list} of subscriptions as
    342                  L{Subscription<wokkel.pubsub.Subscription>}.
    343         @rtype: L{defer.Deferred}
    344         """
    345 
    346 
    347     def affiliations(request):
    348         """
    349         Called when a affiliations retrieval request has been received.
    350 
    351         @param request: The publish-subscribe request.
    352         @type request: L{wokkel.pubsub.PubSubRequest}
    353         @return: A deferred that fires with a C{list} of affiliations as
    354                  C{tuple}s of (node identifier as C{unicode}, affiliation state
    355                  as C{str}). The affiliation can be C{'owner'}, C{'publisher'},
    356                  or C{'outcast'}.
    357         @rtype: L{defer.Deferred}
    358         """
    359 
    360 
    361     def create(request):
    362         """
    363         Called when a node creation request has been received.
    364 
    365         @param request: The publish-subscribe request.
    366         @type request: L{wokkel.pubsub.PubSubRequest}
    367         @return: A deferred that fires with a C{unicode} that represents
    368                  the identifier of the new node.
     333        @param requestor: The entity the request originated from.
     334        @type requestor: L{jid.JID}
     335        @param service: The publish-subscribe service entity.
     336        @type service: L{jid.JID}
     337        @param nodeIdentifier: Identifier of the node to request the childs for.
     338        @type nodeIdentifier: L{unicode}
     339        @return: A deferred that fires with a list of child node identifiers.
    369340        @rtype: L{defer.Deferred}
    370341        """
    371342
     
    406377        """
    407378
    408379
    409     def getDefaultConfiguration(request):
     380    def publish(request):
     381        """
     382        Called when a publish request has been received.
     383
     384        @param request: The publish-subscribe request.
     385        @type request: L{wokkel.pubsub.PubSubRequest}
     386        @return: deferred that fires on success.
     387        @rtype: L{defer.Deferred}
     388        """
     389
     390
     391    def subscribe(request):
     392        """
     393        Called when a subscribe request has been received.
     394
     395        @param request: The publish-subscribe request.
     396        @type request: L{wokkel.pubsub.PubSubRequest}
     397        @return: A deferred that fires with a
     398                 L{Subscription<wokkel.pubsub.Subscription>}.
     399        @rtype: L{defer.Deferred}
     400        """
     401
     402
     403    def unsubscribe(request):
     404        """
     405        Called when a subscribe request has been received.
     406
     407        @param request: The publish-subscribe request.
     408        @type request: L{wokkel.pubsub.PubSubRequest}
     409        @return: A deferred that fires with C{None} when unsubscription has
     410                 succeeded.
     411        @rtype: L{defer.Deferred}
     412        """
     413
     414
     415    def subscriptions(request):
     416        """
     417        Called when a subscriptions retrieval request has been received.
     418
     419        @param request: The publish-subscribe request.
     420        @type request: L{wokkel.pubsub.PubSubRequest}
     421        @return: A deferred that fires with a C{list} of subscriptions as
     422                 L{Subscription<wokkel.pubsub.Subscription>}.
     423        @rtype: L{defer.Deferred}
     424        """
     425
     426
     427    def affiliations(request):
     428        """
     429        Called when a affiliations retrieval request has been received.
     430
     431        @param request: The publish-subscribe request.
     432        @type request: L{wokkel.pubsub.PubSubRequest}
     433        @return: A deferred that fires with a C{list} of affiliations as
     434                 C{tuple}s of (node identifier as C{unicode}, affiliation state
     435                 as C{str}). The affiliation can be C{'owner'}, C{'publisher'},
     436                 or C{'outcast'}.
     437        @rtype: L{defer.Deferred}
     438        """
     439
     440
     441    def create(request):
     442        """
     443        Called when a node creation request has been received.
     444
     445        @param request: The publish-subscribe request.
     446        @type request: L{wokkel.pubsub.PubSubRequest}
     447        @return: A deferred that fires with a C{unicode} that represents
     448                 the identifier of the new node.
     449        @rtype: L{defer.Deferred}
     450        """
     451
     452
     453    def default(request):
    410454        """
    411455        Called when a default node configuration request has been received.
    412456
     
    420464        """
    421465
    422466
    423     def getConfiguration(request):
     467    def configureGet(request):
    424468        """
    425469        Called when a node configuration retrieval request has been received.
    426470
     
    433477        """
    434478
    435479
    436     def setConfiguration(request):
     480    def configureSet(request):
    437481        """
    438482        Called when a node configuration change request has been received.
    439483
  • wokkel/pubsub.py

    diff -r cf770fa6bcb7 wokkel/pubsub.py
    a b  
    1313from zope.interface import implements
    1414
    1515from twisted.internet import defer
     16from twisted.python import log
    1617from twisted.words.protocols.jabber import jid, error, xmlstream
    1718from twisted.words.xish import domish
    1819
    1920from wokkel import disco, data_form, generic, shim
    2021from wokkel.subprotocols import IQHandlerMixin, XMPPHandler
    21 from wokkel.iwokkel import IPubSubClient, IPubSubService
     22from wokkel.iwokkel import IPubSubClient, IPubSubService, IPubSubResource
    2223
    2324# Iq get and set XPath queries
    2425IQ_GET = '/iq[@type="get"]'
     
    8485
    8586class Unsupported(PubSubError):
    8687    def __init__(self, feature, text=None):
     88        self.feature = feature
    8789        PubSubError.__init__(self, 'feature-not-implemented',
    8890                                   'unsupported',
    8991                                   feature,
    9092                                   text)
    9193
     94    def __str__(self):
     95        message = PubSubError.__str__(self)
     96        message += ', feature %r' % self.feature
     97        return message
    9298
    9399
    94100class Subscription(object):
     
    235241        'retract': ['node', 'itemIdentifiers'],
    236242        'purge': ['node'],
    237243        'delete': ['node'],
    238         'affiliationsGet': [],
     244        'affiliationsGet': ['nodeOrEmpty'],
    239245        'affiliationsSet': [],
    240         'subscriptionsGet': [],
     246        'subscriptionsGet': ['nodeOrEmpty'],
    241247        'subscriptionsSet': [],
    242248    }
    243249
     
    806812            '/*': '_onPubSubRequest',
    807813            }
    808814
     815    hideNodes = False
    809816
    810     def __init__(self):
    811         self.discoIdentity = {'category': 'pubsub',
    812                               'type': 'generic',
    813                               'name': 'Generic Publish-Subscribe Service'}
    814 
    815         self.pubSubFeatures = []
     817    def __init__(self, resource):
     818        self.resource = resource
    816819
    817820
    818821    def connectionMade(self):
     
    820823
    821824
    822825    def getDiscoInfo(self, requestor, target, nodeIdentifier):
    823         info = []
    824 
    825         if not nodeIdentifier:
    826             category, idType, name = self.discoIdentity
    827             info.append(disco.DiscoIdentity(category, idType, name))
    828 
    829             info.append(disco.DiscoFeature(disco.NS_DISCO_ITEMS))
    830             info.extend([disco.DiscoFeature("%s#%s" % (NS_PUBSUB, feature))
    831                          for feature in self.pubSubFeatures])
    832 
    833         def toInfo(nodeInfo):
     826        def toInfo(nodeInfo, info):
    834827            if not nodeInfo:
    835                 return
     828                return info
    836829
    837830            (nodeType, metaData) = nodeInfo['type'], nodeInfo['meta-data']
    838831            info.append(disco.DiscoIdentity('pubsub', nodeType))
     
    852845
    853846                info.append(form)
    854847
    855         d = self.getNodeInfo(requestor, target, nodeIdentifier or '')
    856         d.addCallback(toInfo)
    857         d.addBoth(lambda result: info)
     848            return info
     849
     850        info = []
     851
     852        request = PubSubRequest('discoInfo')
     853        resource = self.resource.locateResource(request)
     854
     855        if not nodeIdentifier:
     856            info.append(resource.discoIdentity)
     857            info.append(disco.DiscoFeature(disco.NS_DISCO_ITEMS))
     858            info.extend([disco.DiscoFeature("%s#%s" % (NS_PUBSUB, feature))
     859                         for feature in resource.features])
     860
     861        d = resource.getInfo(requestor, target, nodeIdentifier or '')
     862        d.addCallback(toInfo, info)
     863        d.addErrback(log.err)
    858864        return d
    859865
    860866
    861867    def getDiscoItems(self, requestor, target, nodeIdentifier):
    862         if nodeIdentifier or self.hideNodes:
     868        request = PubSubRequest('discoInfo')
     869        resource = self.resource.locateResource(request)
     870
     871        if self.hideNodes:
    863872            return defer.succeed([])
    864873
    865         d = self.getNodes(requestor, target)
     874        d = resource.getNodes(requestor, target, nodeIdentifier)
    866875        d.addCallback(lambda nodes: [disco.DiscoItem(target, node)
    867876                                     for node in nodes])
    868877        return d
     
    870879
    871880    def _onPubSubRequest(self, iq):
    872881        request = PubSubRequest.fromElement(iq)
    873         handler = getattr(self, '_on_%s' % request.verb)
    874         return handler(request)
     882        resource = self.resource.locateResource(request)
    875883
     884        # Preprocess the request, knowing the handling resource
     885        try:
     886            preProcessor = getattr(self, '_preProcess_%s' % request.verb)
     887        except AttributeError:
     888            pass
     889        else:
     890            request = preProcessor(resource, request)
     891            if request is None:
     892                return defer.succeed(None)
    876893
    877     def _on_publish(self, request):
    878         return self.publish(request)
     894        # Process the request itself,
     895        try:
     896            handler = getattr(resource, request.verb)
     897        except AttributeError:
     898            # fix lookup feature
     899            text = "Request verb: %s" % request.verb
     900            return defer.fail(Unsupported('', text))
    879901
     902        d = handler(request)
    880903
    881     def _on_subscribe(self, request):
     904        # If needed, translate the result into a response
     905        try:
     906            cb = getattr(self, '_toResponse_%s' % request.verb)
     907        except AttributeError:
     908            pass
     909        else:
     910            d.addCallback(cb, resource, request)
    882911
    883         def toResponse(result):
    884             response = domish.Element((NS_PUBSUB, "pubsub"))
    885             subscription = response.addElement("subscription")
    886             if result.nodeIdentifier:
    887                 subscription["node"] = result.nodeIdentifier
    888             subscription["jid"] = result.subscriber.full()
    889             subscription["subscription"] = result.state
    890             return response
    891 
    892         d = self.subscribe(request)
    893         d.addCallback(toResponse)
    894912        return d
    895913
    896914
    897     def _on_unsubscribe(self, request):
    898         return self.unsubscribe(request)
     915    def _toResponse_subscribe(self, result, resource, request):
     916        response = domish.Element((NS_PUBSUB, "pubsub"))
     917        subscription = response.addElement("subscription")
     918        if result.nodeIdentifier:
     919            subscription["node"] = result.nodeIdentifier
     920        subscription["jid"] = result.subscriber.full()
     921        subscription["subscription"] = result.state
     922        return response
    899923
    900924
    901     def _on_optionsGet(self, request):
    902         raise Unsupported('subscription-options')
     925    def _toResponse_subscriptions(self, result, resource, request):
     926        response = domish.Element((NS_PUBSUB, 'pubsub'))
     927        subscriptions = response.addElement('subscriptions')
     928        for subscription in result:
     929            item = subscriptions.addElement('subscription')
     930            item['node'] = subscription.nodeIdentifier
     931            item['jid'] = subscription.subscriber.full()
     932            item['subscription'] = subscription.state
     933        return response
    903934
    904935
    905     def _on_optionsSet(self, request):
    906         raise Unsupported('subscription-options')
     936    def _toResponse_affiliations(self, result, resource, request):
     937        response = domish.Element((NS_PUBSUB, 'pubsub'))
     938        affiliations = response.addElement('affiliations')
    907939
     940        for nodeIdentifier, affiliation in result:
     941            item = affiliations.addElement('affiliation')
     942            item['node'] = nodeIdentifier
     943            item['affiliation'] = affiliation
    908944
    909     def _on_subscriptions(self, request):
     945        return response
    910946
    911         def toResponse(result):
     947
     948    def _toResponse_create(self, result, resource, request):
     949        if not request.nodeIdentifier or request.nodeIdentifier != result:
    912950            response = domish.Element((NS_PUBSUB, 'pubsub'))
    913             subscriptions = response.addElement('subscriptions')
    914             for subscription in result:
    915                 item = subscriptions.addElement('subscription')
    916                 item['node'] = subscription.nodeIdentifier
    917                 item['jid'] = subscription.subscriber.full()
    918                 item['subscription'] = subscription.state
     951            create = response.addElement('create')
     952            create['node'] = result
    919953            return response
    920 
    921         d = self.subscriptions(request)
    922         d.addCallback(toResponse)
    923         return d
    924 
    925 
    926     def _on_affiliations(self, request):
    927 
    928         def toResponse(result):
    929             response = domish.Element((NS_PUBSUB, 'pubsub'))
    930             affiliations = response.addElement('affiliations')
    931 
    932             for nodeIdentifier, affiliation in result:
    933                 item = affiliations.addElement('affiliation')
    934                 item['node'] = nodeIdentifier
    935                 item['affiliation'] = affiliation
    936 
    937             return response
    938 
    939         d = self.affiliations(request)
    940         d.addCallback(toResponse)
    941         return d
    942 
    943 
    944     def _on_create(self, request):
    945 
    946         def toResponse(result):
    947             if not request.nodeIdentifier or request.nodeIdentifier != result:
    948                 response = domish.Element((NS_PUBSUB, 'pubsub'))
    949                 create = response.addElement('create')
    950                 create['node'] = result
    951                 return response
    952             else:
    953                 return None
    954 
    955         d = self.create(request)
    956         d.addCallback(toResponse)
    957         return d
     954        else:
     955            return None
    958956
    959957
    960958    def _makeFields(self, options, values):
     
    973971        return fields
    974972
    975973
    976     def _formFromConfiguration(self, values):
    977         options = self.getConfigurationOptions()
     974    def _formFromConfiguration(self, resource, values):
     975        options = resource.getConfigurationOptions()
    978976        fields = self._makeFields(options, values)
    979977        form = data_form.Form(formType="form",
    980978                              formNamespace=NS_PUBSUB_NODE_CONFIG,
     
    983981        return form
    984982
    985983
    986     def _checkConfiguration(self, values):
    987         options = self.getConfigurationOptions()
     984    def _checkConfiguration(self, resource, values):
     985        options = resource.getConfigurationOptions()
    988986        processedValues = {}
    989987
    990988        for key, value in values.iteritems():
     
    10081006        return processedValues
    10091007
    10101008
    1011     def _on_default(self, request):
     1009    def _preProcess_default(self, resource, request):
     1010        if request.nodeType not in ('leaf', 'collection'):
     1011            raise error.StanzaError('not-acceptable')
     1012        else:
     1013            return request
    10121014
    1013         def toResponse(options):
    1014             response = domish.Element((NS_PUBSUB_OWNER, "pubsub"))
    1015             default = response.addElement("default")
    1016             default.addChild(self._formFromConfiguration(options).toElement())
    1017             return response
    10181015
    1019         if request.nodeType not in ('leaf', 'collection'):
    1020             return defer.fail(error.StanzaError('not-acceptable'))
     1016    def _toResponse_default(self, options, resource, request):
     1017        response = domish.Element((NS_PUBSUB_OWNER, "pubsub"))
     1018        default = response.addElement("default")
     1019        form = self._formFromConfiguration(resource, options)
     1020        default.addChild(form.toElement())
     1021        return response
    10211022
    1022         d = self.getDefaultConfiguration(request)
    1023         d.addCallback(toResponse)
    1024         return d
    10251023
     1024    def _toResponse_configureGet(self, options, resource, request):
     1025        response = domish.Element((NS_PUBSUB_OWNER, "pubsub"))
     1026        configure = response.addElement("configure")
     1027        form = self._formFromConfiguration(resource, options)
     1028        configure.addChild(form.toElement())
    10261029
    1027     def _on_configureGet(self, request):
    1028         def toResponse(options):
    1029             response = domish.Element((NS_PUBSUB_OWNER, "pubsub"))
    1030             configure = response.addElement("configure")
    1031             form = self._formFromConfiguration(options)
    1032             configure.addChild(form.toElement())
     1030        if request.nodeIdentifier:
     1031            configure["node"] = request.nodeIdentifier
    10331032
    1034             if request.nodeIdentifier:
    1035                 configure["node"] = request.nodeIdentifier
     1033        return response
    10361034
    1037             return response
    10381035
    1039         d = self.getConfiguration(request)
    1040         d.addCallback(toResponse)
    1041         return d
    1042 
    1043 
    1044     def _on_configureSet(self, request):
     1036    def _preProcess_configureSet(self, resource, request):
    10451037        if request.options:
    1046             request.options = self._checkConfiguration(request.options)
    1047             return self.setConfiguration(request)
     1038            request.options = self._checkConfiguration(resource,
     1039                                                       request.options)
     1040            return request
    10481041        else:
    10491042            return None
    10501043
    10511044
     1045    def _toResponse_items(self, result, resource, request):
     1046        response = domish.Element((NS_PUBSUB, 'pubsub'))
     1047        items = response.addElement('items')
     1048        items["node"] = request.nodeIdentifier
    10521049
    1053     def _on_items(self, request):
     1050        for item in result:
     1051            items.addChild(item)
    10541052
    1055         def toResponse(result):
    1056             response = domish.Element((NS_PUBSUB, 'pubsub'))
    1057             items = response.addElement('items')
    1058             items["node"] = request.nodeIdentifier
     1053        return response
    10591054
    1060             for item in result:
    1061                 items.addChild(item)
    1062 
    1063             return response
    1064 
    1065         d = self.items(request)
    1066         d.addCallback(toResponse)
    1067         return d
    1068 
    1069 
    1070     def _on_retract(self, request):
    1071         return self.retract(request)
    1072 
    1073 
    1074     def _on_purge(self, request):
    1075         return self.purge(request)
    1076 
    1077 
    1078     def _on_delete(self, request):
    1079         return self.delete(request)
    1080 
    1081 
    1082     def _on_affiliationsGet(self, iq):
    1083         raise Unsupported('modify-affiliations')
    1084 
    1085 
    1086     def _on_affiliationsSet(self, iq):
    1087         raise Unsupported('modify-affiliations')
    1088 
    1089 
    1090     def _on_subscriptionsGet(self, iq):
    1091         raise Unsupported('manage-subscriptions')
    1092 
    1093 
    1094     def _on_subscriptionsSet(self, iq):
    1095         raise Unsupported('manage-subscriptions')
    1096 
    1097     # public methods
    10981055
    10991056    def _createNotification(self, eventType, service, nodeIdentifier,
    11001057                                  subscriber, subscriptions=None):
     
    11181075
    11191076        return message
    11201077
     1078    # public methods
     1079
    11211080    def notifyPublish(self, service, nodeIdentifier, notifications):
    11221081        for subscriber, subscriptions, items in notifications:
    11231082            message = self._createNotification('items', service,
     
    11391098            self.send(message)
    11401099
    11411100
    1142     def getNodeInfo(self, requestor, service, nodeIdentifier):
    1143         return None
    11441101
     1102class PubSubResource(object):
    11451103
    1146     def getNodes(self, requestor, service):
    1147         return []
     1104    implements(IPubSubResource)
    11481105
     1106    features = []
     1107    discoIdentity = disco.DiscoIdentity('pubsub',
     1108                                        'service',
     1109                                        'Publish-Subscribe Service')
    11491110
    1150     def publish(self, request):
    1151         raise Unsupported('publish')
    11521111
     1112    def locateResource(self, request):
     1113        return self
    11531114
    1154     def subscribe(self, request):
    1155         raise Unsupported('subscribe')
    11561115
     1116    def getInfo(self, requestor, service, nodeIdentifier):
     1117        return defer.succeed(None)
    11571118
    1158     def unsubscribe(self, request):
    1159         raise Unsupported('subscribe')
    11601119
    1161 
    1162     def subscriptions(self, request):
    1163         raise Unsupported('retrieve-subscriptions')
    1164 
    1165 
    1166     def affiliations(self, request):
    1167         raise Unsupported('retrieve-affiliations')
    1168 
    1169 
    1170     def create(self, request):
    1171         raise Unsupported('create-nodes')
     1120    def getNodes(self, requestor, service, nodeIdentifier):
     1121        return defer.succeed([])
    11721122
    11731123
    11741124    def getConfigurationOptions(self):
    11751125        return {}
    11761126
    11771127
    1178     def getDefaultConfiguration(self, request):
    1179         raise Unsupported('retrieve-default')
     1128    def publish(self, request):
     1129        return defer.fail(Unsupported('publish'))
    11801130
    11811131
    1182     def getConfiguration(self, request):
    1183         raise Unsupported('config-node')
     1132    def subscribe(self, request):
     1133        return defer.fail(Unsupported('subscribe'))
    11841134
    11851135
    1186     def setConfiguration(self, request):
    1187         raise Unsupported('config-node')
     1136    def unsubscribe(self, request):
     1137        return defer.fail(Unsupported('subscribe'))
     1138
     1139
     1140    def subscriptions(self, request):
     1141        return defer.fail(Unsupported('retrieve-subscriptions'))
     1142
     1143
     1144    def affiliations(self, request):
     1145        return defer.fail(Unsupported('retrieve-affiliations'))
     1146
     1147
     1148    def create(self, request):
     1149        return defer.fail(Unsupported('create-nodes'))
     1150
     1151
     1152    def default(self, request):
     1153        return defer.fail(Unsupported('retrieve-default'))
     1154
     1155
     1156    def configureGet(self, request):
     1157        return defer.fail(Unsupported('config-node'))
     1158
     1159
     1160    def configureSet(self, request):
     1161        return defer.fail(Unsupported('config-node'))
    11881162
    11891163
    11901164    def items(self, request):
    1191         raise Unsupported('retrieve-items')
     1165        return defer.fail(Unsupported('retrieve-items'))
    11921166
    11931167
    11941168    def retract(self, request):
    1195         raise Unsupported('retract-items')
     1169        return defer.fail(Unsupported('retract-items'))
    11961170
    11971171
    11981172    def purge(self, request):
    1199         raise Unsupported('purge-nodes')
     1173        return defer.fail(Unsupported('purge-nodes'))
    12001174
    12011175
    12021176    def delete(self, request):
    1203         raise Unsupported('delete-nodes')
     1177        return defer.fail(Unsupported('delete-nodes'))
     1178
     1179
     1180    def affiliationsGet(self, request):
     1181        return defer.fail(Unsupported('modify-affiliations'))
     1182
     1183
     1184    def affiliationsSet(self, request):
     1185        return defer.fail(Unsupported('modify-affiliations'))
     1186
     1187
     1188    def subscriptionsGet(self, request):
     1189        return defer.fail(Unsupported('manage-subscriptions'))
     1190
     1191
     1192    def subscriptionsSet(self, request):
     1193        return defer.fail(Unsupported('manage-subscriptions'))
  • wokkel/test/test_pubsub.py

    diff -r cf770fa6bcb7 wokkel/test/test_pubsub.py
    a b  
    12221222
    12231223    def setUp(self):
    12241224        self.stub = XmlStreamStub()
    1225         self.service = pubsub.PubSubService()
     1225        self.resource = pubsub.PubSubResource()
     1226        self.service = pubsub.PubSubService(self.resource)
    12261227        self.service.send = self.stub.xmlstream.send
    12271228
    12281229    def test_interface(self):
     
    12601261        Test getDiscoInfo calls getNodeInfo and returns some minimal info.
    12611262        """
    12621263        def cb(info):
    1263             self.assertEqual(2, len(info))
     1264            discoInfo = disco.DiscoInfo()
     1265            for item in info:
     1266                discoInfo.append(item)
     1267            self.assertIn(('pubsub', 'service'), discoInfo.identities)
     1268            self.assertIn(disco.NS_DISCO_ITEMS, discoInfo.features)
    12641269
    1265         def getNodeInfo(requestor, target, nodeIdentifier):
    1266             return defer.succeed(None)
    1267 
    1268         self.service.getNodeInfo = getNodeInfo
    12691270        d = self.service.getDiscoInfo(JID('user@example.org/home'),
    12701271                                      JID('pubsub.example.org'), '')
    12711272        d.addCallback(cb)
     
    12821283                discoInfo.append(item)
    12831284            self.assertIn(('pubsub', 'collection'), discoInfo.identities)
    12841285
    1285         def getNodeInfo(requestor, target, nodeIdentifier):
     1286        def getInfo(requestor, target, nodeIdentifier):
    12861287            return defer.succeed({'type': 'collection',
    12871288                                  'meta-data': {}})
    12881289
    1289         self.service.getNodeInfo = getNodeInfo
     1290        self.resource.getInfo = getInfo
    12901291        d = self.service.getDiscoInfo(JID('user@example.org/home'),
    12911292                                      JID('pubsub.example.org'), '')
    12921293        d.addCallback(cb)
     
    13071308            form = discoInfo.extensions[NS_PUBSUB_META_DATA]
    13081309            self.assertIn('pubsub#node_type', form.fields)
    13091310
    1310         def getNodeInfo(requestor, target, nodeIdentifier):
     1311        def getInfo(requestor, target, nodeIdentifier):
    13111312            metaData = [{'var': 'pubsub#persist_items',
    13121313                         'label': 'Persist items to storage',
    13131314                         'value': True}]
    13141315            return defer.succeed({'type': 'leaf', 'meta-data': metaData})
    13151316
    1316         self.service.getNodeInfo = getNodeInfo
     1317        self.resource.getInfo = getInfo
    13171318        d = self.service.getDiscoInfo(JID('user@example.org/home'),
    13181319                                      JID('pubsub.example.org'), '')
    13191320        d.addCallback(cb)
    13201321        return d
    13211322
    13221323
    1323     def test_onPublish(self):
     1324    def test_getDiscoInfoResourceFeatures(self):
     1325        """
     1326        Test getDiscoInfo with the resource features.
     1327        """
     1328        def cb(info):
     1329            discoInfo = disco.DiscoInfo()
     1330            for item in info:
     1331                discoInfo.append(item)
     1332            self.assertIn('http://jabber.org/protocol/pubsub#publish',
     1333                          discoInfo.features)
     1334
     1335        self.resource.features = ['publish']
     1336        d = self.service.getDiscoInfo(JID('user@example.org/home'),
     1337                                      JID('pubsub.example.org'), '')
     1338        d.addCallback(cb)
     1339        return d
     1340
     1341
     1342    def test_getDiscoItemsRoot(self):
     1343        """
     1344        Test getDiscoItems on the root node.
     1345        """
     1346        def getNodes(requestor, service, nodeIdentifier):
     1347            return defer.succeed(['node1', 'node2'])
     1348
     1349        def cb(items):
     1350            self.assertEqual(2, len(items))
     1351            item1, item2 = items
     1352
     1353            self.assertEqual(JID('pubsub.example.org'), item1.entity)
     1354            self.assertEqual('node1', item1.nodeIdentifier)
     1355
     1356            self.assertEqual(JID('pubsub.example.org'), item2.entity)
     1357            self.assertEqual('node2', item2.nodeIdentifier)
     1358
     1359        self.resource.getNodes = getNodes
     1360        d = self.service.getDiscoItems(JID('user@example.org/home'),
     1361                                       JID('pubsub.example.org'),
     1362                                       '')
     1363        d.addCallback(cb)
     1364        return d
     1365
     1366
     1367    def test_getDiscoItemsRootHideNodes(self):
     1368        """
     1369        Test getDiscoItems on the root node.
     1370        """
     1371        def getNodes(requestor, service, nodeIdentifier):
     1372            raise Exception("Unexpected call to getNodes")
     1373
     1374        def cb(items):
     1375            self.assertEqual([], items)
     1376
     1377        self.service.hideNodes = True
     1378        self.resource.getNodes = getNodes
     1379        d = self.service.getDiscoItems(JID('user@example.org/home'),
     1380                                       JID('pubsub.example.org'),
     1381                                       '')
     1382        d.addCallback(cb)
     1383        return d
     1384
     1385
     1386    def test_getDiscoItemsNonRoot(self):
     1387        """
     1388        Test getDiscoItems on a non-root node.
     1389        """
     1390        def getNodes(requestor, service, nodeIdentifier):
     1391            return defer.succeed(['node1', 'node2'])
     1392
     1393        def cb(items):
     1394            self.assertEqual(2, len(items))
     1395
     1396        self.resource.getNodes = getNodes
     1397        d = self.service.getDiscoItems(JID('user@example.org/home'),
     1398                                       JID('pubsub.example.org'),
     1399                                       'test')
     1400        d.addCallback(cb)
     1401        return d
     1402
     1403
     1404    def test_on_publish(self):
    13241405        """
    13251406        A publish request should result in L{PubSubService.publish} being
    13261407        called.
     
    13381419        def publish(request):
    13391420            return defer.succeed(None)
    13401421
    1341         self.service.publish = publish
    1342         verify.verifyObject(iwokkel.IPubSubService, self.service)
     1422        self.resource.publish = publish
     1423        verify.verifyObject(iwokkel.IPubSubResource, self.resource)
    13431424        return self.handleRequest(xml)
    13441425
    13451426
    1346     def test_onSubscribe(self):
     1427    def test_on_subscribe(self):
    13471428        """
    13481429        A successful subscription should return the current subscription.
    13491430        """
     
    13711452            self.assertEqual('user@example.org/Home', subscription['jid'])
    13721453            self.assertEqual('subscribed', subscription['subscription'])
    13731454
    1374         self.service.subscribe = subscribe
    1375         verify.verifyObject(iwokkel.IPubSubService, self.service)
     1455        self.resource.subscribe = subscribe
     1456        verify.verifyObject(iwokkel.IPubSubResource, self.resource)
    13761457        d = self.handleRequest(xml)
    13771458        d.addCallback(cb)
    13781459        return d
    13791460
    13801461
    1381     def test_onSubscribeEmptyNode(self):
     1462    def test_on_subscribeEmptyNode(self):
    13821463        """
    13831464        A successful subscription on root node should return no node attribute.
    13841465        """
     
    14001481        def cb(element):
    14011482            self.assertFalse(element.subscription.hasAttribute('node'))
    14021483
    1403         self.service.subscribe = subscribe
    1404         verify.verifyObject(iwokkel.IPubSubService, self.service)
     1484        self.resource.subscribe = subscribe
     1485        verify.verifyObject(iwokkel.IPubSubResource, self.resource)
    14051486        d = self.handleRequest(xml)
    14061487        d.addCallback(cb)
    14071488        return d
    14081489
    14091490
    1410     def test_onUnsubscribe(self):
     1491    def test_on_unsubscribe(self):
    14111492        """
    14121493        A successful unsubscription should return an empty response.
    14131494        """
     
    14271508        def cb(element):
    14281509            self.assertIdentical(None, element)
    14291510
    1430         self.service.unsubscribe = unsubscribe
    1431         verify.verifyObject(iwokkel.IPubSubService, self.service)
     1511        self.resource.unsubscribe = unsubscribe
     1512        verify.verifyObject(iwokkel.IPubSubResource, self.resource)
    14321513        d = self.handleRequest(xml)
    14331514        d.addCallback(cb)
    14341515        return d
    14351516
    14361517
    1437     def test_onOptionsGet(self):
     1518    def test_on_optionsGet(self):
    14381519        """
    14391520        Getting subscription options is not supported.
    14401521        """
     
    14591540        return d
    14601541
    14611542
    1462     def test_onOptionsSet(self):
     1543    def test_on_optionsSet(self):
    14631544        """
    14641545        Setting subscription options is not supported.
    14651546        """
     
    14911572        return d
    14921573
    14931574
    1494     def test_onSubscriptions(self):
     1575    def test_on_subscriptions(self):
    14951576        """
    14961577        A subscriptions request should result in
    14971578        L{PubSubService.subscriptions} being called and the result prepared
     
    15071588        </iq>
    15081589        """
    15091590
     1591        def subscriptions(request):
     1592            subscription = pubsub.Subscription('test', JID('user@example.org'),
     1593                                               'subscribed')
     1594            return defer.succeed([subscription])
     1595
    15101596        def cb(element):
    15111597            self.assertEqual('pubsub', element.name)
    15121598            self.assertEqual(NS_PUBSUB, element.uri)
     
    15201606            self.assertEqual('test', subscription['node'])
    15211607            self.assertEqual('subscribed', subscription['subscription'])
    15221608
    1523 
    1524         def subscriptions(request):
    1525             subscription = pubsub.Subscription('test', JID('user@example.org'),
    1526                                                'subscribed')
    1527             return defer.succeed([subscription])
    1528 
    1529         self.service.subscriptions = subscriptions
    1530         verify.verifyObject(iwokkel.IPubSubService, self.service)
     1609        self.resource.subscriptions = subscriptions
     1610        verify.verifyObject(iwokkel.IPubSubResource, self.resource)
    15311611        d = self.handleRequest(xml)
    15321612        d.addCallback(cb)
    15331613        return d
    15341614
    15351615
    1536     def test_onAffiliations(self):
     1616    def test_on_affiliations(self):
    15371617        """
    15381618        A subscriptions request should result in
    15391619        L{PubSubService.affiliations} being called and the result prepared
     
    15491629        </iq>
    15501630        """
    15511631
     1632        def affiliations(request):
     1633            affiliation = ('test', 'owner')
     1634            return defer.succeed([affiliation])
     1635
    15521636        def cb(element):
    15531637            self.assertEqual('pubsub', element.name)
    15541638            self.assertEqual(NS_PUBSUB, element.uri)
     
    15611645            self.assertEqual('test', affiliation['node'])
    15621646            self.assertEqual('owner', affiliation['affiliation'])
    15631647
    1564 
    1565         def affiliations(request):
    1566             affiliation = ('test', 'owner')
    1567             return defer.succeed([affiliation])
    1568 
    1569         self.service.affiliations = affiliations
    1570         verify.verifyObject(iwokkel.IPubSubService, self.service)
     1648        self.resource.affiliations = affiliations
     1649        verify.verifyObject(iwokkel.IPubSubResource, self.resource)
    15711650        d = self.handleRequest(xml)
    15721651        d.addCallback(cb)
    15731652        return d
    15741653
    15751654
    1576     def test_onCreate(self):
     1655    def test_on_create(self):
    15771656        """
    15781657        Replies to create node requests don't return the created node.
    15791658        """
     
    15931672        def cb(element):
    15941673            self.assertIdentical(None, element)
    15951674
    1596         self.service.create = create
    1597         verify.verifyObject(iwokkel.IPubSubService, self.service)
     1675        self.resource.create = create
     1676        verify.verifyObject(iwokkel.IPubSubResource, self.resource)
    15981677        d = self.handleRequest(xml)
    15991678        d.addCallback(cb)
    16001679        return d
    16011680
    16021681
    1603     def test_onCreateChanged(self):
     1682    def test_on_createChanged(self):
    16041683        """
    16051684        Replies to create node requests return the created node if changed.
    16061685        """
     
    16241703            self.assertEqual(u'myrenamednode',
    16251704                             element.create.getAttribute('node'))
    16261705
    1627         self.service.create = create
    1628         verify.verifyObject(iwokkel.IPubSubService, self.service)
     1706        self.resource.create = create
     1707        verify.verifyObject(iwokkel.IPubSubResource, self.resource)
    16291708        d = self.handleRequest(xml)
    16301709        d.addCallback(cb)
    16311710        return d
    16321711
    16331712
    1634     def test_onCreateInstant(self):
     1713    def test_on_createInstant(self):
    16351714        """
    16361715        Replies to create instant node requests return the created node.
    16371716        """
     
    16541733            self.assertEqual(NS_PUBSUB, element.create.uri)
    16551734            self.assertEqual(u'random', element.create.getAttribute('node'))
    16561735
    1657         self.service.create = create
    1658         verify.verifyObject(iwokkel.IPubSubService, self.service)
     1736        self.resource.create = create
     1737        verify.verifyObject(iwokkel.IPubSubResource, self.resource)
    16591738        d = self.handleRequest(xml)
    16601739        d.addCallback(cb)
    16611740        return d
    16621741
    16631742
    1664     def test_onDefault(self):
     1743    def test_on_default(self):
    16651744        """
    16661745        A default request should result in
    16671746        L{PubSubService.getDefaultConfiguration} being called.
     
    16861765                     "label": "Deliver payloads with event notifications"}
    16871766                }
    16881767
    1689         def getDefaultConfiguration(request):
     1768        def default(request):
    16901769            return defer.succeed({})
    16911770
    16921771        def cb(element):
     
    16961775            form = data_form.Form.fromElement(element.default.x)
    16971776            self.assertEqual(NS_PUBSUB_CONFIG, form.formNamespace)
    16981777
    1699         self.service.getConfigurationOptions = getConfigurationOptions
    1700         self.service.getDefaultConfiguration = getDefaultConfiguration
    1701         verify.verifyObject(iwokkel.IPubSubService, self.service)
     1778        self.resource.getConfigurationOptions = getConfigurationOptions
     1779        self.resource.default = default
     1780        verify.verifyObject(iwokkel.IPubSubResource, self.resource)
    17021781        d = self.handleRequest(xml)
    17031782        d.addCallback(cb)
    17041783        return d
    17051784
    17061785
    1707     def test_onDefaultCollection(self):
     1786    def test_on_defaultCollection(self):
    17081787        """
    17091788        Responses to default requests should depend on passed node type.
    17101789        """
     
    17351814                     "label": "Deliver payloads with event notifications"}
    17361815                }
    17371816
    1738         def getDefaultConfiguration(request):
     1817        def default(request):
    17391818            return defer.succeed({})
    17401819
    1741         self.service.getConfigurationOptions = getConfigurationOptions
    1742         self.service.getDefaultConfiguration = getDefaultConfiguration
    1743         verify.verifyObject(iwokkel.IPubSubService, self.service)
     1820        self.resource.getConfigurationOptions = getConfigurationOptions
     1821        self.resource.default = default
     1822        verify.verifyObject(iwokkel.IPubSubResource, self.resource)
    17441823        return self.handleRequest(xml)
    17451824
    17461825
    1747     def test_onDefaultUnknownNodeType(self):
     1826    def test_on_defaultUnknownNodeType(self):
    17481827        """
    17491828        A default request should result in
    1750         L{PubSubService.getDefaultConfiguration} being called.
     1829        L{PubSubResource.default} being called.
    17511830        """
    17521831
    17531832        xml = """
     
    17691848        </iq>
    17701849        """
    17711850
    1772         def getDefaultConfiguration(request):
     1851        def default(request):
    17731852            self.fail("Unexpected call to getConfiguration")
    17741853
    17751854        def cb(result):
    17761855            self.assertEquals('not-acceptable', result.condition)
    17771856
    1778         self.service.getDefaultConfiguration = getDefaultConfiguration
    1779         verify.verifyObject(iwokkel.IPubSubService, self.service)
     1857        self.resource.default = default
     1858        verify.verifyObject(iwokkel.IPubSubResource, self.resource)
    17801859        d = self.handleRequest(xml)
    17811860        self.assertFailure(d, error.StanzaError)
    17821861        d.addCallback(cb)
    17831862        return d
    17841863
    17851864
    1786     def test_onConfigureGet(self):
     1865    def test_on_configureGet(self):
    17871866        """
    1788         On a node configuration get request L{PubSubService.getConfiguration}
    1789         is called and results in a data form with the configuration.
     1867        On a node configuration get
     1868        requestL{PubSubResource.configureGet} is called and results in a
     1869        data form with the configuration.
    17901870        """
    17911871
    17921872        xml = """
     
    18111891                     "label": "Owner of the node"}
    18121892                }
    18131893
    1814         def getConfiguration(request):
     1894        def configureGet(request):
    18151895            return defer.succeed({'pubsub#deliver_payloads': '0',
    18161896                                  'pubsub#persist_items': '1',
    18171897                                  'pubsub#owner': JID('user@example.org'),
     
    18451925
    18461926            self.assertNotIn('x-myfield', fields)
    18471927
    1848 
    1849         self.service.getConfigurationOptions = getConfigurationOptions
    1850         self.service.getConfiguration = getConfiguration
    1851         verify.verifyObject(iwokkel.IPubSubService, self.service)
     1928        self.resource.getConfigurationOptions = getConfigurationOptions
     1929        self.resource.configureGet = configureGet
     1930        verify.verifyObject(iwokkel.IPubSubResource, self.resource)
    18521931        d = self.handleRequest(xml)
    18531932        d.addCallback(cb)
    18541933        return d
    18551934
    18561935
    1857     def test_onConfigureSet(self):
     1936    def test_on_configureSet(self):
    18581937        """
    18591938        On a node configuration set request the Data Form is parsed and
    1860         L{PubSubService.setConfiguration} is called with the passed options.
     1939        L{PubSubResource.configureSet} is called with the passed options.
    18611940        """
    18621941
    18631942        xml = """
     
    18871966                     "label": "Deliver payloads with event notifications"}
    18881967                }
    18891968
    1890         def setConfiguration(request):
     1969        def configureSet(request):
    18911970            self.assertEqual({'pubsub#deliver_payloads': False,
    18921971                              'pubsub#persist_items': True}, request.options)
    18931972            return defer.succeed(None)
    18941973
    1895         self.service.getConfigurationOptions = getConfigurationOptions
    1896         self.service.setConfiguration = setConfiguration
    1897         verify.verifyObject(iwokkel.IPubSubService, self.service)
     1974        self.resource.getConfigurationOptions = getConfigurationOptions
     1975        self.resource.configureSet = configureSet
     1976        verify.verifyObject(iwokkel.IPubSubResource, self.resource)
    18981977        return self.handleRequest(xml)
    18991978
    19001979
    1901     def test_onConfigureSetCancel(self):
     1980    def test_on_configureSetCancel(self):
    19021981        """
    1903         The node configuration is cancelled, L{PubSubService.setConfiguration}
    1904         not called.
     1982        The node configuration is cancelled,
     1983        L{PubSubResource.configureSet} not called.
    19051984        """
    19061985
    19071986        xml = """
     
    19191998        </iq>
    19201999        """
    19212000
    1922         def setConfiguration(request):
     2001        def configureSet(request):
    19232002            self.fail("Unexpected call to setConfiguration")
    19242003
    1925         self.service.setConfiguration = setConfiguration
    1926         verify.verifyObject(iwokkel.IPubSubService, self.service)
     2004        self.resource.configureSet = configureSet
     2005        verify.verifyObject(iwokkel.IPubSubResource, self.resource)
    19272006        return self.handleRequest(xml)
    19282007
    19292008
    1930     def test_onConfigureSetIgnoreUnknown(self):
     2009    def test_on_configureSetIgnoreUnknown(self):
    19312010        """
    19322011        On a node configuration set request unknown fields should be ignored.
    19332012        """
     
    19592038                     "label": "Deliver payloads with event notifications"}
    19602039                }
    19612040
    1962         def setConfiguration(request):
     2041        def configureSet(request):
    19632042            self.assertEquals(['pubsub#deliver_payloads'],
    19642043                              request.options.keys())
    19652044
    1966         self.service.getConfigurationOptions = getConfigurationOptions
    1967         self.service.setConfiguration = setConfiguration
    1968         verify.verifyObject(iwokkel.IPubSubService, self.service)
     2045        self.resource.getConfigurationOptions = getConfigurationOptions
     2046        self.resource.configureSet = configureSet
     2047        verify.verifyObject(iwokkel.IPubSubResource, self.resource)
    19692048        return self.handleRequest(xml)
    19702049
    19712050
    1972     def test_onConfigureSetBadFormType(self):
     2051    def test_on_configureSetBadFormType(self):
    19732052        """
    19742053        On a node configuration set request unknown fields should be ignored.
    19752054        """
     
    20002079        return d
    20012080
    20022081
    2003     def test_onItems(self):
     2082    def test_on_items(self):
    20042083        """
    20052084        On a items request, return all items for the given node.
    20062085        """
     
    20262105            self.assertEqual(NS_PUBSUB, item.uri)
    20272106            self.assertEqual('current', item['id'])
    20282107
    2029         self.service.items = items
     2108        self.resource.items = items
     2109        verify.verifyObject(iwokkel.IPubSubResource, self.resource)
    20302110        d = self.handleRequest(xml)
    20312111        d.addCallback(cb)
    20322112        return d
    20332113
    20342114
    2035     def test_onRetract(self):
     2115    def test_on_retract(self):
    20362116        """
    2037         A retract request should result in L{PubSubService.retract} being
    2038         called.
     2117        A retract request should result in L{PubSubResource.retract}
     2118        being called.
    20392119        """
    20402120
    20412121        xml = """
     
    20532133        def retract(request):
    20542134            return defer.succeed(None)
    20552135
    2056         self.service.retract = retract
     2136        self.resource.retract = retract
     2137        verify.verifyObject(iwokkel.IPubSubResource, self.resource)
    20572138        return self.handleRequest(xml)
    20582139
    20592140
    2060     def test_onPurge(self):
     2141    def test_on_purge(self):
    20612142        """
    2062         A purge request should result in L{PubSubService.purge} being
     2143        A purge request should result in L{PubSubResource.purge} being
    20632144        called.
    20642145        """
    20652146
     
    20752156        def purge(request):
    20762157            return defer.succeed(None)
    20772158
    2078         self.service.purge = purge
     2159        self.resource.purge = purge
     2160        verify.verifyObject(iwokkel.IPubSubResource, self.resource)
    20792161        return self.handleRequest(xml)
    20802162
    20812163
    2082     def test_onDelete(self):
     2164    def test_on_delete(self):
    20832165        """
    2084         A delete request should result in L{PubSubService.delete} being
     2166        A delete request should result in L{PubSubResource.delete} being
    20852167        called.
    20862168        """
    20872169
     
    20972179        def delete(request):
    20982180            return defer.succeed(None)
    20992181
    2100         self.service.delete = delete
     2182        self.resource.delete = delete
     2183        verify.verifyObject(iwokkel.IPubSubResource, self.resource)
    21012184        return self.handleRequest(xml)
    21022185
    21032186
     
    21482231        self.assertEqual(redirectURI, message.event.delete.redirect['uri'])
    21492232
    21502233
    2151     def test_onSubscriptionsGet(self):
     2234    def test_on_subscriptionsGet(self):
    21522235        """
    21532236        Getting subscription options is not supported.
    21542237        """
     
    21752258        return d
    21762259
    21772260
    2178     def test_onSubscriptionsSet(self):
     2261    def test_on_subscriptionsSet(self):
    21792262        """
    21802263        Setting subscription options is not supported.
    21812264        """
     
    22022285        return d
    22032286
    22042287
    2205     def test_onAffiliationsGet(self):
     2288    def test_on_affiliationsGet(self):
    22062289        """
    22072290        Getting subscription options is not supported.
    22082291        """
     
    22292312        return d
    22302313
    22312314
    2232     def test_onAffiliationsSet(self):
     2315    def test_on_affiliationsSet(self):
    22332316        """
    22342317        Setting subscription options is not supported.
    22352318        """
     
    22562339        return d
    22572340
    22582341
     2342
     2343class PubSubResourceTest(unittest.TestCase):
     2344
     2345    def setUp(self):
     2346        self.resource = pubsub.PubSubResource()
     2347
     2348
     2349    def test_interface(self):
     2350        """
     2351        Do instances of L{pubsub.PubSubResource} provide L{iwokkel.IPubSubResource}?
     2352        """
     2353        verify.verifyObject(iwokkel.IPubSubResource, self.resource)
     2354
     2355
     2356    def test_getNodes(self):
     2357        """
     2358        Default getNodes returns an empty list.
     2359        """
     2360        def cb(nodes):
     2361            self.assertEquals([], nodes)
     2362
     2363        d = self.resource.getNodes(JID('user@example.org/home'),
     2364                                   JID('pubsub.example.org'),
     2365                                   '')
     2366        d.addCallback(cb)
     2367        return d
     2368
     2369
    22592370    def test_publish(self):
    22602371        """
    2261         Non-overridden L{PubSubService.publish} yields unsupported error.
    2262         """
    2263 
    2264         xml = """
    2265         <iq type='set' to='pubsub.example.org'
    2266                        from='user@example.org'>
    2267           <pubsub xmlns='http://jabber.org/protocol/pubsub'>
    2268             <publish node='mynode'/>
    2269           </pubsub>
    2270         </iq>
     2372        Non-overridden L{PubSubResource.publish} yields unsupported
     2373        error.
    22712374        """
    22722375
    22732376        def cb(result):
     
    22762379            self.assertEquals(NS_PUBSUB_ERRORS, result.appCondition.uri)
    22772380            self.assertEquals('publish', result.appCondition['feature'])
    22782381
    2279         d = self.handleRequest(xml)
     2382        d = self.resource.publish(pubsub.PubSubRequest())
    22802383        self.assertFailure(d, error.StanzaError)
    22812384        d.addCallback(cb)
    22822385        return d
     
    22842387
    22852388    def test_subscribe(self):
    22862389        """
    2287         Non-overridden L{PubSubService.subscribe} yields unsupported error.
    2288         """
    2289 
    2290         xml = """
    2291         <iq type='set' to='pubsub.example.org'
    2292                        from='user@example.org'>
    2293           <pubsub xmlns='http://jabber.org/protocol/pubsub'>
    2294             <subscribe node='test' jid='user@example.org/Home'/>
    2295           </pubsub>
    2296         </iq>
     2390        Non-overridden subscriptions yields unsupported error.
    22972391        """
    22982392
    22992393        def cb(result):
     
    23022396            self.assertEquals(NS_PUBSUB_ERRORS, result.appCondition.uri)
    23032397            self.assertEquals('subscribe', result.appCondition['feature'])
    23042398
    2305         d = self.handleRequest(xml)
     2399        d = self.resource.subscribe(pubsub.PubSubRequest())
    23062400        self.assertFailure(d, error.StanzaError)
    23072401        d.addCallback(cb)
    23082402        return d
     
    23102404
    23112405    def test_unsubscribe(self):
    23122406        """
    2313         Non-overridden L{PubSubService.unsubscribe} yields unsupported error.
    2314         """
    2315 
    2316         xml = """
    2317         <iq type='set' to='pubsub.example.org'
    2318                        from='user@example.org'>
    2319           <pubsub xmlns='http://jabber.org/protocol/pubsub'>
    2320             <unsubscribe node='test' jid='user@example.org/Home'/>
    2321           </pubsub>
    2322         </iq>
     2407        Non-overridden unsubscribe yields unsupported error.
    23232408        """
    23242409
    23252410        def cb(result):
     
    23282413            self.assertEquals(NS_PUBSUB_ERRORS, result.appCondition.uri)
    23292414            self.assertEquals('subscribe', result.appCondition['feature'])
    23302415
    2331         d = self.handleRequest(xml)
     2416        d = self.resource.unsubscribe(pubsub.PubSubRequest())
    23322417        self.assertFailure(d, error.StanzaError)
    23332418        d.addCallback(cb)
    23342419        return d
     
    23362421
    23372422    def test_subscriptions(self):
    23382423        """
    2339         Non-overridden L{PubSubService.subscriptions} yields unsupported error.
    2340         """
    2341 
    2342         xml = """
    2343         <iq type='get' to='pubsub.example.org'
    2344                        from='user@example.org'>
    2345           <pubsub xmlns='http://jabber.org/protocol/pubsub'>
    2346             <subscriptions/>
    2347           </pubsub>
    2348         </iq>
     2424        Non-overridden subscriptions yields unsupported error.
    23492425        """
    23502426
    23512427        def cb(result):
     
    23552431            self.assertEquals('retrieve-subscriptions',
    23562432                              result.appCondition['feature'])
    23572433
    2358         d = self.handleRequest(xml)
     2434        d = self.resource.subscriptions(pubsub.PubSubRequest())
    23592435        self.assertFailure(d, error.StanzaError)
    23602436        d.addCallback(cb)
    23612437        return d
     
    23632439
    23642440    def test_affiliations(self):
    23652441        """
    2366         Non-overridden L{PubSubService.affiliations} yields unsupported error.
    2367         """
    2368 
    2369         xml = """
    2370         <iq type='get' to='pubsub.example.org'
    2371                        from='user@example.org'>
    2372           <pubsub xmlns='http://jabber.org/protocol/pubsub'>
    2373             <affiliations/>
    2374           </pubsub>
    2375         </iq>
     2442        Non-overridden affiliations yields unsupported error.
    23762443        """
    23772444
    23782445        def cb(result):
     
    23822449            self.assertEquals('retrieve-affiliations',
    23832450                              result.appCondition['feature'])
    23842451
    2385         d = self.handleRequest(xml)
     2452        d = self.resource.affiliations(pubsub.PubSubRequest())
    23862453        self.assertFailure(d, error.StanzaError)
    23872454        d.addCallback(cb)
    23882455        return d
     
    23902457
    23912458    def test_create(self):
    23922459        """
    2393         Non-overridden L{PubSubService.create} yields unsupported error.
    2394         """
    2395 
    2396         xml = """
    2397         <iq type='set' to='pubsub.example.org'
    2398                        from='user@example.org'>
    2399           <pubsub xmlns='http://jabber.org/protocol/pubsub'>
    2400             <create node='mynode'/>
    2401           </pubsub>
    2402         </iq>
     2460        Non-overridden create yields unsupported error.
    24032461        """
    24042462
    24052463        def cb(result):
     
    24082466            self.assertEquals(NS_PUBSUB_ERRORS, result.appCondition.uri)
    24092467            self.assertEquals('create-nodes', result.appCondition['feature'])
    24102468
    2411         d = self.handleRequest(xml)
     2469        d = self.resource.create(pubsub.PubSubRequest())
    24122470        self.assertFailure(d, error.StanzaError)
    24132471        d.addCallback(cb)
    24142472        return d
    24152473
    24162474
    2417     def test_getDefaultConfiguration(self):
     2475    def test_default(self):
    24182476        """
    2419         Non-overridden L{PubSubService.getDefaultConfiguration} yields
    2420         unsupported error.
    2421         """
    2422 
    2423         xml = """
    2424         <iq type='get' to='pubsub.example.org'
    2425                        from='user@example.org'>
    2426           <pubsub xmlns='http://jabber.org/protocol/pubsub#owner'>
    2427             <default/>
    2428           </pubsub>
    2429         </iq>
     2477        Non-overridden default yields unsupported error.
    24302478        """
    24312479
    24322480        def cb(result):
    24332481            self.assertEquals('feature-not-implemented', result.condition)
    24342482            self.assertEquals('unsupported', result.appCondition.name)
    24352483            self.assertEquals(NS_PUBSUB_ERRORS, result.appCondition.uri)
    2436             self.assertEquals('retrieve-default', result.appCondition['feature'])
     2484            self.assertEquals('retrieve-default',
     2485                              result.appCondition['feature'])
    24372486
    2438         d = self.handleRequest(xml)
     2487        d = self.resource.default(pubsub.PubSubRequest())
    24392488        self.assertFailure(d, error.StanzaError)
    24402489        d.addCallback(cb)
    24412490        return d
    24422491
    24432492
    2444     def test_getConfiguration(self):
     2493    def test_configureGet(self):
    24452494        """
    2446         Non-overridden L{PubSubService.getConfiguration} yields unsupported
     2495        Non-overridden configureGet yields unsupported
    24472496        error.
    24482497        """
    24492498
    2450         xml = """
    2451         <iq type='get' to='pubsub.example.org'
    2452                        from='user@example.org'>
    2453           <pubsub xmlns='http://jabber.org/protocol/pubsub#owner'>
    2454             <configure/>
    2455           </pubsub>
    2456         </iq>
    2457         """
    2458 
    24592499        def cb(result):
    24602500            self.assertEquals('feature-not-implemented', result.condition)
    24612501            self.assertEquals('unsupported', result.appCondition.name)
    24622502            self.assertEquals(NS_PUBSUB_ERRORS, result.appCondition.uri)
    24632503            self.assertEquals('config-node', result.appCondition['feature'])
    24642504
    2465         d = self.handleRequest(xml)
     2505        d = self.resource.configureGet(pubsub.PubSubRequest())
    24662506        self.assertFailure(d, error.StanzaError)
    24672507        d.addCallback(cb)
    24682508        return d
    24692509
    24702510
    2471     def test_setConfiguration(self):
     2511    def test_configureSet(self):
    24722512        """
    2473         Non-overridden L{PubSubService.setConfiguration} yields unsupported
    2474         error.
    2475         """
    2476 
    2477         xml = """
    2478         <iq type='set' to='pubsub.example.org'
    2479                        from='user@example.org'>
    2480           <pubsub xmlns='http://jabber.org/protocol/pubsub#owner'>
    2481             <configure node='test'>
    2482               <x xmlns='jabber:x:data' type='submit'>
    2483                 <field var='FORM_TYPE' type='hidden'>
    2484                   <value>http://jabber.org/protocol/pubsub#node_config</value>
    2485                 </field>
    2486                 <field var='pubsub#deliver_payloads'><value>0</value></field>
    2487                 <field var='pubsub#persist_items'><value>1</value></field>
    2488               </x>
    2489             </configure>
    2490           </pubsub>
    2491         </iq>
     2513        Non-overridden configureSet yields unsupported error.
    24922514        """
    24932515
    24942516        def cb(result):
     
    24972519            self.assertEquals(NS_PUBSUB_ERRORS, result.appCondition.uri)
    24982520            self.assertEquals('config-node', result.appCondition['feature'])
    24992521
    2500         d = self.handleRequest(xml)
     2522        d = self.resource.configureSet(pubsub.PubSubRequest())
    25012523        self.assertFailure(d, error.StanzaError)
    25022524        d.addCallback(cb)
    25032525        return d
     
    25052527
    25062528    def test_items(self):
    25072529        """
    2508         Non-overridden L{PubSubService.items} yields unsupported error.
    2509         """
    2510         xml = """
    2511         <iq type='get' to='pubsub.example.org'
    2512                        from='user@example.org'>
    2513           <pubsub xmlns='http://jabber.org/protocol/pubsub'>
    2514             <items node='test'/>
    2515           </pubsub>
    2516         </iq>
     2530        Non-overridden items yields unsupported error.
    25172531        """
    25182532
    25192533        def cb(result):
     
    25222536            self.assertEquals(NS_PUBSUB_ERRORS, result.appCondition.uri)
    25232537            self.assertEquals('retrieve-items', result.appCondition['feature'])
    25242538
    2525         d = self.handleRequest(xml)
     2539        d = self.resource.items(pubsub.PubSubRequest())
    25262540        self.assertFailure(d, error.StanzaError)
    25272541        d.addCallback(cb)
    25282542        return d
     
    25302544
    25312545    def test_retract(self):
    25322546        """
    2533         Non-overridden L{PubSubService.retract} yields unsupported error.
    2534         """
    2535         xml = """
    2536         <iq type='set' to='pubsub.example.org'
    2537                        from='user@example.org'>
    2538           <pubsub xmlns='http://jabber.org/protocol/pubsub'>
    2539             <retract node='test'>
    2540               <item id='item1'/>
    2541               <item id='item2'/>
    2542             </retract>
    2543           </pubsub>
    2544         </iq>
     2547        Non-overridden retract yields unsupported error.
    25452548        """
    25462549
    25472550        def cb(result):
     
    25502553            self.assertEquals(NS_PUBSUB_ERRORS, result.appCondition.uri)
    25512554            self.assertEquals('retract-items', result.appCondition['feature'])
    25522555
    2553         d = self.handleRequest(xml)
     2556        d = self.resource.retract(pubsub.PubSubRequest())
    25542557        self.assertFailure(d, error.StanzaError)
    25552558        d.addCallback(cb)
    25562559        return d
     
    25582561
    25592562    def test_purge(self):
    25602563        """
    2561         Non-overridden L{PubSubService.purge} yields unsupported error.
    2562         """
    2563         xml = """
    2564         <iq type='set' to='pubsub.example.org'
    2565                        from='user@example.org'>
    2566           <pubsub xmlns='http://jabber.org/protocol/pubsub#owner'>
    2567             <purge node='test'/>
    2568           </pubsub>
    2569         </iq>
     2564        Non-overridden purge yields unsupported error.
    25702565        """
    25712566
    25722567        def cb(result):
     
    25752570            self.assertEquals(NS_PUBSUB_ERRORS, result.appCondition.uri)
    25762571            self.assertEquals('purge-nodes', result.appCondition['feature'])
    25772572
    2578         d = self.handleRequest(xml)
     2573        d = self.resource.purge(pubsub.PubSubRequest())
    25792574        self.assertFailure(d, error.StanzaError)
    25802575        d.addCallback(cb)
    25812576        return d
     
    25832578
    25842579    def test_delete(self):
    25852580        """
    2586         Non-overridden L{PubSubService.delete} yields unsupported error.
    2587         """
    2588         xml = """
    2589         <iq type='set' to='pubsub.example.org'
    2590                        from='user@example.org'>
    2591           <pubsub xmlns='http://jabber.org/protocol/pubsub#owner'>
    2592             <delete node='test'/>
    2593           </pubsub>
    2594         </iq>
     2581        Non-overridden delete yields unsupported error.
    25952582        """
    25962583
    25972584        def cb(result):
     
    26002587            self.assertEquals(NS_PUBSUB_ERRORS, result.appCondition.uri)
    26012588            self.assertEquals('delete-nodes', result.appCondition['feature'])
    26022589
    2603         d = self.handleRequest(xml)
     2590        d = self.resource.delete(pubsub.PubSubRequest())
    26042591        self.assertFailure(d, error.StanzaError)
    26052592        d.addCallback(cb)
    26062593        return d
Note: See TracBrowser for help on using the repository browser.