Mercurial > libervia-backend
annotate tests/unit/conftest.py @ 4171:da7d360a87be
core (constants): user platform temporary dir by default for PID files
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 04 Dec 2023 17:51:28 +0100 |
parents | 7c5654c54fed |
children | 0fbe5c605eb6 |
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 |
4072
040095a5dc7f
refactoring: rename `SAT` class to `LiberviaBackend`
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
42 class MockSAT(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 = {} |
495184d00360
tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
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 |
495184d00360
tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
70 def get_test_config(self, section, name, default=None): |
495184d00360
tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
71 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
|
72 |
495184d00360
tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
73 def set_test_config(self, section, name, value): |
495184d00360
tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
74 self._test_config[(section or None, name)] = value |
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 clear_test_config(self): |
495184d00360
tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
77 self._test_config.clear() |
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 @contextmanager |
495184d00360
tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
80 def use_option_and_reload(self, section, name, value): |
495184d00360
tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
81 self.set_test_config(section, name, value) |
495184d00360
tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
82 self.reload_plugins() |
495184d00360
tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
83 try: |
495184d00360
tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
84 yield self |
495184d00360
tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
85 finally: |
495184d00360
tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
86 self.clear_test_config() |
495184d00360
tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
87 self.reload_plugins() |
495184d00360
tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
88 |
495184d00360
tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
89 def reload_plugins(self): |
495184d00360
tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
90 self.plugins.clear() |
495184d00360
tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
91 self.trigger._TriggerManager__triggers.clear() |
495184d00360
tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
92 self.ns_map = { |
495184d00360
tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
93 "x-data": xmpp.NS_X_DATA, |
495184d00360
tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
94 "disco#info": xmpp.NS_DISCO_INFO, |
495184d00360
tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
95 } |
495184d00360
tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
96 self._import_plugins() |
495184d00360
tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
97 |
495184d00360
tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
98 def _init(self): |
495184d00360
tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
99 pass |
495184d00360
tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
100 |
3989
f5ba7594cced
core (main): log Exception when `bridge_pi` fails
Goffi <goffi@goffi.org>
parents:
3623
diff
changeset
|
101 async def _post_init(self): |
3623
495184d00360
tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
102 pass |
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 |
495184d00360
tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
105 @fixture(scope="session") |
495184d00360
tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
106 def host(bridge, storage): |
495184d00360
tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
107 host = MockSAT(bridge=bridge, storage=storage) |
495184d00360
tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
108 return host |
495184d00360
tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
109 |
495184d00360
tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
110 |
495184d00360
tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
111 @fixture |
495184d00360
tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
112 def client(): |
495184d00360
tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
113 client = MagicMock() |
495184d00360
tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
114 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
|
115 client.server_jid = jid.JID("test.example") |
3623
495184d00360
tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
116 client.pubsub_service = jid.JID("pubsub.test.example") |
495184d00360
tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
117 client.pubsub_client = AsyncMock() |
495184d00360
tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
118 return client |