Skip to content

Commit

Permalink
Allow publish-subscribe client requests to come from a specific JID.
Browse files Browse the repository at this point in the history
Author: ralphm.
Fixes #46.

--HG--
extra : convert_revision : svn%3Ab33ecbfc-034c-dc11-8662-000475d9059e/trunk%40170
  • Loading branch information
ralphm committed Apr 22, 2009
1 parent 13b09c5 commit 5bc6abe
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 6 deletions.
18 changes: 12 additions & 6 deletions wokkel/pubsub.py
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ def purgeReceived(self, event):
pass


def createNode(self, service, nodeIdentifier=None):
def createNode(self, service, nodeIdentifier=None, sender=None):
"""
Create a publish subscribe node.
Expand All @@ -636,6 +636,7 @@ def createNode(self, service, nodeIdentifier=None):
request = PubSubRequest('create')
request.recipient = service
request.nodeIdentifier = nodeIdentifier
request.sender = sender

def cb(iq):
try:
Expand All @@ -650,7 +651,7 @@ def cb(iq):
return d


def deleteNode(self, service, nodeIdentifier):
def deleteNode(self, service, nodeIdentifier, sender=None):
"""
Delete a publish subscribe node.
Expand All @@ -662,10 +663,11 @@ def deleteNode(self, service, nodeIdentifier):
request = PubSubRequest('delete')
request.recipient = service
request.nodeIdentifier = nodeIdentifier
request.sender = sender
return request.send(self.xmlstream)


def subscribe(self, service, nodeIdentifier, subscriber):
def subscribe(self, service, nodeIdentifier, subscriber, sender=None):
"""
Subscribe to a publish subscribe node.
Expand All @@ -681,6 +683,7 @@ def subscribe(self, service, nodeIdentifier, subscriber):
request.recipient = service
request.nodeIdentifier = nodeIdentifier
request.subscriber = subscriber
request.sender = sender

def cb(iq):
subscription = iq.pubsub.subscription["subscription"]
Expand All @@ -700,7 +703,7 @@ def cb(iq):
return d


def unsubscribe(self, service, nodeIdentifier, subscriber):
def unsubscribe(self, service, nodeIdentifier, subscriber, sender=None):
"""
Unsubscribe from a publish subscribe node.
Expand All @@ -715,10 +718,11 @@ def unsubscribe(self, service, nodeIdentifier, subscriber):
request.recipient = service
request.nodeIdentifier = nodeIdentifier
request.subscriber = subscriber
request.sender = sender
return request.send(self.xmlstream)


def publish(self, service, nodeIdentifier, items=None):
def publish(self, service, nodeIdentifier, items=None, sender=None):
"""
Publish to a publish subscribe node.
Expand All @@ -733,10 +737,11 @@ def publish(self, service, nodeIdentifier, items=None):
request.recipient = service
request.nodeIdentifier = nodeIdentifier
request.items = items
request.sender = sender
return request.send(self.xmlstream)


def items(self, service, nodeIdentifier, maxItems=None):
def items(self, service, nodeIdentifier, maxItems=None, sender=None):
"""
Retrieve previously published items from a publish subscribe node.
Expand All @@ -752,6 +757,7 @@ def items(self, service, nodeIdentifier, maxItems=None):
request.nodeIdentifier = nodeIdentifier
if maxItems:
request.maxItems = str(int(maxItems))
request.sender = sender

def cb(iq):
items = []
Expand Down
103 changes: 103 additions & 0 deletions wokkel/test/test_pubsub.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,22 @@ def cb(nodeIdentifier):
return d


def test_createNodeWithSender(self):
"""
Test sending create request from a specific JID.
"""

d = self.protocol.createNode(JID('pubsub.example.org'), 'test',
sender=JID('[email protected]'))

iq = self.stub.output[-1]
self.assertEquals('[email protected]', iq['from'])

response = toResponse(iq, 'result')
self.stub.send(response)
return d


def test_deleteNode(self):
"""
Test sending delete request.
Expand All @@ -278,6 +294,22 @@ def test_deleteNode(self):
return d


def test_deleteNodeWithSender(self):
"""
Test sending delete request.
"""

d = self.protocol.deleteNode(JID('pubsub.example.org'), 'test',
sender=JID('[email protected]'))

iq = self.stub.output[-1]
self.assertEquals('[email protected]', iq['from'])

response = toResponse(iq, 'result')
self.stub.send(response)
return d


def test_publish(self):
"""
Test sending publish request.
Expand Down Expand Up @@ -329,6 +361,23 @@ def test_publishNoItems(self):
return d


def test_publishWithSender(self):
"""
Test sending publish request from a specific JID.
"""

item = pubsub.Item()
d = self.protocol.publish(JID('pubsub.example.org'), 'test', [item],
JID('[email protected]'))

iq = self.stub.output[-1]
self.assertEquals('[email protected]', iq['from'])

response = toResponse(iq, 'result')
self.stub.send(response)
return d


def test_subscribe(self):
"""
Test sending subscription request.
Expand Down Expand Up @@ -398,6 +447,27 @@ def test_subscribeUnconfigured(self):
return d


def test_subscribeWithSender(self):
"""
Test sending subscription request from a specific JID.
"""
d = self.protocol.subscribe(JID('pubsub.example.org'), 'test',
JID('[email protected]'),
sender=JID('[email protected]'))

iq = self.stub.output[-1]
self.assertEquals('[email protected]', iq['from'])

response = toResponse(iq, 'result')
pubsub = response.addElement((NS_PUBSUB, 'pubsub'))
subscription = pubsub.addElement('subscription')
subscription['node'] = 'test'
subscription['jid'] = '[email protected]'
subscription['subscription'] = 'subscribed'
self.stub.send(response)
return d


def test_unsubscribe(self):
"""
Test sending unsubscription request.
Expand All @@ -421,6 +491,20 @@ def test_unsubscribe(self):
return d


def test_unsubscribeWithSender(self):
"""
Test sending unsubscription request from a specific JID.
"""
d = self.protocol.unsubscribe(JID('pubsub.example.org'), 'test',
JID('[email protected]'),
sender=JID('[email protected]'))

iq = self.stub.output[-1]
self.assertEquals('[email protected]', iq['from'])
self.stub.send(toResponse(iq, 'result'))
return d


def test_items(self):
"""
Test sending items request.
Expand Down Expand Up @@ -487,6 +571,25 @@ def cb(items):
return d


def test_itemsWithSender(self):
"""
Test sending items request from a specific JID.
"""

d = self.protocol.items(JID('pubsub.example.org'), 'test',
sender=JID('[email protected]'))

iq = self.stub.output[-1]
self.assertEquals('[email protected]', iq['from'])

response = toResponse(iq, 'result')
items = response.addElement((NS_PUBSUB, 'pubsub')).addElement('items')
items['node'] = 'test'

self.stub.send(response)
return d



class PubSubRequestTest(unittest.TestCase):

Expand Down

0 comments on commit 5bc6abe

Please sign in to comment.