Mercurial > libervia-backend
annotate sat/test/test_plugin_xep_0085.py @ 3036:3338c6a634f5
install (setup.py): updated classifiers
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 25 Aug 2019 12:16:18 +0200 |
parents | ab2696e34d29 |
children | 9d0df638c8b4 |
rev | line source |
---|---|
3028 | 1 #!/usr/bin/env python3 |
783 | 2 # -*- coding: utf-8 -*- |
3 | |
4 # SAT: a jabber client | |
2771 | 5 # Copyright (C) 2009-2019 Jérôme Poisson (goffi@goffi.org) |
1766 | 6 # Copyright (C) 2013-2016 Adrien Cossa (souliane@mailoo.org) |
783 | 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 | |
3028 | 23 from .constants import Const |
783 | 24 from sat.test import helpers |
915 | 25 from sat.core.constants import Const as C |
783 | 26 from sat.plugins import plugin_xep_0085 as plugin |
27 from copy import deepcopy | |
28 from twisted.internet import defer | |
29 from wokkel.generic import parseXml | |
30 | |
31 | |
32 class XEP_0085Test(helpers.SatTestCase): | |
33 def setUp(self): | |
34 self.host = helpers.FakeSAT() | |
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 | 43 |
44 def test_messageReceived(self): | |
45 for state in plugin.CHAT_STATES: | |
3028 | 46 xml = """ |
786
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 | 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 | 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 | 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 | 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 } |
3028 | 80 stanza = """ |
786
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 | 82 %s |
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 | 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 |
2765
378188abe941
misc: replaced all "dummy" by the more conventional and readable "__" ("_" being used for gettext)
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
101 def cb_list(__): # 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) |