Completed basic test coverage of xmlns http://jabber.org/protocol/pubsub stanzas

This commit is contained in:
Nathan Fritz 2010-01-13 09:04:05 -08:00
parent d14045f5a6
commit 6e1aa0690f
3 changed files with 160 additions and 84 deletions

View file

@ -51,9 +51,25 @@ class Affiliations(ElementBase):
self.xml.append(affiliation.xml) self.xml.append(affiliation.xml)
return self.iterables.append(affiliation) return self.iterables.append(affiliation)
stanzaPlugin(Pubsub, Affiliations) stanzaPlugin(Pubsub, Affiliations)
class Subscription(ElementBase):
namespace = 'http://jabber.org/protocol/pubsub'
name = 'subscription'
plugin_attrib = name
interfaces = set(('jid', 'node', 'subscription'))
plugin_attrib_map = {}
plugin_tag_map = {}
def setJid(self, value):
self._setAttr('jid', str(value))
def getJid(self):
return JID(self._getAttr('jid'))
stanzaPlugin(Pubsub, Subscription)
class Subscriptions(ElementBase): class Subscriptions(ElementBase):
namespace = 'http://jabber.org/protocol/pubsub' namespace = 'http://jabber.org/protocol/pubsub'
name = 'subscriptions' name = 'subscriptions'
@ -61,6 +77,7 @@ class Subscriptions(ElementBase):
interfaces = set(tuple()) interfaces = set(tuple())
plugin_attrib_map = {} plugin_attrib_map = {}
plugin_tag_map = {} plugin_tag_map = {}
subitem = Subscription
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
ElementBase.__init__(self, *args, **kwargs) ElementBase.__init__(self, *args, **kwargs)
@ -97,25 +114,8 @@ class Subscriptions(ElementBase):
stanzaPlugin(Pubsub, Subscriptions) stanzaPlugin(Pubsub, Subscriptions)
class Subscription(ElementBase):
namespace = 'http://jabber.org/protocol/pubsub'
name = 'subscription'
plugin_attrib = name
interfaces = set(('jid', 'node', 'subid', 'subscription'))
plugin_attrib_map = {}
plugin_tag_map = {}
def setJid(self, value):
self._setAttr('jid', str(value))
def getJid(self):
return JID(self._getAttr('from'))
stanzaPlugin(Pubsub, Subscription)
class OptionalSetting(object): class OptionalSetting(object):
interfaces = set(('required')) interfaces = set(('required',))
def setRequired(self, value): def setRequired(self, value):
value = bool(value) value = bool(value)
@ -140,55 +140,13 @@ class OptionalSetting(object):
class SubscribeOptions(ElementBase, OptionalSetting): class SubscribeOptions(ElementBase, OptionalSetting):
namespace = 'http://jabber.org/protocol/pubsub' namespace = 'http://jabber.org/protocol/pubsub'
name = 'subscribe-options' name = 'subscribe-options'
plugin_attrib = 'options' plugin_attrib = 'suboptions'
plugin_attrib_map = {} plugin_attrib_map = {}
plugin_tag_map = {} plugin_tag_map = {}
interfaces = set(('required',))
stanzaPlugin(Subscription, SubscribeOptions) stanzaPlugin(Subscription, SubscribeOptions)
class Items(ElementBase):
namespace = 'http://jabber.org/protocol/pubsub'
name = 'items'
plugin_attrib = 'items'
interfaces = set(tuple())
plugin_attrib_map = {}
plugin_tag_map = {}
def __init__(self, *args, **kwargs):
ElementBase.__init__(self, *args, **kwargs)
self.items = []
self.idx = 0
def __iter__(self):
self.idx = 0
return self
def __next__(self):
self.idx += 1
if self.idx + 1 > len(self.items):
self.idx = 0
raise StopIteration
return self.items[self.idx]
def __len__(self):
return len(self.items)
def append(self, item):
if not isinstance(item, Item):
raise TypeError
self.xml.append(item.xml)
return self.items.append(item)
def pop(self, idx=0):
aff = self.items.pop(idx)
self.xml.remove(aff.xml)
return aff
def find(self, item):
return self.items.find(item)
stanzaPlugin(Pubsub, Items)
class Item(ElementBase): class Item(ElementBase):
namespace = 'http://jabber.org/protocol/pubsub' namespace = 'http://jabber.org/protocol/pubsub'
name = 'item' name = 'item'
@ -209,30 +167,41 @@ class Item(ElementBase):
for child in self.xml.getchildren(): for child in self.xml.getchildren():
self.xml.remove(child) self.xml.remove(child)
class Items(ElementBase):
namespace = 'http://jabber.org/protocol/pubsub'
name = 'items'
plugin_attrib = 'items'
interfaces = set(tuple())
plugin_attrib_map = {}
plugin_tag_map = {}
subitem = Item
stanzaPlugin(Pubsub, Items)
class Create(ElementBase): class Create(ElementBase):
namespace = 'http://jabber.org/protocol/pubsub' namespace = 'http://jabber.org/protocol/pubsub'
name = 'create' name = 'create'
plugin_attrib = name plugin_attrib = name
interfaces = set(('node')) interfaces = set(('node',))
plugin_attrib_map = {} plugin_attrib_map = {}
plugin_tag_map = {} plugin_tag_map = {}
stanzaPlugin(Pubsub, Create) stanzaPlugin(Pubsub, Create)
class Default(ElementBase): #class Default(ElementBase):
namespace = 'http://jabber.org/protocol/pubsub' # namespace = 'http://jabber.org/protocol/pubsub'
name = 'default' # name = 'default'
plugin_attrib = name # plugin_attrib = name
interfaces = set(('node', 'type')) # interfaces = set(('node', 'type'))
plugin_attrib_map = {} # plugin_attrib_map = {}
plugin_tag_map = {} # plugin_tag_map = {}
#
def getType(self): # def getType(self):
t = self._getAttr('type') # t = self._getAttr('type')
if not t: t == 'leaf' # if not t: t == 'leaf'
return t # return t
#
stanzaPlugin(Pubsub, Default) #stanzaPlugin(Pubsub, Default)
class Publish(Items): class Publish(Items):
namespace = 'http://jabber.org/protocol/pubsub' namespace = 'http://jabber.org/protocol/pubsub'
@ -280,7 +249,7 @@ class Subscribe(ElementBase):
self._setAttr('jid', str(value)) self._setAttr('jid', str(value))
def getJid(self): def getJid(self):
return JID(self._getAttr('from')) return JID(self._getAttr('jid'))
stanzaPlugin(Pubsub, Subscribe) stanzaPlugin(Pubsub, Subscribe)
@ -313,11 +282,12 @@ class Configure(ElementBase):
self.xml.remove(config) self.xml.remove(config)
stanzaPlugin(Pubsub, Configure) stanzaPlugin(Pubsub, Configure)
stanzaPlugin(Create, Configure)
class DefaultConfig(ElementBase): class DefaultConfig(ElementBase):
namespace = 'http://jabber.org/protocol/pubsub' namespace = 'http://jabber.org/protocol/pubsub'
name = 'default' name = 'default'
plugin_attrib = 'defaultconfig' plugin_attrib = 'default'
interfaces = set(('node', 'type', 'config')) interfaces = set(('node', 'type', 'config'))
plugin_attrib_map = {} plugin_attrib_map = {}
plugin_tag_map = {} plugin_tag_map = {}
@ -340,6 +310,11 @@ class DefaultConfig(ElementBase):
config = self.xml.find('{jabber:x:data}x') config = self.xml.find('{jabber:x:data}x')
self.xml.remove(config) self.xml.remove(config)
def getType(self):
t = self._getAttr('type')
if not t: t == 'leaf'
return t
stanzaPlugin(Pubsub, DefaultConfig) stanzaPlugin(Pubsub, DefaultConfig)
class Options(ElementBase): class Options(ElementBase):
@ -372,10 +347,10 @@ class Options(ElementBase):
self._setAttr('jid', str(value)) self._setAttr('jid', str(value))
def getJid(self): def getJid(self):
return JID(self._getAttr('from')) return JID(self._getAttr('jid'))
stanzaPlugin(Pubsub, Options) stanzaPlugin(Pubsub, Options)
stanzaPlugin(Subscribe, Options)
#iq = Iq() #iq = Iq()
#iq['pubsub']['defaultconfig'] #iq['pubsub']['defaultconfig']

