diff src/test/test_plugin_xep_0033.py @ 999:c37a24922f27

plugin XEP_0033: fixes the server part and the tests
author souliane <souliane@mailoo.org>
date Fri, 11 Apr 2014 11:02:42 +0200
parents b3f383ab39da
children 7ea0215e7092
line wrap: on
line diff
--- a/src/test/test_plugin_xep_0033.py	Wed Apr 23 12:01:59 2014 +0200
+++ b/src/test/test_plugin_xep_0033.py	Fri Apr 11 11:02:42 2014 +0200
@@ -31,9 +31,15 @@
 from twisted.words.protocols.jabber.jid import JID
 from logging import ERROR
 
+PROFILE = Const.PROFILE[0]
+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]
+
 
 class XEP_0033Test(helpers.SatTestCase):
-    skip = "Must be fixed after disco changes"
 
     def setUp(self):
         self.host = helpers.FakeSAT()
@@ -50,15 +56,14 @@
                 <address type='bcc' jid='%s'/>
             </addresses>
         </message>
-        """ % (Const.JID_STR[1], self.host.getClientHostJid(Const.PROFILE[0]),
-               Const.JID_STR[0], Const.JID_STR[1], Const.JID_STR[2])
+        """ % (JID_STR_FROM, JID_STR_TO, JID_STR_X_TO, JID_STR_X_CC, JID_STR_X_BCC)
         stanza = parseXml(xml.encode("utf-8"))
         treatments = defer.Deferred()
-        self.plugin.messageReceivedTrigger(stanza, treatments, Const.PROFILE[0])
+        self.plugin.messageReceivedTrigger(stanza, treatments, PROFILE)
         data = {'extra': {}}
 
         def cb(data):
-            expected = ('to', Const.JID_STR[0], 'cc', Const.JID_STR[1], 'bcc', Const.JID_STR[2])
+            expected = ('to', JID_STR_X_TO, 'cc', JID_STR_X_CC, 'bcc', JID_STR_X_BCC)
             msg = 'Expected: %s\nGot:      %s' % (expected, data['extra']['addresses'])
             self.assertEqual(data['extra']['addresses'], '%s:%s\n%s:%s\n%s:%s\n' % expected, msg)
 
@@ -66,18 +71,18 @@
         treatments.callback(data)
 
     def test_sendMessageTrigger(self):
-        mess_data = {"to": self.host.getClientHostJid(Const.PROFILE[0]),
+        mess_data = {"to": JID(JID_STR_TO),
                      "type": "chat",
                      "message": "content",
                      "extra": {}
                      }
-        addresses = ('to', Const.JID_STR[0], 'cc', Const.JID_STR[1], 'bcc', Const.JID_STR[2])
+        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
         original_stanza = u"""
         <message type="chat" from="%s" to="%s" id="test_1">
             <body>content</body>
         </message>
-        """ % (Const.JID_STR[1], self.host.getClientHostJid(Const.PROFILE[0]))
+        """ % (JID_STR_FROM, JID_STR_TO)
         mess_data['xml'] = parseXml(original_stanza.encode("utf-8"))
         expected = deepcopy(mess_data['xml'])
         addresses_extra = """
@@ -97,29 +102,28 @@
         def sendMessageErrback(failure, exception_class):
             """If the failure does encapsulate the expected exception, it will be silently
             trapped, otherwise it will be re-raised and will make the test fail"""
+            failure.trap(exception_class)
             if exception_class == MessageSentAndStored:
                 assertAddresses(failure.value.mess_data)
-            failure.trap(exception_class)
 
         def checkSentAndStored():
             """Check that all the recipients got their messages and that the history has been filled.
             /!\ see the comments in XEP_0033.sendAndStoreMessage"""
             sent = []
             stored = []
-            cache = set()
-            for to_s in [addresses[1], addresses[3], addresses[5]]:
+            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
-                if self.host.memory.hasServerFeature(plugin.NS_ADDRESS, host, Const.PROFILE[0]):
-                    if host not in cache:
+                entities = yield self.host.findFeaturesSet([plugin.NS_ADDRESS], jid_=host, profile_key=PROFILE)
+                if host in entities:
+                    if host not in sent:  # send the message to the entity offering the feature
                         sent.append(host)
                         stored.append(host)
-                        cache.add(host)
-                    stored.append(to_jid)
-                else:
+                    stored.append(to_jid)  # store in history for each recipient
+                else:  # feature not supported, use normal behavior
                     sent.append(to_jid)
                     stored.append(to_jid)
                 logger.setLevel(level)
@@ -140,7 +144,7 @@
             logger.setLevel(ERROR)  # remove log.warning pollution
             pre_treatments = defer.Deferred()
             post_treatments = defer.Deferred()
-            self.plugin.sendMessageTrigger(data, pre_treatments, post_treatments, Const.PROFILE[0])
+            self.plugin.sendMessageTrigger(data, pre_treatments, post_treatments, PROFILE)
             post_treatments.callback(data)
             logger.setLevel(level)
             post_treatments.addCallbacks(assertAddresses, lambda failure: sendMessageErrback(failure, exception))
@@ -150,9 +154,20 @@
         data = deepcopy(mess_data)
         trigger(data, AbortSendMessage)
 
-        # feature is supported
+        # feature is supported by the main target server
         self.host.init()
         self.host.memory.init()
+        self.host.addFeature(JID(JID_STR_TO), plugin.NS_ADDRESS, PROFILE)
+        data = deepcopy(mess_data)
+        trigger(data, MessageSentAndStored)
+        checkSentAndStored()
+
+        # feature is supported by all target servers
+        self.host.init()
+        self.host.memory.init()
+        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, MessageSentAndStored)
         checkSentAndStored()
@@ -160,7 +175,10 @@
         # check that a wrong recipient entity is fixed by the backend
         self.host.init()
         self.host.memory.init()
+        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["to"] = Const.JID[0]
+        data["to"] = JID(JID_STR_X_TO)
         trigger(data, MessageSentAndStored)
         checkSentAndStored()