Mercurial > libervia-backend
annotate libervia/backend/test/test_plugin_xep_0085.py @ 4100:810921c33a47
tools (common/template): add filter to get media types:
Add 2 filters to get main type and subtype of media type. Jinja2 and Nunjucks don't handle
slices in the same way (Python way for Jinja2, JS way for Nunjucks), making it difficult
to retrieve main type of a media from media type. Thoses filters work in both cases.
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 22 Jun 2023 15:49:06 +0200 |
parents | 4b842c1fb686 |
children |
rev | line source |
---|---|
3028 | 1 #!/usr/bin/env python3 |
3137 | 2 |
783 | 3 |
4 # SAT: a jabber client | |
3479 | 5 # Copyright (C) 2009-2021 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 |
4071
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
24 from libervia.backend.test import helpers |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
25 from libervia.backend.core.constants import Const as C |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
26 from libervia.backend.plugins import plugin_xep_0085 as plugin |
783 | 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) | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
36 self.host.memory.param_set( |
2624
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 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
44 def test_message_received(self): |
783 | 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")) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
59 self.host.bridge.expect_call( |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
60 "chat_state_received", Const.JID_STR[1], state, Const.PROFILE[0] |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
61 ) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
62 self.plugin.message_received_trigger( |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
63 self.host.get_client(Const.PROFILE[0]), stanza, None |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
64 ) |
783 | 65 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
66 def test_message_send_trigger(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") |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
69 self.assert_equal_xml(xml, expected.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
|
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( |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
94 self.host.get_client(Const.PROFILE[0]), mess_data, None, post_treatments |
2624
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) |