diff src/test/test_plugin_xep_0085.py @ 1278:347aee3a3f5c

test: fix/improve tests for plugins XEP-0033 and XEP-0085 (mainly to return a Deferred)
author souliane <souliane@mailoo.org>
date Thu, 25 Dec 2014 11:49:13 +0100
parents 2308f8405ffb
children 069ad98b360d
line wrap: on
line diff
--- a/src/test/test_plugin_xep_0085.py	Fri Dec 19 14:43:42 2014 +0100
+++ b/src/test/test_plugin_xep_0085.py	Thu Dec 25 11:49:13 2014 +0100
@@ -27,7 +27,6 @@
 from copy import deepcopy
 from twisted.internet import defer
 from wokkel.generic import parseXml
-from twisted.words.protocols.jabber.jid import JID
 
 
 class XEP_0085Test(helpers.SatTestCase):
@@ -35,10 +34,9 @@
     def setUp(self):
         self.host = helpers.FakeSAT()
         self.plugin = plugin.XEP_0085(self.host)
+        self.host.memory.setParam(plugin.PARAM_NAME, True, plugin.PARAM_KEY, C.NO_SECURITY_LIMIT, Const.PROFILE[0])
 
     def test_messageReceived(self):
-        self.host.memory.reinit()
-        self.host.memory.setParam(plugin.PARAM_NAME, True, plugin.PARAM_KEY, C.NO_SECURITY_LIMIT, Const.PROFILE[0])
         for state in plugin.CHAT_STATES:
             xml = u"""
             <message type="chat" from="%s" to="%s" id="test_1">
@@ -51,11 +49,15 @@
                    state, plugin.NS_CHAT_STATES)
             stanza = parseXml(xml.encode("utf-8"))
             self.host.bridge.expectCall("chatStateReceived", Const.JID_STR[1], state, Const.PROFILE[0])
-            self.plugin.messageReceivedTrigger(stanza, defer.Deferred(), Const.PROFILE[0])
+            self.plugin.messageReceivedTrigger(stanza, None, Const.PROFILE[0])
 
     def test_sendMessageTrigger(self):
-        self.host.memory.reinit()
-        self.host.memory.setParam(plugin.PARAM_NAME, True, plugin.PARAM_KEY, C.NO_SECURITY_LIMIT, Const.PROFILE[0])
+        def cb(data):
+            xml = data['xml'].toXml().encode("utf-8")
+            self.assertEqualXML(xml, expected.toXml().encode("utf-8"))
+
+        d_list = []
+
         for state in plugin.CHAT_STATES:
             mess_data = {"to": Const.JID[0],
                          "type": "chat",
@@ -70,9 +72,14 @@
             mess_data['xml'] = parseXml(stanza.encode("utf-8"))
             expected = deepcopy(mess_data['xml'])
             expected.addElement(state, plugin.NS_CHAT_STATES)
-            treatments = defer.Deferred()
-            self.plugin.sendMessageTrigger(mess_data, defer.Deferred(), treatments, Const.PROFILE[0])
-            xml = treatments.callbacks[0][0][0](mess_data)
-            # cancel the timer to not block the process
+            post_treatments = defer.Deferred()
+            self.plugin.sendMessageTrigger(mess_data, None, post_treatments, Const.PROFILE[0])
+
+            post_treatments.addCallback(cb)
+            post_treatments.callback(mess_data)
+            d_list.append(post_treatments)
+
+        def cb_list(dummy):  # cancel the timer to not block the process
             self.plugin.map[Const.PROFILE[0]][Const.JID[0]].timer.cancel()
-            self.assertEqualXML(xml['xml'].toXml().encode("utf-8"), expected.toXml().encode("utf-8"))
+
+        return defer.DeferredList(d_list).addCallback(cb_list)