comparison src/test/test_plugin_xep_0085.py @ 783:27581cddb758

test: added tests for XEP-0085
author souliane <souliane@mailoo.org>
date Sat, 04 Jan 2014 21:09:32 +0100
parents
children c3acc1298a2f
comparison
equal deleted inserted replaced
782:0e5807193721 783:27581cddb758
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # SAT: a jabber client
5 # Copyright (C) 2009, 2010, 2011, 2012, 2013 Jérôme Poisson (goffi@goffi.org)
6 # Copyright (C) 2013 Adrien Cossa (souliane@mailoo.org)
7
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU Affero General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU Affero General Public License for more details.
17
18 # You should have received a copy of the GNU Affero General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20
21 """ Plugin chat states notification tests """
22
23 from constants import Const
24 from sat.test import helpers
25 from sat.plugins import plugin_xep_0085 as plugin
26 from sat.memory.memory import NO_SECURITY_LIMIT
27 from copy import deepcopy
28 from twisted.internet import defer
29 from wokkel.generic import parseXml
30 from twisted.words.protocols.jabber.jid import JID
31
32
33 class XEP_0085Test(helpers.SatTestCase):
34
35 def setUp(self):
36 self.host = helpers.FakeSAT()
37 self.plugin = plugin.XEP_0085(self.host)
38
39 def test_messageReceived(self):
40 self.host.memory.init()
41 self.host.memory.setParam(plugin.PARAM_NAME, True, plugin.PARAM_KEY, NO_SECURITY_LIMIT, Const.TEST_PROFILE)
42 for state in plugin.CHAT_STATES:
43 xml = """
44 <message type="chat" from="sender@example.net/house" to="sender@example.net/house" id="test_1">
45 %s
46 <%s xmlns='http://jabber.org/protocol/chatstates'/>
47 </message>
48 """ % ("<body>test</body>" if state == "active" else "", state)
49 stanza = parseXml(xml)
50 self.host.bridge.expectCall("chatStateReceived", u"sender@example.net/house", state, Const.TEST_PROFILE)
51 self.plugin.messageReceivedTrigger(stanza, defer.Deferred(), Const.TEST_PROFILE)
52
53 def test_sendMessageTrigger(self):
54 self.host.memory.init()
55 self.host.memory.setParam(plugin.PARAM_NAME, True, plugin.PARAM_KEY, NO_SECURITY_LIMIT, Const.TEST_PROFILE)
56 for state in plugin.CHAT_STATES:
57 mess_data = {"to": "test@example.org/SàT",
58 "type": "chat",
59 "message": "content",
60 "extra": {} if state == "active" else {"chat_state": state}}
61 mess_data['xml'] = parseXml("""
62 <message type="chat" from="sender@example.net/house" to="test@example.org/SàT" id="test_1">
63 %s
64 </message>
65 """ % (("<body>%s</body>" % mess_data['message']) if state == "active" else "",))
66 expected = deepcopy(mess_data['xml'])
67 expected.addElement(state, plugin.NS_CHAT_STATES)
68 treatments = defer.Deferred()
69 self.plugin.sendMessageTrigger(mess_data, treatments, Const.TEST_PROFILE)
70 xml = treatments.callbacks[0][0][0](mess_data)
71 # cancel the timer to not block the process
72 self.plugin.map[Const.TEST_PROFILE][Const.TEST_JID.userhostJID()].timer.cancel()
73 self.assertEqualXML(xml['xml'].toXml().encode("utf-8"), expected.toXml().encode("utf-8"))