[Telepathy-commits] [telepathy-gabble/master] Add helper functions to join a MUC in tests.

Will Thompson will.thompson at collabora.co.uk
Sun Mar 1 08:03:05 PST 2009


In passing, fixes a race in tubes/offer-muc-dbus-tube.py, which didn't
wait for Gabble to ask to join the MUC before sending presences for the
MUC's members.

References fd.o #20405
---
 tests/twisted/Makefile.am                   |    1 +
 tests/twisted/muc/send-error.py             |   39 +++-------------
 tests/twisted/muc/test-muc.py               |   43 ++++--------------
 tests/twisted/mucutil.py                    |   65 +++++++++++++++++++++++++++
 tests/twisted/tubes/accept-muc-dbus-tube.py |   26 +++--------
 tests/twisted/tubes/offer-muc-dbus-tube.py  |   30 +++++--------
 6 files changed, 101 insertions(+), 103 deletions(-)
 create mode 100644 tests/twisted/mucutil.py

diff --git a/tests/twisted/Makefile.am b/tests/twisted/Makefile.am
index 22343f5..af983a7 100644
--- a/tests/twisted/Makefile.am
+++ b/tests/twisted/Makefile.am
@@ -151,6 +151,7 @@ EXTRA_DIST = \
 	servicetest.py \
 	jingle/jingletest.py \
 	jingle/jingletest2.py \
+	mucutil.py \
 	ns.py \
 	olpc/util.py \
 	tubes/muctubeutil.py \
diff --git a/tests/twisted/muc/send-error.py b/tests/twisted/muc/send-error.py
index f978033..e66290b 100644
--- a/tests/twisted/muc/send-error.py
+++ b/tests/twisted/muc/send-error.py
@@ -6,41 +6,19 @@ import dbus
 
 from twisted.words.xish import domish
 
-from gabbletest import exec_test, make_muc_presence, request_muc_handle
-from servicetest import call_async, EventPattern
+from gabbletest import exec_test
+from servicetest import EventPattern
 import ns
 
+from mucutil import join_muc_and_check
+
 def test(q, bus, conn, stream):
     conn.Connect()
     q.expect('dbus-signal', signal='StatusChanged', args=[0, 1])
 
-    room_handle = request_muc_handle(q, conn, stream, 'chat at conf.localhost')
-    call_async(q, conn, 'RequestChannel',
-        'org.freedesktop.Telepathy.Channel.Type.Text', 2, room_handle, True)
-
-    gfc, _, _ = q.expect_many(
-        EventPattern('dbus-signal', signal='GroupFlagsChanged'),
-        EventPattern('dbus-signal', signal='MembersChanged',
-            args=[u'', [], [], [], [2], 0, 0]),
-        EventPattern('stream-presence', to='chat at conf.localhost/test'))
-    assert gfc.args[1] == 0
-
-    # Send presence for other member of room.
-    stream.send(make_muc_presence(
-        'owner', 'moderator', 'chat at conf.localhost', 'bob'))
-
-    # Send presence for own membership of room.
-    stream.send(make_muc_presence(
-        'none', 'participant', 'chat at conf.localhost', 'test'))
-
-    event = q.expect('dbus-signal', signal='MembersChanged',
-        args=[u'', [2, 3], [], [], [], 0, 0])
-    assert conn.InspectHandles(1, [2]) == ['chat at conf.localhost/test']
-    assert conn.InspectHandles(1, [3]) == ['chat at conf.localhost/bob']
-
-    event = q.expect('dbus-return', method='RequestChannel')
-    text_chan = bus.get_object(conn.bus_name, event.value[0])
-
+    muc = 'chat at conf.localhost'
+    _, text_chan, test_handle, bob_handle = \
+        join_muc_and_check(q, bus, conn, stream, muc)
 
     # Suppose we don't have permission to speak in this MUC.  Send a message to
     # the channel, and have the MUC reject it as unauthorized.
