| 1 |
# Copyright (c) Ralph Meijer. |
| 2 |
# See LICENSE for details. |
| 3 |
|
| 4 |
""" |
| 5 |
Tests for L{tx_xmpp.muc} |
| 6 |
""" |
| 7 |
|
| 8 |
from datetime import datetime |
| 9 |
from dateutil.tz import tzutc |
| 10 |
|
| 11 |
from zope.interface import verify |
| 12 |
|
| 13 |
from twisted.trial import unittest |
| 14 |
from twisted.internet import defer, task |
| 15 |
from twisted.words.xish import domish, xpath |
| 16 |
from twisted.words.protocols.jabber.jid import JID |
| 17 |
from twisted.words.protocols.jabber.error import StanzaError |
| 18 |
from twisted.words.protocols.jabber.xmlstream import TimeoutError, toResponse |
| 19 |
|
| 20 |
from .. import data_form, delay, itx_xmpp, muc |
| 21 |
from ..generic import parseXml |
| 22 |
from .helpers import TestableStreamManager |
| 23 |
|
| 24 |
NS_MUC_ADMIN = "http://jabber.org/protocol/muc#admin" |
| 25 |
|
| 26 |
|
| 27 |
def calledAsync(fn): |
| 28 |
""" |
| 29 |
Function wrapper that fires a deferred upon calling the given function. |
| 30 |
""" |
| 31 |
d = defer.Deferred() |
| 32 |
|
| 33 |
def func(*args, **kwargs): |
| 34 |
try: |
| 35 |
result = fn(*args, **kwargs) |
| 36 |
except: |
| 37 |
d.errback() |
| 38 |
else: |
| 39 |
d.callback(result) |
| 40 |
|
| 41 |
return d, func |
| 42 |
|
| 43 |
|
| 44 |
class StatusCodeTest(unittest.TestCase): |
| 45 |
""" |
| 46 |
Tests for L{muc.STATUS_CODE}. |
| 47 |
""" |
| 48 |
|
| 49 |
def test_lookupByValue(self): |
| 50 |
""" |
| 51 |
The registered MUC status codes map to STATUS_CODE value constants. |
| 52 |
|
| 53 |
Note: the identifiers used in the dictionary of status codes are |
| 54 |
borrowed from U{XEP-0306<http://xmpp.org/extensions/xep-0306.html>} |
| 55 |
that defines Extensible Status Conditions for Multi-User Chat. If this |
| 56 |
specification is implemented itself, the dictionary could move there. |
| 57 |
""" |
| 58 |
codes = { |
| 59 |
100: "realjid-public", |
| 60 |
101: "affiliation-changed", |
| 61 |
102: "unavailable-shown", |
| 62 |
103: "unavailable-not-shown", |
| 63 |
104: "configuration-changed", |
| 64 |
110: "self-presence", |
| 65 |
170: "logging-enabled", |
| 66 |
171: "logging-disabled", |
| 67 |
172: "non-anonymous", |
| 68 |
173: "semi-anonymous", |
| 69 |
174: "fully-anonymous", |
| 70 |
201: "room-created", |
| 71 |
210: "nick-assigned", |
| 72 |
301: "banned", |
| 73 |
303: "new-nick", |
| 74 |
307: "kicked", |
| 75 |
321: "removed-affiliation", |
| 76 |
322: "removed-membership", |
| 77 |
332: "removed-shutdown", |
| 78 |
} |
| 79 |
|
| 80 |
for code, condition in codes.items(): |
| 81 |
constantName = condition.replace("-", "_").upper() |
| 82 |
self.assertEqual( |
| 83 |
getattr(muc.STATUS_CODE, constantName), |
| 84 |
muc.STATUS_CODE.lookupByValue(code), |
| 85 |
) |
| 86 |
|
| 87 |
|
| 88 |
class StatusesTest(unittest.TestCase): |
| 89 |
""" |
| 90 |
Tests for L{muc.Statuses}. |
| 91 |
""" |
| 92 |
|
| 93 |
def setUp(self): |
| 94 |
self.mucStatuses = muc.Statuses() |
| 95 |
self.mucStatuses.add(muc.STATUS_CODE.SELF_PRESENCE) |
| 96 |
self.mucStatuses.add(muc.STATUS_CODE.ROOM_CREATED) |
| 97 |
|
| 98 |
def test_interface(self): |
| 99 |
""" |
| 100 |
Instances of L{Statuses} provide L{itx_xmpp.IMUCStatuses}. |
| 101 |
""" |
| 102 |
verify.verifyObject(itx_xmpp.IMUCStatuses, self.mucStatuses) |
| 103 |
|
| 104 |
def test_contains(self): |
| 105 |
""" |
| 106 |
The status contained are 'in' the container. |
| 107 |
""" |
| 108 |
self.assertIn(muc.STATUS_CODE.SELF_PRESENCE, self.mucStatuses) |
| 109 |
self.assertIn(muc.STATUS_CODE.ROOM_CREATED, self.mucStatuses) |
| 110 |
self.assertNotIn(muc.STATUS_CODE.NON_ANONYMOUS, self.mucStatuses) |
| 111 |
|
| 112 |
def test_iter(self): |
| 113 |
""" |
| 114 |
All statuses can be iterated over. |
| 115 |
""" |
| 116 |
statuses = set() |
| 117 |
for status in self.mucStatuses: |
| 118 |
statuses.add(status) |
| 119 |
|
| 120 |
self.assertEqual( |
| 121 |
set([muc.STATUS_CODE.SELF_PRESENCE, muc.STATUS_CODE.ROOM_CREATED]), statuses |
| 122 |
) |
| 123 |
|
| 124 |
def test_len(self): |
| 125 |
""" |
| 126 |
The number of items in this container is returned by C{__len__}. |
| 127 |
""" |
| 128 |
self.assertEqual(2, len(self.mucStatuses)) |
| 129 |
|
| 130 |
|
| 131 |
class GroupChatTest(unittest.TestCase): |
| 132 |
""" |
| 133 |
Tests for L{muc.GroupChat}. |
| 134 |
""" |
| 135 |
|
| 136 |
def test_toElementDelay(self): |
| 137 |
""" |
| 138 |
If the delay attribute is set, toElement has it rendered. |
| 139 |
""" |
| 140 |
message = muc.GroupChat() |
| 141 |
message.delay = delay.Delay( |
| 142 |
stamp=datetime(2002, 10, 13, 23, 58, 37, tzinfo=tzutc()) |
| 143 |
) |
| 144 |
|
| 145 |
element = message.toElement() |
| 146 |
|
| 147 |
query = "/message/delay[@xmlns='%s']" % (delay.NS_DELAY,) |
| 148 |
nodes = xpath.queryForNodes(query, element) |
| 149 |
self.assertNotIdentical(None, nodes, "Missing delay element") |
| 150 |
|
| 151 |
def test_toElementDelayLegacy(self): |
| 152 |
""" |
| 153 |
If legacy delay is requested, the legacy format is rendered. |
| 154 |
""" |
| 155 |
message = muc.GroupChat() |
| 156 |
message.delay = delay.Delay( |
| 157 |
stamp=datetime(2002, 10, 13, 23, 58, 37, tzinfo=tzutc()) |
| 158 |
) |
| 159 |
|
| 160 |
element = message.toElement(legacyDelay=True) |
| 161 |
|
| 162 |
query = "/message/x[@xmlns='%s']" % (delay.NS_JABBER_DELAY,) |
| 163 |
nodes = xpath.queryForNodes(query, element) |
| 164 |
self.assertNotIdentical(None, nodes, "Missing legacy delay element") |
| 165 |
|
| 166 |
|
| 167 |
class HistoryOptionsTest(unittest.TestCase): |
| 168 |
""" |
| 169 |
Tests for L{muc.HistoryOptionsTest}. |
| 170 |
""" |
| 171 |
|
| 172 |
def test_toElement(self): |
| 173 |
""" |
| 174 |
toElement renders the history element in the right namespace. |
| 175 |
""" |
| 176 |
history = muc.HistoryOptions() |
| 177 |
|
| 178 |
element = history.toElement() |
| 179 |
|
| 180 |
self.assertEqual(muc.NS_MUC, element.uri) |
| 181 |
self.assertEqual("history", element.name) |
| 182 |
|
| 183 |
def test_toElementMaxStanzas(self): |
| 184 |
""" |
| 185 |
If C{maxStanzas} is set, the element has the attribute C{'maxstanzas'}. |
| 186 |
""" |
| 187 |
history = muc.HistoryOptions(maxStanzas=10) |
| 188 |
|
| 189 |
element = history.toElement() |
| 190 |
|
| 191 |
self.assertEqual("10", element.getAttribute("maxstanzas")) |
| 192 |
|
| 193 |
def test_toElementSince(self): |
| 194 |
""" |
| 195 |
If C{since} is set, the attribute C{'since'} has a rendered timestamp. |
| 196 |
""" |
| 197 |
history = muc.HistoryOptions( |
| 198 |
since=datetime(2002, 10, 13, 23, 58, 37, tzinfo=tzutc()) |
| 199 |
) |
| 200 |
|
| 201 |
element = history.toElement() |
| 202 |
|
| 203 |
self.assertEqual("2002-10-13T23:58:37Z", element.getAttribute("since")) |
| 204 |
|
| 205 |
|
| 206 |
class UserPresenceTest(unittest.TestCase): |
| 207 |
""" |
| 208 |
Tests for L{muc.UserPresence}. |
| 209 |
""" |
| 210 |
|
| 211 |
def test_fromElementNoUserElement(self): |
| 212 |
""" |
| 213 |
Without user element, all associated attributes are None. |
| 214 |
""" |
| 215 |
xml = """ |
| 216 |
<presence from='[email protected]/thirdwitch' |
| 217 |
id='026B3509-2CCE-4D69-96D6-25F41FFDC408' |
| 218 |
to='[email protected]/pda'> |
| 219 |
</presence> |
| 220 |
""" |
| 221 |
|
| 222 |
element = parseXml(xml) |
| 223 |
presence = muc.UserPresence.fromElement(element) |
| 224 |
|
| 225 |
self.assertIdentical(None, presence.affiliation) |
| 226 |
self.assertIdentical(None, presence.role) |
| 227 |
self.assertIdentical(None, presence.entity) |
| 228 |
self.assertIdentical(None, presence.nick) |
| 229 |
self.assertEqual(0, len(presence.mucStatuses)) |
| 230 |
|
| 231 |
def test_fromElementUnknownChild(self): |
| 232 |
""" |
| 233 |
Unknown child elements are ignored. |
| 234 |
""" |
| 235 |
xml = """ |
| 236 |
<presence from='[email protected]/thirdwitch' |
| 237 |
id='026B3509-2CCE-4D69-96D6-25F41FFDC408' |
| 238 |
to='[email protected]/pda'> |
| 239 |
<x xmlns='http://jabber.org/protocol/muc#user'> |
| 240 |
<status xmlns='myns' code='110'/> |
| 241 |
</x> |
| 242 |
</presence> |
| 243 |
""" |
| 244 |
|
| 245 |
element = parseXml(xml) |
| 246 |
presence = muc.UserPresence.fromElement(element) |
| 247 |
|
| 248 |
self.assertEqual(0, len(presence.mucStatuses)) |
| 249 |
|
| 250 |
def test_fromElementStatusOne(self): |
| 251 |
""" |
| 252 |
Status codes are extracted. |
| 253 |
""" |
| 254 |
xml = """ |
| 255 |
<presence from='[email protected]/thirdwitch' |
| 256 |
id='026B3509-2CCE-4D69-96D6-25F41FFDC408' |
| 257 |
to='[email protected]/pda'> |
| 258 |
<x xmlns='http://jabber.org/protocol/muc#user'> |
| 259 |
<item affiliation='member' role='participant'/> |
| 260 |
<status code='110'/> |
| 261 |
</x> |
| 262 |
</presence> |
| 263 |
""" |
| 264 |
|
| 265 |
element = parseXml(xml) |
| 266 |
presence = muc.UserPresence.fromElement(element) |
| 267 |
|
| 268 |
self.assertIn(muc.STATUS_CODE.SELF_PRESENCE, presence.mucStatuses) |
| 269 |
|
| 270 |
def test_fromElementStatusMultiple(self): |
| 271 |
""" |
| 272 |
Multiple status codes are all extracted. |
| 273 |
""" |
| 274 |
xml = """ |
| 275 |
<presence from='[email protected]/thirdwitch' |
| 276 |
id='026B3509-2CCE-4D69-96D6-25F41FFDC408' |
| 277 |
to='[email protected]/pda'> |
| 278 |
<x xmlns='http://jabber.org/protocol/muc#user'> |
| 279 |
<item affiliation='member' role='participant'/> |
| 280 |
<status code='100'/> |
| 281 |
<status code='110'/> |
| 282 |
</x> |
| 283 |
</presence> |
| 284 |
""" |
| 285 |
|
| 286 |
element = parseXml(xml) |
| 287 |
presence = muc.UserPresence.fromElement(element) |
| 288 |
|
| 289 |
self.assertIn(muc.STATUS_CODE.SELF_PRESENCE, presence.mucStatuses) |
| 290 |
self.assertIn(muc.STATUS_CODE.REALJID_PUBLIC, presence.mucStatuses) |
| 291 |
|
| 292 |
def test_fromElementStatusEmpty(self): |
| 293 |
""" |
| 294 |
Empty status elements are ignored. |
| 295 |
""" |
| 296 |
xml = """ |
| 297 |
<presence from='[email protected]/thirdwitch' |
| 298 |
id='026B3509-2CCE-4D69-96D6-25F41FFDC408' |
| 299 |
to='[email protected]/pda'> |
| 300 |
<x xmlns='http://jabber.org/protocol/muc#user'> |
| 301 |
<item affiliation='member' role='participant'/> |
| 302 |
<status/> |
| 303 |
</x> |
| 304 |
</presence> |
| 305 |
""" |
| 306 |
|
| 307 |
element = parseXml(xml) |
| 308 |
presence = muc.UserPresence.fromElement(element) |
| 309 |
|
| 310 |
self.assertEqual(0, len(presence.mucStatuses)) |
| 311 |
|
| 312 |
def test_fromElementStatusBad(self): |
| 313 |
""" |
| 314 |
Bad status codes are ignored. |
| 315 |
""" |
| 316 |
xml = """ |
| 317 |
<presence from='[email protected]/thirdwitch' |
| 318 |
id='026B3509-2CCE-4D69-96D6-25F41FFDC408' |
| 319 |
to='[email protected]/pda'> |
| 320 |
<x xmlns='http://jabber.org/protocol/muc#user'> |
| 321 |
<item affiliation='member' role='participant'/> |
| 322 |
<status code="badvalue"/> |
| 323 |
</x> |
| 324 |
</presence> |
| 325 |
""" |
| 326 |
|
| 327 |
element = parseXml(xml) |
| 328 |
presence = muc.UserPresence.fromElement(element) |
| 329 |
|
| 330 |
self.assertEqual(0, len(presence.mucStatuses)) |
| 331 |
|
| 332 |
def test_fromElementStatusUnknown(self): |
| 333 |
""" |
| 334 |
Unknown status codes are not recorded in C{mucStatuses}. |
| 335 |
""" |
| 336 |
xml = """ |
| 337 |
<presence from='[email protected]/thirdwitch' |
| 338 |
id='026B3509-2CCE-4D69-96D6-25F41FFDC408' |
| 339 |
to='[email protected]/pda'> |
| 340 |
<x xmlns='http://jabber.org/protocol/muc#user'> |
| 341 |
<item affiliation='member' role='participant'/> |
| 342 |
<status code="999"/> |
| 343 |
</x> |
| 344 |
</presence> |
| 345 |
""" |
| 346 |
|
| 347 |
element = parseXml(xml) |
| 348 |
presence = muc.UserPresence.fromElement(element) |
| 349 |
|
| 350 |
self.assertEqual(0, len(presence.mucStatuses)) |
| 351 |
|
| 352 |
def test_fromElementItem(self): |
| 353 |
""" |
| 354 |
Item attributes are parsed properly. |
| 355 |
""" |
| 356 |
xml = """ |
| 357 |
<presence from='[email protected]/thirdwitch' |
| 358 |
to='[email protected]/desktop'> |
| 359 |
<x xmlns='http://jabber.org/protocol/muc#user'> |
| 360 |
<item affiliation='member' |
| 361 |
jid='[email protected]/pda' |
| 362 |
role='participant' |
| 363 |
nick='thirdwitch'/> |
| 364 |
</x> |
| 365 |
</presence> |
| 366 |
""" |
| 367 |
|
| 368 |
element = parseXml(xml) |
| 369 |
presence = muc.UserPresence.fromElement(element) |
| 370 |
self.assertEqual("member", presence.affiliation) |
| 371 |
self.assertEqual("participant", presence.role) |
| 372 |
self.assertEqual(JID("[email protected]/pda"), presence.entity) |
| 373 |
self.assertEqual("thirdwitch", presence.nick) |
| 374 |
|
| 375 |
|
| 376 |
class MUCClientProtocolTest(unittest.TestCase): |
| 377 |
""" |
| 378 |
Tests for L{muc.MUCClientProtocol}. |
| 379 |
""" |
| 380 |
|
| 381 |
def setUp(self): |
| 382 |
self.clock = task.Clock() |
| 383 |
self.sessionManager = TestableStreamManager(reactor=self.clock) |
| 384 |
self.stub = self.sessionManager.stub |
| 385 |
self.protocol = muc.MUCClientProtocol(reactor=self.clock) |
| 386 |
self.protocol.setHandlerParent(self.sessionManager) |
| 387 |
|
| 388 |
self.roomIdentifier = "test" |
| 389 |
self.service = "conference.example.org" |
| 390 |
self.nick = "Nick" |
| 391 |
|
| 392 |
self.occupantJID = JID(tuple=(self.roomIdentifier, self.service, self.nick)) |
| 393 |
self.roomJID = self.occupantJID.userhostJID() |
| 394 |
self.userJID = JID("[email protected]/Testing") |
| 395 |
|
| 396 |
def test_initNoReactor(self): |
| 397 |
""" |
| 398 |
If no reactor is passed, the default reactor is used. |
| 399 |
""" |
| 400 |
protocol = muc.MUCClientProtocol() |
| 401 |
from twisted.internet import reactor |
| 402 |
|
| 403 |
self.assertEqual(reactor, protocol._reactor) |
| 404 |
|
| 405 |
def test_groupChatReceived(self): |
| 406 |
""" |
| 407 |
Messages of type groupchat are parsed and passed to L{groupChatReceived}. |
| 408 |
""" |
| 409 |
xml = """ |
| 410 |
<message to='[email protected]' from='%s' type='groupchat'> |
| 411 |
<body>test</body> |
| 412 |
</message> |
| 413 |
""" % (self.occupantJID) |
| 414 |
|
| 415 |
def groupChatReceived(message): |
| 416 |
self.assertEquals("test", message.body, "Wrong group chat message") |
| 417 |
self.assertEquals( |
| 418 |
self.roomIdentifier, message.sender.user, "Wrong room identifier" |
| 419 |
) |
| 420 |
|
| 421 |
d, self.protocol.groupChatReceived = calledAsync(groupChatReceived) |
| 422 |
self.stub.send(parseXml(xml)) |
| 423 |
return d |
| 424 |
|
| 425 |
def test_groupChatReceivedNotOverridden(self): |
| 426 |
""" |
| 427 |
If L{groupChatReceived} has not been overridden, no errors should occur. |
| 428 |
""" |
| 429 |
xml = """ |
| 430 |
<message to='[email protected]' from='%s' type='groupchat'> |
| 431 |
<body>test</body> |
| 432 |
</message> |
| 433 |
""" % (self.occupantJID) |
| 434 |
|
| 435 |
self.stub.send(parseXml(xml)) |
| 436 |
|
| 437 |
def test_join(self): |
| 438 |
""" |
| 439 |
Joining a room waits for confirmation, deferred fires user presence. |
| 440 |
""" |
| 441 |
|
| 442 |
def cb(presence): |
| 443 |
self.assertEquals(self.occupantJID, presence.sender) |
| 444 |
|
| 445 |
# Join the room |
| 446 |
d = self.protocol.join(self.roomJID, self.nick) |
| 447 |
d.addCallback(cb) |
| 448 |
|
| 449 |
element = self.stub.output[-1] |
| 450 |
|
| 451 |
self.assertEquals("presence", element.name, "Need to be presence") |
| 452 |
self.assertNotIdentical(None, element.x, "No muc x element") |
| 453 |
|
| 454 |
# send back user presence, they joined |
| 455 |
xml = """ |
| 456 |
<presence from='%s@%s/%s'> |
| 457 |
<x xmlns='http://jabber.org/protocol/muc#user'> |
| 458 |
<item affiliation='member' role='participant'/> |
| 459 |
</x> |
| 460 |
</presence> |
| 461 |
""" % (self.roomIdentifier, self.service, self.nick) |
| 462 |
self.stub.send(parseXml(xml)) |
| 463 |
return d |
| 464 |
|
| 465 |
def test_joinHistory(self): |
| 466 |
""" |
| 467 |
Passing a history parameter sends a 'maxStanzas' history limit. |
| 468 |
""" |
| 469 |
|
| 470 |
historyOptions = muc.HistoryOptions(maxStanzas=10) |
| 471 |
d = self.protocol.join(self.roomJID, self.nick, historyOptions) |
| 472 |
|
| 473 |
element = self.stub.output[-1] |
| 474 |
query = "/*/x[@xmlns='%s']/history[@xmlns='%s']" % (muc.NS_MUC, muc.NS_MUC) |
| 475 |
result = xpath.queryForNodes(query, element) |
| 476 |
history = result[0] |
| 477 |
self.assertEquals("10", history.getAttribute("maxstanzas")) |
| 478 |
|
| 479 |
# send back user presence, they joined |
| 480 |
xml = """ |
| 481 |
<presence from='%s@%s/%s'> |
| 482 |
<x xmlns='http://jabber.org/protocol/muc#user'> |
| 483 |
<item affiliation='member' role='participant'/> |
| 484 |
</x> |
| 485 |
</presence> |
| 486 |
""" % (self.roomIdentifier, self.service, self.nick) |
| 487 |
self.stub.send(parseXml(xml)) |
| 488 |
return d |
| 489 |
|
| 490 |
def test_joinForbidden(self): |
| 491 |
""" |
| 492 |
A forbidden error in response to a join errbacks with L{StanzaError}. |
| 493 |
""" |
| 494 |
|
| 495 |
def cb(error): |
| 496 |
self.assertEquals("forbidden", error.condition, "Wrong muc condition") |
| 497 |
|
| 498 |
d = self.protocol.join(self.roomJID, self.nick) |
| 499 |
self.assertFailure(d, StanzaError) |
| 500 |
d.addCallback(cb) |
| 501 |
|
| 502 |
# send back error, forbidden |
| 503 |
xml = """ |
| 504 |
<presence from='%s' type='error'> |
| 505 |
<error type='auth'> |
| 506 |
<forbidden xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/> |
| 507 |
</error> |
| 508 |
</presence> |
| 509 |
""" % (self.occupantJID) |
| 510 |
self.stub.send(parseXml(xml)) |
| 511 |
return d |
| 512 |
|
| 513 |
def test_joinForbiddenFromRoomJID(self): |
| 514 |
""" |
| 515 |
An error response to a join sent from the room JID should errback. |
| 516 |
|
| 517 |
Some service implementations send error stanzas from the room JID |
| 518 |
instead of the JID the join presence was sent to. |
| 519 |
""" |
| 520 |
|
| 521 |
d = self.protocol.join(self.roomJID, self.nick) |
| 522 |
self.assertFailure(d, StanzaError) |
| 523 |
|
| 524 |
# send back error, forbidden |
| 525 |
xml = """ |
| 526 |
<presence from='%s' type='error'> |
| 527 |
<error type='auth'> |
| 528 |
<forbidden xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/> |
| 529 |
</error> |
| 530 |
</presence> |
| 531 |
""" % (self.roomJID) |
| 532 |
self.stub.send(parseXml(xml)) |
| 533 |
return d |
| 534 |
|
| 535 |
def test_joinBadJID(self): |
| 536 |
""" |
| 537 |
Client joining a room and getting a jid-malformed error. |
| 538 |
""" |
| 539 |
|
| 540 |
def cb(error): |
| 541 |
self.assertEquals("jid-malformed", error.condition, "Wrong muc condition") |
| 542 |
|
| 543 |
d = self.protocol.join(self.roomJID, self.nick) |
| 544 |
self.assertFailure(d, StanzaError) |
| 545 |
d.addCallback(cb) |
| 546 |
|
| 547 |
# send back error, bad JID |
| 548 |
xml = """ |
| 549 |
<presence from='%s' type='error'> |
| 550 |
<error type='modify'> |
| 551 |
<jid-malformed xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/> |
| 552 |
</error> |
| 553 |
</presence> |
| 554 |
""" % (self.occupantJID) |
| 555 |
self.stub.send(parseXml(xml)) |
| 556 |
return d |
| 557 |
|
| 558 |
def test_joinTimeout(self): |
| 559 |
""" |
| 560 |
After not receiving a response to a join, errback with L{TimeoutError}. |
| 561 |
""" |
| 562 |
|
| 563 |
d = self.protocol.join(self.roomJID, self.nick) |
| 564 |
self.assertFailure(d, TimeoutError) |
| 565 |
self.clock.advance(muc.DEFER_TIMEOUT) |
| 566 |
return d |
| 567 |
|
| 568 |
def test_joinPassword(self): |
| 569 |
""" |
| 570 |
Sending a password via presence to a password protected room. |
| 571 |
""" |
| 572 |
|
| 573 |
self.protocol.join(self.roomJID, self.nick, password="secret") |
| 574 |
|
| 575 |
element = self.stub.output[-1] |
| 576 |
|
| 577 |
self.assertTrue( |
| 578 |
xpath.matches( |
| 579 |
"/presence[@to='%s']/x/password" |
| 580 |
"[text()='secret']" % (self.occupantJID,), |
| 581 |
element, |
| 582 |
), |
| 583 |
"Wrong presence stanza", |
| 584 |
) |
| 585 |
|
| 586 |
def test_nick(self): |
| 587 |
""" |
| 588 |
Send a nick change to the server. |
| 589 |
""" |
| 590 |
newNick = "newNick" |
| 591 |
|
| 592 |
def cb(presence): |
| 593 |
self.assertEquals( |
| 594 |
JID(tuple=(self.roomIdentifier, self.service, newNick)), presence.sender |
| 595 |
) |
| 596 |
|
| 597 |
d = self.protocol.nick(self.roomJID, newNick) |
| 598 |
d.addCallback(cb) |
| 599 |
|
| 600 |
element = self.stub.output[-1] |
| 601 |
self.assertEquals("presence", element.name, "Need to be presence") |
| 602 |
self.assertNotIdentical(None, element.x, "No muc x element") |
| 603 |
|
| 604 |
# send back user presence, nick changed |
| 605 |
xml = """ |
| 606 |
<presence from='%s/%s'> |
| 607 |
<x xmlns='http://jabber.org/protocol/muc#user'> |
| 608 |
<item affiliation='member' role='participant'/> |
| 609 |
</x> |
| 610 |
</presence> |
| 611 |
""" % (self.roomJID, newNick) |
| 612 |
self.stub.send(parseXml(xml)) |
| 613 |
return d |
| 614 |
|
| 615 |
def test_nickConflict(self): |
| 616 |
""" |
| 617 |
If the server finds the new nick in conflict, the errback is called. |
| 618 |
""" |
| 619 |
newNick = "newNick" |
| 620 |
|
| 621 |
d = self.protocol.nick(self.roomJID, newNick) |
| 622 |
self.assertFailure(d, StanzaError) |
| 623 |
|
| 624 |
element = self.stub.output[-1] |
| 625 |
self.assertEquals("presence", element.name, "Need to be presence") |
| 626 |
self.assertNotIdentical(None, element.x, "No muc x element") |
| 627 |
|
| 628 |
# send back error presence, nick conflicted |
| 629 |
xml = """ |
| 630 |
<presence from='%s/%s' type='error'> |
| 631 |
<x xmlns='http://jabber.org/protocol/muc'/> |
| 632 |
<error type='cancel'> |
| 633 |
<conflict xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/> |
| 634 |
</error> |
| 635 |
</presence> |
| 636 |
""" % (self.roomJID, newNick) |
| 637 |
self.stub.send(parseXml(xml)) |
| 638 |
return d |
| 639 |
|
| 640 |
def test_status(self): |
| 641 |
""" |
| 642 |
Change status |
| 643 |
""" |
| 644 |
|
| 645 |
def joined(_): |
| 646 |
d = self.protocol.status(self.roomJID, "xa", "testing MUC") |
| 647 |
d.addCallback(statusChanged) |
| 648 |
return d |
| 649 |
|
| 650 |
def statusChanged(presence): |
| 651 |
self.assertEqual(self.occupantJID, presence.sender) |
| 652 |
|
| 653 |
# Join the room |
| 654 |
d = self.protocol.join(self.roomJID, self.nick) |
| 655 |
d.addCallback(joined) |
| 656 |
|
| 657 |
# Receive presence back from the room: joined. |
| 658 |
xml = """ |
| 659 |
<presence to='%s' from='%s'/> |
| 660 |
""" % (self.userJID, self.occupantJID) |
| 661 |
self.stub.send(parseXml(xml)) |
| 662 |
|
| 663 |
# The presence for the status change should have been sent now. |
| 664 |
element = self.stub.output[-1] |
| 665 |
|
| 666 |
self.assertEquals("presence", element.name, "Need to be presence") |
| 667 |
self.assertTrue(getattr(element, "x", None), "No muc x element") |
| 668 |
|
| 669 |
# send back user presence, status changed |
| 670 |
xml = """ |
| 671 |
<presence from='%s'> |
| 672 |
<x xmlns='http://jabber.org/protocol/muc#user'> |
| 673 |
<item affiliation='member' role='participant'/> |
| 674 |
</x> |
| 675 |
<show>xa</show> |
| 676 |
<status>testing MUC</status> |
| 677 |
</presence> |
| 678 |
""" % self.occupantJID |
| 679 |
self.stub.send(parseXml(xml)) |
| 680 |
|
| 681 |
return d |
| 682 |
|
| 683 |
def test_leave(self): |
| 684 |
""" |
| 685 |
Client leaves a room |
| 686 |
""" |
| 687 |
|
| 688 |
def joined(_): |
| 689 |
return self.protocol.leave(self.roomJID) |
| 690 |
|
| 691 |
# Join the room |
| 692 |
d = self.protocol.join(self.roomJID, self.nick) |
| 693 |
d.addCallback(joined) |
| 694 |
|
| 695 |
# Receive presence back from the room: joined. |
| 696 |
xml = """ |
| 697 |
<presence to='%s' from='%s'/> |
| 698 |
""" % (self.userJID, self.occupantJID) |
| 699 |
self.stub.send(parseXml(xml)) |
| 700 |
|
| 701 |
# The presence for leaving the room should have been sent now. |
| 702 |
element = self.stub.output[-1] |
| 703 |
|
| 704 |
self.assertEquals("unavailable", element["type"], "Unavailable is not being sent") |
| 705 |
|
| 706 |
# Receive presence back from the room: left. |
| 707 |
xml = """ |
| 708 |
<presence to='%s' from='%s' type='unavailable'/> |
| 709 |
""" % (self.userJID, self.occupantJID) |
| 710 |
self.stub.send(parseXml(xml)) |
| 711 |
|
| 712 |
return d |
| 713 |
|
| 714 |
def test_groupChat(self): |
| 715 |
""" |
| 716 |
Send private messages to muc entities. |
| 717 |
""" |
| 718 |
self.protocol.groupChat(self.roomJID, "This is a test") |
| 719 |
|
| 720 |
message = self.stub.output[-1] |
| 721 |
|
| 722 |
self.assertEquals("message", message.name) |
| 723 |
self.assertEquals(self.roomJID.full(), message.getAttribute("to")) |
| 724 |
self.assertEquals("groupchat", message.getAttribute("type")) |
| 725 |
self.assertEquals("This is a test", str(message.body)) |
| 726 |
|
| 727 |
def test_chat(self): |
| 728 |
""" |
| 729 |
Send private messages to muc entities. |
| 730 |
""" |
| 731 |
otherOccupantJID = JID(self.occupantJID.userhost() + "/OtherNick") |
| 732 |
|
| 733 |
self.protocol.chat(otherOccupantJID, "This is a test") |
| 734 |
|
| 735 |
message = self.stub.output[-1] |
| 736 |
|
| 737 |
self.assertEquals("message", message.name) |
| 738 |
self.assertEquals(otherOccupantJID.full(), message.getAttribute("to")) |
| 739 |
self.assertEquals("chat", message.getAttribute("type")) |
| 740 |
self.assertEquals("This is a test", str(message.body)) |
| 741 |
|
| 742 |
def test_subject(self): |
| 743 |
""" |
| 744 |
Change subject of the room. |
| 745 |
""" |
| 746 |
self.protocol.subject(self.roomJID, "This is a test") |
| 747 |
|
| 748 |
message = self.stub.output[-1] |
| 749 |
|
| 750 |
self.assertEquals("message", message.name) |
| 751 |
self.assertEquals(self.roomJID.full(), message.getAttribute("to")) |
| 752 |
self.assertEquals("groupchat", message.getAttribute("type")) |
| 753 |
self.assertEquals("This is a test", str(message.subject)) |
| 754 |
|
| 755 |
def test_invite(self): |
| 756 |
""" |
| 757 |
Invite a user to a room |
| 758 |
""" |
| 759 |
invitee = JID("[email protected]") |
| 760 |
|
| 761 |
self.protocol.invite(self.roomJID, invitee, "This is a test") |
| 762 |
|
| 763 |
message = self.stub.output[-1] |
| 764 |
|
| 765 |
self.assertEquals("message", message.name) |
| 766 |
self.assertEquals(self.roomJID.full(), message.getAttribute("to")) |
| 767 |
self.assertEquals(muc.NS_MUC_USER, message.x.uri) |
| 768 |
self.assertEquals(muc.NS_MUC_USER, message.x.invite.uri) |
| 769 |
self.assertEquals(invitee.full(), message.x.invite.getAttribute("to")) |
| 770 |
self.assertEquals(muc.NS_MUC_USER, message.x.invite.reason.uri) |
| 771 |
self.assertEquals("This is a test", str(message.x.invite.reason)) |
| 772 |
|
| 773 |
def test_getRegisterForm(self): |
| 774 |
""" |
| 775 |
The response of a register form request should extract the form. |
| 776 |
""" |
| 777 |
|
| 778 |
def cb(form): |
| 779 |
self.assertEquals("form", form.formType) |
| 780 |
|
| 781 |
d = self.protocol.getRegisterForm(self.roomJID) |
| 782 |
d.addCallback(cb) |
| 783 |
|
| 784 |
iq = self.stub.output[-1] |
| 785 |
|
| 786 |
query = "/iq/query[@xmlns='%s']" % (muc.NS_REGISTER) |
| 787 |
nodes = xpath.queryForNodes(query, iq) |
| 788 |
self.assertNotIdentical(None, nodes, "Missing query element") |
| 789 |
|
| 790 |
self.assertRaises(StopIteration, next, nodes[0].elements()) |
| 791 |
|
| 792 |
xml = """ |
| 793 |
<iq from='%s' id='%s' to='%s' type='result'> |
| 794 |
<query xmlns='jabber:iq:register'> |
| 795 |
<x xmlns='jabber:x:data' type='form'> |
| 796 |
<field type='hidden' |
| 797 |
var='FORM_TYPE'> |
| 798 |
<value>http://jabber.org/protocol/muc#register</value> |
| 799 |
</field> |
| 800 |
<field label='Desired Nickname' |
| 801 |
type='text-single' |
| 802 |
var='muc#register_roomnick'> |
| 803 |
<required/> |
| 804 |
</field> |
| 805 |
</x> |
| 806 |
</query> |
| 807 |
</iq> |
| 808 |
""" % (self.roomJID, iq["id"], self.userJID) |
| 809 |
self.stub.send(parseXml(xml)) |
| 810 |
|
| 811 |
return d |
| 812 |
|
| 813 |
def test_register(self): |
| 814 |
""" |
| 815 |
Client registering with a room. |
| 816 |
|
| 817 |
http://xmpp.org/extensions/xep-0045.html#register |
| 818 |
""" |
| 819 |
|
| 820 |
def cb(iq): |
| 821 |
# check for a result |
| 822 |
self.assertEquals("result", iq["type"], "We did not get a result") |
| 823 |
|
| 824 |
d = self.protocol.register(self.roomJID, {"muc#register_roomnick": "thirdwitch"}) |
| 825 |
d.addCallback(cb) |
| 826 |
|
| 827 |
iq = self.stub.output[-1] |
| 828 |
|
| 829 |
query = "/iq/query[@xmlns='%s']" % muc.NS_REGISTER |
| 830 |
nodes = xpath.queryForNodes(query, iq) |
| 831 |
self.assertNotIdentical(None, nodes, "Invalid registration request") |
| 832 |
|
| 833 |
form = data_form.findForm(nodes[0], muc.NS_MUC_REGISTER) |
| 834 |
self.assertNotIdentical(None, form, "Missing registration form") |
| 835 |
self.assertEquals("submit", form.formType) |
| 836 |
self.assertIn("muc#register_roomnick", form.fields) |
| 837 |
|
| 838 |
response = toResponse(iq, "result") |
| 839 |
self.stub.send(response) |
| 840 |
return d |
| 841 |
|
| 842 |
def test_registerCancel(self): |
| 843 |
""" |
| 844 |
Cancelling a registration request sends a cancel form. |
| 845 |
""" |
| 846 |
|
| 847 |
d = self.protocol.register(self.roomJID, None) |
| 848 |
|
| 849 |
iq = self.stub.output[-1] |
| 850 |
|
| 851 |
query = "/iq/query[@xmlns='%s']" % muc.NS_REGISTER |
| 852 |
nodes = xpath.queryForNodes(query, iq) |
| 853 |
self.assertNotIdentical(None, nodes, "Invalid registration request") |
| 854 |
|
| 855 |
form = data_form.findForm(nodes[0], muc.NS_MUC_REGISTER) |
| 856 |
self.assertNotIdentical(None, form, "Missing registration form") |
| 857 |
self.assertEquals("cancel", form.formType) |
| 858 |
|
| 859 |
response = toResponse(iq, "result") |
| 860 |
self.stub.send(response) |
| 861 |
return d |
| 862 |
|
| 863 |
def test_voice(self): |
| 864 |
""" |
| 865 |
Client requesting voice for a room. |
| 866 |
""" |
| 867 |
self.protocol.voice(self.occupantJID) |
| 868 |
|
| 869 |
m = self.stub.output[-1] |
| 870 |
|
| 871 |
query = ( |
| 872 |
"/message/x[@type='submit']/field/value" "[text()='%s']" |
| 873 |
) % muc.NS_MUC_REQUEST |
| 874 |
self.assertTrue(xpath.matches(query, m), "Invalid voice message stanza") |
| 875 |
|
| 876 |
def test_history(self): |
| 877 |
""" |
| 878 |
Converting a one to one chat to a multi-user chat. |
| 879 |
""" |
| 880 |
archive = [] |
| 881 |
thread = "e0ffe42b28561960c6b12b944a092794b9683a38" |
| 882 |
# create messages |
| 883 |
element = domish.Element((None, "message")) |
| 884 |
element["to"] = "[email protected]" |
| 885 |
element["type"] = "chat" |
| 886 |
element.addElement("body", None, "test") |
| 887 |
element.addElement("thread", None, thread) |
| 888 |
|
| 889 |
archive.append( |
| 890 |
{ |
| 891 |
"stanza": element, |
| 892 |
"timestamp": datetime(2002, 10, 13, 23, 58, 37, tzinfo=tzutc()), |
| 893 |
} |
| 894 |
) |
| 895 |
|
| 896 |
element = domish.Element((None, "message")) |
| 897 |
element["to"] = "[email protected]" |
| 898 |
element["type"] = "chat" |
| 899 |
element.addElement("body", None, "yo") |
| 900 |
element.addElement("thread", None, thread) |
| 901 |
|
| 902 |
archive.append( |
| 903 |
{ |
| 904 |
"stanza": element, |
| 905 |
"timestamp": datetime(2002, 10, 13, 23, 58, 43, tzinfo=tzutc()), |
| 906 |
} |
| 907 |
) |
| 908 |
|
| 909 |
self.protocol.history(self.occupantJID, archive) |
| 910 |
|
| 911 |
while len(self.stub.output) > 0: |
| 912 |
element = self.stub.output.pop() |
| 913 |
# check for delay element |
| 914 |
self.assertEquals("message", element.name, "Wrong stanza") |
| 915 |
self.assertTrue( |
| 916 |
xpath.matches("/message/delay", element), "Invalid history stanza" |
| 917 |
) |
| 918 |
|
| 919 |
def test_getConfiguration(self): |
| 920 |
""" |
| 921 |
The response of a configure form request should extract the form. |
| 922 |
""" |
| 923 |
|
| 924 |
def cb(form): |
| 925 |
self.assertEquals("form", form.formType) |
| 926 |
|
| 927 |
d = self.protocol.getConfiguration(self.roomJID) |
| 928 |
d.addCallback(cb) |
| 929 |
|
| 930 |
iq = self.stub.output[-1] |
| 931 |
|
| 932 |
query = "/iq/query[@xmlns='%s']" % (muc.NS_MUC_OWNER) |
| 933 |
nodes = xpath.queryForNodes(query, iq) |
| 934 |
self.assertNotIdentical(None, nodes, "Missing query element") |
| 935 |
|
| 936 |
self.assertRaises(StopIteration, next, nodes[0].elements()) |
| 937 |
|
| 938 |
xml = """ |
| 939 |
<iq from='%s' id='%s' to='%s' type='result'> |
| 940 |
<query xmlns='http://jabber.org/protocol/muc#owner'> |
| 941 |
<x xmlns='jabber:x:data' type='form'> |
| 942 |
<field type='hidden' |
| 943 |
var='FORM_TYPE'> |
| 944 |
<value>http://jabber.org/protocol/muc#roomconfig</value> |
| 945 |
</field> |
| 946 |
<field label='Natural-Language Room Name' |
| 947 |
type='text-single' |
| 948 |
var='muc#roomconfig_roomname'/> |
| 949 |
</x> |
| 950 |
</query> |
| 951 |
</iq> |
| 952 |
""" % (self.roomJID, iq["id"], self.userJID) |
| 953 |
self.stub.send(parseXml(xml)) |
| 954 |
|
| 955 |
return d |
| 956 |
|
| 957 |
def test_getConfigurationNoOptions(self): |
| 958 |
""" |
| 959 |
The response of a configure form request should extract the form. |
| 960 |
""" |
| 961 |
|
| 962 |
def cb(form): |
| 963 |
self.assertIdentical(None, form) |
| 964 |
|
| 965 |
d = self.protocol.getConfiguration(self.roomJID) |
| 966 |
d.addCallback(cb) |
| 967 |
|
| 968 |
iq = self.stub.output[-1] |
| 969 |
|
| 970 |
xml = """ |
| 971 |
<iq from='%s' id='%s' to='%s' type='result'> |
| 972 |
<query xmlns='http://jabber.org/protocol/muc#owner'/> |
| 973 |
</iq> |
| 974 |
""" % (self.roomJID, iq["id"], self.userJID) |
| 975 |
self.stub.send(parseXml(xml)) |
| 976 |
|
| 977 |
return d |
| 978 |
|
| 979 |
def test_configure(self): |
| 980 |
""" |
| 981 |
Default configure and changing the room name. |
| 982 |
""" |
| 983 |
|
| 984 |
def cb(iq): |
| 985 |
self.assertEquals("result", iq["type"], "Not a result") |
| 986 |
|
| 987 |
values = {"muc#roomconfig_roomname": self.roomIdentifier} |
| 988 |
|
| 989 |
d = self.protocol.configure(self.roomJID, values) |
| 990 |
d.addCallback(cb) |
| 991 |
|
| 992 |
iq = self.stub.output[-1] |
| 993 |
|
| 994 |
self.assertEquals("set", iq.getAttribute("type")) |
| 995 |
self.assertEquals(self.roomJID.full(), iq.getAttribute("to")) |
| 996 |
|
| 997 |
query = "/iq/query[@xmlns='%s']" % (muc.NS_MUC_OWNER) |
| 998 |
nodes = xpath.queryForNodes(query, iq) |
| 999 |
self.assertNotIdentical(None, nodes, "Bad configure request") |
| 1000 |
|
| 1001 |
form = data_form.findForm(nodes[0], muc.NS_MUC_CONFIG) |
| 1002 |
self.assertNotIdentical(None, form, "Missing configuration form") |
| 1003 |
self.assertEquals("submit", form.formType) |
| 1004 |
|
| 1005 |
response = toResponse(iq, "result") |
| 1006 |
self.stub.send(response) |
| 1007 |
return d |
| 1008 |
|
| 1009 |
def test_configureEmpty(self): |
| 1010 |
""" |
| 1011 |
Accept default configuration by sending an empty form. |
| 1012 |
""" |
| 1013 |
|
| 1014 |
values = {} |
| 1015 |
|
| 1016 |
d = self.protocol.configure(self.roomJID, values) |
| 1017 |
|
| 1018 |
iq = self.stub.output[-1] |
| 1019 |
|
| 1020 |
query = "/iq/query[@xmlns='%s']" % (muc.NS_MUC_OWNER) |
| 1021 |
nodes = xpath.queryForNodes(query, iq) |
| 1022 |
|
| 1023 |
form = data_form.findForm(nodes[0], muc.NS_MUC_CONFIG) |
| 1024 |
self.assertNotIdentical(None, form, "Missing configuration form") |
| 1025 |
self.assertEquals("submit", form.formType) |
| 1026 |
|
| 1027 |
response = toResponse(iq, "result") |
| 1028 |
self.stub.send(response) |
| 1029 |
return d |
| 1030 |
|
| 1031 |
def test_configureCancel(self): |
| 1032 |
""" |
| 1033 |
Cancelling room configuration should send a cancel form. |
| 1034 |
""" |
| 1035 |
|
| 1036 |
d = self.protocol.configure(self.roomJID, None) |
| 1037 |
|
| 1038 |
iq = self.stub.output[-1] |
| 1039 |
|
| 1040 |
query = "/iq/query[@xmlns='%s']" % (muc.NS_MUC_OWNER) |
| 1041 |
nodes = xpath.queryForNodes(query, iq) |
| 1042 |
|
| 1043 |
form = data_form.findForm(nodes[0], muc.NS_MUC_CONFIG) |
| 1044 |
self.assertNotIdentical(None, form, "Missing configuration form") |
| 1045 |
self.assertEquals("cancel", form.formType) |
| 1046 |
|
| 1047 |
response = toResponse(iq, "result") |
| 1048 |
self.stub.send(response) |
| 1049 |
return d |
| 1050 |
|
| 1051 |
def test_getMemberList(self): |
| 1052 |
""" |
| 1053 |
Retrieving the member list returns a list of L{muc.AdminItem}s |
| 1054 |
|
| 1055 |
The request asks for the affiliation C{'member'}. |
| 1056 |
""" |
| 1057 |
|
| 1058 |
def cb(items): |
| 1059 |
self.assertEquals(1, len(items)) |
| 1060 |
item = items[0] |
| 1061 |
self.assertEquals(JID("[email protected]"), item.entity) |
| 1062 |
self.assertEquals("thirdwitch", item.nick) |
| 1063 |
self.assertEquals("member", item.affiliation) |
| 1064 |
|
| 1065 |
d = self.protocol.getMemberList(self.roomJID) |
| 1066 |
d.addCallback(cb) |
| 1067 |
|
| 1068 |
iq = self.stub.output[-1] |
| 1069 |
self.assertEquals("get", iq.getAttribute("type")) |
| 1070 |
query = "/iq/query[@xmlns='%s']/item[@xmlns='%s']" % ( |
| 1071 |
muc.NS_MUC_ADMIN, |
| 1072 |
muc.NS_MUC_ADMIN, |
| 1073 |
) |
| 1074 |
items = xpath.queryForNodes(query, iq) |
| 1075 |
self.assertNotIdentical(None, items) |
| 1076 |
self.assertEquals(1, len(items)) |
| 1077 |
self.assertEquals("member", items[0].getAttribute("affiliation")) |
| 1078 |
|
| 1079 |
response = toResponse(iq, "result") |
| 1080 |
query = response.addElement((NS_MUC_ADMIN, "query")) |
| 1081 |
item = query.addElement("item") |
| 1082 |
item["affiliation"] = "member" |
| 1083 |
item["jid"] = "[email protected]" |
| 1084 |
item["nick"] = "thirdwitch" |
| 1085 |
item["role"] = "participant" |
| 1086 |
self.stub.send(response) |
| 1087 |
|
| 1088 |
return d |
| 1089 |
|
| 1090 |
def test_getAdminList(self): |
| 1091 |
""" |
| 1092 |
Retrieving the admin list returns a list of L{muc.AdminItem}s |
| 1093 |
|
| 1094 |
The request asks for the affiliation C{'admin'}. |
| 1095 |
""" |
| 1096 |
d = self.protocol.getAdminList(self.roomJID) |
| 1097 |
|
| 1098 |
iq = self.stub.output[-1] |
| 1099 |
query = "/iq/query[@xmlns='%s']/item[@xmlns='%s']" % ( |
| 1100 |
muc.NS_MUC_ADMIN, |
| 1101 |
muc.NS_MUC_ADMIN, |
| 1102 |
) |
| 1103 |
items = xpath.queryForNodes(query, iq) |
| 1104 |
self.assertEquals("admin", items[0].getAttribute("affiliation")) |
| 1105 |
|
| 1106 |
response = toResponse(iq, "result") |
| 1107 |
query = response.addElement((NS_MUC_ADMIN, "query")) |
| 1108 |
self.stub.send(response) |
| 1109 |
|
| 1110 |
return d |
| 1111 |
|
| 1112 |
def test_getBanList(self): |
| 1113 |
""" |
| 1114 |
Retrieving the ban list returns a list of L{muc.AdminItem}s |
| 1115 |
|
| 1116 |
The request asks for the affiliation C{'outcast'}. |
| 1117 |
""" |
| 1118 |
|
| 1119 |
def cb(items): |
| 1120 |
self.assertEquals(1, len(items)) |
| 1121 |
item = items[0] |
| 1122 |
self.assertEquals(JID("[email protected]"), item.entity) |
| 1123 |
self.assertEquals("outcast", item.affiliation) |
| 1124 |
self.assertEquals("Trouble making", item.reason) |
| 1125 |
|
| 1126 |
d = self.protocol.getBanList(self.roomJID) |
| 1127 |
d.addCallback(cb) |
| 1128 |
|
| 1129 |
iq = self.stub.output[-1] |
| 1130 |
query = "/iq/query[@xmlns='%s']/item[@xmlns='%s']" % ( |
| 1131 |
muc.NS_MUC_ADMIN, |
| 1132 |
muc.NS_MUC_ADMIN, |
| 1133 |
) |
| 1134 |
items = xpath.queryForNodes(query, iq) |
| 1135 |
self.assertEquals("outcast", items[0].getAttribute("affiliation")) |
| 1136 |
|
| 1137 |
response = toResponse(iq, "result") |
| 1138 |
query = response.addElement((NS_MUC_ADMIN, "query")) |
| 1139 |
item = query.addElement("item") |
| 1140 |
item["affiliation"] = "outcast" |
| 1141 |
item["jid"] = "[email protected]" |
| 1142 |
item.addElement("reason", content="Trouble making") |
| 1143 |
self.stub.send(response) |
| 1144 |
|
| 1145 |
return d |
| 1146 |
|
| 1147 |
def test_getOwnerList(self): |
| 1148 |
""" |
| 1149 |
Retrieving the owner list returns a list of L{muc.AdminItem}s |
| 1150 |
|
| 1151 |
The request asks for the affiliation C{'owner'}. |
| 1152 |
""" |
| 1153 |
d = self.protocol.getOwnerList(self.roomJID) |
| 1154 |
|
| 1155 |
iq = self.stub.output[-1] |
| 1156 |
query = "/iq/query[@xmlns='%s']/item[@xmlns='%s']" % ( |
| 1157 |
muc.NS_MUC_ADMIN, |
| 1158 |
muc.NS_MUC_ADMIN, |
| 1159 |
) |
| 1160 |
items = xpath.queryForNodes(query, iq) |
| 1161 |
self.assertEquals("owner", items[0].getAttribute("affiliation")) |
| 1162 |
|
| 1163 |
response = toResponse(iq, "result") |
| 1164 |
query = response.addElement((NS_MUC_ADMIN, "query")) |
| 1165 |
self.stub.send(response) |
| 1166 |
|
| 1167 |
return d |
| 1168 |
|
| 1169 |
def test_getModeratorList(self): |
| 1170 |
""" |
| 1171 |
Retrieving the moderator returns a list of L{muc.AdminItem}s. |
| 1172 |
|
| 1173 |
The request asks for the role C{'moderator'}. |
| 1174 |
""" |
| 1175 |
|
| 1176 |
def cb(items): |
| 1177 |
self.assertEquals(1, len(items)) |
| 1178 |
item = items[0] |
| 1179 |
self.assertEquals(JID("[email protected]"), item.entity) |
| 1180 |
self.assertEquals("thirdwitch", item.nick) |
| 1181 |
self.assertEquals("moderator", item.role) |
| 1182 |
|
| 1183 |
d = self.protocol.getModeratorList(self.roomJID) |
| 1184 |
d.addCallback(cb) |
| 1185 |
|
| 1186 |
iq = self.stub.output[-1] |
| 1187 |
self.assertEquals("get", iq.getAttribute("type")) |
| 1188 |
query = "/iq/query[@xmlns='%s']/item[@xmlns='%s']" % ( |
| 1189 |
muc.NS_MUC_ADMIN, |
| 1190 |
muc.NS_MUC_ADMIN, |
| 1191 |
) |
| 1192 |
items = xpath.queryForNodes(query, iq) |
| 1193 |
self.assertNotIdentical(None, items) |
| 1194 |
self.assertEquals(1, len(items)) |
| 1195 |
self.assertEquals("moderator", items[0].getAttribute("role")) |
| 1196 |
|
| 1197 |
response = toResponse(iq, "result") |
| 1198 |
query = response.addElement((NS_MUC_ADMIN, "query")) |
| 1199 |
item = query.addElement("item") |
| 1200 |
item["affiliation"] = "member" |
| 1201 |
item["jid"] = "[email protected]" |
| 1202 |
item["nick"] = "thirdwitch" |
| 1203 |
item["role"] = "moderator" |
| 1204 |
self.stub.send(response) |
| 1205 |
|
| 1206 |
return d |
| 1207 |
|
| 1208 |
def test_modifyAffiliationList(self): |
| 1209 |
|
| 1210 |
entities = [JID("[email protected]"), JID("[email protected]")] |
| 1211 |
d = self.protocol.modifyAffiliationList(self.roomJID, entities, "admin") |
| 1212 |
|
| 1213 |
iq = self.stub.output[-1] |
| 1214 |
query = "/iq/query[@xmlns='%s']/item[@xmlns='%s']" % ( |
| 1215 |
muc.NS_MUC_ADMIN, |
| 1216 |
muc.NS_MUC_ADMIN, |
| 1217 |
) |
| 1218 |
items = xpath.queryForNodes(query, iq) |
| 1219 |
self.assertNotIdentical(None, items) |
| 1220 |
self.assertEquals(entities[0], JID(items[0].getAttribute("jid"))) |
| 1221 |
self.assertEquals("admin", items[0].getAttribute("affiliation")) |
| 1222 |
self.assertEquals(entities[1], JID(items[1].getAttribute("jid"))) |
| 1223 |
self.assertEquals("admin", items[1].getAttribute("affiliation")) |
| 1224 |
|
| 1225 |
# Send a response to have the deferred fire. |
| 1226 |
response = toResponse(iq, "result") |
| 1227 |
self.stub.send(response) |
| 1228 |
return d |
| 1229 |
|
| 1230 |
def test_grantVoice(self): |
| 1231 |
""" |
| 1232 |
Granting voice sends request to set role to 'participant'. |
| 1233 |
""" |
| 1234 |
nick = "TroubleMaker" |
| 1235 |
|
| 1236 |
def cb(give_voice): |
| 1237 |
self.assertTrue(give_voice, "Did not give voice user") |
| 1238 |
|
| 1239 |
d = self.protocol.grantVoice(self.roomJID, nick, sender=self.userJID) |
| 1240 |
d.addCallback(cb) |
| 1241 |
|
| 1242 |
iq = self.stub.output[-1] |
| 1243 |
|
| 1244 |
query = ( |
| 1245 |
"/iq[@type='set' and @to='%s']/query/item" "[@role='participant']" |
| 1246 |
) % self.roomJID |
| 1247 |
self.assertTrue(xpath.matches(query, iq), "Wrong voice stanza") |
| 1248 |
|
| 1249 |
response = toResponse(iq, "result") |
| 1250 |
self.stub.send(response) |
| 1251 |
return d |
| 1252 |
|
| 1253 |
def test_revokeVoice(self): |
| 1254 |
""" |
| 1255 |
Revoking voice sends request to set role to 'visitor'. |
| 1256 |
""" |
| 1257 |
nick = "TroubleMaker" |
| 1258 |
|
| 1259 |
d = self.protocol.revokeVoice( |
| 1260 |
self.roomJID, nick, reason="Trouble maker", sender=self.userJID |
| 1261 |
) |
| 1262 |
|
| 1263 |
iq = self.stub.output[-1] |
| 1264 |
|
| 1265 |
query = ( |
| 1266 |
"/iq[@type='set' and @to='%s']/query/item" "[@role='visitor']" |
| 1267 |
) % self.roomJID |
| 1268 |
self.assertTrue(xpath.matches(query, iq), "Wrong voice stanza") |
| 1269 |
|
| 1270 |
response = toResponse(iq, "result") |
| 1271 |
self.stub.send(response) |
| 1272 |
return d |
| 1273 |
|
| 1274 |
def test_grantModerator(self): |
| 1275 |
""" |
| 1276 |
Granting moderator privileges sends request to set role to 'moderator'. |
| 1277 |
""" |
| 1278 |
nick = "TroubleMaker" |
| 1279 |
|
| 1280 |
d = self.protocol.grantModerator(self.roomJID, nick, sender=self.userJID) |
| 1281 |
|
| 1282 |
iq = self.stub.output[-1] |
| 1283 |
|
| 1284 |
query = ( |
| 1285 |
"/iq[@type='set' and @to='%s']/query/item" "[@role='moderator']" |
| 1286 |
) % self.roomJID |
| 1287 |
self.assertTrue(xpath.matches(query, iq), "Wrong voice stanza") |
| 1288 |
|
| 1289 |
response = toResponse(iq, "result") |
| 1290 |
self.stub.send(response) |
| 1291 |
return d |
| 1292 |
|
| 1293 |
def test_ban(self): |
| 1294 |
""" |
| 1295 |
Ban an entity in a room. |
| 1296 |
""" |
| 1297 |
banned = JID("[email protected]/TroubleMaker") |
| 1298 |
|
| 1299 |
def cb(banned): |
| 1300 |
self.assertTrue(banned, "Did not ban user") |
| 1301 |
|
| 1302 |
d = self.protocol.ban(self.roomJID, banned, reason="Spam", sender=self.userJID) |
| 1303 |
d.addCallback(cb) |
| 1304 |
|
| 1305 |
iq = self.stub.output[-1] |
| 1306 |
|
| 1307 |
self.assertTrue( |
| 1308 |
xpath.matches( |
| 1309 |
"/iq[@type='set' and @to='%s']/query/item" |
| 1310 |
"[@affiliation='outcast']" % (self.roomJID,), |
| 1311 |
iq, |
| 1312 |
), |
| 1313 |
"Wrong ban stanza", |
| 1314 |
) |
| 1315 |
|
| 1316 |
response = toResponse(iq, "result") |
| 1317 |
self.stub.send(response) |
| 1318 |
|
| 1319 |
return d |
| 1320 |
|
| 1321 |
def test_kick(self): |
| 1322 |
""" |
| 1323 |
Kick an entity from a room. |
| 1324 |
""" |
| 1325 |
nick = "TroubleMaker" |
| 1326 |
|
| 1327 |
def cb(kicked): |
| 1328 |
self.assertTrue(kicked, "Did not kick user") |
| 1329 |
|
| 1330 |
d = self.protocol.kick(self.roomJID, nick, reason="Spam", sender=self.userJID) |
| 1331 |
d.addCallback(cb) |
| 1332 |
|
| 1333 |
iq = self.stub.output[-1] |
| 1334 |
|
| 1335 |
self.assertTrue( |
| 1336 |
xpath.matches( |
| 1337 |
"/iq[@type='set' and @to='%s']/query/item" |
| 1338 |
"[@role='none']" % (self.roomJID,), |
| 1339 |
iq, |
| 1340 |
), |
| 1341 |
"Wrong kick stanza", |
| 1342 |
) |
| 1343 |
|
| 1344 |
response = toResponse(iq, "result") |
| 1345 |
self.stub.send(response) |
| 1346 |
|
| 1347 |
return d |
| 1348 |
|
| 1349 |
def test_destroy(self): |
| 1350 |
""" |
| 1351 |
Destroy a room. |
| 1352 |
""" |
| 1353 |
d = self.protocol.destroy( |
| 1354 |
self.occupantJID, |
| 1355 |
reason="Time to leave", |
| 1356 |
alternate=JID("other@%s" % self.service), |
| 1357 |
password="secret", |
| 1358 |
) |
| 1359 |
|
| 1360 |
iq = self.stub.output[-1] |
| 1361 |
|
| 1362 |
query = "/iq[@type='set']/query[@xmlns='%s']/destroy[@xmlns='%s']" % ( |
| 1363 |
muc.NS_MUC_OWNER, |
| 1364 |
muc.NS_MUC_OWNER, |
| 1365 |
) |
| 1366 |
|
| 1367 |
nodes = xpath.queryForNodes(query, iq) |
| 1368 |
self.assertNotIdentical(None, nodes, "Bad configure request") |
| 1369 |
destroy = nodes[0] |
| 1370 |
self.assertEquals("Time to leave", str(destroy.reason)) |
| 1371 |
|
| 1372 |
response = toResponse(iq, "result") |
| 1373 |
self.stub.send(response) |
| 1374 |
return d |
| 1375 |
|
| 1376 |
|
| 1377 |
class MUCClientTest(unittest.TestCase): |
| 1378 |
""" |
| 1379 |
Tests for C{muc.MUCClient}. |
| 1380 |
""" |
| 1381 |
|
| 1382 |
def setUp(self): |
| 1383 |
self.clock = task.Clock() |
| 1384 |
self.sessionManager = TestableStreamManager(reactor=self.clock) |
| 1385 |
self.stub = self.sessionManager.stub |
| 1386 |
self.protocol = muc.MUCClient(reactor=self.clock) |
| 1387 |
self.protocol.setHandlerParent(self.sessionManager) |
| 1388 |
|
| 1389 |
self.roomIdentifier = "test" |
| 1390 |
self.service = "conference.example.org" |
| 1391 |
self.nick = "Nick" |
| 1392 |
|
| 1393 |
self.occupantJID = JID(tuple=(self.roomIdentifier, self.service, self.nick)) |
| 1394 |
self.roomJID = self.occupantJID.userhostJID() |
| 1395 |
self.userJID = JID("[email protected]/Testing") |
| 1396 |
|
| 1397 |
def _createRoom(self): |
| 1398 |
""" |
| 1399 |
A helper method to create a test room. |
| 1400 |
""" |
| 1401 |
# create a room |
| 1402 |
room = muc.Room(self.roomJID, self.nick) |
| 1403 |
self.protocol._addRoom(room) |
| 1404 |
return room |
| 1405 |
|
| 1406 |
def test_interface(self): |
| 1407 |
""" |
| 1408 |
Do instances of L{muc.MUCClient} provide L{itx_xmpp.IMUCClient}? |
| 1409 |
""" |
| 1410 |
verify.verifyObject(itx_xmpp.IMUCClient, self.protocol) |
| 1411 |
|
| 1412 |
def _testPresence(self, sender="", available=True): |
| 1413 |
""" |
| 1414 |
Helper for presence tests. |
| 1415 |
""" |
| 1416 |
|
| 1417 |
def userUpdatedStatus(room, user, show, status): |
| 1418 |
self.fail("Unexpected call to userUpdatedStatus") |
| 1419 |
|
| 1420 |
def userJoinedRoom(room, user): |
| 1421 |
self.fail("Unexpected call to userJoinedRoom") |
| 1422 |
|
| 1423 |
if available: |
| 1424 |
available = "" |
| 1425 |
else: |
| 1426 |
available = " type='unavailable'" |
| 1427 |
|
| 1428 |
if sender: |
| 1429 |
sender = " from='%s'" % sender |
| 1430 |
|
| 1431 |
xml = """ |
| 1432 |
<presence to='%s'%s%s> |
| 1433 |
<x xmlns='http://jabber.org/protocol/muc#user'> |
| 1434 |
<item affiliation='member' role='participant'/> |
| 1435 |
</x> |
| 1436 |
</presence> |
| 1437 |
""" % (self.userJID, sender, available) |
| 1438 |
|
| 1439 |
self.protocol.userUpdatedStatus = userUpdatedStatus |
| 1440 |
self.protocol.userJoinedRoom = userJoinedRoom |
| 1441 |
self.stub.send(parseXml(xml)) |
| 1442 |
|
| 1443 |
def test_availableReceivedEmptySender(self): |
| 1444 |
""" |
| 1445 |
Availability presence from empty sender is ignored. |
| 1446 |
""" |
| 1447 |
self._testPresence(sender="") |
| 1448 |
|
| 1449 |
def test_availableReceivedNotInRoom(self): |
| 1450 |
""" |
| 1451 |
Availability presence from unknown entities is ignored. |
| 1452 |
""" |
| 1453 |
otherOccupantJID = JID(self.occupantJID.userhost() + "/OtherNick") |
| 1454 |
self._testPresence(sender=otherOccupantJID) |
| 1455 |
|
| 1456 |
def test_availableReceivedSetsUserRole(self): |
| 1457 |
""" |
| 1458 |
The role received in a presence update is stored on the user. |
| 1459 |
""" |
| 1460 |
room = self._createRoom() |
| 1461 |
user = muc.User(self.nick) |
| 1462 |
room.addUser(user) |
| 1463 |
self.assertEquals("none", user.role) |
| 1464 |
|
| 1465 |
xml = """ |
| 1466 |
<presence to='%s' from='%s'> |
| 1467 |
<x xmlns='http://jabber.org/protocol/muc#user'> |
| 1468 |
<item affiliation='member' role='participant'/> |
| 1469 |
</x> |
| 1470 |
</presence> |
| 1471 |
""" % (self.userJID, self.occupantJID) |
| 1472 |
self.stub.send(parseXml(xml)) |
| 1473 |
|
| 1474 |
self.assertEquals("participant", user.role) |
| 1475 |
|
| 1476 |
def test_availableReceivedSetsUserAffiliation(self): |
| 1477 |
""" |
| 1478 |
The affiliation received in a presence update is stored on the user. |
| 1479 |
""" |
| 1480 |
room = self._createRoom() |
| 1481 |
user = muc.User(self.nick) |
| 1482 |
room.addUser(user) |
| 1483 |
self.assertEquals("none", user.affiliation) |
| 1484 |
|
| 1485 |
xml = """ |
| 1486 |
<presence to='%s' from='%s'> |
| 1487 |
<x xmlns='http://jabber.org/protocol/muc#user'> |
| 1488 |
<item affiliation='member' role='participant'/> |
| 1489 |
</x> |
| 1490 |
</presence> |
| 1491 |
""" % (self.userJID, self.occupantJID) |
| 1492 |
self.stub.send(parseXml(xml)) |
| 1493 |
self.assertEquals("member", user.affiliation) |
| 1494 |
|
| 1495 |
def test_unavailableReceivedEmptySender(self): |
| 1496 |
""" |
| 1497 |
Availability presence from empty sender is ignored. |
| 1498 |
""" |
| 1499 |
self._testPresence(sender="", available=False) |
| 1500 |
|
| 1501 |
def test_unavailableReceivedNotInRoom(self): |
| 1502 |
""" |
| 1503 |
Availability presence from unknown entities is ignored. |
| 1504 |
""" |
| 1505 |
otherOccupantJID = JID(self.occupantJID.userhost() + "/OtherNick") |
| 1506 |
self._testPresence(sender=otherOccupantJID, available=False) |
| 1507 |
|
| 1508 |
def test_unavailableReceivedNotInRoster(self): |
| 1509 |
""" |
| 1510 |
Availability presence from unknown entities is ignored. |
| 1511 |
""" |
| 1512 |
room = self._createRoom() |
| 1513 |
user = muc.User(self.nick) |
| 1514 |
room.addUser(user) |
| 1515 |
otherOccupantJID = JID(self.occupantJID.userhost() + "/OtherNick") |
| 1516 |
self._testPresence(sender=otherOccupantJID, available=False) |
| 1517 |
|
| 1518 |
def test_userJoinedRoom(self): |
| 1519 |
""" |
| 1520 |
Joins by others to a room we're in are passed to userJoinedRoom |
| 1521 |
""" |
| 1522 |
xml = """ |
| 1523 |
<presence to='%s' from='%s'> |
| 1524 |
<x xmlns='http://jabber.org/protocol/muc#user'> |
| 1525 |
<item affiliation='member' role='participant'/> |
| 1526 |
</x> |
| 1527 |
</presence> |
| 1528 |
""" % (self.userJID.full(), self.occupantJID.full()) |
| 1529 |
|
| 1530 |
# create a room |
| 1531 |
self._createRoom() |
| 1532 |
|
| 1533 |
def userJoinedRoom(room, user): |
| 1534 |
self.assertEquals(self.roomJID, room.roomJID, "Wrong room name") |
| 1535 |
self.assertTrue(room.inRoster(user), "User not in roster") |
| 1536 |
|
| 1537 |
d, self.protocol.userJoinedRoom = calledAsync(userJoinedRoom) |
| 1538 |
self.stub.send(parseXml(xml)) |
| 1539 |
return d |
| 1540 |
|
| 1541 |
def test_receivedSubject(self): |
| 1542 |
""" |
| 1543 |
Subject received from a room we're in are passed to receivedSubject. |
| 1544 |
""" |
| 1545 |
xml = """ |
| 1546 |
<message to='%s' from='%s' type='groupchat'> |
| 1547 |
<subject>test</subject> |
| 1548 |
</message> |
| 1549 |
""" % (self.userJID, self.occupantJID) |
| 1550 |
|
| 1551 |
self._createRoom() |
| 1552 |
|
| 1553 |
# add user to room |
| 1554 |
user = muc.User(self.nick) |
| 1555 |
room = self.protocol._getRoom(self.roomJID) |
| 1556 |
room.addUser(user) |
| 1557 |
|
| 1558 |
def receivedSubject(room, user, subject): |
| 1559 |
self.assertEquals("test", subject, "Wrong group chat message") |
| 1560 |
self.assertEquals(self.roomJID, room.roomJID, "Wrong room name") |
| 1561 |
self.assertEquals(self.nick, user.nick) |
| 1562 |
|
| 1563 |
d, self.protocol.receivedSubject = calledAsync(receivedSubject) |
| 1564 |
self.stub.send(parseXml(xml)) |
| 1565 |
return d |
| 1566 |
|
| 1567 |
def test_receivedSubjectNotOverridden(self): |
| 1568 |
""" |
| 1569 |
Not overriding receivedSubject is ok. |
| 1570 |
""" |
| 1571 |
xml = """ |
| 1572 |
<message to='%s' from='%s' type='groupchat'> |
| 1573 |
<subject>test</subject> |
| 1574 |
</message> |
| 1575 |
""" % (self.userJID, self.occupantJID) |
| 1576 |
|
| 1577 |
self._createRoom() |
| 1578 |
self.stub.send(parseXml(xml)) |
| 1579 |
|
| 1580 |
def test_receivedGroupChat(self): |
| 1581 |
""" |
| 1582 |
Messages received from a room we're in are passed to receivedGroupChat. |
| 1583 |
""" |
| 1584 |
xml = """ |
| 1585 |
<message to='[email protected]' from='%s' type='groupchat'> |
| 1586 |
<body>test</body> |
| 1587 |
</message> |
| 1588 |
""" % (self.occupantJID) |
| 1589 |
|
| 1590 |
self._createRoom() |
| 1591 |
|
| 1592 |
def receivedGroupChat(room, user, message): |
| 1593 |
self.assertEquals("test", message.body, "Wrong group chat message") |
| 1594 |
self.assertEquals(self.roomJID, room.roomJID, "Wrong room name") |
| 1595 |
|
| 1596 |
d, self.protocol.receivedGroupChat = calledAsync(receivedGroupChat) |
| 1597 |
self.stub.send(parseXml(xml)) |
| 1598 |
return d |
| 1599 |
|
| 1600 |
def test_receivedGroupChatRoom(self): |
| 1601 |
""" |
| 1602 |
Messages received from the room itself have C{user} set to C{None}. |
| 1603 |
""" |
| 1604 |
xml = """ |
| 1605 |
<message to='[email protected]' from='%s' type='groupchat'> |
| 1606 |
<body>test</body> |
| 1607 |
</message> |
| 1608 |
""" % (self.roomJID) |
| 1609 |
|
| 1610 |
self._createRoom() |
| 1611 |
|
| 1612 |
def receivedGroupChat(room, user, message): |
| 1613 |
self.assertIdentical(None, user) |
| 1614 |
|
| 1615 |
d, self.protocol.receivedGroupChat = calledAsync(receivedGroupChat) |
| 1616 |
self.stub.send(parseXml(xml)) |
| 1617 |
return d |
| 1618 |
|
| 1619 |
def test_receivedGroupChatNotInRoom(self): |
| 1620 |
""" |
| 1621 |
Messages received from a room we're not in are ignored. |
| 1622 |
""" |
| 1623 |
xml = """ |
| 1624 |
<message to='[email protected]' from='%s' type='groupchat'> |
| 1625 |
<body>test</body> |
| 1626 |
</message> |
| 1627 |
""" % (self.occupantJID) |
| 1628 |
|
| 1629 |
def receivedGroupChat(room, user, message): |
| 1630 |
self.fail("Unexpected call to receivedGroupChat") |
| 1631 |
|
| 1632 |
self.protocol.receivedGroupChat = receivedGroupChat |
| 1633 |
self.stub.send(parseXml(xml)) |
| 1634 |
|
| 1635 |
def test_receivedGroupChatNotOverridden(self): |
| 1636 |
""" |
| 1637 |
Not overriding receivedGroupChat is ok. |
| 1638 |
""" |
| 1639 |
xml = """ |
| 1640 |
<message to='[email protected]' from='%s' type='groupchat'> |
| 1641 |
<body>test</body> |
| 1642 |
</message> |
| 1643 |
""" % (self.occupantJID) |
| 1644 |
|
| 1645 |
self._createRoom() |
| 1646 |
self.stub.send(parseXml(xml)) |
| 1647 |
|
| 1648 |
def test_join(self): |
| 1649 |
""" |
| 1650 |
Joining a room waits for confirmation, deferred fires room. |
| 1651 |
""" |
| 1652 |
|
| 1653 |
def cb(room): |
| 1654 |
self.assertEqual(self.roomJID, room.roomJID) |
| 1655 |
self.assertFalse(room.locked) |
| 1656 |
|
| 1657 |
d = self.protocol.join(self.roomJID, self.nick) |
| 1658 |
d.addCallback(cb) |
| 1659 |
|
| 1660 |
# send back user presence, they joined |
| 1661 |
xml = """ |
| 1662 |
<presence from='%s@%s/%s'> |
| 1663 |
<x xmlns='http://jabber.org/protocol/muc#user'> |
| 1664 |
<item affiliation='member' role='participant'/> |
| 1665 |
</x> |
| 1666 |
</presence> |
| 1667 |
""" % (self.roomIdentifier, self.service, self.nick) |
| 1668 |
self.stub.send(parseXml(xml)) |
| 1669 |
return d |
| 1670 |
|
| 1671 |
def test_joinLocked(self): |
| 1672 |
""" |
| 1673 |
A new room is locked by default. |
| 1674 |
""" |
| 1675 |
|
| 1676 |
def cb(room): |
| 1677 |
self.assertTrue(room.locked, "Room is not marked as locked") |
| 1678 |
|
| 1679 |
d = self.protocol.join(self.roomJID, self.nick) |
| 1680 |
d.addCallback(cb) |
| 1681 |
|
| 1682 |
# send back user presence, they joined |
| 1683 |
xml = """ |
| 1684 |
<presence from='%s@%s/%s'> |
| 1685 |
<x xmlns='http://jabber.org/protocol/muc#user'> |
| 1686 |
<item affiliation='owner' role='moderator'/> |
| 1687 |
<status code="110"/> |
| 1688 |
<status code="201"/> |
| 1689 |
</x> |
| 1690 |
</presence> |
| 1691 |
""" % (self.roomIdentifier, self.service, self.nick) |
| 1692 |
self.stub.send(parseXml(xml)) |
| 1693 |
return d |
| 1694 |
|
| 1695 |
def test_joinForbidden(self): |
| 1696 |
""" |
| 1697 |
A forbidden error in response to a join errbacks with L{StanzaError}. |
| 1698 |
""" |
| 1699 |
|
| 1700 |
def cb(error): |
| 1701 |
self.assertEquals("forbidden", error.condition, "Wrong muc condition") |
| 1702 |
self.assertIdentical(None, self.protocol._getRoom(self.roomJID)) |
| 1703 |
|
| 1704 |
d = self.protocol.join(self.roomJID, self.nick) |
| 1705 |
self.assertFailure(d, StanzaError) |
| 1706 |
d.addCallback(cb) |
| 1707 |
|
| 1708 |
# send back error, forbidden |
| 1709 |
xml = """ |
| 1710 |
<presence from='%s' type='error'> |
| 1711 |
<error type='auth'> |
| 1712 |
<forbidden xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/> |
| 1713 |
</error> |
| 1714 |
</presence> |
| 1715 |
""" % (self.occupantJID) |
| 1716 |
self.stub.send(parseXml(xml)) |
| 1717 |
return d |
| 1718 |
|
| 1719 |
def test_userLeftRoom(self): |
| 1720 |
""" |
| 1721 |
Unavailable presence from a participant removes it from the room. |
| 1722 |
""" |
| 1723 |
|
| 1724 |
xml = """ |
| 1725 |
<presence to='%s' from='%s' type='unavailable'/> |
| 1726 |
""" % (self.userJID, self.occupantJID) |
| 1727 |
|
| 1728 |
# create a room |
| 1729 |
self._createRoom() |
| 1730 |
|
| 1731 |
# add user to room |
| 1732 |
user = muc.User(self.nick) |
| 1733 |
room = self.protocol._getRoom(self.roomJID) |
| 1734 |
room.addUser(user) |
| 1735 |
|
| 1736 |
def userLeftRoom(room, user): |
| 1737 |
self.assertEquals(self.roomJID, room.roomJID, "Wrong room name") |
| 1738 |
self.assertFalse(room.inRoster(user), "User in roster") |
| 1739 |
|
| 1740 |
d, self.protocol.userLeftRoom = calledAsync(userLeftRoom) |
| 1741 |
self.stub.send(parseXml(xml)) |
| 1742 |
return d |
| 1743 |
|
| 1744 |
def test_userChangedNick(self): |
| 1745 |
""" |
| 1746 |
Unavailable presence with status code 303 triggers userChangedNick. |
| 1747 |
""" |
| 1748 |
newNick = "NewNick" |
| 1749 |
|
| 1750 |
xml = """ |
| 1751 |
<presence to='%s' from='%s' type='unavailable'> |
| 1752 |
<x xmlns='http://jabber.org/protocol/muc#user'> |
| 1753 |
<item affiliation='member' role='participant' nick='%s'/> |
| 1754 |
<status code='303'/> |
| 1755 |
</x> |
| 1756 |
</presence> |
| 1757 |
""" % (self.userJID, self.occupantJID, newNick) |
| 1758 |
|
| 1759 |
# create a room |
| 1760 |
self._createRoom() |
| 1761 |
|
| 1762 |
# add user to room |
| 1763 |
user = muc.User(self.nick) |
| 1764 |
room = self.protocol._getRoom(self.roomJID) |
| 1765 |
room.addUser(user) |
| 1766 |
|
| 1767 |
def userChangedNick(room, user, newNick): |
| 1768 |
self.assertEquals(self.roomJID, room.roomJID, "Wrong room name") |
| 1769 |
self.assertEquals(self.nick, user.nick, "Wrong old nick") |
| 1770 |
self.assertEquals(newNick, newNick, "Wrong new nick") |
| 1771 |
self.assertFalse(room.inRoster(user), "Old user still in roster") |
| 1772 |
|
| 1773 |
def userLeftRoom(room, user): |
| 1774 |
self.fail("userLeftRoom should not be called on nick change") |
| 1775 |
|
| 1776 |
d, self.protocol.userChangedNick = calledAsync(userChangedNick) |
| 1777 |
self.protocol.userLeftRoom = userLeftRoom |
| 1778 |
self.stub.send(parseXml(xml)) |
| 1779 |
return d |
| 1780 |
|
| 1781 |
def test_receivedHistory(self): |
| 1782 |
""" |
| 1783 |
Receiving history on room join. |
| 1784 |
""" |
| 1785 |
xml = """ |
| 1786 |
<message to='[email protected]' from='%s' type='groupchat'> |
| 1787 |
<body>test</body> |
| 1788 |
<delay xmlns='urn:xmpp:delay' stamp="2002-10-13T23:58:37Z" |
| 1789 |
from="%s"/> |
| 1790 |
</message> |
| 1791 |
""" % (self.occupantJID, self.userJID) |
| 1792 |
|
| 1793 |
self._createRoom() |
| 1794 |
|
| 1795 |
def receivedHistory(room, user, message): |
| 1796 |
self.assertEquals("test", message.body, "wrong message body") |
| 1797 |
stamp = datetime(2002, 10, 13, 23, 58, 37, tzinfo=tzutc()) |
| 1798 |
self.assertEquals(stamp, message.delay.stamp, "Does not have a history stamp") |
| 1799 |
|
| 1800 |
d, self.protocol.receivedHistory = calledAsync(receivedHistory) |
| 1801 |
self.stub.send(parseXml(xml)) |
| 1802 |
return d |
| 1803 |
|
| 1804 |
def test_receivedHistoryNotOverridden(self): |
| 1805 |
""" |
| 1806 |
Not overriding receivedHistory is ok. |
| 1807 |
""" |
| 1808 |
xml = """ |
| 1809 |
<message to='[email protected]' from='%s' type='groupchat'> |
| 1810 |
<body>test</body> |
| 1811 |
<delay xmlns='urn:xmpp:delay' stamp="2002-10-13T23:58:37Z" |
| 1812 |
from="%s"/> |
| 1813 |
</message> |
| 1814 |
""" % (self.occupantJID, self.userJID) |
| 1815 |
|
| 1816 |
self._createRoom() |
| 1817 |
self.stub.send(parseXml(xml)) |
| 1818 |
|
| 1819 |
def test_nickConflict(self): |
| 1820 |
""" |
| 1821 |
If the server finds the new nick in conflict, the errback is called. |
| 1822 |
""" |
| 1823 |
|
| 1824 |
def cb(failure, room): |
| 1825 |
user = room.getUser(otherNick) |
| 1826 |
self.assertNotIdentical(None, user) |
| 1827 |
self.assertEqual(otherJID, user.entity) |
| 1828 |
|
| 1829 |
def joined(room): |
| 1830 |
d = self.protocol.nick(room.roomJID, otherNick) |
| 1831 |
self.assertFailure(d, StanzaError) |
| 1832 |
d.addCallback(cb, room) |
| 1833 |
|
| 1834 |
otherJID = JID("[email protected]/Home") |
| 1835 |
otherNick = "otherNick" |
| 1836 |
|
| 1837 |
d = self.protocol.join(self.roomJID, self.nick) |
| 1838 |
d.addCallback(joined) |
| 1839 |
|
| 1840 |
# Send back other partipant's presence. |
| 1841 |
xml = """ |
| 1842 |
<presence from='%s/%s'> |
| 1843 |
<x xmlns='http://jabber.org/protocol/muc#user'> |
| 1844 |
<item affiliation='member' role='participant' jid='%s'/> |
| 1845 |
</x> |
| 1846 |
</presence> |
| 1847 |
""" % (self.roomJID, otherNick, otherJID) |
| 1848 |
self.stub.send(parseXml(xml)) |
| 1849 |
|
| 1850 |
# send back user presence, they joined |
| 1851 |
xml = """ |
| 1852 |
<presence from='%s/%s'> |
| 1853 |
<x xmlns='http://jabber.org/protocol/muc#user'> |
| 1854 |
<item affiliation='member' role='participant'/> |
| 1855 |
</x> |
| 1856 |
</presence> |
| 1857 |
""" % (self.roomJID, self.nick) |
| 1858 |
self.stub.send(parseXml(xml)) |
| 1859 |
|
| 1860 |
# send back error presence, nick conflicted |
| 1861 |
xml = """ |
| 1862 |
<presence from='%s/%s' type='error'> |
| 1863 |
<x xmlns='http://jabber.org/protocol/muc'/> |
| 1864 |
<error type='cancel'> |
| 1865 |
<conflict xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/> |
| 1866 |
</error> |
| 1867 |
</presence> |
| 1868 |
""" % (self.roomJID, otherNick) |
| 1869 |
self.stub.send(parseXml(xml)) |
| 1870 |
return d |
| 1871 |
|
| 1872 |
def test_nick(self): |
| 1873 |
""" |
| 1874 |
Send a nick change to the server. |
| 1875 |
""" |
| 1876 |
newNick = "newNick" |
| 1877 |
|
| 1878 |
room = self._createRoom() |
| 1879 |
|
| 1880 |
def joined(room): |
| 1881 |
self.assertEqual(self.roomJID, room.roomJID) |
| 1882 |
self.assertEqual(newNick, room.nick) |
| 1883 |
user = room.getUser(newNick) |
| 1884 |
self.assertNotIdentical(None, user) |
| 1885 |
self.assertEqual(newNick, user.nick) |
| 1886 |
|
| 1887 |
d = self.protocol.nick(self.roomJID, newNick) |
| 1888 |
d.addCallback(joined) |
| 1889 |
|
| 1890 |
# Nick should not have been changed, yet, as we haven't gotten |
| 1891 |
# confirmation, yet. |
| 1892 |
|
| 1893 |
self.assertEquals(self.nick, room.nick) |
| 1894 |
|
| 1895 |
# send back user presence, nick changed |
| 1896 |
xml = """ |
| 1897 |
<presence from='%s/%s'> |
| 1898 |
<x xmlns='http://jabber.org/protocol/muc#user'> |
| 1899 |
<item affiliation='member' role='participant'/> |
| 1900 |
</x> |
| 1901 |
</presence> |
| 1902 |
""" % (self.roomJID, newNick) |
| 1903 |
|
| 1904 |
self.stub.send(parseXml(xml)) |
| 1905 |
return d |
| 1906 |
|
| 1907 |
def test_leave(self): |
| 1908 |
""" |
| 1909 |
Client leaves a room |
| 1910 |
""" |
| 1911 |
|
| 1912 |
def joined(_): |
| 1913 |
return self.protocol.leave(self.roomJID) |
| 1914 |
|
| 1915 |
def left(_): |
| 1916 |
self.assertIdentical(None, self.protocol._getRoom(self.roomJID)) |
| 1917 |
|
| 1918 |
# Join the room |
| 1919 |
d = self.protocol.join(self.roomJID, self.nick) |
| 1920 |
d.addCallback(joined) |
| 1921 |
d.addCallback(left) |
| 1922 |
|
| 1923 |
# Receive presence back from the room: joined. |
| 1924 |
xml = """ |
| 1925 |
<presence to='%s' from='%s'/> |
| 1926 |
""" % (self.userJID, self.occupantJID) |
| 1927 |
self.stub.send(parseXml(xml)) |
| 1928 |
|
| 1929 |
# Receive presence back from the room: left. |
| 1930 |
xml = """ |
| 1931 |
<presence to='%s' from='%s' type='unavailable'/> |
| 1932 |
""" % (self.userJID, self.occupantJID) |
| 1933 |
self.stub.send(parseXml(xml)) |
| 1934 |
|
| 1935 |
return d |
| 1936 |
|
| 1937 |
def test_status(self): |
| 1938 |
""" |
| 1939 |
Change status |
| 1940 |
""" |
| 1941 |
|
| 1942 |
def joined(_): |
| 1943 |
d = self.protocol.status(self.roomJID, "xa", "testing MUC") |
| 1944 |
d.addCallback(statusChanged) |
| 1945 |
return d |
| 1946 |
|
| 1947 |
def statusChanged(room): |
| 1948 |
self.assertEqual(self.roomJID, room.roomJID) |
| 1949 |
user = room.getUser(self.nick) |
| 1950 |
self.assertNotIdentical(None, user, "User not found") |
| 1951 |
self.assertEqual("testing MUC", user.status, "Wrong status") |
| 1952 |
self.assertEqual("xa", user.show, "Wrong show") |
| 1953 |
|
| 1954 |
# Join the room |
| 1955 |
d = self.protocol.join(self.roomJID, self.nick) |
| 1956 |
d.addCallback(joined) |
| 1957 |
|
| 1958 |
# Receive presence back from the room: joined. |
| 1959 |
xml = """ |
| 1960 |
<presence to='%s' from='%s'/> |
| 1961 |
""" % (self.userJID, self.occupantJID) |
| 1962 |
self.stub.send(parseXml(xml)) |
| 1963 |
|
| 1964 |
# send back user presence, status changed |
| 1965 |
xml = """ |
| 1966 |
<presence from='%s'> |
| 1967 |
<x xmlns='http://jabber.org/protocol/muc#user'> |
| 1968 |
<item affiliation='member' role='participant'/> |
| 1969 |
</x> |
| 1970 |
<show>xa</show> |
| 1971 |
<status>testing MUC</status> |
| 1972 |
</presence> |
| 1973 |
""" % self.occupantJID |
| 1974 |
|
| 1975 |
self.stub.send(parseXml(xml)) |
| 1976 |
return d |
| 1977 |
|
| 1978 |
def test_destroy(self): |
| 1979 |
""" |
| 1980 |
Destroy a room. |
| 1981 |
""" |
| 1982 |
|
| 1983 |
def destroyed(_): |
| 1984 |
self.assertIdentical(None, self.protocol._getRoom(self.roomJID)) |
| 1985 |
|
| 1986 |
d = self.protocol.destroy( |
| 1987 |
self.occupantJID, |
| 1988 |
reason="Time to leave", |
| 1989 |
alternate=JID("other@%s" % self.service), |
| 1990 |
password="secret", |
| 1991 |
) |
| 1992 |
d.addCallback(destroyed) |
| 1993 |
|
| 1994 |
iq = self.stub.output[-1] |
| 1995 |
response = toResponse(iq, "result") |
| 1996 |
self.stub.send(response) |
| 1997 |
return d |