annotate sat/test/test_plugin_xep_0085.py @ 2685:eb69ec549d05

quick frontend (app): create the contact list immediatly on plug This avoid crash when contact list is used while connexion is not done yet or not possible (e.g. server unavailable).
author Goffi <goffi@goffi.org>
date Mon, 05 Nov 2018 09:13:05 +0100
parents 56f94936df1e
children 378188abe941
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1934
2daf7b4c6756 use of /usr/bin/env instead of /usr/bin/python in shebang
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
1 #!/usr/bin/env python2
783
27581cddb758 test: added tests for XEP-0085
souliane <souliane@mailoo.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
27581cddb758 test: added tests for XEP-0085
souliane <souliane@mailoo.org>
parents:
diff changeset
3
27581cddb758 test: added tests for XEP-0085
souliane <souliane@mailoo.org>
parents:
diff changeset
4 # SAT: a jabber client
2483
0046283a285d dates update
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org)
1766
d17772b0fe22 copyright update
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
6 # Copyright (C) 2013-2016 Adrien Cossa (souliane@mailoo.org)
783
27581cddb758 test: added tests for XEP-0085
souliane <souliane@mailoo.org>
parents:
diff changeset
7
27581cddb758 test: added tests for XEP-0085
souliane <souliane@mailoo.org>
parents:
diff changeset
8 # This program is free software: you can redistribute it and/or modify
27581cddb758 test: added tests for XEP-0085
souliane <souliane@mailoo.org>
parents:
diff changeset
9 # it under the terms of the GNU Affero General Public License as published by
27581cddb758 test: added tests for XEP-0085
souliane <souliane@mailoo.org>
parents:
diff changeset
10 # the Free Software Foundation, either version 3 of the License, or
27581cddb758 test: added tests for XEP-0085
souliane <souliane@mailoo.org>
parents:
diff changeset
11 # (at your option) any later version.
27581cddb758 test: added tests for XEP-0085
souliane <souliane@mailoo.org>
parents:
diff changeset
12
27581cddb758 test: added tests for XEP-0085
souliane <souliane@mailoo.org>
parents:
diff changeset
13 # This program is distributed in the hope that it will be useful,
27581cddb758 test: added tests for XEP-0085
souliane <souliane@mailoo.org>
parents:
diff changeset
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
27581cddb758 test: added tests for XEP-0085
souliane <souliane@mailoo.org>
parents:
diff changeset
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27581cddb758 test: added tests for XEP-0085
souliane <souliane@mailoo.org>
parents:
diff changeset
16 # GNU Affero General Public License for more details.
27581cddb758 test: added tests for XEP-0085
souliane <souliane@mailoo.org>
parents:
diff changeset
17
27581cddb758 test: added tests for XEP-0085
souliane <souliane@mailoo.org>
parents:
diff changeset
18 # You should have received a copy of the GNU Affero General Public License
27581cddb758 test: added tests for XEP-0085
souliane <souliane@mailoo.org>
parents:
diff changeset
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
27581cddb758 test: added tests for XEP-0085
souliane <souliane@mailoo.org>
parents:
diff changeset
20
27581cddb758 test: added tests for XEP-0085
souliane <souliane@mailoo.org>
parents:
diff changeset
21 """ Plugin chat states notification tests """
27581cddb758 test: added tests for XEP-0085
souliane <souliane@mailoo.org>
parents:
diff changeset
22
27581cddb758 test: added tests for XEP-0085
souliane <souliane@mailoo.org>
parents:
diff changeset
23 from constants import Const
27581cddb758 test: added tests for XEP-0085
souliane <souliane@mailoo.org>
parents:
diff changeset
24 from sat.test import helpers
915
6f96ee4d8cc0 tests: tests fixes
Goffi <goffi@goffi.org>
parents: 811
diff changeset
25 from sat.core.constants import Const as C
783
27581cddb758 test: added tests for XEP-0085
souliane <souliane@mailoo.org>
parents:
diff changeset
26 from sat.plugins import plugin_xep_0085 as plugin
27581cddb758 test: added tests for XEP-0085
souliane <souliane@mailoo.org>
parents:
diff changeset
27 from copy import deepcopy
27581cddb758 test: added tests for XEP-0085
souliane <souliane@mailoo.org>
parents:
diff changeset
28 from twisted.internet import defer
27581cddb758 test: added tests for XEP-0085
souliane <souliane@mailoo.org>
parents:
diff changeset
29 from wokkel.generic import parseXml
27581cddb758 test: added tests for XEP-0085
souliane <souliane@mailoo.org>
parents:
diff changeset
30
27581cddb758 test: added tests for XEP-0085
souliane <souliane@mailoo.org>
parents:
diff changeset
31
27581cddb758 test: added tests for XEP-0085
souliane <souliane@mailoo.org>
parents:
diff changeset
32 class XEP_0085Test(helpers.SatTestCase):
27581cddb758 test: added tests for XEP-0085
souliane <souliane@mailoo.org>
parents:
diff changeset
33 def setUp(self):
27581cddb758 test: added tests for XEP-0085
souliane <souliane@mailoo.org>
parents:
diff changeset
34 self.host = helpers.FakeSAT()
27581cddb758 test: added tests for XEP-0085
souliane <souliane@mailoo.org>
parents:
diff changeset
35 self.plugin = plugin.XEP_0085(self.host)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
36 self.host.memory.setParam(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
37 plugin.PARAM_NAME,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
38 True,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
39 plugin.PARAM_KEY,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
40 C.NO_SECURITY_LIMIT,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
41 Const.PROFILE[0],
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
42 )
783
27581cddb758 test: added tests for XEP-0085
souliane <souliane@mailoo.org>
parents:
diff changeset
43
27581cddb758 test: added tests for XEP-0085
souliane <souliane@mailoo.org>
parents:
diff changeset
44 def test_messageReceived(self):
27581cddb758 test: added tests for XEP-0085
souliane <souliane@mailoo.org>
parents:
diff changeset
45 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
46 xml = u"""
c3acc1298a2f test: FakeMemory inherits from Memory + more helpers basic support + cleaning
souliane <souliane@mailoo.org>
parents: 783
diff changeset
47 <message type="chat" from="%s" to="%s" id="test_1">
783
27581cddb758 test: added tests for XEP-0085
souliane <souliane@mailoo.org>
parents:
diff changeset
48 %s
786
c3acc1298a2f test: FakeMemory inherits from Memory + more helpers basic support + cleaning
souliane <souliane@mailoo.org>
parents: 783
diff changeset
49 <%s xmlns='%s'/>
783
27581cddb758 test: added tests for XEP-0085
souliane <souliane@mailoo.org>
parents:
diff changeset
50 </message>
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
51 """ % (
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
52 Const.JID_STR[1],
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
53 Const.JID_STR[0],
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
54 "<body>test</body>" if state == "active" else "",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
55 state,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
56 plugin.NS_CHAT_STATES,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
57 )
786
c3acc1298a2f test: FakeMemory inherits from Memory + more helpers basic support + cleaning
souliane <souliane@mailoo.org>
parents: 783
diff changeset
58 stanza = parseXml(xml.encode("utf-8"))
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
59 self.host.bridge.expectCall(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
60 "chatStateReceived", Const.JID_STR[1], state, Const.PROFILE[0]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
61 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
62 self.plugin.messageReceivedTrigger(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
63 self.host.getClient(Const.PROFILE[0]), stanza, None
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
64 )
783
27581cddb758 test: added tests for XEP-0085
souliane <souliane@mailoo.org>
parents:
diff changeset
65
1955
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
66 def test_messageSendTrigger(self):
1278
347aee3a3f5c test: fix/improve tests for plugins XEP-0033 and XEP-0085 (mainly to return a Deferred)
souliane <souliane@mailoo.org>
parents: 1271
diff changeset
67 def cb(data):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
68 xml = data["xml"].toXml().encode("utf-8")
1278
347aee3a3f5c test: fix/improve tests for plugins XEP-0033 and XEP-0085 (mainly to return a Deferred)
souliane <souliane@mailoo.org>
parents: 1271
diff changeset
69 self.assertEqualXML(xml, expected.toXml().encode("utf-8"))
347aee3a3f5c test: fix/improve tests for plugins XEP-0033 and XEP-0085 (mainly to return a Deferred)
souliane <souliane@mailoo.org>
parents: 1271
diff changeset
70
347aee3a3f5c test: fix/improve tests for plugins XEP-0033 and XEP-0085 (mainly to return a Deferred)
souliane <souliane@mailoo.org>
parents: 1271
diff changeset
71 d_list = []
347aee3a3f5c test: fix/improve tests for plugins XEP-0033 and XEP-0085 (mainly to return a Deferred)
souliane <souliane@mailoo.org>
parents: 1271
diff changeset
72
783
27581cddb758 test: added tests for XEP-0085
souliane <souliane@mailoo.org>
parents:
diff changeset
73 for state in plugin.CHAT_STATES:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
74 mess_data = {
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
75 "to": Const.JID[0],
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
76 "type": "chat",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
77 "message": "content",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
78 "extra": {} if state == "active" else {"chat_state": state},
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
79 }
786
c3acc1298a2f test: FakeMemory inherits from Memory + more helpers basic support + cleaning
souliane <souliane@mailoo.org>
parents: 783
diff changeset
80 stanza = u"""
c3acc1298a2f test: FakeMemory inherits from Memory + more helpers basic support + cleaning
souliane <souliane@mailoo.org>
parents: 783
diff changeset
81 <message type="chat" from="%s" to="%s" id="test_1">
783
27581cddb758 test: added tests for XEP-0085
souliane <souliane@mailoo.org>
parents:
diff changeset
82 %s
27581cddb758 test: added tests for XEP-0085
souliane <souliane@mailoo.org>
parents:
diff changeset
83 </message>
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
84 """ % (
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
85 Const.JID_STR[1],
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
86 Const.JID_STR[0],
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
87 ("<body>%s</body>" % mess_data["message"]) if state == "active" else "",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
88 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
89 mess_data["xml"] = parseXml(stanza.encode("utf-8"))
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
90 expected = deepcopy(mess_data["xml"])
783
27581cddb758 test: added tests for XEP-0085
souliane <souliane@mailoo.org>
parents:
diff changeset
91 expected.addElement(state, plugin.NS_CHAT_STATES)
1278
347aee3a3f5c test: fix/improve tests for plugins XEP-0033 and XEP-0085 (mainly to return a Deferred)
souliane <souliane@mailoo.org>
parents: 1271
diff changeset
92 post_treatments = defer.Deferred()
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
93 self.plugin.messageSendTrigger(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
94 self.host.getClient(Const.PROFILE[0]), mess_data, None, post_treatments
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
95 )
1278
347aee3a3f5c test: fix/improve tests for plugins XEP-0033 and XEP-0085 (mainly to return a Deferred)
souliane <souliane@mailoo.org>
parents: 1271
diff changeset
96
347aee3a3f5c test: fix/improve tests for plugins XEP-0033 and XEP-0085 (mainly to return a Deferred)
souliane <souliane@mailoo.org>
parents: 1271
diff changeset
97 post_treatments.addCallback(cb)
347aee3a3f5c test: fix/improve tests for plugins XEP-0033 and XEP-0085 (mainly to return a Deferred)
souliane <souliane@mailoo.org>
parents: 1271
diff changeset
98 post_treatments.callback(mess_data)
347aee3a3f5c test: fix/improve tests for plugins XEP-0033 and XEP-0085 (mainly to return a Deferred)
souliane <souliane@mailoo.org>
parents: 1271
diff changeset
99 d_list.append(post_treatments)
347aee3a3f5c test: fix/improve tests for plugins XEP-0033 and XEP-0085 (mainly to return a Deferred)
souliane <souliane@mailoo.org>
parents: 1271
diff changeset
100
347aee3a3f5c test: fix/improve tests for plugins XEP-0033 and XEP-0085 (mainly to return a Deferred)
souliane <souliane@mailoo.org>
parents: 1271
diff changeset
101 def cb_list(dummy): # cancel the timer to not block the process
1254
87fbe4640448 test: fixes test for XEP-0085 (use full JID)
souliane <souliane@mailoo.org>
parents: 924
diff changeset
102 self.plugin.map[Const.PROFILE[0]][Const.JID[0]].timer.cancel()
1278
347aee3a3f5c test: fix/improve tests for plugins XEP-0033 and XEP-0085 (mainly to return a Deferred)
souliane <souliane@mailoo.org>
parents: 1271
diff changeset
103
347aee3a3f5c test: fix/improve tests for plugins XEP-0033 and XEP-0085 (mainly to return a Deferred)
souliane <souliane@mailoo.org>
parents: 1271
diff changeset
104 return defer.DeferredList(d_list).addCallback(cb_list)