2010-10-24 23:57:07 +00:00
|
|
|
import time
|
2013-07-26 11:02:26 +00:00
|
|
|
import unittest
|
2014-07-17 12:19:04 +00:00
|
|
|
from slixmpp.test import SlixTest
|
2010-10-24 23:57:07 +00:00
|
|
|
|
|
|
|
|
2014-07-17 12:19:04 +00:00
|
|
|
class TestStreamPresence(SlixTest):
|
2010-10-24 23:57:07 +00:00
|
|
|
"""
|
|
|
|
Test handling roster updates.
|
|
|
|
"""
|
|
|
|
|
2012-03-10 20:48:35 +00:00
|
|
|
def setUp(self):
|
|
|
|
self.stream_start(jid='tester@localhost', plugins=[])
|
|
|
|
|
2010-10-24 23:57:07 +00:00
|
|
|
def tearDown(self):
|
|
|
|
self.stream_close()
|
|
|
|
|
|
|
|
def testInitialUnavailablePresences(self):
|
|
|
|
"""
|
|
|
|
Test receiving unavailable presences from JIDs that
|
|
|
|
are not online.
|
|
|
|
"""
|
|
|
|
events = set()
|
|
|
|
|
|
|
|
def got_offline(presence):
|
|
|
|
# The got_offline event should not be triggered.
|
|
|
|
events.add('got_offline')
|
|
|
|
|
|
|
|
def unavailable(presence):
|
|
|
|
# The presence_unavailable event should be triggered.
|
|
|
|
events.add('unavailable')
|
|
|
|
|
|
|
|
self.xmpp.add_event_handler('got_offline', got_offline)
|
|
|
|
self.xmpp.add_event_handler('presence_unavailable', unavailable)
|
|
|
|
|
2010-11-05 18:45:58 +00:00
|
|
|
self.recv("""
|
2010-10-27 03:47:17 +00:00
|
|
|
<presence type="unavailable"
|
|
|
|
from="otheruser@localhost"
|
|
|
|
to="tester@localhost"/>
|
2010-10-24 23:57:07 +00:00
|
|
|
""")
|
|
|
|
|
|
|
|
self.assertEqual(events, set(('unavailable',)),
|
|
|
|
"Got offline incorrectly triggered: %s." % events)
|
|
|
|
|
|
|
|
def testGotOffline(self):
|
|
|
|
"""Test that got_offline is triggered properly."""
|
|
|
|
events = []
|
2010-10-25 19:09:56 +00:00
|
|
|
|
2010-10-24 23:57:07 +00:00
|
|
|
def got_offline(presence):
|
|
|
|
events.append('got_offline')
|
|
|
|
|
|
|
|
self.xmpp.add_event_handler('got_offline', got_offline)
|
|
|
|
|
|
|
|
# Setup roster. Use a 'set' instead of 'result' so we
|
|
|
|
# don't have to handle get_roster() blocking.
|
|
|
|
#
|
|
|
|
# We use the stream to initialize the roster to make
|
|
|
|
# the test independent of the roster implementation.
|
2010-11-05 18:45:58 +00:00
|
|
|
self.recv("""
|
2010-10-24 23:57:07 +00:00
|
|
|
<iq type="set">
|
|
|
|
<query xmlns="jabber:iq:roster">
|
|
|
|
<item jid="otheruser@localhost"
|
|
|
|
name="Other User"
|
|
|
|
subscription="both">
|
|
|
|
<group>Testers</group>
|
|
|
|
</item>
|
|
|
|
</query>
|
|
|
|
</iq>
|
|
|
|
""")
|
|
|
|
|
|
|
|
# Contact comes online.
|
2010-11-05 18:45:58 +00:00
|
|
|
self.recv("""
|
2010-10-27 03:47:17 +00:00
|
|
|
<presence from="otheruser@localhost/foobar"
|
|
|
|
to="tester@localhost" />
|
2010-10-24 23:57:07 +00:00
|
|
|
""")
|
|
|
|
|
|
|
|
# Contact goes offline, should trigger got_offline.
|
2010-11-05 18:45:58 +00:00
|
|
|
self.recv("""
|
2010-10-24 23:57:07 +00:00
|
|
|
<presence from="otheruser@localhost/foobar"
|
2010-10-27 03:47:17 +00:00
|
|
|
to="tester@localhost"
|
2010-10-24 23:57:07 +00:00
|
|
|
type="unavailable" />
|
|
|
|
""")
|
|
|
|
|
|
|
|
self.assertEqual(events, ['got_offline'],
|
|
|
|
"Got offline incorrectly triggered: %s" % events)
|
|
|
|
|
2010-10-25 17:19:19 +00:00
|
|
|
def testGotOnline(self):
|
|
|
|
"""Test that got_online is triggered properly."""
|
|
|
|
|
|
|
|
events = set()
|
|
|
|
|
|
|
|
def presence_available(p):
|
|
|
|
events.add('presence_available')
|
|
|
|
|
|
|
|
def got_online(p):
|
|
|
|
events.add('got_online')
|
|
|
|
|
|
|
|
self.xmpp.add_event_handler('presence_available', presence_available)
|
|
|
|
self.xmpp.add_event_handler('got_online', got_online)
|
|
|
|
|
2010-11-05 18:45:58 +00:00
|
|
|
self.recv("""
|
2010-10-27 03:47:17 +00:00
|
|
|
<presence from="user@localhost"
|
|
|
|
to="tester@localhost" />
|
2010-10-25 17:19:19 +00:00
|
|
|
""")
|
|
|
|
|
|
|
|
expected = set(('presence_available', 'got_online'))
|
|
|
|
self.assertEqual(events, expected,
|
|
|
|
"Incorrect events triggered: %s" % events)
|
|
|
|
|
2010-10-25 16:52:32 +00:00
|
|
|
def testAutoAuthorizeAndSubscribe(self):
|
|
|
|
"""
|
|
|
|
Test auto authorizing and auto subscribing
|
|
|
|
to subscription requests.
|
|
|
|
"""
|
|
|
|
|
|
|
|
events = set()
|
|
|
|
|
|
|
|
def presence_subscribe(p):
|
|
|
|
events.add('presence_subscribe')
|
|
|
|
|
|
|
|
def changed_subscription(p):
|
|
|
|
events.add('changed_subscription')
|
|
|
|
|
|
|
|
|
2010-10-25 19:09:56 +00:00
|
|
|
self.xmpp.add_event_handler('changed_subscription',
|
2010-10-25 16:52:32 +00:00
|
|
|
changed_subscription)
|
|
|
|
self.xmpp.add_event_handler('presence_subscribe',
|
|
|
|
presence_subscribe)
|
|
|
|
|
|
|
|
# With these settings we should accept a subscription
|
|
|
|
# and request a subscription in return.
|
|
|
|
self.xmpp.auto_authorize = True
|
|
|
|
self.xmpp.auto_subscribe = True
|
|
|
|
|
2010-11-05 18:45:58 +00:00
|
|
|
self.recv("""
|
2010-10-27 03:47:17 +00:00
|
|
|
<presence from="user@localhost"
|
|
|
|
to="tester@localhost"
|
|
|
|
type="subscribe" />
|
|
|
|
""")
|
|
|
|
|
|
|
|
self.send("""
|
|
|
|
<presence to="user@localhost"
|
|
|
|
type="subscribed" />
|
2010-10-25 16:52:32 +00:00
|
|
|
""")
|
|
|
|
|
2010-11-05 18:45:58 +00:00
|
|
|
self.send("""
|
2010-10-27 03:47:17 +00:00
|
|
|
<presence to="user@localhost" />
|
2010-10-25 16:52:32 +00:00
|
|
|
""")
|
|
|
|
|
2010-11-05 18:45:58 +00:00
|
|
|
self.send("""
|
2010-10-27 03:47:17 +00:00
|
|
|
<presence to="user@localhost"
|
|
|
|
type="subscribe" />
|
2010-10-25 16:52:32 +00:00
|
|
|
""")
|
|
|
|
|
|
|
|
expected = set(('presence_subscribe', 'changed_subscription'))
|
2010-10-25 19:09:56 +00:00
|
|
|
self.assertEqual(events, expected,
|
2010-10-25 16:52:32 +00:00
|
|
|
"Incorrect events triggered: %s" % events)
|
|
|
|
|
2010-10-25 17:19:19 +00:00
|
|
|
def testNoAutoAuthorize(self):
|
|
|
|
"""Test auto rejecting subscription requests."""
|
|
|
|
|
|
|
|
events = set()
|
|
|
|
|
|
|
|
def presence_subscribe(p):
|
|
|
|
events.add('presence_subscribe')
|
|
|
|
|
|
|
|
def changed_subscription(p):
|
|
|
|
events.add('changed_subscription')
|
2010-10-25 19:09:56 +00:00
|
|
|
|
|
|
|
self.xmpp.add_event_handler('changed_subscription',
|
2010-10-25 17:19:19 +00:00
|
|
|
changed_subscription)
|
|
|
|
self.xmpp.add_event_handler('presence_subscribe',
|
|
|
|
presence_subscribe)
|
|
|
|
|
|
|
|
# With this setting we should reject all subscriptions.
|
2010-10-27 12:09:50 +00:00
|
|
|
self.xmpp.roster['tester@localhost'].auto_authorize = False
|
2010-10-25 17:19:19 +00:00
|
|
|
|
2010-11-05 18:45:58 +00:00
|
|
|
self.recv("""
|
2010-10-27 03:47:17 +00:00
|
|
|
<presence from="user@localhost"
|
|
|
|
to="tester@localhost"
|
|
|
|
type="subscribe" />
|
2010-10-25 17:19:19 +00:00
|
|
|
""")
|
|
|
|
|
2010-11-05 18:45:58 +00:00
|
|
|
self.send("""
|
2010-10-27 03:47:17 +00:00
|
|
|
<presence to="user@localhost"
|
|
|
|
type="unsubscribed" />
|
2010-10-25 17:19:19 +00:00
|
|
|
""")
|
|
|
|
|
|
|
|
expected = set(('presence_subscribe', 'changed_subscription'))
|
2010-10-25 19:09:56 +00:00
|
|
|
self.assertEqual(events, expected,
|
2010-10-25 17:19:19 +00:00
|
|
|
"Incorrect events triggered: %s" % events)
|
|
|
|
|
2011-07-06 04:29:31 +00:00
|
|
|
def test_presence_events(self):
|
|
|
|
"""Test that presence events are raised."""
|
|
|
|
|
|
|
|
events = []
|
|
|
|
|
|
|
|
ptypes = ['available', 'away', 'dnd', 'xa', 'chat',
|
|
|
|
'unavailable', 'subscribe', 'subscribed',
|
|
|
|
'unsubscribe', 'unsubscribed']
|
|
|
|
|
|
|
|
for ptype in ptypes:
|
|
|
|
handler = lambda p: events.append(p['type'])
|
|
|
|
self.xmpp.add_event_handler('presence_%s' % ptype, handler)
|
|
|
|
|
|
|
|
self.recv("""
|
|
|
|
<presence />
|
|
|
|
""")
|
|
|
|
self.recv("""
|
|
|
|
<presence><show>away</show></presence>
|
|
|
|
""")
|
|
|
|
self.recv("""
|
|
|
|
<presence><show>dnd</show></presence>
|
|
|
|
""")
|
|
|
|
self.recv("""
|
|
|
|
<presence><show>xa</show></presence>
|
|
|
|
""")
|
|
|
|
self.recv("""
|
|
|
|
<presence><show>chat</show></presence>
|
|
|
|
""")
|
|
|
|
self.recv("""
|
|
|
|
<presence type="unavailable" />
|
|
|
|
""")
|
|
|
|
self.recv("""
|
|
|
|
<presence type="subscribe" />
|
|
|
|
""")
|
|
|
|
self.recv("""
|
|
|
|
<presence type="subscribed" />
|
|
|
|
""")
|
|
|
|
self.recv("""
|
|
|
|
<presence type="unsubscribe" />
|
|
|
|
""")
|
|
|
|
self.recv("""
|
|
|
|
<presence type="unsubscribed" />
|
|
|
|
""")
|
|
|
|
|
|
|
|
self.assertEqual(events, ptypes,
|
|
|
|
"Not all events raised: %s" % events)
|
|
|
|
|
2011-11-18 21:37:57 +00:00
|
|
|
def test_changed_status(self):
|
|
|
|
"""Test that the changed_status event is handled properly."""
|
|
|
|
events = []
|
|
|
|
|
|
|
|
def changed_status(presence):
|
|
|
|
events.append(presence['type'])
|
|
|
|
|
|
|
|
self.xmpp.add_event_handler('changed_status', changed_status)
|
|
|
|
|
|
|
|
self.recv("""
|
|
|
|
<presence from="user@example.com" to="tester@localhost" />
|
|
|
|
""")
|
|
|
|
|
|
|
|
self.recv("""
|
|
|
|
<presence from="user@example.com" to="tester@localhost" />
|
|
|
|
""")
|
|
|
|
|
|
|
|
self.recv("""
|
|
|
|
<presence from="user@example.com" to="tester@localhost">
|
|
|
|
<show>away</show>
|
|
|
|
</presence>
|
|
|
|
""")
|
|
|
|
|
|
|
|
self.recv("""
|
|
|
|
<presence from="user@example.com" to="tester@localhost">
|
|
|
|
<show>away</show>
|
|
|
|
</presence>
|
|
|
|
""")
|
|
|
|
|
|
|
|
self.recv("""
|
|
|
|
<presence from="user@example.com" to="tester@localhost">
|
|
|
|
<show>dnd</show>
|
|
|
|
</presence>
|
|
|
|
""")
|
|
|
|
|
|
|
|
self.recv("""
|
|
|
|
<presence from="user@example.com" to="tester@localhost">
|
|
|
|
<show>dnd</show>
|
|
|
|
</presence>
|
|
|
|
""")
|
|
|
|
|
|
|
|
self.recv("""
|
|
|
|
<presence from="user@example.com" to="tester@localhost">
|
|
|
|
<show>chat</show>
|
|
|
|
</presence>
|
|
|
|
""")
|
|
|
|
|
|
|
|
self.recv("""
|
|
|
|
<presence from="user@example.com" to="tester@localhost">
|
|
|
|
<show>chat</show>
|
|
|
|
</presence>
|
|
|
|
""")
|
|
|
|
|
|
|
|
self.recv("""
|
|
|
|
<presence from="user@example.com" to="tester@localhost">
|
|
|
|
<show>xa</show>
|
|
|
|
</presence>
|
|
|
|
""")
|
|
|
|
|
|
|
|
self.recv("""
|
|
|
|
<presence from="user@example.com" to="tester@localhost">
|
|
|
|
<show>xa</show>
|
|
|
|
</presence>
|
|
|
|
""")
|
|
|
|
|
|
|
|
self.recv("""
|
|
|
|
<presence from="user@example.com"
|
|
|
|
to="tester@localhost"
|
|
|
|
type="unavailable" />
|
|
|
|
""")
|
|
|
|
|
|
|
|
self.recv("""
|
|
|
|
<presence from="user@example.com"
|
|
|
|
to="tester@localhost"
|
|
|
|
type="unavailable" />
|
|
|
|
""")
|
|
|
|
|
|
|
|
self.recv("""
|
|
|
|
<presence from="user@example.com" to="tester@localhost" />
|
|
|
|
""")
|
|
|
|
|
|
|
|
self.recv("""
|
|
|
|
<presence from="user@example.com" to="tester@localhost" />
|
|
|
|
""")
|
|
|
|
|
2011-11-18 21:57:41 +00:00
|
|
|
self.recv("""
|
|
|
|
<presence from="user@example.com" to="tester@localhost" />
|
|
|
|
""")
|
|
|
|
|
|
|
|
# Changed status text, so fire new event
|
|
|
|
self.recv("""
|
|
|
|
<presence from="user@example.com" to="tester@localhost">
|
|
|
|
<status>Testing!</status>
|
|
|
|
</presence>
|
|
|
|
""")
|
|
|
|
|
|
|
|
# No change in show/status values, no event
|
|
|
|
self.recv("""
|
|
|
|
<presence from="user@example.com" to="tester@localhost">
|
|
|
|
<status>Testing!</status>
|
|
|
|
</presence>
|
|
|
|
""")
|
|
|
|
|
|
|
|
self.recv("""
|
|
|
|
<presence from="user@example.com" to="tester@localhost">
|
|
|
|
<show>dnd</show>
|
|
|
|
<status>Testing!</status>
|
|
|
|
</presence>
|
|
|
|
""")
|
|
|
|
|
|
|
|
self.recv("""
|
|
|
|
<presence from="user@example.com" to="tester@localhost">
|
|
|
|
<show>dnd</show>
|
|
|
|
<status>Testing!</status>
|
|
|
|
</presence>
|
|
|
|
""")
|
2011-11-18 21:37:57 +00:00
|
|
|
|
|
|
|
self.assertEqual(events, ['available', 'away', 'dnd', 'chat',
|
2011-11-18 21:57:41 +00:00
|
|
|
'xa', 'unavailable', 'available',
|
|
|
|
'available', 'dnd'],
|
2011-11-18 21:37:57 +00:00
|
|
|
"Changed status events incorrect: %s" % events)
|
|
|
|
|
|
|
|
|
2010-10-24 23:57:07 +00:00
|
|
|
suite = unittest.TestLoader().loadTestsFromTestCase(TestStreamPresence)
|