added test for unsolicided unavailable presence and fixed bug to make it pass

This commit is contained in:
Nathan Fritz 2010-05-12 18:07:20 -07:00
parent 223507f36f
commit ae41c08fec
2 changed files with 20 additions and 3 deletions

View file

@ -271,12 +271,13 @@ class basexmpp(object):
name = self.roster[jid].get('name', '') name = self.roster[jid].get('name', '')
if show == 'unavailable': if show == 'unavailable':
logging.debug("%s %s got offline" % (jid, resource)) logging.debug("%s %s got offline" % (jid, resource))
if len(self.roster[jid]['presence']): del self.roster[jid]['presence'][resource]
del self.roster[jid]['presence'][resource] if len(self.roster[jid]['presence']) == 0 and not self.roster[jid]['in_roster']:
else:
del self.roster[jid] del self.roster[jid]
if not wasoffline: if not wasoffline:
self.event("got_offline", presence) self.event("got_offline", presence)
else:
return False
self.event("changed_status", presence) self.event("changed_status", presence)
name = '' name = ''
if name: if name:

View file

@ -12,4 +12,20 @@ class testpresencestanzas(unittest.TestCase):
p['type'] = 'dnd' p['type'] = 'dnd'
self.failUnless(str(p) == "<presence><show>dnd</show></presence>") self.failUnless(str(p) == "<presence><show>dnd</show></presence>")
def testPresenceUnsolicitedOffline(self):
"Unsolicted offline presence does not spawn changed_status or update roster"
p = self.p.Presence()
p['type'] = 'unavailable'
p['from'] = 'bill@chadmore.com/gmail15af'
import sleekxmpp
c = sleekxmpp.ClientXMPP('crap@wherever', 'password')
happened = []
def handlechangedpresence(event):
happened.append(True)
c.add_event_handler("changed_status", handlechangedpresence)
c._handlePresence(p)
self.failUnless(happened == [], "changed_status event triggered for superfulous unavailable presence")
self.failUnless(c.roster == {}, "Roster updated for superfulous unavailable presence")
suite = unittest.TestLoader().loadTestsFromTestCase(testpresencestanzas) suite = unittest.TestLoader().loadTestsFromTestCase(testpresencestanzas)