View file

@ -64,7 +64,8 @@ class ElementBase(object):
if not isinstance(item, ElementBase): if not isinstance(item, ElementBase):
raise TypeError raise TypeError
self.xml.append(item.xml) self.xml.append(item.xml)
return self.iterables.append(item) self.iterables.append(item)
return self
def pop(self, idx=0): def pop(self, idx=0):
aff = self.iterables.pop(idx) aff = self.iterables.pop(idx)
@ -236,7 +237,7 @@ class ElementBase(object):
self.plugins[interface].setValues(attrib[interface]) self.plugins[interface].setValues(attrib[interface])
return self return self
def append(self, xml): def appendxml(self, xml):
self.xml.append(xml) self.xml.append(xml)
return self return self

View file

@ -1,4 +1,5 @@
import unittest import unittest
from xml.etree import cElementTree as ET
class testpubsubstanzas(unittest.TestCase): class testpubsubstanzas(unittest.TestCase):
@ -24,5 +25,104 @@ class testpubsubstanzas(unittest.TestCase):
iq3.setValues(values) iq3.setValues(values)
self.failUnless(xmlstring == str(iq) == str(iq2) == str(iq3)) self.failUnless(xmlstring == str(iq) == str(iq2) == str(iq3))
def testSubscriptions(self):
"Testing iq/pubsub/subscriptions/subscription stanzas"
iq = self.ps.Iq()
sub1 = self.ps.Subscription()
sub1['node'] = 'testnode'
sub1['jid'] = 'steve@myserver.tld/someresource'
sub2 = self.ps.Subscription()
sub2['node'] = 'testnode2'
sub2['jid'] = 'boogers@bork.top/bill'
iq['pubsub']['subscriptions'].append(sub1)
iq['pubsub']['subscriptions'].append(sub2)
xmlstring = """<iq id="0"><pubsub xmlns="http://jabber.org/protocol/pubsub"><subscriptions><subscription node="testnode" jid="steve@myserver.tld/someresource" /><subscription node="testnode2" jid="boogers@bork.top/bill" /></subscriptions></pubsub></iq>"""
iq2 = self.ps.Iq(None, self.ps.ET.fromstring(xmlstring))
iq3 = self.ps.Iq()
values = iq2.getValues()
iq3.setValues(values)
self.failUnless(xmlstring == str(iq) == str(iq2) == str(iq3))
def testOptionalSettings(self):
"Testing iq/pubsub/subscription/subscribe-options stanzas"
iq = self.ps.Iq()
iq['pubsub']['subscription']['suboptions']['required'] = True
iq['pubsub']['subscription']['node'] = 'testnode alsdkjfas'
iq['pubsub']['subscription']['jid'] = "fritzy@netflint.net/sleekxmpp"
iq['pubsub']['subscription']['subscription'] = 'unconfigured'
xmlstring = """<iq id="0"><pubsub xmlns="http://jabber.org/protocol/pubsub"><subscription node="testnode alsdkjfas" jid="fritzy@netflint.net/sleekxmpp" subscription="unconfigured"><subscribe-options><required /></subscribe-options></subscription></pubsub></iq>"""
iq2 = self.ps.Iq(None, self.ps.ET.fromstring(xmlstring))
iq3 = self.ps.Iq()
values = iq2.getValues()
iq3.setValues(values)
self.failUnless(xmlstring == str(iq) == str(iq2) == str(iq3))
def testItems(self):
iq = self.ps.Iq()
iq['pubsub']['items']
payload = ET.fromstring("""<thinger xmlns="http://andyet.net/protocol/thinger" x="1" y='2'><child1 /><child2 normandy='cheese' foo='bar' /></thinger>""")
payload2 = ET.fromstring("""<thinger2 xmlns="http://andyet.net/protocol/thinger2" x="12" y='22'><child12 /><child22 normandy='cheese2' foo='bar2' /></thinger2>""")
item = self.ps.Item()
item['id'] = 'asdf'
item['payload'] = payload
item2 = self.ps.Item()
item2['id'] = 'asdf2'
item2['payload'] = payload2
iq['pubsub']['items'].append(item)
iq['pubsub']['items'].append(item2)
xmlstring = """<iq id="0"><pubsub xmlns="http://jabber.org/protocol/pubsub"><items><item id="asdf"><thinger xmlns="http://andyet.net/protocol/thinger" y="2" x="1"><child1 /><child2 foo="bar" normandy="cheese" /></thinger></item><item id="asdf2"><thinger2 xmlns="http://andyet.net/protocol/thinger2" y="22" x="12"><child12 /><child22 foo="bar2" normandy="cheese2" /></thinger2></item></items></pubsub></iq>"""
iq2 = self.ps.Iq(None, self.ps.ET.fromstring(xmlstring))
iq3 = self.ps.Iq()
values = iq2.getValues()
iq3.setValues(values)
self.failUnless(xmlstring == str(iq) == str(iq2) == str(iq3))
def testCreate(self):
from sleekxmpp.plugins import xep_0004
iq = self.ps.Iq()
iq['pubsub']['create']['configure']
iq['pubsub']['create']['node'] = 'mynode'
form = xep_0004.Form()
form.addField('pubsub#title', ftype='text-single', value='This thing is awesome')
iq['pubsub']['create']['configure']['config'] = form
xmlstring = """<iq id="0"><pubsub xmlns="http://jabber.org/protocol/pubsub"><create node="mynode"><configure><x xmlns="jabber:x:data" type="form"><field var="pubsub#title" type="text-single"><value>This thing is awesome</value></field></x></configure></create></pubsub></iq>"""
iq2 = self.ps.Iq(None, self.ps.ET.fromstring(xmlstring))
iq3 = self.ps.Iq()
values = iq2.getValues()
iq3.setValues(values)
self.failUnless(xmlstring == str(iq) == str(iq2) == str(iq3))
def testDefault(self):
from sleekxmpp.plugins import xep_0004
iq = self.ps.Iq()
iq['pubsub']['default']
iq['pubsub']['default']['node'] = 'mynode'
form = xep_0004.Form()
form.addField('pubsub#title', ftype='text-single', value='This thing is awesome')
iq['pubsub']['default']['config'] = form
xmlstring = """<iq id="0"><pubsub xmlns="http://jabber.org/protocol/pubsub"><default node="mynode"><x xmlns="jabber:x:data" type="form"><field var="pubsub#title" type="text-single"><value>This thing is awesome</value></field></x></default></pubsub></iq>"""
iq2 = self.ps.Iq(None, self.ps.ET.fromstring(xmlstring))
iq3 = self.ps.Iq()
values = iq2.getValues()
iq3.setValues(values)
self.failUnless(xmlstring == str(iq) == str(iq2) == str(iq3))
def testSubscribe(self):
from sleekxmpp.plugins import xep_0004
iq = self.ps.Iq()
iq['pubsub']['subscribe']['options']
iq['pubsub']['subscribe']['node'] = 'cheese'
iq['pubsub']['subscribe']['jid'] = 'fritzy@netflint.net/sleekxmpp'
iq['pubsub']['subscribe']['options']['node'] = 'cheese'
iq['pubsub']['subscribe']['options']['jid'] = 'fritzy@netflint.net/sleekxmpp'
form = xep_0004.Form()
form.addField('pubsub#title', ftype='text-single', value='This thing is awesome')
iq['pubsub']['subscribe']['options']['options'] = form
xmlstring = """<iq id="0"><pubsub xmlns="http://jabber.org/protocol/pubsub"><subscribe node="cheese" jid="fritzy@netflint.net/sleekxmpp"><options node="cheese" jid="fritzy@netflint.net/sleekxmpp"><x xmlns="jabber:x:data" type="form"><field var="pubsub#title" type="text-single"><value>This thing is awesome</value></field></x></options></subscribe></pubsub></iq>"""
iq2 = self.ps.Iq(None, self.ps.ET.fromstring(xmlstring))
iq3 = self.ps.Iq()
values = iq2.getValues()
iq3.setValues(values)
self.failUnless(xmlstring == str(iq) == str(iq2) == str(iq3))
suite = unittest.TestLoader().loadTestsFromTestCase(testpubsubstanzas) suite = unittest.TestLoader().loadTestsFromTestCase(testpubsubstanzas)