changeset 1272:7fbc858cd1cd

test: fixes tests for XEP-0033
author souliane <souliane@mailoo.org>
date Fri, 19 Dec 2014 11:36:43 +0100
parents 2308f8405ffb
children f7977b2b727e
files src/test/test_plugin_xep_0033.py
diffstat 1 files changed, 45 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/src/test/test_plugin_xep_0033.py	Fri Dec 19 11:36:00 2014 +0100
+++ b/src/test/test_plugin_xep_0033.py	Fri Dec 19 11:36:43 2014 +0100
@@ -31,13 +31,16 @@
 from twisted.words.protocols.jabber.jid import JID
 from logging import ERROR
 
-PROFILE = Const.PROFILE[0]
+PROFILE_INDEX = 0
+PROFILE = Const.PROFILE[PROFILE_INDEX]
 JID_STR_FROM = Const.JID_STR[1]
 JID_STR_TO = Const.PROFILE_DICT[PROFILE].host
 JID_STR_X_TO = Const.JID_STR[0]
 JID_STR_X_CC = Const.JID_STR[1]
 JID_STR_X_BCC = Const.JID_STR[2]
 
+ADDRS = ('to', JID_STR_X_TO, 'cc', JID_STR_X_CC, 'bcc', JID_STR_X_BCC)
+
 
 class XEP_0033Test(helpers.SatTestCase):
 
@@ -70,27 +73,29 @@
         treatments.addCallback(cb)
         treatments.callback(data)
 
-    def test_sendMessageTrigger(self):
+    def get_mess_data(self):
         mess_data = {"to": JID(JID_STR_TO),
                      "type": "chat",
                      "message": "content",
                      "extra": {}
                      }
-        addresses = ('to', JID_STR_X_TO, 'cc', JID_STR_X_CC, 'bcc', JID_STR_X_BCC)
-        mess_data["extra"]["address"] = '%s:%s\n%s:%s\n%s:%s\n' % addresses
+        mess_data["extra"]["address"] = '%s:%s\n%s:%s\n%s:%s\n' % ADDRS
         original_stanza = u"""
         <message type="chat" from="%s" to="%s" id="test_1">
             <body>content</body>
         </message>
         """ % (JID_STR_FROM, JID_STR_TO)
         mess_data['xml'] = parseXml(original_stanza.encode("utf-8"))
-        expected = deepcopy(mess_data['xml'])
+        return mess_data
+
+    def test_sendMessageTrigger(self):
+        expected = self.get_mess_data()['xml']
         addresses_extra = """
         <addresses xmlns='http://jabber.org/protocol/address'>
             <address type='%s' jid='%s'/>
             <address type='%s' jid='%s'/>
             <address type='%s' jid='%s'/>
-        </addresses>""" % addresses
+        </addresses>""" % ADDRS
         addresses_element = parseXml(addresses_extra.encode('utf-8'))
         expected.addChild(addresses_element)
 
@@ -109,13 +114,9 @@
             /!\ see the comments in XEP_0033.sendAndStoreMessage"""
             sent = []
             stored = []
-            for to_s in (JID_STR_X_TO, JID_STR_X_CC, JID_STR_X_BCC):
-                to_jid = JID(to_s)
-                host = JID(to_jid.host)
-                logger = getLogger()
-                level = logger.getEffectiveLevel()
-                logger.setLevel(ERROR)  # remove log.warning pollution
-                entities = yield self.host.findFeaturesSet([plugin.NS_ADDRESS], jid_=host, profile_key=PROFILE)
+            d_list = []
+
+            def cb(entities, to_jid, logger, level):
                 if host in entities:
                     if host not in sent:  # send the message to the entity offering the feature
                         sent.append(host)
@@ -125,9 +126,24 @@
                     sent.append(to_jid)
                     stored.append(to_jid)
                 logger.setLevel(level)
-            msg = "/!\ see the comments in XEP_0033.sendAndStoreMessage"
-            self.assertEqualUnsortedList(self.host.sent_messages, sent, msg)
-            self.assertEqualUnsortedList(self.host.stored_messages, stored, msg)
+
+            for to_s in (JID_STR_X_TO, JID_STR_X_CC, JID_STR_X_BCC):
+                to_jid = JID(to_s)
+                host = JID(to_jid.host)
+                logger = getLogger()
+                level = logger.getEffectiveLevel()
+                logger.setLevel(ERROR)  # remove log.warning pollution
+                d = self.host.findFeaturesSet([plugin.NS_ADDRESS], jid_=host, profile_key=PROFILE)
+                d.addCallback(cb, to_jid, logger, level)
+                d_list.append(d)
+
+            def cb_list(dummy):
+                msg = "/!\ see the comments in XEP_0033.sendAndStoreMessage"
+                sent_recipients = [JID(elt['to']) for elt in self.host.getSentMessages(PROFILE_INDEX)]
+                self.assertEqualUnsortedList(sent_recipients, sent, msg)
+                self.assertEqualUnsortedList(self.host.stored_messages, stored, msg)
+
+            return defer.DeferredList(d_list).addCallback(cb_list)
 
         def trigger(data, exception):
             """Execute self.plugin.sendMessageTrigger with a different logging
@@ -146,34 +162,36 @@
             post_treatments.callback(data)
             logger.setLevel(level)
             post_treatments.addCallbacks(assertAddresses, lambda failure: sendMessageErrback(failure, exception))
+            return post_treatments
 
         # feature is not supported, abort the message
-        data = deepcopy(mess_data)
-        trigger(data, CancelError)
         self.host.memory.reinit()
+        data = self.get_mess_data()
+        d = trigger(data, CancelError)
 
         # feature is supported by the main target server
         self.host.reinit()
         self.host.addFeature(JID(JID_STR_TO), plugin.NS_ADDRESS, PROFILE)
-        data = deepcopy(mess_data)
-        trigger(data, CancelError)
-        checkSentAndStored()
+        data = self.get_mess_data()
+        d.addCallback(lambda dummy: trigger(data, CancelError))
+        d.addCallback(lambda dummy: checkSentAndStored())
 
         # feature is supported by all target servers
         self.host.reinit()
         self.host.addFeature(JID(JID_STR_TO), plugin.NS_ADDRESS, PROFILE)
         for dest in (JID_STR_X_TO, JID_STR_X_CC, JID_STR_X_BCC):
             self.host.addFeature(JID(JID(dest).host), plugin.NS_ADDRESS, PROFILE)
-        data = deepcopy(mess_data)
-        trigger(data, CancelError)
-        checkSentAndStored()
+        data = self.get_mess_data()
+        d.addCallback(lambda dummy: trigger(data, CancelError))
+        d.addCallback(lambda dummy: checkSentAndStored())
 
         # check that a wrong recipient entity is fixed by the backend
         self.host.reinit()
         self.host.addFeature(JID(JID_STR_TO), plugin.NS_ADDRESS, PROFILE)
         for dest in (JID_STR_X_TO, JID_STR_X_CC, JID_STR_X_BCC):
             self.host.addFeature(JID(JID(dest).host), plugin.NS_ADDRESS, PROFILE)
-        data = deepcopy(mess_data)
+        data = self.get_mess_data()
         data["to"] = JID(JID_STR_X_TO)
-        trigger(data, CancelError)
-        checkSentAndStored()
+        d.addCallback(lambda dummy: trigger(data, CancelError))
+        d.addCallback(lambda dummy: checkSentAndStored())
+        return d