| 1 |
# Copyright (c) Ralph Meijer. |
| 2 |
# See LICENSE for details |
| 3 |
|
| 4 |
""" |
| 5 |
Tests for L{tx_xmpp.xmppim}. |
| 6 |
""" |
| 7 |
|
| 8 |
from twisted.internet import defer |
| 9 |
from twisted.trial import unittest |
| 10 |
from twisted.words.protocols.jabber import error |
| 11 |
from twisted.words.protocols.jabber.jid import JID |
| 12 |
from twisted.words.protocols.jabber.xmlstream import toResponse |
| 13 |
from twisted.words.xish import domish, utility |
| 14 |
|
| 15 |
from .. import xmppim |
| 16 |
from ..constants import NS_XML |
| 17 |
from ..generic import ErrorStanza, parseXml |
| 18 |
from .helpers import TestableRequestHandlerMixin, XmlStreamStub |
| 19 |
|
| 20 |
NS_ROSTER = "jabber:iq:roster" |
| 21 |
|
| 22 |
|
| 23 |
class PresenceClientProtocolTest(unittest.TestCase): |
| 24 |
def setUp(self): |
| 25 |
self.output = [] |
| 26 |
self.protocol = xmppim.PresenceClientProtocol() |
| 27 |
self.protocol.parent = self |
| 28 |
|
| 29 |
def send(self, obj): |
| 30 |
self.output.append(obj) |
| 31 |
|
| 32 |
def test_unavailableDirected(self): |
| 33 |
""" |
| 34 |
Test sending of directed unavailable presence broadcast. |
| 35 |
""" |
| 36 |
|
| 37 |
self.protocol.unavailable(JID("[email protected]")) |
| 38 |
presence = self.output[-1] |
| 39 |
self.assertEquals("presence", presence.name) |
| 40 |
self.assertEquals(None, presence.uri) |
| 41 |
self.assertEquals("[email protected]", presence.getAttribute("to")) |
| 42 |
self.assertEquals("unavailable", presence.getAttribute("type")) |
| 43 |
|
| 44 |
def test_unavailableWithStatus(self): |
| 45 |
""" |
| 46 |
Test sending of directed unavailable presence broadcast with status. |
| 47 |
""" |
| 48 |
|
| 49 |
self.protocol.unavailable(JID("[email protected]"), {None: "Disconnected"}) |
| 50 |
presence = self.output[-1] |
| 51 |
self.assertEquals("presence", presence.name) |
| 52 |
self.assertEquals(None, presence.uri) |
| 53 |
self.assertEquals("[email protected]", presence.getAttribute("to")) |
| 54 |
self.assertEquals("unavailable", presence.getAttribute("type")) |
| 55 |
self.assertEquals("Disconnected", str(presence.status)) |
| 56 |
|
| 57 |
def test_unavailableBroadcast(self): |
| 58 |
""" |
| 59 |
Test sending of unavailable presence broadcast. |
| 60 |
""" |
| 61 |
|
| 62 |
self.protocol.unavailable(None) |
| 63 |
presence = self.output[-1] |
| 64 |
self.assertEquals("presence", presence.name) |
| 65 |
self.assertEquals(None, presence.uri) |
| 66 |
self.assertEquals(None, presence.getAttribute("to")) |
| 67 |
self.assertEquals("unavailable", presence.getAttribute("type")) |
| 68 |
|
| 69 |
def test_unavailableBroadcastNoEntityParameter(self): |
| 70 |
""" |
| 71 |
Test sending of unavailable presence broadcast by not passing entity. |
| 72 |
""" |
| 73 |
|
| 74 |
self.protocol.unavailable() |
| 75 |
presence = self.output[-1] |
| 76 |
self.assertEquals("presence", presence.name) |
| 77 |
self.assertEquals(None, presence.uri) |
| 78 |
self.assertEquals(None, presence.getAttribute("to")) |
| 79 |
self.assertEquals("unavailable", presence.getAttribute("type")) |
| 80 |
|
| 81 |
|
| 82 |
class AvailabilityPresenceTest(unittest.TestCase): |
| 83 |
|
| 84 |
def test_fromElement(self): |
| 85 |
xml = """<presence from='[email protected]' to='[email protected]'> |
| 86 |
<show>chat</show> |
| 87 |
<status>Let's chat!</status> |
| 88 |
<priority>50</priority> |
| 89 |
</presence> |
| 90 |
""" |
| 91 |
|
| 92 |
presence = xmppim.AvailabilityPresence.fromElement(parseXml(xml)) |
| 93 |
self.assertEquals(JID("[email protected]"), presence.sender) |
| 94 |
self.assertEquals(JID("[email protected]"), presence.recipient) |
| 95 |
self.assertTrue(presence.available) |
| 96 |
self.assertEquals("chat", presence.show) |
| 97 |
self.assertEquals({None: "Let's chat!"}, presence.statuses) |
| 98 |
self.assertEquals(50, presence.priority) |
| 99 |
|
| 100 |
|
| 101 |
class PresenceProtocolTest(unittest.TestCase): |
| 102 |
""" |
| 103 |
Tests for L{xmppim.PresenceProtocol} |
| 104 |
""" |
| 105 |
|
| 106 |
def setUp(self): |
| 107 |
self.output = [] |
| 108 |
self.protocol = xmppim.PresenceProtocol() |
| 109 |
self.protocol.parent = self |
| 110 |
self.protocol.xmlstream = utility.EventDispatcher() |
| 111 |
self.protocol.connectionInitialized() |
| 112 |
|
| 113 |
def send(self, obj): |
| 114 |
self.output.append(obj) |
| 115 |
|
| 116 |
def test_errorReceived(self): |
| 117 |
""" |
| 118 |
Incoming presence stanzas are parsed and dispatched. |
| 119 |
""" |
| 120 |
xml = """<presence type="error"/>""" |
| 121 |
|
| 122 |
def errorReceived(error): |
| 123 |
xmppim.PresenceProtocol.errorReceived(self.protocol, error) |
| 124 |
try: |
| 125 |
self.assertIsInstance(error, ErrorStanza) |
| 126 |
except: |
| 127 |
d.errback() |
| 128 |
else: |
| 129 |
d.callback(None) |
| 130 |
|
| 131 |
d = defer.Deferred() |
| 132 |
self.protocol.errorReceived = errorReceived |
| 133 |
self.protocol.xmlstream.dispatch(parseXml(xml)) |
| 134 |
return d |
| 135 |
|
| 136 |
def test_availableReceived(self): |
| 137 |
""" |
| 138 |
Incoming presence stanzas are parsed and dispatched. |
| 139 |
""" |
| 140 |
xml = """<presence/>""" |
| 141 |
|
| 142 |
def availableReceived(presence): |
| 143 |
xmppim.PresenceProtocol.availableReceived(self.protocol, presence) |
| 144 |
try: |
| 145 |
self.assertIsInstance(presence, xmppim.AvailabilityPresence) |
| 146 |
except: |
| 147 |
d.errback() |
| 148 |
else: |
| 149 |
d.callback(None) |
| 150 |
|
| 151 |
d = defer.Deferred() |
| 152 |
self.protocol.availableReceived = availableReceived |
| 153 |
self.protocol.xmlstream.dispatch(parseXml(xml)) |
| 154 |
return d |
| 155 |
|
| 156 |
def test_unavailableReceived(self): |
| 157 |
""" |
| 158 |
Incoming presence stanzas are parsed and dispatched. |
| 159 |
""" |
| 160 |
xml = """<presence type='unavailable'/>""" |
| 161 |
|
| 162 |
def unavailableReceived(presence): |
| 163 |
xmppim.PresenceProtocol.unavailableReceived(self.protocol, presence) |
| 164 |
try: |
| 165 |
self.assertIsInstance(presence, xmppim.AvailabilityPresence) |
| 166 |
except: |
| 167 |
d.errback() |
| 168 |
else: |
| 169 |
d.callback(None) |
| 170 |
|
| 171 |
d = defer.Deferred() |
| 172 |
self.protocol.unavailableReceived = unavailableReceived |
| 173 |
self.protocol.xmlstream.dispatch(parseXml(xml)) |
| 174 |
return d |
| 175 |
|
| 176 |
def test_subscribeReceived(self): |
| 177 |
""" |
| 178 |
Incoming presence stanzas are parsed and dispatched. |
| 179 |
""" |
| 180 |
xml = """<presence type='subscribe'/>""" |
| 181 |
|
| 182 |
def subscribeReceived(presence): |
| 183 |
xmppim.PresenceProtocol.subscribeReceived(self.protocol, presence) |
| 184 |
try: |
| 185 |
self.assertIsInstance(presence, xmppim.SubscriptionPresence) |
| 186 |
except: |
| 187 |
d.errback() |
| 188 |
else: |
| 189 |
d.callback(None) |
| 190 |
|
| 191 |
d = defer.Deferred() |
| 192 |
self.protocol.subscribeReceived = subscribeReceived |
| 193 |
self.protocol.xmlstream.dispatch(parseXml(xml)) |
| 194 |
return d |
| 195 |
|
| 196 |
def test_unsubscribeReceived(self): |
| 197 |
""" |
| 198 |
Incoming presence stanzas are parsed and dispatched. |
| 199 |
""" |
| 200 |
xml = """<presence type='unsubscribe'/>""" |
| 201 |
|
| 202 |
def unsubscribeReceived(presence): |
| 203 |
xmppim.PresenceProtocol.unsubscribeReceived(self.protocol, presence) |
| 204 |
try: |
| 205 |
self.assertIsInstance(presence, xmppim.SubscriptionPresence) |
| 206 |
except: |
| 207 |
d.errback() |
| 208 |
else: |
| 209 |
d.callback(None) |
| 210 |
|
| 211 |
d = defer.Deferred() |
| 212 |
self.protocol.unsubscribeReceived = unsubscribeReceived |
| 213 |
self.protocol.xmlstream.dispatch(parseXml(xml)) |
| 214 |
return d |
| 215 |
|
| 216 |
def test_subscribedReceived(self): |
| 217 |
""" |
| 218 |
Incoming presence stanzas are parsed and dispatched. |
| 219 |
""" |
| 220 |
xml = """<presence type='subscribed'/>""" |
| 221 |
|
| 222 |
def subscribedReceived(presence): |
| 223 |
xmppim.PresenceProtocol.subscribedReceived(self.protocol, presence) |
| 224 |
try: |
| 225 |
self.assertIsInstance(presence, xmppim.SubscriptionPresence) |
| 226 |
except: |
| 227 |
d.errback() |
| 228 |
else: |
| 229 |
d.callback(None) |
| 230 |
|
| 231 |
d = defer.Deferred() |
| 232 |
self.protocol.subscribedReceived = subscribedReceived |
| 233 |
self.protocol.xmlstream.dispatch(parseXml(xml)) |
| 234 |
return d |
| 235 |
|
| 236 |
def test_unsubscribedReceived(self): |
| 237 |
""" |
| 238 |
Incoming presence stanzas are parsed and dispatched. |
| 239 |
""" |
| 240 |
xml = """<presence type='unsubscribed'/>""" |
| 241 |
|
| 242 |
def unsubscribedReceived(presence): |
| 243 |
xmppim.PresenceProtocol.unsubscribedReceived(self.protocol, presence) |
| 244 |
try: |
| 245 |
self.assertIsInstance(presence, xmppim.SubscriptionPresence) |
| 246 |
except: |
| 247 |
d.errback() |
| 248 |
else: |
| 249 |
d.callback(None) |
| 250 |
|
| 251 |
d = defer.Deferred() |
| 252 |
self.protocol.unsubscribedReceived = unsubscribedReceived |
| 253 |
self.protocol.xmlstream.dispatch(parseXml(xml)) |
| 254 |
return d |
| 255 |
|
| 256 |
def test_probeReceived(self): |
| 257 |
""" |
| 258 |
Incoming presence stanzas are parsed and dispatched. |
| 259 |
""" |
| 260 |
xml = """<presence type='probe'/>""" |
| 261 |
|
| 262 |
def probeReceived(presence): |
| 263 |
xmppim.PresenceProtocol.probeReceived(self.protocol, presence) |
| 264 |
try: |
| 265 |
self.assertIsInstance(presence, xmppim.ProbePresence) |
| 266 |
except: |
| 267 |
d.errback() |
| 268 |
else: |
| 269 |
d.callback(None) |
| 270 |
|
| 271 |
d = defer.Deferred() |
| 272 |
self.protocol.probeReceived = probeReceived |
| 273 |
self.protocol.xmlstream.dispatch(parseXml(xml)) |
| 274 |
return d |
| 275 |
|
| 276 |
def test_available(self): |
| 277 |
""" |
| 278 |
It should be possible to pass a sender address. |
| 279 |
""" |
| 280 |
self.protocol.available( |
| 281 |
JID("[email protected]"), show="chat", status="Talk to me!", priority=50 |
| 282 |
) |
| 283 |
element = self.output[-1] |
| 284 |
self.assertEquals("[email protected]", element.getAttribute("to")) |
| 285 |
self.assertIdentical(None, element.getAttribute("type")) |
| 286 |
self.assertEquals("chat", str(element.show)) |
| 287 |
self.assertEquals("Talk to me!", str(element.status)) |
| 288 |
self.assertEquals("50", str(element.priority)) |
| 289 |
|
| 290 |
def test_availableLanguages(self): |
| 291 |
""" |
| 292 |
It should be possible to pass a sender address. |
| 293 |
""" |
| 294 |
self.protocol.available( |
| 295 |
JID("[email protected]"), |
| 296 |
show="chat", |
| 297 |
statuses={None: "Talk to me!", "nl": "Praat met me!"}, |
| 298 |
priority=50, |
| 299 |
) |
| 300 |
element = self.output[-1] |
| 301 |
self.assertEquals("[email protected]", element.getAttribute("to")) |
| 302 |
self.assertIdentical(None, element.getAttribute("type")) |
| 303 |
self.assertEquals("chat", str(element.show)) |
| 304 |
|
| 305 |
statuses = {} |
| 306 |
for status in element.elements(): |
| 307 |
if status.name == "status": |
| 308 |
lang = status.getAttribute((NS_XML, "lang")) |
| 309 |
statuses[lang] = str(status) |
| 310 |
|
| 311 |
self.assertIn(None, statuses) |
| 312 |
self.assertEquals("Talk to me!", statuses[None]) |
| 313 |
self.assertIn("nl", statuses) |
| 314 |
self.assertEquals("Praat met me!", statuses["nl"]) |
| 315 |
self.assertEquals("50", str(element.priority)) |
| 316 |
|
| 317 |
def test_availableSender(self): |
| 318 |
""" |
| 319 |
It should be possible to pass a sender address. |
| 320 |
""" |
| 321 |
self.protocol.available(JID("[email protected]"), sender=JID("[email protected]")) |
| 322 |
element = self.output[-1] |
| 323 |
self.assertEquals("[email protected]", element.getAttribute("from")) |
| 324 |
|
| 325 |
def test_unavailableDirected(self): |
| 326 |
""" |
| 327 |
Test sending of directed unavailable presence broadcast. |
| 328 |
""" |
| 329 |
|
| 330 |
self.protocol.unavailable(JID("[email protected]")) |
| 331 |
element = self.output[-1] |
| 332 |
self.assertEquals("presence", element.name) |
| 333 |
self.assertEquals(None, element.uri) |
| 334 |
self.assertEquals("[email protected]", element.getAttribute("to")) |
| 335 |
self.assertEquals("unavailable", element.getAttribute("type")) |
| 336 |
|
| 337 |
def test_unavailableWithStatus(self): |
| 338 |
""" |
| 339 |
Test sending of directed unavailable presence broadcast with status. |
| 340 |
""" |
| 341 |
|
| 342 |
self.protocol.unavailable(JID("[email protected]"), {None: "Disconnected"}) |
| 343 |
element = self.output[-1] |
| 344 |
self.assertEquals("presence", element.name) |
| 345 |
self.assertEquals(None, element.uri) |
| 346 |
self.assertEquals("[email protected]", element.getAttribute("to")) |
| 347 |
self.assertEquals("unavailable", element.getAttribute("type")) |
| 348 |
self.assertEquals("Disconnected", str(element.status)) |
| 349 |
|
| 350 |
def test_unavailableBroadcast(self): |
| 351 |
""" |
| 352 |
Test sending of unavailable presence broadcast. |
| 353 |
""" |
| 354 |
|
| 355 |
self.protocol.unavailable(None) |
| 356 |
element = self.output[-1] |
| 357 |
self.assertEquals("presence", element.name) |
| 358 |
self.assertEquals(None, element.uri) |
| 359 |
self.assertEquals(None, element.getAttribute("to")) |
| 360 |
self.assertEquals("unavailable", element.getAttribute("type")) |
| 361 |
|
| 362 |
def test_unavailableBroadcastNoRecipientParameter(self): |
| 363 |
""" |
| 364 |
Test sending of unavailable presence broadcast by not passing entity. |
| 365 |
""" |
| 366 |
|
| 367 |
self.protocol.unavailable() |
| 368 |
element = self.output[-1] |
| 369 |
self.assertEquals("presence", element.name) |
| 370 |
self.assertEquals(None, element.uri) |
| 371 |
self.assertEquals(None, element.getAttribute("to")) |
| 372 |
self.assertEquals("unavailable", element.getAttribute("type")) |
| 373 |
|
| 374 |
def test_unavailableSender(self): |
| 375 |
""" |
| 376 |
It should be possible to pass a sender address. |
| 377 |
""" |
| 378 |
self.protocol.unavailable(JID("[email protected]"), sender=JID("[email protected]")) |
| 379 |
element = self.output[-1] |
| 380 |
self.assertEquals("[email protected]", element.getAttribute("from")) |
| 381 |
|
| 382 |
def test_subscribeSender(self): |
| 383 |
""" |
| 384 |
It should be possible to pass a sender address. |
| 385 |
""" |
| 386 |
self.protocol.subscribe(JID("[email protected]"), sender=JID("[email protected]")) |
| 387 |
element = self.output[-1] |
| 388 |
self.assertEquals("[email protected]", element.getAttribute("from")) |
| 389 |
|
| 390 |
def test_unsubscribeSender(self): |
| 391 |
""" |
| 392 |
It should be possible to pass a sender address. |
| 393 |
""" |
| 394 |
self.protocol.unsubscribe(JID("[email protected]"), sender=JID("[email protected]")) |
| 395 |
element = self.output[-1] |
| 396 |
self.assertEquals("[email protected]", element.getAttribute("from")) |
| 397 |
|
| 398 |
def test_subscribedSender(self): |
| 399 |
""" |
| 400 |
It should be possible to pass a sender address. |
| 401 |
""" |
| 402 |
self.protocol.subscribed(JID("[email protected]"), sender=JID("[email protected]")) |
| 403 |
element = self.output[-1] |
| 404 |
self.assertEquals("[email protected]", element.getAttribute("from")) |
| 405 |
|
| 406 |
def test_unsubscribedSender(self): |
| 407 |
""" |
| 408 |
It should be possible to pass a sender address. |
| 409 |
""" |
| 410 |
self.protocol.unsubscribed( |
| 411 |
JID("[email protected]"), sender=JID("[email protected]") |
| 412 |
) |
| 413 |
element = self.output[-1] |
| 414 |
self.assertEquals("[email protected]", element.getAttribute("from")) |
| 415 |
|
| 416 |
def test_probeSender(self): |
| 417 |
""" |
| 418 |
It should be possible to pass a sender address. |
| 419 |
""" |
| 420 |
self.protocol.probe(JID("[email protected]"), sender=JID("[email protected]")) |
| 421 |
element = self.output[-1] |
| 422 |
self.assertEquals("[email protected]", element.getAttribute("from")) |
| 423 |
|
| 424 |
|
| 425 |
class RosterItemTest(unittest.TestCase): |
| 426 |
""" |
| 427 |
Tests for L{xmppim.RosterItem}. |
| 428 |
""" |
| 429 |
|
| 430 |
def test_toElement(self): |
| 431 |
""" |
| 432 |
A roster item has the correct namespace/name, lacks unset attributes. |
| 433 |
""" |
| 434 |
item = xmppim.RosterItem(JID("[email protected]")) |
| 435 |
element = item.toElement() |
| 436 |
self.assertEqual("item", element.name) |
| 437 |
self.assertEqual(NS_ROSTER, element.uri) |
| 438 |
self.assertFalse(element.hasAttribute("subscription")) |
| 439 |
self.assertFalse(element.hasAttribute("ask")) |
| 440 |
self.assertEqual("", element.getAttribute("name", "")) |
| 441 |
self.assertFalse(element.hasAttribute("approved")) |
| 442 |
self.assertEquals(0, len(list(element.elements()))) |
| 443 |
|
| 444 |
def test_toElementMinimal(self): |
| 445 |
""" |
| 446 |
A bare roster item only has a jid attribute. |
| 447 |
""" |
| 448 |
item = xmppim.RosterItem(JID("[email protected]")) |
| 449 |
element = item.toElement() |
| 450 |
self.assertEqual("[email protected]", element.getAttribute("jid")) |
| 451 |
|
| 452 |
def test_toElementSubscriptionNone(self): |
| 453 |
""" |
| 454 |
A roster item with no subscription has no subscription attribute. |
| 455 |
""" |
| 456 |
item = xmppim.RosterItem( |
| 457 |
JID("[email protected]"), subscriptionTo=False, subscriptionFrom=False |
| 458 |
) |
| 459 |
element = item.toElement() |
| 460 |
self.assertIdentical(None, element.getAttribute("subscription")) |
| 461 |
|
| 462 |
def test_toElementSubscriptionTo(self): |
| 463 |
""" |
| 464 |
A roster item with subscriptionTo set has subscription 'to'. |
| 465 |
""" |
| 466 |
item = xmppim.RosterItem( |
| 467 |
JID("[email protected]"), subscriptionTo=True, subscriptionFrom=False |
| 468 |
) |
| 469 |
element = item.toElement() |
| 470 |
self.assertEqual("to", element.getAttribute("subscription")) |
| 471 |
|
| 472 |
def test_toElementSubscriptionFrom(self): |
| 473 |
""" |
| 474 |
A roster item with subscriptionFrom set has subscription 'to'. |
| 475 |
""" |
| 476 |
item = xmppim.RosterItem( |
| 477 |
JID("[email protected]"), subscriptionTo=False, subscriptionFrom=True |
| 478 |
) |
| 479 |
element = item.toElement() |
| 480 |
self.assertEqual("from", element.getAttribute("subscription")) |
| 481 |
|
| 482 |
def test_toElementSubscriptionBoth(self): |
| 483 |
""" |
| 484 |
A roster item with mutual subscription has subscription 'both'. |
| 485 |
""" |
| 486 |
item = xmppim.RosterItem( |
| 487 |
JID("[email protected]"), subscriptionTo=True, subscriptionFrom=True |
| 488 |
) |
| 489 |
element = item.toElement() |
| 490 |
self.assertEqual("both", element.getAttribute("subscription")) |
| 491 |
|
| 492 |
def test_toElementSubscriptionRemove(self): |
| 493 |
""" |
| 494 |
A roster item with remove set has subscription 'remove'. |
| 495 |
""" |
| 496 |
item = xmppim.RosterItem(JID("[email protected]")) |
| 497 |
item.remove = True |
| 498 |
element = item.toElement() |
| 499 |
self.assertEqual("remove", element.getAttribute("subscription")) |
| 500 |
|
| 501 |
def test_toElementAsk(self): |
| 502 |
""" |
| 503 |
A roster item with pendingOut set has subscription 'ask'. |
| 504 |
""" |
| 505 |
item = xmppim.RosterItem(JID("[email protected]")) |
| 506 |
item.pendingOut = True |
| 507 |
element = item.toElement() |
| 508 |
self.assertEqual("subscribe", element.getAttribute("ask")) |
| 509 |
|
| 510 |
def test_toElementName(self): |
| 511 |
""" |
| 512 |
A roster item's name is rendered to the 'name' attribute. |
| 513 |
""" |
| 514 |
item = xmppim.RosterItem(JID("[email protected]"), name="Joe User") |
| 515 |
element = item.toElement() |
| 516 |
self.assertEqual("Joe User", element.getAttribute("name")) |
| 517 |
|
| 518 |
def test_toElementGroups(self): |
| 519 |
""" |
| 520 |
A roster item's groups are rendered as 'group' child elements. |
| 521 |
""" |
| 522 |
groups = set(["Friends", "Jabber"]) |
| 523 |
item = xmppim.RosterItem(JID("[email protected]"), groups=groups) |
| 524 |
|
| 525 |
element = item.toElement() |
| 526 |
foundGroups = set() |
| 527 |
for child in element.elements(): |
| 528 |
if child.uri == NS_ROSTER and child.name == "group": |
| 529 |
foundGroups.add(str(child)) |
| 530 |
|
| 531 |
self.assertEqual(groups, foundGroups) |
| 532 |
|
| 533 |
def test_toElementApproved(self): |
| 534 |
""" |
| 535 |
A pre-approved subscription for a roster item has an 'approved' flag. |
| 536 |
""" |
| 537 |
item = xmppim.RosterItem(JID("[email protected]")) |
| 538 |
item.approved = True |
| 539 |
element = item.toElement() |
| 540 |
self.assertEqual("true", element.getAttribute("approved")) |
| 541 |
|
| 542 |
def test_fromElementMinimal(self): |
| 543 |
""" |
| 544 |
A minimal roster item has a reference to the JID of the contact. |
| 545 |
""" |
| 546 |
|
| 547 |
xml = """ |
| 548 |
<item xmlns="jabber:iq:roster" |
| 549 |
jid="[email protected]"/> |
| 550 |
""" |
| 551 |
|
| 552 |
item = xmppim.RosterItem.fromElement(parseXml(xml)) |
| 553 |
self.assertEqual(JID("[email protected]"), item.entity) |
| 554 |
self.assertEqual("", item.name) |
| 555 |
self.assertFalse(item.subscriptionTo) |
| 556 |
self.assertFalse(item.subscriptionFrom) |
| 557 |
self.assertFalse(item.pendingOut) |
| 558 |
self.assertFalse(item.approved) |
| 559 |
self.assertEqual(set(), item.groups) |
| 560 |
|
| 561 |
def test_fromElementName(self): |
| 562 |
""" |
| 563 |
A roster item may have an optional name. |
| 564 |
""" |
| 565 |
|
| 566 |
xml = """ |
| 567 |
<item xmlns="jabber:iq:roster" |
| 568 |
jid="[email protected]" |
| 569 |
name="Test User"/> |
| 570 |
""" |
| 571 |
|
| 572 |
item = xmppim.RosterItem.fromElement(parseXml(xml)) |
| 573 |
self.assertEqual("Test User", item.name) |
| 574 |
|
| 575 |
def test_fromElementGroups(self): |
| 576 |
""" |
| 577 |
A roster item may have one or more groups. |
| 578 |
""" |
| 579 |
|
| 580 |
xml = """ |
| 581 |
<item xmlns="jabber:iq:roster" |
| 582 |
jid="[email protected]"> |
| 583 |
<group>Friends</group> |
| 584 |
<group>Twisted</group> |
| 585 |
</item> |
| 586 |
""" |
| 587 |
|
| 588 |
item = xmppim.RosterItem.fromElement(parseXml(xml)) |
| 589 |
self.assertIn("Twisted", item.groups) |
| 590 |
self.assertIn("Friends", item.groups) |
| 591 |
|
| 592 |
def test_fromElementSubscriptionNone(self): |
| 593 |
""" |
| 594 |
Subscription 'none' sets both attributes to False. |
| 595 |
""" |
| 596 |
|
| 597 |
xml = """ |
| 598 |
<item xmlns="jabber:iq:roster" |
| 599 |
jid="[email protected]" |
| 600 |
subscription="none"/> |
| 601 |
""" |
| 602 |
|
| 603 |
item = xmppim.RosterItem.fromElement(parseXml(xml)) |
| 604 |
self.assertFalse(item.remove) |
| 605 |
self.assertFalse(item.subscriptionTo) |
| 606 |
self.assertFalse(item.subscriptionFrom) |
| 607 |
|
| 608 |
def test_fromElementSubscriptionTo(self): |
| 609 |
""" |
| 610 |
Subscription 'to' sets the corresponding attribute to True. |
| 611 |
""" |
| 612 |
|
| 613 |
xml = """ |
| 614 |
<item xmlns="jabber:iq:roster" |
| 615 |
jid="[email protected]" |
| 616 |
subscription="to"/> |
| 617 |
""" |
| 618 |
|
| 619 |
item = xmppim.RosterItem.fromElement(parseXml(xml)) |
| 620 |
self.assertFalse(item.remove) |
| 621 |
self.assertTrue(item.subscriptionTo) |
| 622 |
self.assertFalse(item.subscriptionFrom) |
| 623 |
|
| 624 |
def test_fromElementSubscriptionFrom(self): |
| 625 |
""" |
| 626 |
Subscription 'from' sets the corresponding attribute to True. |
| 627 |
""" |
| 628 |
|
| 629 |
xml = """ |
| 630 |
<item xmlns="jabber:iq:roster" |
| 631 |
jid="[email protected]" |
| 632 |
subscription="from"/> |
| 633 |
""" |
| 634 |
|
| 635 |
item = xmppim.RosterItem.fromElement(parseXml(xml)) |
| 636 |
self.assertFalse(item.remove) |
| 637 |
self.assertFalse(item.subscriptionTo) |
| 638 |
self.assertTrue(item.subscriptionFrom) |
| 639 |
|
| 640 |
def test_fromElementSubscriptionBoth(self): |
| 641 |
""" |
| 642 |
Subscription 'both' sets both attributes to True. |
| 643 |
""" |
| 644 |
|
| 645 |
xml = """ |
| 646 |
<item xmlns="jabber:iq:roster" |
| 647 |
jid="[email protected]" |
| 648 |
subscription="both"/> |
| 649 |
""" |
| 650 |
|
| 651 |
item = xmppim.RosterItem.fromElement(parseXml(xml)) |
| 652 |
self.assertFalse(item.remove) |
| 653 |
self.assertTrue(item.subscriptionTo) |
| 654 |
self.assertTrue(item.subscriptionFrom) |
| 655 |
|
| 656 |
def test_fromElementSubscriptionRemove(self): |
| 657 |
""" |
| 658 |
Subscription 'remove' sets the remove attribute. |
| 659 |
""" |
| 660 |
|
| 661 |
xml = """ |
| 662 |
<item xmlns="jabber:iq:roster" |
| 663 |
jid="[email protected]" |
| 664 |
subscription="remove"/> |
| 665 |
""" |
| 666 |
|
| 667 |
item = xmppim.RosterItem.fromElement(parseXml(xml)) |
| 668 |
self.assertTrue(item.remove) |
| 669 |
|
| 670 |
def test_fromElementPendingOut(self): |
| 671 |
""" |
| 672 |
The ask attribute, if set to 'subscription', means pending out. |
| 673 |
""" |
| 674 |
|
| 675 |
xml = """ |
| 676 |
<item xmlns="jabber:iq:roster" |
| 677 |
jid="[email protected]" |
| 678 |
ask="subscribe"/> |
| 679 |
""" |
| 680 |
|
| 681 |
item = xmppim.RosterItem.fromElement(parseXml(xml)) |
| 682 |
self.assertTrue(item.pendingOut) |
| 683 |
|
| 684 |
def test_fromElementApprovedTrue(self): |
| 685 |
""" |
| 686 |
The approved attribute (true) signals a pre-approved subscription. |
| 687 |
""" |
| 688 |
|
| 689 |
xml = """ |
| 690 |
<item xmlns="jabber:iq:roster" |
| 691 |
jid="[email protected]" |
| 692 |
approved="true"/> |
| 693 |
""" |
| 694 |
|
| 695 |
item = xmppim.RosterItem.fromElement(parseXml(xml)) |
| 696 |
self.assertTrue(item.approved) |
| 697 |
|
| 698 |
def test_fromElementApproved1(self): |
| 699 |
""" |
| 700 |
The approved attribute (1) signals a pre-approved subscription. |
| 701 |
""" |
| 702 |
|
| 703 |
xml = """ |
| 704 |
<item xmlns="jabber:iq:roster" |
| 705 |
jid="[email protected]" |
| 706 |
approved="1"/> |
| 707 |
""" |
| 708 |
|
| 709 |
item = xmppim.RosterItem.fromElement(parseXml(xml)) |
| 710 |
self.assertTrue(item.approved) |
| 711 |
|
| 712 |
|
| 713 |
class RosterRequestTest(unittest.TestCase): |
| 714 |
""" |
| 715 |
Tests for L{xmppim.RosterRequest}. |
| 716 |
""" |
| 717 |
|
| 718 |
def test_fromElement(self): |
| 719 |
""" |
| 720 |
A bare roster request is parsed and missing information is None. |
| 721 |
""" |
| 722 |
xml = """ |
| 723 |
<iq type='get' to='[email protected]/Home' from='[email protected]'> |
| 724 |
<query xmlns='jabber:iq:roster'/> |
| 725 |
</iq> |
| 726 |
""" |
| 727 |
|
| 728 |
request = xmppim.RosterRequest.fromElement(parseXml(xml)) |
| 729 |
self.assertEqual("get", request.stanzaType) |
| 730 |
self.assertEqual(JID("[email protected]/Home"), request.recipient) |
| 731 |
self.assertEqual(JID("[email protected]"), request.sender) |
| 732 |
self.assertEqual(None, request.item) |
| 733 |
self.assertEqual(None, request.version) |
| 734 |
|
| 735 |
def test_fromElementItem(self): |
| 736 |
""" |
| 737 |
If an item is present, parse it and put it in the request item. |
| 738 |
""" |
| 739 |
xml = """ |
| 740 |
<iq type='set' to='[email protected]/Home' from='[email protected]'> |
| 741 |
<query xmlns='jabber:iq:roster'> |
| 742 |
<item jid='[email protected]'/> |
| 743 |
</query> |
| 744 |
</iq> |
| 745 |
""" |
| 746 |
|
| 747 |
request = xmppim.RosterRequest.fromElement(parseXml(xml)) |
| 748 |
self.assertNotIdentical(None, request.item) |
| 749 |
self.assertEqual(JID("[email protected]"), request.item.entity) |
| 750 |
|
| 751 |
def test_fromElementVersion(self): |
| 752 |
""" |
| 753 |
If a ver attribute is present, put it in the request version. |
| 754 |
""" |
| 755 |
xml = """ |
| 756 |
<iq type='set' to='[email protected]/Home' from='[email protected]'> |
| 757 |
<query xmlns='jabber:iq:roster' ver='ver72'> |
| 758 |
<item jid='[email protected]'/> |
| 759 |
</query> |
| 760 |
</iq> |
| 761 |
""" |
| 762 |
request = xmppim.RosterRequest.fromElement(parseXml(xml)) |
| 763 |
self.assertEqual("ver72", request.version) |
| 764 |
|
| 765 |
def test_fromElementVersionEmpty(self): |
| 766 |
""" |
| 767 |
The ver attribute may be empty. |
| 768 |
""" |
| 769 |
xml = """ |
| 770 |
<iq type='get' to='[email protected]/Home' from='[email protected]'> |
| 771 |
<query xmlns='jabber:iq:roster' ver=''/> |
| 772 |
</iq> |
| 773 |
""" |
| 774 |
request = xmppim.RosterRequest.fromElement(parseXml(xml)) |
| 775 |
self.assertEqual("", request.version) |
| 776 |
|
| 777 |
def test_toElement(self): |
| 778 |
""" |
| 779 |
A roster request has a query element in the roster namespace. |
| 780 |
""" |
| 781 |
request = xmppim.RosterRequest() |
| 782 |
element = request.toElement() |
| 783 |
children = element.elements() |
| 784 |
child = next(children) |
| 785 |
self.assertEqual(NS_ROSTER, child.uri) |
| 786 |
self.assertEqual("query", child.name) |
| 787 |
|
| 788 |
def test_toElementItem(self): |
| 789 |
""" |
| 790 |
If an item is set, it is rendered as a child of the query. |
| 791 |
""" |
| 792 |
request = xmppim.RosterRequest() |
| 793 |
request.item = xmppim.RosterItem(JID("[email protected]")) |
| 794 |
element = request.toElement() |
| 795 |
children = element.query.elements() |
| 796 |
child = next(children) |
| 797 |
self.assertEqual(NS_ROSTER, child.uri) |
| 798 |
self.assertEqual("item", child.name) |
| 799 |
|
| 800 |
|
| 801 |
class FakeClient: |
| 802 |
""" |
| 803 |
Fake client stream manager for roster tests. |
| 804 |
""" |
| 805 |
|
| 806 |
def __init__(self, xmlstream, jid): |
| 807 |
self.xmlstream = xmlstream |
| 808 |
self.jid = jid |
| 809 |
|
| 810 |
def request(self, request): |
| 811 |
element = request.toElement() |
| 812 |
self.xmlstream.send(element) |
| 813 |
return defer.Deferred() |
| 814 |
|
| 815 |
def addHandler(self, handler): |
| 816 |
handler.makeConnection(self.xmlstream) |
| 817 |
handler.connectionInitialized() |
| 818 |
|
| 819 |
def test_toElementVersion(self): |
| 820 |
""" |
| 821 |
If the roster version is set, a 'ver' attribute is added. |
| 822 |
""" |
| 823 |
request = xmppim.RosterRequest() |
| 824 |
request.version = "ver72" |
| 825 |
element = request.toElement() |
| 826 |
self.assertEqual("ver72", element.query.getAttribute("ver")) |
| 827 |
|
| 828 |
def test_toElementVersionEmpty(self): |
| 829 |
""" |
| 830 |
If the roster version is the empty string, it should add 'ver', too. |
| 831 |
""" |
| 832 |
request = xmppim.RosterRequest() |
| 833 |
request.version = "" |
| 834 |
element = request.toElement() |
| 835 |
self.assertEqual("", element.query.getAttribute("ver")) |
| 836 |
|
| 837 |
|
| 838 |
class RosterClientProtocolTest(unittest.TestCase, TestableRequestHandlerMixin): |
| 839 |
""" |
| 840 |
Tests for L{xmppim.RosterClientProtocol}. |
| 841 |
""" |
| 842 |
|
| 843 |
def setUp(self): |
| 844 |
self.stub = XmlStreamStub() |
| 845 |
self.client = FakeClient(self.stub.xmlstream, JID("[email protected]")) |
| 846 |
self.service = xmppim.RosterClientProtocol() |
| 847 |
self.service.setHandlerParent(self.client) |
| 848 |
|
| 849 |
def test_setItem(self): |
| 850 |
""" |
| 851 |
Setting a roster item renders the item and sends it out. |
| 852 |
""" |
| 853 |
item = xmppim.RosterItem( |
| 854 |
JID("[email protected]"), name="Joe User", groups=set(["Friends", "Jabber"]) |
| 855 |
) |
| 856 |
d = self.service.setItem(item) |
| 857 |
|
| 858 |
# Inspect outgoing iq request |
| 859 |
|
| 860 |
iq = self.stub.output[-1] |
| 861 |
self.assertEqual("set", iq.getAttribute("type")) |
| 862 |
self.assertNotIdentical(None, iq.query) |
| 863 |
self.assertEqual(NS_ROSTER, iq.query.uri) |
| 864 |
|
| 865 |
children = list( |
| 866 |
domish.generateElementsQNamed(iq.query.children, "item", NS_ROSTER) |
| 867 |
) |
| 868 |
self.assertEqual(1, len(children)) |
| 869 |
child = children[0] |
| 870 |
self.assertEqual("[email protected]", child["jid"]) |
| 871 |
self.assertIdentical(None, child.getAttribute("subscription")) |
| 872 |
|
| 873 |
# Fake successful response |
| 874 |
|
| 875 |
response = toResponse(iq, "result") |
| 876 |
d.callback(response) |
| 877 |
return d |
| 878 |
|
| 879 |
def test_setItemIgnoreAttributes(self): |
| 880 |
""" |
| 881 |
Certain attributes should be rendered for roster set. |
| 882 |
""" |
| 883 |
item = xmppim.RosterItem( |
| 884 |
JID("[email protected]"), |
| 885 |
subscriptionTo=True, |
| 886 |
subscriptionFrom=False, |
| 887 |
name="Joe User", |
| 888 |
groups=set(["Friends", "Jabber"]), |
| 889 |
) |
| 890 |
item.pendingOut = True |
| 891 |
item.approved = True |
| 892 |
d = self.service.setItem(item) |
| 893 |
|
| 894 |
# Inspect outgoing iq request |
| 895 |
|
| 896 |
iq = self.stub.output[-1] |
| 897 |
self.assertEqual("set", iq.getAttribute("type")) |
| 898 |
self.assertNotIdentical(None, iq.query) |
| 899 |
self.assertEqual(NS_ROSTER, iq.query.uri) |
| 900 |
|
| 901 |
children = list( |
| 902 |
domish.generateElementsQNamed(iq.query.children, "item", NS_ROSTER) |
| 903 |
) |
| 904 |
self.assertEqual(1, len(children)) |
| 905 |
child = children[0] |
| 906 |
self.assertIdentical(None, child.getAttribute("ask")) |
| 907 |
self.assertIdentical(None, child.getAttribute("approved")) |
| 908 |
self.assertIdentical(None, child.getAttribute("subscription")) |
| 909 |
|
| 910 |
# Fake successful response |
| 911 |
|
| 912 |
response = toResponse(iq, "result") |
| 913 |
d.callback(response) |
| 914 |
return d |
| 915 |
|
| 916 |
def test_removeItem(self): |
| 917 |
""" |
| 918 |
Removing a roster item is setting an item with subscription C{remove}. |
| 919 |
""" |
| 920 |
d = self.service.removeItem(JID("[email protected]")) |
| 921 |
|
| 922 |
# Inspect outgoing iq request |
| 923 |
|
| 924 |
iq = self.stub.output[-1] |
| 925 |
self.assertEqual("set", iq.getAttribute("type")) |
| 926 |
self.assertNotIdentical(None, iq.query) |
| 927 |
self.assertEqual(NS_ROSTER, iq.query.uri) |
| 928 |
|
| 929 |
children = list( |
| 930 |
domish.generateElementsQNamed(iq.query.children, "item", NS_ROSTER) |
| 931 |
) |
| 932 |
self.assertEqual(1, len(children)) |
| 933 |
child = children[0] |
| 934 |
self.assertEqual("[email protected]", child["jid"]) |
| 935 |
self.assertEqual("remove", child.getAttribute("subscription")) |
| 936 |
|
| 937 |
# Fake successful response |
| 938 |
|
| 939 |
response = toResponse(iq, "result") |
| 940 |
d.callback(response) |
| 941 |
return d |
| 942 |
|
| 943 |
def test_getRoster(self): |
| 944 |
""" |
| 945 |
A request for the roster is sent out and the response is parsed. |
| 946 |
""" |
| 947 |
|
| 948 |
def cb(roster): |
| 949 |
self.assertIn(JID("[email protected]"), roster) |
| 950 |
self.assertIdentical(None, getattr(roster, "version")) |
| 951 |
|
| 952 |
d = self.service.getRoster() |
| 953 |
d.addCallback(cb) |
| 954 |
|
| 955 |
# Inspect outgoing iq request |
| 956 |
|
| 957 |
iq = self.stub.output[-1] |
| 958 |
self.assertEqual("get", iq.getAttribute("type")) |
| 959 |
self.assertNotIdentical(None, iq.query) |
| 960 |
self.assertEqual(NS_ROSTER, iq.query.uri) |
| 961 |
self.assertFalse(iq.query.hasAttribute("ver")) |
| 962 |
|
| 963 |
# Fake successful response |
| 964 |
response = toResponse(iq, "result") |
| 965 |
query = response.addElement((NS_ROSTER, "query")) |
| 966 |
item = query.addElement("item") |
| 967 |
item["jid"] = "[email protected]" |
| 968 |
|
| 969 |
d.callback(response) |
| 970 |
return d |
| 971 |
|
| 972 |
def test_getRosterVer(self): |
| 973 |
""" |
| 974 |
A request for the roster with version passes the version on. |
| 975 |
""" |
| 976 |
|
| 977 |
def cb(roster): |
| 978 |
self.assertEqual("ver96", getattr(roster, "version")) |
| 979 |
|
| 980 |
d = self.service.getRoster(version="ver72") |
| 981 |
d.addCallback(cb) |
| 982 |
|
| 983 |
# Inspect outgoing iq request |
| 984 |
|
| 985 |
iq = self.stub.output[-1] |
| 986 |
self.assertEqual("ver72", iq.query.getAttribute("ver")) |
| 987 |
|
| 988 |
# Fake successful response |
| 989 |
response = toResponse(iq, "result") |
| 990 |
query = response.addElement((NS_ROSTER, "query")) |
| 991 |
query["ver"] = "ver96" |
| 992 |
item = query.addElement("item") |
| 993 |
item["jid"] = "[email protected]" |
| 994 |
|
| 995 |
d.callback(response) |
| 996 |
return d |
| 997 |
|
| 998 |
def test_getRosterVerEmptyResult(self): |
| 999 |
""" |
| 1000 |
An empty response is returned as None. |
| 1001 |
""" |
| 1002 |
|
| 1003 |
def cb(response): |
| 1004 |
self.assertIdentical(None, response) |
| 1005 |
|
| 1006 |
d = self.service.getRoster(version="ver72") |
| 1007 |
d.addCallback(cb) |
| 1008 |
|
| 1009 |
# Inspect outgoing iq request |
| 1010 |
|
| 1011 |
iq = self.stub.output[-1] |
| 1012 |
|
| 1013 |
# Fake successful response |
| 1014 |
response = toResponse(iq, "result") |
| 1015 |
d.callback(response) |
| 1016 |
return d |
| 1017 |
|
| 1018 |
def test_setReceived(self): |
| 1019 |
""" |
| 1020 |
A roster set push causes setReceived. |
| 1021 |
""" |
| 1022 |
xml = """ |
| 1023 |
<iq type='set'> |
| 1024 |
<query xmlns='jabber:iq:roster'> |
| 1025 |
<item jid='[email protected]'/> |
| 1026 |
</query> |
| 1027 |
</iq> |
| 1028 |
""" |
| 1029 |
|
| 1030 |
requests = [] |
| 1031 |
|
| 1032 |
def setReceived(request): |
| 1033 |
requests.append(request) |
| 1034 |
|
| 1035 |
def cb(result): |
| 1036 |
self.assertEqual(1, len(requests), "setReceived was not called") |
| 1037 |
self.assertEqual(JID("[email protected]"), requests[0].item.entity) |
| 1038 |
|
| 1039 |
self.service.setReceived = setReceived |
| 1040 |
|
| 1041 |
d = self.handleRequest(xml) |
| 1042 |
d.addCallback(cb) |
| 1043 |
return d |
| 1044 |
|
| 1045 |
def test_setReceivedOtherSource(self): |
| 1046 |
""" |
| 1047 |
Roster pushes can be sent from other entities, too, ignore them. |
| 1048 |
""" |
| 1049 |
xml = """ |
| 1050 |
<iq type='set' to='[email protected]/Home' from='[email protected]'> |
| 1051 |
<query xmlns='jabber:iq:roster'> |
| 1052 |
<item jid='[email protected]'/> |
| 1053 |
</query> |
| 1054 |
</iq> |
| 1055 |
""" |
| 1056 |
|
| 1057 |
def cb(result): |
| 1058 |
self.assertEquals("service-unavailable", result.condition) |
| 1059 |
|
| 1060 |
d = self.handleRequest(xml) |
| 1061 |
self.assertFailure(d, error.StanzaError) |
| 1062 |
d.addCallback(cb) |
| 1063 |
return d |
| 1064 |
|
| 1065 |
def test_setReceivedOtherSourceAllowed(self): |
| 1066 |
""" |
| 1067 |
Roster pushes can be sent from other entities, allow them. |
| 1068 |
""" |
| 1069 |
xml = """ |
| 1070 |
<iq type='set' to='[email protected]/Home' from='[email protected]'> |
| 1071 |
<query xmlns='jabber:iq:roster'> |
| 1072 |
<item jid='[email protected]'/> |
| 1073 |
</query> |
| 1074 |
</iq> |
| 1075 |
""" |
| 1076 |
|
| 1077 |
self.service.allowAnySender = True |
| 1078 |
requests = [] |
| 1079 |
|
| 1080 |
def setReceived(request): |
| 1081 |
requests.append(request) |
| 1082 |
|
| 1083 |
def cb(result): |
| 1084 |
self.assertEqual(1, len(requests), "setReceived was not called") |
| 1085 |
|
| 1086 |
self.service.setReceived = setReceived |
| 1087 |
|
| 1088 |
d = self.handleRequest(xml) |
| 1089 |
d.addCallback(cb) |
| 1090 |
return d |
| 1091 |
|
| 1092 |
def test_setReceivedOtherSourceIgnored(self): |
| 1093 |
""" |
| 1094 |
Roster pushes can be sent from other entities, allow them. |
| 1095 |
""" |
| 1096 |
xml = """ |
| 1097 |
<iq type='set' to='[email protected]/Home' from='[email protected]'> |
| 1098 |
<query xmlns='jabber:iq:roster'> |
| 1099 |
<item jid='[email protected]'/> |
| 1100 |
</query> |
| 1101 |
</iq> |
| 1102 |
""" |
| 1103 |
|
| 1104 |
self.service.allowAnySender = True |
| 1105 |
|
| 1106 |
def setReceived(request): |
| 1107 |
if request.sender == JID("[email protected]"): |
| 1108 |
raise xmppim.RosterPushIgnored() |
| 1109 |
|
| 1110 |
def cb(result): |
| 1111 |
self.assertEquals("service-unavailable", result.condition) |
| 1112 |
|
| 1113 |
self.service.setReceived = setReceived |
| 1114 |
|
| 1115 |
d = self.handleRequest(xml) |
| 1116 |
self.assertFailure(d, error.StanzaError) |
| 1117 |
d.addCallback(cb) |
| 1118 |
return d |
| 1119 |
|
| 1120 |
def test_removeReceived(self): |
| 1121 |
""" |
| 1122 |
A roster remove push causes removeReceived. |
| 1123 |
""" |
| 1124 |
xml = """ |
| 1125 |
<iq type='set'> |
| 1126 |
<query xmlns='jabber:iq:roster'> |
| 1127 |
<item jid='[email protected]' subscription='remove'/> |
| 1128 |
</query> |
| 1129 |
</iq> |
| 1130 |
""" |
| 1131 |
|
| 1132 |
requests = [] |
| 1133 |
|
| 1134 |
def removeReceived(request): |
| 1135 |
requests.append(request) |
| 1136 |
|
| 1137 |
def cb(result): |
| 1138 |
self.assertEqual(1, len(requests), "removeReceived was not called") |
| 1139 |
self.assertEqual(JID("[email protected]"), requests[0].item.entity) |
| 1140 |
|
| 1141 |
self.service.removeReceived = removeReceived |
| 1142 |
|
| 1143 |
d = self.handleRequest(xml) |
| 1144 |
d.addCallback(cb) |
| 1145 |
return d |