annotate tests/unit/test_pubsub-cache.py @ 3934:e345d93fb6e5

plugin OXPS: OpenPGP for XMPP Pubsub implementation: OpenPGP for XMPP Pubsub (https://xmpp.org/extensions/inbox/pubsub-encryption.html, currently a protoXEP) is implemented and activated when `encrypted` is set to `True` in pubsub's `extra` data. On item retrieval, the decryption is transparent if the key is known, except if the `decrypt` key in `extra` is set to `False` (notably useful when one wants to checks that data is well encrypted). Methods and corresponding bridge methods have been implemented to manage shared secrets (to share, revoke or rotate the secrets). plugin XEP-0060's `XEP-0060_publish` trigger point as been move before actual publish so item can be modified (here e2ee) by the triggers. A new `XEP-0060_items` trigger point has also been added. `encrypted` flag can be used with plugin XEP-0277's microblog data rel 380
author Goffi <goffi@goffi.org>
date Sat, 15 Oct 2022 20:36:53 +0200
parents fe9cb52f4a9c
children 524856bd7b19
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3624
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python3
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
2
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
3 # Libervia: an XMPP client
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
5
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
6 # This program is free software: you can redistribute it and/or modify
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # it under the terms of the GNU Affero General Public License as published by
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # the Free Software Foundation, either version 3 of the License, or
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # (at your option) any later version.
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
10
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
11 # This program is distributed in the hope that it will be useful,
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # GNU Affero General Public License for more details.
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
15
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
16 # You should have received a copy of the GNU Affero General Public License
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
18
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
19 from twisted.internet import defer
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
20 from pytest_twisted import ensureDeferred as ed
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
21 from unittest.mock import MagicMock, patch
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from sat.memory.sqla import PubsubNode, SyncState
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from sat.core.constants import Const as C
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
24
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
25
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
26 class TestPubsubCache:
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
27
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
28 @ed
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
29 async def test_cache_is_used_transparently(self, host, client):
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
30 """Cache is used when a pubsub getItems operation is done"""
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
31 items_ret = defer.Deferred()
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
32 items_ret.callback(([], {}))
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
33 client.pubsub_client.items = MagicMock(return_value=items_ret)
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
34 host.memory.storage.getPubsubNode.return_value = None
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
35 pubsub_node = host.memory.storage.setPubsubNode.return_value = PubsubNode(
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
36 sync_state = None
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
37 )
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
38 with patch.object(host.plugins["PUBSUB_CACHE"], "cacheNode") as cacheNode:
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
39 await host.plugins["XEP-0060"].getItems(
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
40 client,
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
41 None,
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
42 "urn:xmpp:microblog:0",
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
43 )
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
44 assert cacheNode.call_count == 1
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
45 assert cacheNode.call_args.args[-1] == pubsub_node
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
46
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
47 @ed
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
48 async def test_cache_is_skipped_with_use_cache_false(self, host, client):
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
49 """Cache is skipped when 'use_cache' extra field is False"""
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
50 items_ret = defer.Deferred()
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
51 items_ret.callback(([], {}))
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
52 client.pubsub_client.items = MagicMock(return_value=items_ret)
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
53 host.memory.storage.getPubsubNode.return_value = None
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
54 host.memory.storage.setPubsubNode.return_value = PubsubNode(
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
55 sync_state = None
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
56 )
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
57 with patch.object(host.plugins["PUBSUB_CACHE"], "cacheNode") as cacheNode:
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
58 await host.plugins["XEP-0060"].getItems(
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
59 client,
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
60 None,
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
61 "urn:xmpp:microblog:0",
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
62 extra = {C.KEY_USE_CACHE: False}
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
63 )
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
64 assert not cacheNode.called
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
65
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
66 @ed
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
67 async def test_cache_is_not_used_when_no_cache(self, host, client):
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
68 """Cache is skipped when 'pubsub_cache_strategy' is set to 'no_cache'"""
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
69 with host.use_option_and_reload(None, "pubsub_cache_strategy", "no_cache"):
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
70 items_ret = defer.Deferred()
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
71 items_ret.callback(([], {}))
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
72 client.pubsub_client.items = MagicMock(return_value=items_ret)
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
73 host.memory.storage.getPubsubNode.return_value = None
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
74 host.memory.storage.setPubsubNode.return_value = PubsubNode(
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
75 sync_state = None
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
76 )
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
77 with patch.object(host.plugins["PUBSUB_CACHE"], "cacheNode") as cacheNode:
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
78 await host.plugins["XEP-0060"].getItems(
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
79 client,
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
80 None,
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
81 "urn:xmpp:microblog:0",
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
82 )
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
83 assert not cacheNode.called
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
84
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
85
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
86 @ed
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
87 async def test_no_pubsub_get_when_cache_completed(self, host, client):
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
88 """No pubsub get is emitted when items are fully cached"""
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
89 items_ret = defer.Deferred()
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
90 items_ret.callback(([], {}))
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
91 client.pubsub_client.items = MagicMock(return_value=items_ret)
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
92 host.memory.storage.getPubsubNode.return_value = PubsubNode(
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
93 sync_state = SyncState.COMPLETED
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
94 )
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
95 with patch.object(
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
96 host.plugins["PUBSUB_CACHE"],
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
97 "getItemsFromCache"
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
98 ) as getItemsFromCache:
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
99 getItemsFromCache.return_value = ([], {})
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
100 await host.plugins["XEP-0060"].getItems(
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
101 client,
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
102 None,
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
103 "urn:xmpp:microblog:0",
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
104 )
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
105 assert getItemsFromCache.call_count == 1
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
106 assert not client.pubsub_client.items.called
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
107
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
108 @ed
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
109 async def test_pubsub_get_when_cache_in_progress(self, host, client):
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
110 """Pubsub get is emitted when items are currently being cached"""
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
111 items_ret = defer.Deferred()
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
112 items_ret.callback(([], {}))
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
113 client.pubsub_client.items = MagicMock(return_value=items_ret)
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
114 host.memory.storage.getPubsubNode.return_value = PubsubNode(
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
115 sync_state = SyncState.IN_PROGRESS
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
116 )
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
117 with patch.object(host.plugins["PUBSUB_CACHE"], "analyseNode") as analyseNode:
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
118 analyseNode.return_value = {"to_sync": True}
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
119 with patch.object(
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
120 host.plugins["PUBSUB_CACHE"],
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
121 "getItemsFromCache"
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
122 ) as getItemsFromCache:
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
123 getItemsFromCache.return_value = ([], {})
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
124 assert client.pubsub_client.items.call_count == 0
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
125 await host.plugins["XEP-0060"].getItems(
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
126 client,
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
127 None,
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
128 "urn:xmpp:microblog:0",
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
129 )
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
130 assert not getItemsFromCache.called
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
131 assert client.pubsub_client.items.call_count == 1