@@ -119,8 +97,7 @@ def test(q, bus, conn, stream):
     assert 'delivery-echo' in part, part
     echo = part['delivery-echo']
     assert len(echo) == len(greeting), (echo, greeting)
-    # Earlier in this test we checked that handle 2 is us.
-    assert echo[0]['message-sender'] == 2, echo[0]
+    assert echo[0]['message-sender'] == test_handle, echo[0]
     for i in range(0, len(echo)):
         for key in greeting[i]:
             assert key in echo[i], (i, key, echo)
diff --git a/tests/twisted/muc/test-muc.py b/tests/twisted/muc/test-muc.py
index 37bee3b..f8ebd5b 100644
--- a/tests/twisted/muc/test-muc.py
+++ b/tests/twisted/muc/test-muc.py
@@ -7,39 +7,18 @@ import dbus
 
 from twisted.words.xish import domish
 
-from gabbletest import exec_test, make_muc_presence, request_muc_handle
-from servicetest import call_async, EventPattern
+from gabbletest import exec_test
+from servicetest import EventPattern
+
+from mucutil import join_muc_and_check
 
 def test(q, bus, conn, stream):
     conn.Connect()
     q.expect('dbus-signal', signal='StatusChanged', args=[0, 1])
 
-    room_handle = request_muc_handle(q, conn, stream, 'chat at conf.localhost')
-    call_async(q, conn, 'RequestChannel',
-        'org.freedesktop.Telepathy.Channel.Type.Text', 2, room_handle, True)
-
-    gfc, _, _ = q.expect_many(
-        EventPattern('dbus-signal', signal='GroupFlagsChanged'),
-        EventPattern('dbus-signal', signal='MembersChanged',
-            args=[u'', [], [], [], [2], 0, 0]),
-        EventPattern('stream-presence', to='chat at conf.localhost/test'))
-    assert gfc.args[1] == 0
-
-    # Send presence for other member of room.
-    stream.send(make_muc_presence(
-        'owner', 'moderator', 'chat at conf.localhost', 'bob'))
-
-    # Send presence for own membership of room.
-    stream.send(make_muc_presence(
-        'none', 'participant', 'chat at conf.localhost', 'test'))
-
-    event = q.expect('dbus-signal', signal='MembersChanged',
-        args=[u'', [2, 3], [], [], [], 0, 0])
-    assert conn.InspectHandles(1, [2]) == ['chat at conf.localhost/test']
-    assert conn.InspectHandles(1, [3]) == ['chat at conf.localhost/bob']
-
-    event = q.expect('dbus-return', method='RequestChannel')
-    text_chan = bus.get_object(conn.bus_name, event.value[0])
+    room = 'chat at conf.localhost'
+    room_handle, text_chan, test_handle, bob_handle = \
+        join_muc_and_check(q, bus, conn, stream, room)
 
     # Exercise basic Channel Properties from spec 0.17.7
     channel_props = text_chan.GetAll(
@@ -97,7 +76,7 @@ def test(q, bus, conn, stream):
 
     # Check Channel.Type.Text.Received:
     # sender: bob
-    assert received.args[2] == 3
+    assert received.args[2] == bob_handle
     # message type: normal
     assert received.args[3] == 0
     # flags: none
@@ -112,8 +91,7 @@ def test(q, bus, conn, stream):
     assert len(message) == 2, message
     header, body = message
 
-    # 3 is bob
-    assert header['message-sender'] == 3, header
+    assert header['message-sender'] == bob_handle, header
     # the spec says that message-type "SHOULD be omitted for normal chat
     # messages."
     assert 'message-type' not in header, header
@@ -209,8 +187,7 @@ def test(q, bus, conn, stream):
     # message we sent.
     echo = part['delivery-echo']
     assert len(echo) == len(greeting), (echo, greeting)
-    # Earlier in this test we checked that handle 2 is us.
-    assert echo[0]['message-sender'] == 2, echo[0]
+    assert echo[0]['message-sender'] == test_handle, echo[0]
     for i in range(0, len(echo)):
         for key in greeting[i]:
             assert key in echo[i], (i, key, echo)
diff --git a/tests/twisted/mucutil.py b/tests/twisted/mucutil.py
new file mode 100644
index 0000000..99679a8
--- /dev/null
+++ b/tests/twisted/mucutil.py
@@ -0,0 +1,65 @@
+"""
+Utility functions for tests that need to interact with MUCs.
+"""
+
+import dbus
+
+from servicetest import call_async
+from gabbletest import make_muc_presence, request_muc_handle
+
+import constants as cs
+
+def join_muc(q, bus, conn, stream, muc, request=None):
+    """
+    Joins 'muc', returning the muc's handle, a proxy object for the channel,
+    its path and its immutable properties just after the CreateChannel event
+    has fired. The room contains one other member.
+    """
+    if request is None:
+        request = {
+            cs.CHANNEL_TYPE: cs.CHANNEL_TYPE_TEXT,
+            cs.TARGET_HANDLE_TYPE: cs.HT_ROOM,
+            cs.TARGET_ID: muc,
+        }
+
+    muc_handle = request_muc_handle(q, conn, stream, muc)
+
+    requests = dbus.Interface(conn, cs.CONN_IFACE_REQUESTS)
+    call_async(q, requests, 'CreateChannel',
+        dbus.Dictionary(request, signature='sv'))
+
+    q.expect('stream-presence', to='%s/test' % muc)
+
+    # Send presence for other member of room.
+    stream.send(make_muc_presence('owner', 'moderator', muc, 'bob'))
+
+    # Send presence for own membership of room.
+    stream.send(make_muc_presence('none', 'participant', muc, 'test'))
+
+    event = q.expect('dbus-return', method='CreateChannel')
+    path, props = event.value
+    chan = bus.get_object(conn.bus_name, path)
+
+    return (muc_handle, chan, path, props)
+
+def join_muc_and_check(q, bus, conn, stream, muc, request=None):
+    """
+    Like join_muc(), but also checks the NewChannels and NewChannel signals and
+    the Members property, and returns both members' handles.
+    """
+    muc_handle, chan, path, props = \
+        join_muc(q, bus, conn, stream, muc, request=request)
+
+    q.expect('dbus-signal', signal='NewChannels', args=[[(path, props)]])
+    q.expect('dbus-signal', signal='NewChannel',
+        args=[path, cs.CHANNEL_TYPE_TEXT, cs.HT_ROOM, muc_handle, True])
+
+    test_handle, bob_handle = conn.RequestHandles(cs.HT_CONTACT,
+        ['%s/test' % muc, '%s/bob' % muc])
+
+    members = chan.Get(cs.CHANNEL_IFACE_GROUP, 'Members',
+        dbus_interface=cs.PROPERTIES_IFACE)
+    assert set(members) == set([test_handle, bob_handle]), \
+        (members, (test_handle, bob_handle))
+
+    return (muc_handle, chan, test_handle, bob_handle)
diff --git a/tests/twisted/tubes/accept-muc-dbus-tube.py b/tests/twisted/tubes/accept-muc-dbus-tube.py
index 96ae0e5..8a23716 100644
--- a/tests/twisted/tubes/accept-muc-dbus-tube.py
+++ b/tests/twisted/tubes/accept-muc-dbus-tube.py
@@ -1,12 +1,14 @@
 import dbus
 
 from servicetest import call_async, EventPattern
-from gabbletest import exec_test, acknowledge_iq, make_muc_presence
+from gabbletest import exec_test, acknowledge_iq
 import constants as c
 
 from twisted.words.xish import domish, xpath
 import ns
 
+from mucutil import join_muc_and_check
+
 sample_parameters = dbus.Dictionary({
     's': 'hello',
     'ay': dbus.ByteArray('hello'),
@@ -23,26 +25,10 @@ def test(q, bus, conn, stream):
             query_name='vCard'))
 
     acknowledge_iq(stream, iq_event.stanza)
-    requestotron = dbus.Interface(conn, c.CONN_IFACE_REQUESTS)
-
-    # join room
-    call_async(q, requestotron, 'CreateChannel', {
-        c.CHANNEL_TYPE: c.CHANNEL_TYPE_TEXT,
-        c.TARGET_HANDLE_TYPE: c.HT_ROOM,
-        c.TARGET_ID: 'chat at conf.localhost'})
-
-    event = q.expect('stream-presence', to='chat at conf.localhost/test')
-
-    # Send presence for other member of room.
-    stream.send(make_muc_presence('owner', 'moderator', 'chat at conf.localhost', 'bob'))
-
-    # Send presence for own membership of room.
-    stream.send(make_muc_presence('none', 'participant', 'chat at conf.localhost', 'test'))
-
-    event = q.expect('dbus-return', method='CreateChannel')
 
-    # text channel is announced
-    q.expect('dbus-signal', signal='NewChannels')
+    muc = 'chat at conf.localhost'
+    _, _, test_handle, bob_handle = \
+        join_muc_and_check(q, bus, conn, stream, muc)
 
     # Bob offers a stream tube
     bob_bus_name = ':2.Ym9i'
diff --git a/tests/twisted/tubes/offer-muc-dbus-tube.py b/tests/twisted/tubes/offer-muc-dbus-tube.py
index af0d783..890ce3c 100644
--- a/tests/twisted/tubes/offer-muc-dbus-tube.py
+++ b/tests/twisted/tubes/offer-muc-dbus-tube.py
@@ -6,14 +6,15 @@ import dbus
 from dbus.connection import Connection
 from dbus.lowlevel import SignalMessage
 
-from servicetest import call_async, EventPattern, tp_name_prefix
-from gabbletest import exec_test, make_result_iq, acknowledge_iq, make_muc_presence, elem
+from servicetest import call_async, EventPattern
+from gabbletest import exec_test, make_result_iq, acknowledge_iq, elem
 from constants import *
 import ns
 import tubetestutil as t
 
 from twisted.words.xish import domish, xpath
 
+from mucutil import join_muc
 from muctubeutil import get_muc_tubes_channel
 
 sample_parameters = dbus.Dictionary({
@@ -182,23 +183,14 @@ def test(q, bus, conn, stream):
     fire_signal_on_tube(q, tube, 'chat at conf.localhost', dbus_stream_id, my_bus_name)
 
     # offer a D-Bus tube to another room using new API
-    requestotron = dbus.Interface(conn, CONN_IFACE_REQUESTS)
-
-    call_async(q, requestotron, 'CreateChannel',
-            {CHANNEL_TYPE: CHANNEL_TYPE_DBUS_TUBE,
-         TARGET_HANDLE_TYPE: HT_ROOM,
-         TARGET_ID: 'chat2 at conf.localhost',
-         DBUS_TUBE_SERVICE_NAME: 'com.example.TestCase',
-        })
-
-    # Send presence for other member of room.
-    stream.send(make_muc_presence('owner', 'moderator', 'chat2 at conf.localhost', 'bob'))
-
-    # Send presence for own membership of room.
-    stream.send(make_muc_presence('none', 'participant', 'chat2 at conf.localhost', 'test'))
-
-    event = q.expect('dbus-return', method='CreateChannel')
-    new_tube_path, new_tube_props = event.value
+    muc = 'chat2 at conf.localhost'
+    request = {
+        CHANNEL_TYPE: CHANNEL_TYPE_DBUS_TUBE,
+        TARGET_HANDLE_TYPE: HT_ROOM,
+        TARGET_ID: 'chat2 at conf.localhost',
+        DBUS_TUBE_SERVICE_NAME: 'com.example.TestCase',
+    }
+    join_muc(q, bus, conn, stream, muc, request=request)
 
     # first text and tubes channels are announced
     event = q.expect('dbus-signal', signal='NewChannels')
-- 
1.5.6.5



More information about the telepathy-commits mailing list