telepathy-gabble: muc/send-error: refactor

Will Thompson wjt at kemper.freedesktop.org
Mon Mar 4 04:22:17 PST 2013


Module: telepathy-gabble
Branch: master
Commit: 717c765b11fe101d9f32bb352f0377ccc762726d
URL:    http://cgit.freedesktop.org/telepathy/telepathy-gabble/commit/?id=717c765b11fe101d9f32bb352f0377ccc762726d

Author: Will Thompson <will.thompson at collabora.co.uk>
Date:   Fri Feb 15 08:56:42 2013 +0000

muc/send-error: refactor

There will be more cases shortly

---

 tests/twisted/constants.py      |   14 ++++++++
 tests/twisted/muc/send-error.py |   63 ++++++++++++++++++++++----------------
 2 files changed, 50 insertions(+), 27 deletions(-)

diff --git a/tests/twisted/constants.py b/tests/twisted/constants.py
index 0ea9d5b..d541c75 100644
--- a/tests/twisted/constants.py
+++ b/tests/twisted/constants.py
@@ -463,6 +463,20 @@ MT_NOTICE = 2
 MT_AUTO_REPLY = 3
 MT_DELIVERY_REPORT = 4
 
+class MessageFlag(object):
+    TRUNCATED = 1
+    NON_TEXT_CONTENT = 2
+    SCROLLBACK = 4
+    RESCUED = 8
+
+class SendError(object):
+    UNKNOWN = 0
+    OFFLINE = 1
+    INVALID_CONTACT = 2
+    PERMISSION_DENIED = 3
+    TOO_LONG = 4
+    NOT_IMPLEMENTED = 5
+
 PROTOCOL = 'org.freedesktop.Telepathy.Protocol'
 PROTOCOL_IFACE_PRESENCES = PROTOCOL + '.Interface.Presence'
 PROTOCOL_IFACE_ADDRESSING = PROTOCOL + '.Interface.Addressing'
diff --git a/tests/twisted/muc/send-error.py b/tests/twisted/muc/send-error.py
index 545d642..38f24b3 100644
--- a/tests/twisted/muc/send-error.py
+++ b/tests/twisted/muc/send-error.py
@@ -5,7 +5,7 @@ Test incoming error messages in MUC channels.
 import dbus
 
 from gabbletest import exec_test
-from servicetest import EventPattern
+from servicetest import EventPattern, assertEquals, assertLength, assertContains
 import constants as cs
 import ns
 
@@ -18,7 +18,18 @@ def test(q, bus, conn, stream):
 
     # 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.
-    content = u"hi r ther ne warez n this chanel?"
+    send_message_and_expect_error(q, stream,
+        text_chan, test_handle, bob_handle,
+        u"hi r ther ne warez n this chanel?",
+        '401', 'auth', 'not-authorized',
+        cs.DELIVERY_STATUS_PERMANENTLY_FAILED,
+        cs.SendError.PERMISSION_DENIED)
+
+def send_message_and_expect_error(q, stream,
+                                  text_chan, test_handle, bob_handle,
+                                  content,
+                                  code, type_, element,
+                                  delivery_status, send_error_value):
     greeting = [
         dbus.Dictionary({ }, signature='sv'),
         { 'content-type': 'text/plain',
@@ -26,8 +37,7 @@ def test(q, bus, conn, stream):
         }
     ]
 
-    sent_token = dbus.Interface(text_chan, cs.CHANNEL_IFACE_MESSAGES) \
-        .SendMessage(greeting, dbus.UInt32(0))
+    sent_token = text_chan.Messages.SendMessage(greeting, dbus.UInt32(0))
 
     stream_message, _, _ = q.expect_many(
         EventPattern('stream-message'),
@@ -41,9 +51,9 @@ def test(q, bus, conn, stream):
     elem['to'] = 'chat at conf.localhost/test'
     elem['type'] = 'error'
     error = elem.addElement('error')
-    error['code'] = '401'
-    error['type'] = 'auth'
-    error.addElement((ns.STANZA, 'not-authorized'))
+    error['code'] = code
+    error['type'] = type_
+    error.addElement((ns.STANZA, element))
 
     stream.send(elem)
 
@@ -54,44 +64,43 @@ def test(q, bus, conn, stream):
         EventPattern('dbus-signal', signal='MessageReceived'),
         )
 
-    PERMISSION_DENIED = 3
-
     err, timestamp, type, text = send_error.args
-    assert err == PERMISSION_DENIED, send_error.args
+    assertEquals(send_error_value, err)
     # there's no way to tell when the original message was sent from the error stanza
-    assert timestamp == 0, send_error.args
+    assertEquals(0, timestamp)
     # Gabble can't determine the type of the original message; see muc/test-muc.py
     # assert type == 0, send_error.args
-    assert text == content, send_error.args
+    assertEquals(content, text)
 
     # The Text.Received signal should be a "you're not tall enough" stub
     id, timestamp, sender, type, flags, text = received.args
-    assert sender == 0, received.args
-    assert type == 4, received.args # Message_Type_Delivery_Report
-    assert flags == 2, received.args # Non_Text_Content
-    assert text == '', received.args
+    assertEquals(
+        (0, cs.MT_DELIVERY_REPORT, cs.MessageFlag.NON_TEXT_CONTENT, ''),
+        (sender, type, flags, text))
 
     # Check that the Messages.MessageReceived signal was a failed delivery report
-    assert len(message_received.args) == 1, message_received.args
+    assertLength(1, message_received.args)
     parts = message_received.args[0]
     # The delivery report should just be a header, no body.
-    assert len(parts) == 1, parts
+    assertLength(1, parts)
     part = parts[0]
     # The intended recipient was the MUC, so there's no contact handle
     # suitable for being 'message-sender'.
-    assert 'message-sender' not in part or part['message-sender'] == 0, part
-    assert part['message-type'] == 4, part # Message_Type_Delivery_Report
-    assert part['delivery-status'] == 3, part # Delivery_Status_Permanently_Failed
-    assert part['delivery-error'] == PERMISSION_DENIED, part
-    assert part['delivery-token'] == sent_token, part
+    assertEquals(0, part.get('message-sender', 0))
+    assertEquals(cs.MT_DELIVERY_REPORT, part['message-type'])
+    assertEquals(delivery_status, part['delivery-status'])
+    assertEquals(send_error_value, part['delivery-error'])
+    assertEquals(sent_token, part['delivery-token'])
 
     # Check that the included echo is from us, and matches all the keys in the
     # message we sent.
-    assert 'delivery-echo' in part, part
+    assertContains('delivery-echo', part)
     echo = part['delivery-echo']
-    assert len(echo) == len(greeting), (echo, greeting)
-    assert echo[0]['message-sender'] == test_handle, echo[0]
-    assert echo[0]['message-token'] == sent_token, echo[0]
+    assertLength(len(greeting), echo)
+    echo_header = echo[0]
+    assertEquals(test_handle, echo_header['message-sender'])
+    assertEquals(sent_token, echo_header['message-token'])
+
     for i in range(0, len(echo)):
         for key in greeting[i]:
             assert key in echo[i], (i, key, echo)



More information about the telepathy-commits mailing list