comparison tests/unit/test_plugin_xep_0461.py @ 4371:ed683d56b64c default tip

test (XEP-0461): some tests for XEP-0461: rel 457
author Goffi <goffi@goffi.org>
date Tue, 06 May 2025 00:34:01 +0200
parents
children
comparison
equal deleted inserted replaced
4370:0eaa50f21efb 4371:ed683d56b64c
1 #!/usr/bin/env python3
2
3 # Libervia: an XMPP client
4 # Copyright (C) 2009-2025 Jérôme Poisson (goffi@goffi.org)
5
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU Affero General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU Affero General Public License for more details.
15
16 # You should have received a copy of the GNU Affero General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19
20 from unittest.mock import MagicMock, patch
21
22 import pytest
23 from twisted.words.protocols.jabber import jid
24 from twisted.words.xish import domish
25
26 from libervia.backend.core import exceptions
27 from libervia.backend.memory.sqla_mapping import History
28 from libervia.backend.models.core import MessageData
29 from libervia.backend.plugins.plugin_xep_0461 import NS_REPLY, ReplyTo, XEP_0461
30 from pytest_twisted import ensureDeferred as ed
31
32
33 class TestXEP0461:
34 @ed
35 async def test_reply_to_from_element(self):
36 """<reply> element parsing works."""
37 # Test correct case
38 element = domish.Element((NS_REPLY, "reply"), attribs={"id": "123", "to": "user@example.com"})
39 reply = ReplyTo.from_element(element)
40 assert reply.to == jid.JID("user@example.com")
41 assert reply.id == "123"
42 assert not reply.internal_uid
43
44 # Test child element
45 parent = domish.Element((None, "some"))
46 child = domish.Element((NS_REPLY, "reply"), attribs={"id": "456"})
47 parent.addChild(child)
48 reply = ReplyTo.from_element(parent)
49 assert reply.id == "456"
50 assert reply.to is None
51
52 # Test missing id
53 element = domish.Element((NS_REPLY, "reply"))
54 with pytest.raises(exceptions.DataError):
55 ReplyTo.from_element(element)
56
57 # Test no reply
58 element = domish.Element((None, "some"))
59 with pytest.raises(exceptions.NotFound):
60 ReplyTo.from_element(element)
61
62 @ed
63 async def test_reply_to_to_element(self, host, client):
64 "<reply> element generation works."
65 reply = ReplyTo(id="123", internal_uid=True)
66 with pytest.raises(exceptions.DataError):
67 reply.to_element()
68
69 reply = ReplyTo(id="123", internal_uid=False)
70 elt = reply.to_element()
71 assert elt.uri == NS_REPLY
72 assert elt.name == "reply"
73 assert elt["id"] == "123"
74 assert not elt.hasAttribute("to")
75
76 reply = ReplyTo(id="123", to=jid.JID("user@example.com"), internal_uid=False)
77 elt = reply.to_element()
78 assert elt["to"] == "user@example.com"
79
80 @ed
81 async def test_xep_0461_message_received_trigger(self, host, client):
82 """post_treat callback is added."""
83 xep = XEP_0461(host)
84 post_treat = MagicMock()
85 xep._message_received_trigger(client, None, post_treat)
86 assert post_treat.addCallback.called
87
88 @ed
89 async def test_xep_0461_send_message_trigger(self, host, client):
90 "post_xml_treatments callback is added."
91 xep = XEP_0461(host)
92 mess_data = MessageData(extra={"reply": {"id": "123", "internal_uid": False}})
93 pre_xml_treatments = MagicMock()
94 post_xml_treatments = MagicMock()
95 xep._sendMessage_trigger(client, mess_data, pre_xml_treatments, post_xml_treatments)
96 assert post_xml_treatments.addCallback.called