1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
|
import time
import unittest
from slixmpp.test import SlixTest
class TestStreamPresence(SlixTest):
"""
Test handling roster updates.
"""
def setUp(self):
self.stream_start(jid='tester@localhost', plugins=[])
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)
self.recv("""
<presence type="unavailable"
from="otheruser@localhost"
to="tester@localhost"/>
""")
self.assertEqual(events, {'unavailable'},
"Got offline incorrectly triggered: %s." % events)
def testGotOffline(self):
"""Test that got_offline is triggered properly."""
events = []
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.
self.recv("""
<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.
self.recv("""
<presence from="otheruser@localhost/foobar"
to="tester@localhost" />
""")
# Contact goes offline, should trigger got_offline.
self.recv("""
<presence from="otheruser@localhost/foobar"
to="tester@localhost"
type="unavailable" />
""")
self.assertEqual(events, ['got_offline'],
"Got offline incorrectly triggered: %s" % events)
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)
self.recv("""
<presence from="user@localhost"
to="tester@localhost" />
""")
expected = {'presence_available', 'got_online'}
self.assertEqual(events, expected,
"Incorrect events triggered: %s" % events)
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')
self.xmpp.add_event_handler('changed_subscription',
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
self.recv("""
<presence from="user@localhost"
to="tester@localhost"
type="subscribe" />
""")
self.send("""
<presence to="user@localhost"
type="subscribed" />
""")
self.send("""
<presence to="user@localhost" />
""")
self.send("""
<presence to="user@localhost"
type="subscribe" />
""")
expected = {'presence_subscribe', 'changed_subscription'}
self.assertEqual(events, expected,
"Incorrect events triggered: %s" % events)
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')
self.xmpp.add_event_handler('changed_subscription',
changed_subscription)
self.xmpp.add_event_handler('presence_subscribe',
presence_subscribe)
# With this setting we should reject all subscriptions.
self.xmpp.roster['tester@localhost'].auto_authorize = False
self.recv("""
<presence from="user@localhost"
to="tester@localhost"
type="subscribe" />
""")
self.send("""
<presence to="user@localhost"
type="unsubscribed" />
""")
expected = {'presence_subscribe', 'changed_subscription'}
self.assertEqual(events, expected,
"Incorrect events triggered: %s" % events)
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)
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" />
""")
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>
""")
self.assertEqual(events, ['available', 'away', 'dnd', 'chat',
'xa', 'unavailable', 'available',
'available', 'dnd'],
"Changed status events incorrect: %s" % events)
suite = unittest.TestLoader().loadTestsFromTestCase(TestStreamPresence)
|