test: fix/improve tests for plugins XEP-0033 and XEP-0085 (mainly to return a Deferred)

This commit is contained in:
souliane 2014-12-25 11:49:13 +01:00
commit 08e293a81d

View file

@ -27,7 +27,6 @@ from sat.plugins import plugin_xep_0085 as plugin
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 @@ class XEP_0085Test(helpers.SatTestCase):
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 @@ class XEP_0085Test(helpers.SatTestCase):
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 @@ class XEP_0085Test(helpers.SatTestCase):
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)