Mercurial > libervia-backend
annotate src/test/test_plugin_xep_0085.py @ 787:dd656d745d6a
test: added tests for XEP-0033
author | souliane <souliane@mailoo.org> |
---|---|
date | Sun, 05 Jan 2014 13:05:31 +0100 |
parents | c3acc1298a2f |
children | 2136be5a44a8 |
rev | line source |
---|---|
783 | 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: | |
786
c3acc1298a2f
test: FakeMemory inherits from Memory + more helpers basic support + cleaning
souliane <souliane@mailoo.org>
parents:
783
diff
changeset
|
43 xml = u""" |
c3acc1298a2f
test: FakeMemory inherits from Memory + more helpers basic support + cleaning
souliane <souliane@mailoo.org>
parents:
783
diff
changeset
|
44 <message type="chat" from="%s" to="%s" id="test_1"> |
783 | 45 %s |
786
c3acc1298a2f
test: FakeMemory inherits from Memory + more helpers basic support + cleaning
souliane <souliane@mailoo.org>
parents:
783
diff
changeset
|
46 <%s xmlns='%s'/> |
783 | 47 </message> |
786
c3acc1298a2f
test: FakeMemory inherits from Memory + more helpers basic support + cleaning
souliane <souliane@mailoo.org>
parents:
783
diff
changeset
|
48 """ % (Const.TEST_JID_2_STR, |
c3acc1298a2f
test: FakeMemory inherits from Memory + more helpers basic support + cleaning
souliane <souliane@mailoo.org>
parents:
783
diff
changeset
|
49 Const.TEST_JID_STR, |
c3acc1298a2f
test: FakeMemory inherits from Memory + more helpers basic support + cleaning
souliane <souliane@mailoo.org>
parents:
783
diff
changeset
|
50 "<body>test</body>" if state == "active" else "", |
c3acc1298a2f
test: FakeMemory inherits from Memory + more helpers basic support + cleaning
souliane <souliane@mailoo.org>
parents:
783
diff
changeset
|
51 state, plugin.NS_CHAT_STATES) |
c3acc1298a2f
test: FakeMemory inherits from Memory + more helpers basic support + cleaning
souliane <souliane@mailoo.org>
parents:
783
diff
changeset
|
52 stanza = parseXml(xml.encode("utf-8")) |
783 | 53 self.host.bridge.expectCall("chatStateReceived", u"sender@example.net/house", state, Const.TEST_PROFILE) |
54 self.plugin.messageReceivedTrigger(stanza, defer.Deferred(), Const.TEST_PROFILE) | |
55 | |
56 def test_sendMessageTrigger(self): | |
57 self.host.memory.init() | |
58 self.host.memory.setParam(plugin.PARAM_NAME, True, plugin.PARAM_KEY, NO_SECURITY_LIMIT, Const.TEST_PROFILE) | |
59 for state in plugin.CHAT_STATES: | |
786
c3acc1298a2f
test: FakeMemory inherits from Memory + more helpers basic support + cleaning
souliane <souliane@mailoo.org>
parents:
783
diff
changeset
|
60 mess_data = {"to": Const.TEST_JID, |
783 | 61 "type": "chat", |
62 "message": "content", | |
63 "extra": {} if state == "active" else {"chat_state": state}} | |
786
c3acc1298a2f
test: FakeMemory inherits from Memory + more helpers basic support + cleaning
souliane <souliane@mailoo.org>
parents:
783
diff
changeset
|
64 stanza = u""" |
c3acc1298a2f
test: FakeMemory inherits from Memory + more helpers basic support + cleaning
souliane <souliane@mailoo.org>
parents:
783
diff
changeset
|
65 <message type="chat" from="%s" to="%s" id="test_1"> |
783 | 66 %s |
67 </message> | |
786
c3acc1298a2f
test: FakeMemory inherits from Memory + more helpers basic support + cleaning
souliane <souliane@mailoo.org>
parents:
783
diff
changeset
|
68 """ % (Const.TEST_JID_2_STR, Const.TEST_JID_STR, |
c3acc1298a2f
test: FakeMemory inherits from Memory + more helpers basic support + cleaning
souliane <souliane@mailoo.org>
parents:
783
diff
changeset
|
69 ("<body>%s</body>" % mess_data['message']) if state == "active" else "") |
c3acc1298a2f
test: FakeMemory inherits from Memory + more helpers basic support + cleaning
souliane <souliane@mailoo.org>
parents:
783
diff
changeset
|
70 mess_data['xml'] = parseXml(stanza.encode("utf-8")) |
783 | 71 expected = deepcopy(mess_data['xml']) |
72 expected.addElement(state, plugin.NS_CHAT_STATES) | |
73 treatments = defer.Deferred() | |
74 self.plugin.sendMessageTrigger(mess_data, treatments, Const.TEST_PROFILE) | |
75 xml = treatments.callbacks[0][0][0](mess_data) | |
76 # cancel the timer to not block the process | |
77 self.plugin.map[Const.TEST_PROFILE][Const.TEST_JID.userhostJID()].timer.cancel() | |
78 self.assertEqualXML(xml['xml'].toXml().encode("utf-8"), expected.toXml().encode("utf-8")) |