Mercurial > libervia-backend
comparison libervia/backend/test/test_plugin_xep_0085.py @ 4071:4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 02 Jun 2023 11:49:51 +0200 |
parents | sat/test/test_plugin_xep_0085.py@524856bd7b19 |
children |
comparison
equal
deleted
inserted
replaced
4070:d10748475025 | 4071:4b842c1fb686 |
---|---|
1 #!/usr/bin/env python3 | |
2 | |
3 | |
4 # SAT: a jabber client | |
5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org) | |
6 # Copyright (C) 2013-2016 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 libervia.backend.test import helpers | |
25 from libervia.backend.core.constants import Const as C | |
26 from libervia.backend.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) | |
36 self.host.memory.param_set( | |
37 plugin.PARAM_NAME, | |
38 True, | |
39 plugin.PARAM_KEY, | |
40 C.NO_SECURITY_LIMIT, | |
41 Const.PROFILE[0], | |
42 ) | |
43 | |
44 def test_message_received(self): | |
45 for state in plugin.CHAT_STATES: | |
46 xml = """ | |
47 <message type="chat" from="%s" to="%s" id="test_1"> | |
48 %s | |
49 <%s xmlns='%s'/> | |
50 </message> | |
51 """ % ( | |
52 Const.JID_STR[1], | |
53 Const.JID_STR[0], | |
54 "<body>test</body>" if state == "active" else "", | |
55 state, | |
56 plugin.NS_CHAT_STATES, | |
57 ) | |
58 stanza = parseXml(xml.encode("utf-8")) | |
59 self.host.bridge.expect_call( | |
60 "chat_state_received", Const.JID_STR[1], state, Const.PROFILE[0] | |
61 ) | |
62 self.plugin.message_received_trigger( | |
63 self.host.get_client(Const.PROFILE[0]), stanza, None | |
64 ) | |
65 | |
66 def test_message_send_trigger(self): | |
67 def cb(data): | |
68 xml = data["xml"].toXml().encode("utf-8") | |
69 self.assert_equal_xml(xml, expected.toXml().encode("utf-8")) | |
70 | |
71 d_list = [] | |
72 | |
73 for state in plugin.CHAT_STATES: | |
74 mess_data = { | |
75 "to": Const.JID[0], | |
76 "type": "chat", | |
77 "message": "content", | |
78 "extra": {} if state == "active" else {"chat_state": state}, | |
79 } | |
80 stanza = """ | |
81 <message type="chat" from="%s" to="%s" id="test_1"> | |
82 %s | |
83 </message> | |
84 """ % ( | |
85 Const.JID_STR[1], | |
86 Const.JID_STR[0], | |
87 ("<body>%s</body>" % mess_data["message"]) if state == "active" else "", | |
88 ) | |
89 mess_data["xml"] = parseXml(stanza.encode("utf-8")) | |
90 expected = deepcopy(mess_data["xml"]) | |
91 expected.addElement(state, plugin.NS_CHAT_STATES) | |
92 post_treatments = defer.Deferred() | |
93 self.plugin.messageSendTrigger( | |
94 self.host.get_client(Const.PROFILE[0]), mess_data, None, post_treatments | |
95 ) | |
96 | |
97 post_treatments.addCallback(cb) | |
98 post_treatments.callback(mess_data) | |
99 d_list.append(post_treatments) | |
100 | |
101 def cb_list(__): # cancel the timer to not block the process | |
102 self.plugin.map[Const.PROFILE[0]][Const.JID[0]].timer.cancel() | |
103 | |
104 return defer.DeferredList(d_list).addCallback(cb_list) |