comparison 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
comparison
equal deleted inserted replaced
1277:3a3e3014f9f8 1278:347aee3a3f5c
25 from sat.core.constants import Const as C 25 from sat.core.constants import Const as C
26 from sat.plugins import plugin_xep_0085 as plugin 26 from sat.plugins import plugin_xep_0085 as plugin
27 from copy import deepcopy 27 from copy import deepcopy
28 from twisted.internet import defer 28 from twisted.internet import defer
29 from wokkel.generic import parseXml 29 from wokkel.generic import parseXml
30 from twisted.words.protocols.jabber.jid import JID
31 30
32 31
33 class XEP_0085Test(helpers.SatTestCase): 32 class XEP_0085Test(helpers.SatTestCase):
34 33
35 def setUp(self): 34 def setUp(self):
36 self.host = helpers.FakeSAT() 35 self.host = helpers.FakeSAT()
37 self.plugin = plugin.XEP_0085(self.host) 36 self.plugin = plugin.XEP_0085(self.host)
37 self.host.memory.setParam(plugin.PARAM_NAME, True, plugin.PARAM_KEY, C.NO_SECURITY_LIMIT, Const.PROFILE[0])
38 38
39 def test_messageReceived(self): 39 def test_messageReceived(self):
40 self.host.memory.reinit()
41 self.host.memory.setParam(plugin.PARAM_NAME, True, plugin.PARAM_KEY, C.NO_SECURITY_LIMIT, Const.PROFILE[0])
42 for state in plugin.CHAT_STATES: 40 for state in plugin.CHAT_STATES:
43 xml = u""" 41 xml = u"""
44 <message type="chat" from="%s" to="%s" id="test_1"> 42 <message type="chat" from="%s" to="%s" id="test_1">
45 %s 43 %s
46 <%s xmlns='%s'/> 44 <%s xmlns='%s'/>
49 Const.JID_STR[0], 47 Const.JID_STR[0],
50 "<body>test</body>" if state == "active" else "", 48 "<body>test</body>" if state == "active" else "",
51 state, plugin.NS_CHAT_STATES) 49 state, plugin.NS_CHAT_STATES)
52 stanza = parseXml(xml.encode("utf-8")) 50 stanza = parseXml(xml.encode("utf-8"))
53 self.host.bridge.expectCall("chatStateReceived", Const.JID_STR[1], state, Const.PROFILE[0]) 51 self.host.bridge.expectCall("chatStateReceived", Const.JID_STR[1], state, Const.PROFILE[0])
54 self.plugin.messageReceivedTrigger(stanza, defer.Deferred(), Const.PROFILE[0]) 52 self.plugin.messageReceivedTrigger(stanza, None, Const.PROFILE[0])
55 53
56 def test_sendMessageTrigger(self): 54 def test_sendMessageTrigger(self):
57 self.host.memory.reinit() 55 def cb(data):
58 self.host.memory.setParam(plugin.PARAM_NAME, True, plugin.PARAM_KEY, C.NO_SECURITY_LIMIT, Const.PROFILE[0]) 56 xml = data['xml'].toXml().encode("utf-8")
57 self.assertEqualXML(xml, expected.toXml().encode("utf-8"))
58
59 d_list = []
60
59 for state in plugin.CHAT_STATES: 61 for state in plugin.CHAT_STATES:
60 mess_data = {"to": Const.JID[0], 62 mess_data = {"to": Const.JID[0],
61 "type": "chat", 63 "type": "chat",
62 "message": "content", 64 "message": "content",
63 "extra": {} if state == "active" else {"chat_state": state}} 65 "extra": {} if state == "active" else {"chat_state": state}}
68 """ % (Const.JID_STR[1], Const.JID_STR[0], 70 """ % (Const.JID_STR[1], Const.JID_STR[0],
69 ("<body>%s</body>" % mess_data['message']) if state == "active" else "") 71 ("<body>%s</body>" % mess_data['message']) if state == "active" else "")
70 mess_data['xml'] = parseXml(stanza.encode("utf-8")) 72 mess_data['xml'] = parseXml(stanza.encode("utf-8"))
71 expected = deepcopy(mess_data['xml']) 73 expected = deepcopy(mess_data['xml'])
72 expected.addElement(state, plugin.NS_CHAT_STATES) 74 expected.addElement(state, plugin.NS_CHAT_STATES)
73 treatments = defer.Deferred() 75 post_treatments = defer.Deferred()
74 self.plugin.sendMessageTrigger(mess_data, defer.Deferred(), treatments, Const.PROFILE[0]) 76 self.plugin.sendMessageTrigger(mess_data, None, post_treatments, Const.PROFILE[0])
75 xml = treatments.callbacks[0][0][0](mess_data) 77
76 # cancel the timer to not block the process 78 post_treatments.addCallback(cb)
79 post_treatments.callback(mess_data)
80 d_list.append(post_treatments)
81
82 def cb_list(dummy): # cancel the timer to not block the process
77 self.plugin.map[Const.PROFILE[0]][Const.JID[0]].timer.cancel() 83 self.plugin.map[Const.PROFILE[0]][Const.JID[0]].timer.cancel()
78 self.assertEqualXML(xml['xml'].toXml().encode("utf-8"), expected.toXml().encode("utf-8")) 84
85 return defer.DeferredList(d_list).addCallback(cb_list)