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