annotate tests/unit/test_plugin_xep_0033.py @ 4326:5fd6a4dc2122

cli (output/std): use `rich` to output JSON.
author Goffi <goffi@goffi.org>
date Wed, 20 Nov 2024 11:38:44 +0100
parents 6a0155f410bd
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4307
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python3
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
2
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
3 # Libervia: an XMPP client
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # Copyright (C) 2009-2024 Jérôme Poisson (goffi@goffi.org)
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
5
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
6 # This program is free software: you can redistribute it and/or modify
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # it under the terms of the GNU Affero General Public License as published by
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # the Free Software Foundation, either version 3 of the License, or
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # (at your option) any later version.
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
10
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
11 # This program is distributed in the hope that it will be useful,
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # GNU Affero General Public License for more details.
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
15
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
16 # You should have received a copy of the GNU Affero General Public License
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
18
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
19 from typing import cast
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
20 from unittest.mock import AsyncMock, MagicMock, patch
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
21
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from pytest_twisted import ensureDeferred as ed
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from twisted.words.protocols.jabber import jid
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from twisted.words.xish import domish
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
25
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from libervia.backend.models.core import MessageData
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from libervia.backend.plugins.plugin_xep_0033 import (
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
28 AddressType,
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
29 AddressesData,
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
30 NS_ADDRESS,
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
31 XEP_0033,
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
32 )
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
33
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
34
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 class TestXEP0033:
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 def test_address_type_from_element(self):
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 """
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 AddressType instance is correctly created from an XML <address> element.
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 """
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
40 address_elt = domish.Element((NS_ADDRESS, "address"))
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 address_elt["jid"] = "test@example.com"
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 address_elt["desc"] = "Test Description"
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
43 address_elt["delivered"] = "true"
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
44 address = AddressType.from_element(address_elt)
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 assert address.jid == jid.JID("test@example.com")
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 assert address.desc == "Test Description"
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 assert address.delivered is True
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
48
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 def test_address_type_to_element(self):
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
50 """
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
51 XML <address> element is correctly built from an AddressType instance.
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
52 """
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 address = AddressType(
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 jid=jid.JID("test@example.com"),
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
55 desc="Test Description",
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 delivered=True,
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 )
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
58 address_elt = address.to_element()
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
59 assert address_elt.uri == NS_ADDRESS
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 assert address_elt.name == "address"
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 assert address_elt["jid"] == "test@example.com"
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 assert address_elt["desc"] == "Test Description"
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
63 assert address_elt["delivered"] == "true"
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
64
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
65 def test_addresses_data_from_element(self):
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 """
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 AddressesData instance is correctly created from an XML <addresses> element.
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 """
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 addresses_elt = domish.Element((NS_ADDRESS, "addresses"))
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 address_elt1 = addresses_elt.addElement("address")
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 address_elt1["type"] = "to"
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 address_elt1["jid"] = "test1@example.com"
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 address_elt2 = addresses_elt.addElement("address")
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
74 address_elt2["type"] = "cc"
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
75 address_elt2["jid"] = "test2@example.com"
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
76 address_elt3 = addresses_elt.addElement("address")
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
77 address_elt3["type"] = "bcc"
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
78 address_elt3["jid"] = "test3@example.com"
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
79 address_elt4 = addresses_elt.addElement("address")
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
80 address_elt4["type"] = "noreply"
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
81 addresses = AddressesData.from_element(addresses_elt)
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
82 assert addresses.to is not None and len(addresses.to) == 1
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
83 assert addresses.to[0].jid == jid.JID("test1@example.com")
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
84 assert addresses.cc is not None and len(addresses.cc) == 1
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
85 assert addresses.cc[0].jid == jid.JID("test2@example.com")
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
86 assert addresses.bcc is not None and len(addresses.bcc) == 1
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
87 assert addresses.bcc[0].jid == jid.JID("test3@example.com")
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
88 assert addresses.noreply
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
89
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
90 def test_addresses_data_to_element(self):
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
91 """
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
92 XML <addresses> element is correctly built from an AddressesData instance.
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
93 """
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
94 addresses = AddressesData(
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
95 to=[AddressType(jid=jid.JID("test1@example.com"))],
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
96 cc=[AddressType(jid=jid.JID("test2@example.com"))],
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
97 bcc=[AddressType(jid=jid.JID("test3@example.com"))],
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
98 noreply=True,
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
99 )
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
100 addresses_elt = addresses.to_element()
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
101 assert addresses_elt.uri == NS_ADDRESS
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
102 assert addresses_elt.name == "addresses"
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
103 assert len(addresses_elt.children) == 4
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
104 for elt in addresses_elt.children:
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
105 assert elt.uri == NS_ADDRESS
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
106 assert elt.name == "address"
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
107 assert addresses_elt.children[0]["type"] == "to"
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
108 assert addresses_elt.children[0]["jid"] == "test1@example.com"
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
109 assert addresses_elt.children[1]["type"] == "cc"
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
110 assert addresses_elt.children[1]["jid"] == "test2@example.com"
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
111 assert addresses_elt.children[2]["type"] == "bcc"
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
112 assert addresses_elt.children[2]["jid"] == "test3@example.com"
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
113 assert addresses_elt.children[3]["type"] == "noreply"
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
114
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
115 @ed
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
116 async def test_handle_addresses(self):
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
117 """
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
118 Server JID is used, <addresses> element is added and messages are delivered.
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
119 """
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
120 xep_0033 = XEP_0033(MagicMock())
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
121 client = MagicMock()
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
122 client.server_jid = jid.JID("server.example.com")
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
123 client.profile = "test_profile"
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
124
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
125 mess_data = MessageData(
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
126 {
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
127 "to": jid.JID("recipient@example.com"),
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
128 "extra": {
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
129 "addresses": {
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
130 "to": [{"jid": "to@example.com"}],
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
131 "cc": [{"jid": "cc@example.com"}],
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
132 }
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
133 },
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
134 "xml": domish.Element(("jabber:client", "message")),
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
135 }
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
136 )
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
137
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
138 xep_0033.host.memory.disco.has_feature = AsyncMock(return_value=True)
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
139 xep_0033.deliver_messages = AsyncMock()
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
140
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
141 with patch.object(xep_0033, "_stop_if_all_delivered", AsyncMock()):
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
142 result = await xep_0033._handle_addresses(client, mess_data)
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
143 cast(AsyncMock, xep_0033._stop_if_all_delivered).assert_called_once()
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
144
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
145 assert result["to"] == client.server_jid
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
146 assert "addresses" in result["xml"].children[0].name
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
147 xep_0033.deliver_messages.assert_called_once()
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
148
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
149 @ed
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
150 async def test_deliver_messages(self):
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
151 """Delivery is done for all recipients."""
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
152 xep_0033 = XEP_0033(MagicMock())
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
153 client = MagicMock()
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
154 client.server_jid = jid.JID("server.example.com")
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
155 client.a_send = AsyncMock()
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
156
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
157 mess_data = MessageData(
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
158 {
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
159 "xml": domish.Element(("jabber:client", "message")),
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
160 }
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
161 )
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
162 to_example_addr = AddressType(jid=jid.JID("to@example.com"))
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
163 cc_example_addr = AddressType(jid=jid.JID("cc@example.com"))
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
164 bcc_example_addr = AddressType(jid=jid.JID("bcc@other.com"))
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
165
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
166 addr_data = AddressesData(
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
167 to=[to_example_addr],
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
168 cc=[cc_example_addr],
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
169 bcc=[bcc_example_addr],
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
170 )
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
171
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
172 domains = {
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
173 "example.com": [
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
174 to_example_addr,
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
175 cc_example_addr,
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
176 ],
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
177 "other.com": [bcc_example_addr],
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
178 }
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
179
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
180 xep_0033.host.memory.disco.has_feature = AsyncMock(return_value=True)
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
181
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
182 await xep_0033.deliver_messages(client, mess_data, addr_data, domains)
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
183
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
184 # Check that messages were sent to the multicast domain and individual recipients
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
185 assert client.a_send.call_count == 2
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
186 calls = client.a_send.call_args_list
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
187 # First call is to the multicast service.
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
188 assert calls[0][0][0]["to"] == "example.com"
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
189 # Second call is the individual BCC.
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
190 assert calls[1][0][0]["to"] == "bcc@other.com"
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
191
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
192 # Everything must have been delivered.
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
193 assert all(address.delivered for address in addr_data.addresses)
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
194
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
195 # And BCC must have been removed.
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
196 assert addr_data.bcc is None
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
197
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
198 @ed
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
199 async def test_deliver_messages_multicast_only(self):
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
200 """Delivery is done only to multicast services."""
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
201 xep_0033 = XEP_0033(MagicMock())
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
202 client = MagicMock()
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
203 client.server_jid = jid.JID("server.example.com")
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
204 client.a_send = AsyncMock()
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
205
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
206 mess_data = MessageData(
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
207 {
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
208 "xml": domish.Element(("jabber:client", "message")),
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
209 }
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
210 )
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
211 to_example_addr = AddressType(jid=jid.JID("to@example.com"))
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
212 cc_example_addr = AddressType(jid=jid.JID("cc@example.com"))
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
213 bcc_example_addr = AddressType(jid=jid.JID("bcc@other.com"))
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
214
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
215 addr_data = AddressesData(
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
216 to=[to_example_addr],
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
217 cc=[cc_example_addr],
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
218 bcc=[bcc_example_addr],
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
219 )
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
220
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
221 domains = {
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
222 "example.com": [
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
223 to_example_addr,
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
224 cc_example_addr,
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
225 ],
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
226 "other.com": [bcc_example_addr],
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
227 }
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
228
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
229 xep_0033.host.memory.disco.has_feature = AsyncMock(return_value=True)
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
230
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
231 await xep_0033.deliver_messages(
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
232 client, mess_data, addr_data, domains, multicast_only=True
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
233 )
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
234
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
235 # Check that only the multicast message was sent
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
236 assert client.a_send.call_count == 1
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
237 assert client.a_send.call_args[0][0]["to"] == "example.com"
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
238
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
239 # Check that only addresses from the multicast domain are marked as delivered
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
240 assert addr_data.to and addr_data.to[0].delivered is True
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
241 assert addr_data.cc and addr_data.cc[0].delivered is True
6a0155f410bd test (unit): add test for plugin XEP-0033:
Goffi <goffi@goffi.org>
parents:
diff changeset
242 assert addr_data.bcc and addr_data.bcc[0].delivered is None