comparison tests/unit/test_email_gateway.py @ 4339:699aa8788d98

tests (unit/email gateway): add tests for pubsub service: rel 453
author Goffi <goffi@goffi.org>
date Tue, 03 Dec 2024 00:52:06 +0100
parents 27bb22eace65
children
comparison
equal deleted inserted replaced
4338:7c0b7ecb816f 4339:699aa8788d98
22 from unittest.mock import AsyncMock, MagicMock 22 from unittest.mock import AsyncMock, MagicMock
23 23
24 import pytest 24 import pytest
25 from pytest_twisted import ensureDeferred as ed 25 from pytest_twisted import ensureDeferred as ed
26 from twisted.words.protocols.jabber import jid 26 from twisted.words.protocols.jabber import jid
27 from wokkel import disco
27 28
28 from libervia.backend.plugins.plugin_comp_email_gateway import ( 29 from libervia.backend.plugins.plugin_comp_email_gateway import (
29 EmailGatewayComponent, 30 EmailGatewayComponent,
30 SendMailExtra, 31 SendMailExtra,
31 ) 32 )
32 from libervia.backend.plugins.plugin_comp_email_gateway.models import ( 33 from libervia.backend.plugins.plugin_comp_email_gateway.models import (
33 Credentials, 34 Credentials,
34 UserData, 35 UserData,
35 ) 36 )
37 from libervia.backend.plugins.plugin_comp_email_gateway.pubsub_service import (
38 EmailGWPubsubService,
39 NODE_CONFIG,
40 NODE_CONFIG_VALUES,
41 NODE_OPTIONS,
42 )
36 from libervia.backend.plugins.plugin_xep_0131 import HeadersData, Urgency 43 from libervia.backend.plugins.plugin_xep_0131 import HeadersData, Urgency
37 44
45 @pytest.fixture
46 def email_gw(host):
47 email_gw = EmailGatewayComponent(host)
48 email_gw.storage = MagicMock()
49 email_gw.users_data = {}
50 return email_gw
38 51
39 class TestEmailGatewayComponent: 52 class TestEmailGatewayComponent:
40 @pytest.fixture
41 def email_gw(self, host):
42 email_gw = EmailGatewayComponent(host)
43 email_gw.storage = MagicMock()
44 email_gw.users_data = {}
45 return email_gw
46 53
47 def test_jid_to_email_gateway_jid(self, email_gw): 54 def test_jid_to_email_gateway_jid(self, email_gw):
48 """JID from the gateway is converted to an email address.""" 55 """JID from the gateway is converted to an email address."""
49 client = MagicMock() 56 client = MagicMock()
50 client.jid = jid.JID("gateway.example.org") 57 client.jid = jid.JID("gateway.example.org")
246 to_jid, 253 to_jid,
247 {"": "Hello, world!\n"}, 254 {"": "Hello, world!\n"},
248 None, 255 None,
249 extra={"headers": {"keywords": "test, example", "urgency": "high"}}, 256 extra={"headers": {"keywords": "test, example", "urgency": "high"}},
250 ) 257 )
258
259
260 class TestPubsubService:
261
262 @pytest.fixture
263 def pubsub_service(self, email_gw, client):
264 email_gw.client = client
265 service = EmailGWPubsubService(email_gw)
266 return service
267
268 @pytest.fixture
269 def pubsub_resource(self, pubsub_service):
270 return pubsub_service.resource
271
272 def test_getNodes(self, pubsub_resource, client):
273 """XEP-0498 well-known node is returned."""
274 requestor = client.jid
275 service = client.pubsub_service
276 nodeIdentifier = "test_node"
277 result = pubsub_resource.getNodes(requestor, service, nodeIdentifier)
278 assert result.result == [pubsub_resource._pfs.namespace]
279
280 @ed
281 async def test_items(self, pubsub_resource, client, host):
282 """Items are retrieved from the storage"""
283 request = MagicMock()
284 request.sender = client.jid
285 request.nodeIdentifier = pubsub_resource._pfs.namespace
286 files = [
287 {"id": "1", "name": "file1", "media_type": "application", "media_subtype":
288 "octet-stream", "size": 123, "hash_algo": "sha-256", "file_hash": "0123456789abcdef",
289 "created": 123},
290 {"id": "2", "name": "file2", "media_type": "application", "media_subtype":
291 "octet-stream", "size": 456, "hash_algo": "sha-256", "file_hash": "0123456789abcdef",
292 "created": 123},
293 ]
294 host.memory.get_files = AsyncMock(return_value=files)
295 result, _ = await pubsub_resource.items(request)
296 assert len(result) == 2
297
298 @ed
299 async def test_retract(self, pubsub_resource, client, host):
300 """Items are retracted from the storage"""
301 request = MagicMock()
302 request.sender = client.jid
303 request.nodeIdentifier = pubsub_resource._pfs.namespace
304 request.itemIdentifiers = ["item_1"]
305 host.memory.file_delete = AsyncMock()
306 await pubsub_resource.retract(request)
307 host.memory.file_delete.assert_called_once()
308
309 def test_getConfigurationOptions(self, pubsub_resource):
310 """Configuration options are returned"""
311 options = pubsub_resource.getConfigurationOptions()
312 assert options == NODE_OPTIONS
313
314 def test_getConfiguration(self, pubsub_resource, client):
315 """Configuration values are returned"""
316 requestor = client.jid
317 service = client.pubsub_service
318 nodeIdentifier = "test_node"
319 result = pubsub_resource.getConfiguration(requestor, service, nodeIdentifier)
320 assert result.result == NODE_CONFIG_VALUES
321
322 def test_getNodeInfo(self, pubsub_resource, client):
323 """Node information is returned"""
324 requestor = client.jid
325 service = client.pubsub_service
326 nodeIdentifier = pubsub_resource._pfs.namespace
327 info = pubsub_resource.getNodeInfo(requestor, service, nodeIdentifier)
328 assert info == {"type": "leaf", "meta-data": NODE_CONFIG}
329
330 @ed
331 async def test_getDiscoInfo(self, pubsub_service, client):
332 """Disco information is returned"""
333 requestor = client.jid
334 target = client.pubsub_service
335 nodeIdentifier = ""
336 result = await pubsub_service.getDiscoInfo(requestor, target, nodeIdentifier)
337 assert len(result) > 0
338 assert any(isinstance(info, disco.DiscoFeature) for info in result)