annotate tests/unit/conftest.py @ 4351:6a0a081485b8

plugin autocrypt: Autocrypt protocol implementation: Implementation of autocrypt: `autocrypt` header is checked, and if present and no public key is known for the peer, the key is imported. `autocrypt` header is also added to outgoing message (only if an email gateway is detected). For the moment, the JID is use as identifier, but the real email used by gateway should be used in the future. rel 456
author Goffi <goffi@goffi.org>
date Fri, 28 Feb 2025 09:23:35 +0100
parents 699aa8788d98
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3623
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python3
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
2
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
3 # Libervia: an XMPP client
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
5
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
6 # This program is free software: you can redistribute it and/or modify
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # it under the terms of the GNU Affero General Public License as published by
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # the Free Software Foundation, either version 3 of the License, or
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # (at your option) any later version.
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
10
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
11 # This program is distributed in the hope that it will be useful,
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # GNU Affero General Public License for more details.
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
15
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
16 # You should have received a copy of the GNU Affero General Public License
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
18
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
19 from contextlib import contextmanager
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
20 from unittest.mock import MagicMock, AsyncMock
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
21 from pytest import fixture
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from twisted.internet import defer
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from twisted.words.protocols.jabber import jid
4073
7c5654c54fed refactoring: rename `core.sat_main` to `core.main`
Goffi <goffi@goffi.org>
parents: 4072
diff changeset
24 from libervia.backend.core.main import LiberviaBackend
4071
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
25 from libervia.backend.tools import async_trigger as trigger
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
26 from libervia.backend.core import xmpp
3623
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
27
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
28
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
29 @fixture(scope="session")
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
30 def bridge():
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
31 bridge = AsyncMock()
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4034
diff changeset
32 bridge.add_signal = MagicMock()
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4034
diff changeset
33 bridge.add_method = MagicMock()
3623
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
34 return bridge
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
35
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
36
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
37 @fixture(scope="session")
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
38 def storage():
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
39 return AsyncMock()
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
40
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
41
4232
0fbe5c605eb6 tests (unit/webrtc,XEP-0176, XEP-0234): Fix tests and add webrtc file transfer tests:
Goffi <goffi@goffi.org>
parents: 4073
diff changeset
42 class MockLiberviaBackend(LiberviaBackend):
3623
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
43
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
44 def __init__(self, bridge, storage):
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
45 self._cb_map = {}
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
46 self._menus = {}
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
47 self._menus_paths = {}
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
48 self._test_config = {}
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
49 self.profiles = {}
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
50 self.plugins = {}
4285
f1d0cde61af7 tests (unit): fix tests + black reformatting.
Goffi <goffi@goffi.org>
parents: 4232
diff changeset
51 # map for short name to whole namespace,
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4034
diff changeset
52 # extended by plugins with register_namespace
3623
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
53 self.ns_map = {
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
54 "x-data": xmpp.NS_X_DATA,
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
55 "disco#info": xmpp.NS_DISCO_INFO,
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
56 }
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
57 self.memory = MagicMock()
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
58 self.memory.storage = storage
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4034
diff changeset
59 self.memory.config_get.side_effect = self.get_test_config
3623
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
60
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
61 self.trigger = trigger.TriggerManager()
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
62 self.bridge = bridge
3989
f5ba7594cced core (main): log Exception when `bridge_pi` fails
Goffi <goffi@goffi.org>
parents: 3623
diff changeset
63 defer.ensureDeferred(self._post_init())
3623
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
64 self.common_cache = AsyncMock()
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
65 self._import_plugins()
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4034
diff changeset
66 self._add_base_menus()
3623
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
67 self.initialised = defer.Deferred()
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
68 self.initialised.callback(None)
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
69
4339
699aa8788d98 tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents: 4285
diff changeset
70 def get_local_path(self, *args, **kwargs):
699aa8788d98 tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents: 4285
diff changeset
71 return MagicMock()
699aa8788d98 tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents: 4285
diff changeset
72
3623
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
73 def get_test_config(self, section, name, default=None):
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
74 return self._test_config.get((section or None, name), default)
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
75
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
76 def set_test_config(self, section, name, value):
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
77 self._test_config[(section or None, name)] = value
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
78
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
79 def clear_test_config(self):
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
80 self._test_config.clear()
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
81
4232
0fbe5c605eb6 tests (unit/webrtc,XEP-0176, XEP-0234): Fix tests and add webrtc file transfer tests:
Goffi <goffi@goffi.org>
parents: 4073
diff changeset
82 def register_namespace(self, short_name, namespace):
0fbe5c605eb6 tests (unit/webrtc,XEP-0176, XEP-0234): Fix tests and add webrtc file transfer tests:
Goffi <goffi@goffi.org>
parents: 4073
diff changeset
83 # Plugin classes may be instantiated several times in tests, so we do nothing in
0fbe5c605eb6 tests (unit/webrtc,XEP-0176, XEP-0234): Fix tests and add webrtc file transfer tests:
Goffi <goffi@goffi.org>
parents: 4073
diff changeset
84 # this method to avoid ConflictError.
0fbe5c605eb6 tests (unit/webrtc,XEP-0176, XEP-0234): Fix tests and add webrtc file transfer tests:
Goffi <goffi@goffi.org>
parents: 4073
diff changeset
85 pass
0fbe5c605eb6 tests (unit/webrtc,XEP-0176, XEP-0234): Fix tests and add webrtc file transfer tests:
Goffi <goffi@goffi.org>
parents: 4073
diff changeset
86
3623
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
87 @contextmanager
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
88 def use_option_and_reload(self, section, name, value):
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
89 self.set_test_config(section, name, value)
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
90 self.reload_plugins()
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
91 try:
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
92 yield self
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
93 finally:
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
94 self.clear_test_config()
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
95 self.reload_plugins()
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
96
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
97 def reload_plugins(self):
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
98 self.plugins.clear()
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
99 self.trigger._TriggerManager__triggers.clear()
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
100 self.ns_map = {
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
101 "x-data": xmpp.NS_X_DATA,
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
102 "disco#info": xmpp.NS_DISCO_INFO,
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
103 }
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
104 self._import_plugins()
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
105
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
106 def _init(self):
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
107 pass
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
108
3989
f5ba7594cced core (main): log Exception when `bridge_pi` fails
Goffi <goffi@goffi.org>
parents: 3623
diff changeset
109 async def _post_init(self):
3623
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
110 pass
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
111
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
112
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
113 @fixture(scope="session")
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
114 def host(bridge, storage):
4232
0fbe5c605eb6 tests (unit/webrtc,XEP-0176, XEP-0234): Fix tests and add webrtc file transfer tests:
Goffi <goffi@goffi.org>
parents: 4073
diff changeset
115 host = MockLiberviaBackend(bridge=bridge, storage=storage)
3623
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
116 return host
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
117
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
118
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
119 @fixture
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
120 def client():
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
121 client = MagicMock()
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
122 client.jid = jid.JID("test_user@test.example/123")
4034
9496f28dadff tests (unit): Add test for plugin XEP-0215:
Goffi <goffi@goffi.org>
parents: 3989
diff changeset
123 client.server_jid = jid.JID("test.example")
3623
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
124 client.pubsub_service = jid.JID("pubsub.test.example")
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
125 client.pubsub_client = AsyncMock()
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
126 return client