test: fix/improve tests for plugins XEP-0033 and XEP-0085 (mainly to return a Deferred)
This commit is contained in:
parent
4a4760b80b
commit
08e293a81d
2 changed files with 91 additions and 90 deletions
|
|
@ -25,7 +25,6 @@ from sat.test import helpers
|
|||
from sat.plugins import plugin_xep_0033 as plugin
|
||||
from sat.core.exceptions import CancelError
|
||||
from sat.core.log import getLogger
|
||||
from copy import deepcopy
|
||||
from twisted.internet import defer
|
||||
from wokkel.generic import parseXml
|
||||
from twisted.words.protocols.jabber.jid import JID
|
||||
|
|
@ -71,9 +70,9 @@ class XEP_0033Test(helpers.SatTestCase):
|
|||
self.assertEqual(data['extra']['addresses'], '%s:%s\n%s:%s\n%s:%s\n' % expected, msg)
|
||||
|
||||
treatments.addCallback(cb)
|
||||
treatments.callback(data)
|
||||
return treatments.callback(data)
|
||||
|
||||
def get_mess_data(self):
|
||||
def _get_mess_data(self):
|
||||
mess_data = {"to": JID(JID_STR_TO),
|
||||
"type": "chat",
|
||||
"message": "content",
|
||||
|
|
@ -88,8 +87,10 @@ class XEP_0033Test(helpers.SatTestCase):
|
|||
mess_data['xml'] = parseXml(original_stanza.encode("utf-8"))
|
||||
return mess_data
|
||||
|
||||
def test_sendMessageTrigger(self):
|
||||
expected = self.get_mess_data()['xml']
|
||||
def _assertAddresses(self, mess_data):
|
||||
"""The mess_data that we got here has been modified by self.plugin.sendMessageTrigger,
|
||||
check that the addresses element has been added to the stanza."""
|
||||
expected = self._get_mess_data()['xml']
|
||||
addresses_extra = """
|
||||
<addresses xmlns='http://jabber.org/protocol/address'>
|
||||
<address type='%s' jid='%s'/>
|
||||
|
|
@ -98,100 +99,93 @@ class XEP_0033Test(helpers.SatTestCase):
|
|||
</addresses>""" % ADDRS
|
||||
addresses_element = parseXml(addresses_extra.encode('utf-8'))
|
||||
expected.addChild(addresses_element)
|
||||
self.assertEqualXML(mess_data['xml'].toXml().encode("utf-8"), expected.toXml().encode("utf-8"))
|
||||
|
||||
def assertAddresses(mess_data):
|
||||
"""The mess_data that we got here has been modified by self.plugin.sendMessageTrigger,
|
||||
check that the addresses element has been added to the stanza."""
|
||||
self.assertEqualXML(mess_data['xml'].toXml().encode("utf-8"), expected.toXml().encode("utf-8"))
|
||||
def _checkSentAndStored(self):
|
||||
"""Check that all the recipients got their messages and that the history has been filled.
|
||||
/!\ see the comments in XEP_0033.sendAndStoreMessage"""
|
||||
sent = []
|
||||
stored = []
|
||||
d_list = []
|
||||
|
||||
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)
|
||||
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)
|
||||
stored.append(host)
|
||||
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)
|
||||
|
||||
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 = []
|
||||
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)
|
||||
stored.append(host)
|
||||
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)
|
||||
|
||||
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
|
||||
level to not pollute the output, then check that the plugin did its
|
||||
job. It should abort sending the message or add the extended
|
||||
addressing information to the stanza.
|
||||
@param data: the data to be processed by self.plugin.sendMessageTrigger
|
||||
@param exception: CancelError
|
||||
"""
|
||||
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
|
||||
pre_treatments = defer.Deferred()
|
||||
post_treatments = defer.Deferred()
|
||||
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))
|
||||
return post_treatments
|
||||
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(self, data):
|
||||
"""Execute self.plugin.sendMessageTrigger with a different logging
|
||||
level to not pollute the output, then check that the plugin did its
|
||||
job. It should abort sending the message or add the extended
|
||||
addressing information to the stanza.
|
||||
@param data: the data to be processed by self.plugin.sendMessageTrigger
|
||||
"""
|
||||
logger = getLogger()
|
||||
level = logger.getEffectiveLevel()
|
||||
logger.setLevel(ERROR) # remove log.warning pollution
|
||||
pre_treatments = defer.Deferred()
|
||||
post_treatments = defer.Deferred()
|
||||
self.plugin.sendMessageTrigger(data, pre_treatments, post_treatments, PROFILE)
|
||||
post_treatments.callback(data)
|
||||
logger.setLevel(level)
|
||||
post_treatments.addCallbacks(self._assertAddresses, lambda failure: failure.trap(CancelError))
|
||||
return post_treatments
|
||||
|
||||
def test_sendMessageTriggerFeatureNotSupported(self):
|
||||
# feature is not supported, abort the message
|
||||
self.host.memory.reinit()
|
||||
data = self.get_mess_data()
|
||||
d = trigger(data, CancelError)
|
||||
data = self._get_mess_data()
|
||||
return self._trigger(data)
|
||||
|
||||
def test_sendMessageTriggerFeatureSupported(self):
|
||||
# feature is supported by the main target server
|
||||
self.host.reinit()
|
||||
self.host.addFeature(JID(JID_STR_TO), plugin.NS_ADDRESS, PROFILE)
|
||||
data = self.get_mess_data()
|
||||
d.addCallback(lambda dummy: trigger(data, CancelError))
|
||||
d.addCallback(lambda dummy: checkSentAndStored())
|
||||
data = self._get_mess_data()
|
||||
d = self._trigger(data)
|
||||
return d.addCallback(lambda dummy: self._checkSentAndStored())
|
||||
|
||||
def test_sendMessageTriggerFeatureFullySupported(self):
|
||||
# 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 = self.get_mess_data()
|
||||
d.addCallback(lambda dummy: trigger(data, CancelError))
|
||||
d.addCallback(lambda dummy: checkSentAndStored())
|
||||
data = self._get_mess_data()
|
||||
d = self._trigger(data)
|
||||
return d.addCallback(lambda dummy: self._checkSentAndStored())
|
||||
|
||||
def test_sendMessageTriggerFixWrongEntity(self):
|
||||
# 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 = self.get_mess_data()
|
||||
data = self._get_mess_data()
|
||||
data["to"] = JID(JID_STR_X_TO)
|
||||
d.addCallback(lambda dummy: trigger(data, CancelError))
|
||||
d.addCallback(lambda dummy: checkSentAndStored())
|
||||
return d
|
||||
d = self._trigger(data)
|
||||
return d.addCallback(lambda dummy: self._checkSentAndStored())
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Reference in a new issue