comparison tests/unit/test_ap-gateway.py @ 3783:fedbf7aade11

tests (unit/ap_gateway): fix tests
author Goffi <goffi@goffi.org>
date Tue, 24 May 2022 17:49:14 +0200
parents f31113777881
children 0b54be42d0aa
comparison
equal deleted inserted replaced
3782:580df5b557be 3783:fedbf7aade11
21 from urllib import parse 21 from urllib import parse
22 from functools import partial 22 from functools import partial
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.internet import defer
26 from twisted.words.protocols.jabber import jid 27 from twisted.words.protocols.jabber import jid
27 from twisted.web.server import Request 28 from twisted.web.server import Request
29 from twisted.words.xish import domish
28 from wokkel import rsm, pubsub 30 from wokkel import rsm, pubsub
29 31
30 from sat.core import exceptions 32 from sat.core import exceptions
33 from sat.core.constants import Const as C
31 from sat.plugins import plugin_comp_ap_gateway 34 from sat.plugins import plugin_comp_ap_gateway
35 from sat.plugins.plugin_comp_ap_gateway import constants as ap_const
32 from sat.plugins.plugin_comp_ap_gateway.http_server import HTTPServer 36 from sat.plugins.plugin_comp_ap_gateway.http_server import HTTPServer
37 from sat.plugins.plugin_xep_0277 import NS_ATOM
33 from sat.plugins.plugin_xep_0465 import NS_PPS 38 from sat.plugins.plugin_xep_0465 import NS_PPS
34 from sat.tools.utils import xmpp_date 39 from sat.tools.utils import xmpp_date
35 from sat.tools import xml_tools 40 from sat.tools import xml_tools
36 from sat.plugins.plugin_comp_ap_gateway import TYPE_ACTOR 41 from sat.plugins.plugin_comp_ap_gateway import TYPE_ACTOR
37 42
38 43
39 TEST_BASE_URL = "https://example.org" 44 TEST_BASE_URL = "https://example.org"
40 TEST_USER = "test_user" 45 TEST_USER = "test_user"
41 TEST_AP_ACCOUNT = f"{TEST_USER}@example.org" 46 TEST_AP_ACCOUNT = f"{TEST_USER}@example.org"
47 TEST_AP_ACTOR_ID = f"{TEST_BASE_URL}/users/{TEST_USER}"
42 PUBLIC_URL = "test.example" 48 PUBLIC_URL = "test.example"
49 TEST_JID = jid.JID(f"some_user@{PUBLIC_URL}")
43 50
44 AP_REQUESTS = { 51 AP_REQUESTS = {
45 f"{TEST_BASE_URL}/.well-known/webfinger?" 52 f"{TEST_BASE_URL}/.well-known/webfinger?"
46 f"resource=acct:{parse.quote(TEST_AP_ACCOUNT)}": { 53 f"resource=acct:{parse.quote(TEST_AP_ACCOUNT)}": {
47 "aliases": [ 54 "aliases": [
365 count=len(ret_items) 372 count=len(ret_items)
366 ) 373 )
367 return ret_items, {"rsm": rsm_resp.toDict(), "complete": True} 374 return ret_items, {"rsm": rsm_resp.toDict(), "complete": True}
368 375
369 376
377 def getVirtualClient(jid):
378 client = MagicMock()
379 client.jid = jid
380 return client
381
382
370 @pytest.fixture(scope="session") 383 @pytest.fixture(scope="session")
371 def ap_gateway(host): 384 def ap_gateway(host):
372 gateway = plugin_comp_ap_gateway.APGateway(host) 385 gateway = plugin_comp_ap_gateway.APGateway(host)
373 gateway.initialised = True 386 gateway.initialised = True
374 gateway.isPubsub = AsyncMock() 387 gateway.isPubsub = AsyncMock()
375 gateway.isPubsub.return_value = False 388 gateway.isPubsub.return_value = False
376 client = MagicMock() 389 client = MagicMock()
377 client.jid = jid.JID("ap.test.example") 390 client.jid = jid.JID("ap.test.example")
378 client.host = "test.example" 391 client.host = "test.example"
392 client.getVirtualClient = getVirtualClient
379 gateway.client = client 393 gateway.client = client
380 gateway.local_only = True 394 gateway.local_only = True
381 gateway.public_url = PUBLIC_URL 395 gateway.public_url = PUBLIC_URL
382 gateway.ap_path = '_ap' 396 gateway.ap_path = '_ap'
383 gateway.base_ap_url = parse.urljoin( 397 gateway.base_ap_url = parse.urljoin(
388 return gateway 402 return gateway
389 403
390 404
391 class TestActivityPubGateway: 405 class TestActivityPubGateway:
392 406
407 def getTitleXHTML(self, item_elt: domish.Element) -> domish.Element:
408 return next(
409 t for t in item_elt.entry.elements(NS_ATOM, "title")
410 if t.getAttribute("type") == "xhtml"
411 )
412
413
393 @ed 414 @ed
394 async def test_jid_and_node_convert_to_ap_handle(self, ap_gateway): 415 async def test_jid_and_node_convert_to_ap_handle(self, ap_gateway):
395 """JID and pubsub node are converted correctly to an AP actor handle""" 416 """JID and pubsub node are converted correctly to an AP actor handle"""
396 get_account = ap_gateway.getAPAccountFromJidAndNode 417 get_account = ap_gateway.getAPAccountFromJidAndNode
397 418
489 assert rsm_resp.count == 4 510 assert rsm_resp.count == 4
490 assert rsm_resp.index == 0 511 assert rsm_resp.index == 0
491 assert rsm_resp.first == "https://example.org/users/test_user/statuses/4" 512 assert rsm_resp.first == "https://example.org/users/test_user/statuses/4"
492 assert rsm_resp.last == "https://example.org/users/test_user/statuses/3" 513 assert rsm_resp.last == "https://example.org/users/test_user/statuses/3"
493 514
494 assert items[0].entry.title.toXml() == ( 515 title_xhtml = self.getTitleXHTML(items[0])
516 assert title_xhtml.toXml() == (
495 "<title xmlns='http://www.w3.org/2005/Atom' type='xhtml'>" 517 "<title xmlns='http://www.w3.org/2005/Atom' type='xhtml'>"
496 "<div xmlns='http://www.w3.org/1999/xhtml'><p>test message 4</p></div>" 518 "<div xmlns='http://www.w3.org/1999/xhtml'><p>test message 4</p></div>"
497 "</title>" 519 "</title>"
498 ) 520 )
499 author_uri = str( 521 author_uri = str(
500 [e for e in items[0].entry.author.elements() if e.name == "uri"][0] 522 [e for e in items[0].entry.author.elements() if e.name == "uri"][0]
501 ) 523 )
502 assert author_uri == "xmpp:test_user\\40example.org@ap.test.example" 524 assert author_uri == "xmpp:test_user\\40example.org@ap.test.example"
503 assert str(items[0].entry.published) == "2021-12-16T17:25:03Z" 525 assert str(items[0].entry.published) == "2021-12-16T17:25:03Z"
504 526
505 assert items[1].entry.title.toXml() == ( 527 title_xhtml = self.getTitleXHTML(items[1])
528 assert title_xhtml.toXml() == (
506 "<title xmlns='http://www.w3.org/2005/Atom' type='xhtml'>" 529 "<title xmlns='http://www.w3.org/2005/Atom' type='xhtml'>"
507 "<div xmlns='http://www.w3.org/1999/xhtml'><p>test message 3</p></div>" 530 "<div xmlns='http://www.w3.org/1999/xhtml'><p>test message 3</p></div>"
508 "</title>" 531 "</title>"
509 ) 532 )
510 author_uri = str( 533 author_uri = str(
522 assert rsm_resp.count == 4 545 assert rsm_resp.count == 4
523 assert rsm_resp.index == 2 546 assert rsm_resp.index == 2
524 assert rsm_resp.first == "https://example.org/users/test_user/statuses/2" 547 assert rsm_resp.first == "https://example.org/users/test_user/statuses/2"
525 assert rsm_resp.last == "https://example.org/users/test_user/statuses/1" 548 assert rsm_resp.last == "https://example.org/users/test_user/statuses/1"
526 549
527 assert items[0].entry.title.toXml() == ( 550 title_xhtml = self.getTitleXHTML(items[0])
551 assert title_xhtml.toXml() == (
528 "<title xmlns='http://www.w3.org/2005/Atom' type='xhtml'>" 552 "<title xmlns='http://www.w3.org/2005/Atom' type='xhtml'>"
529 "<div xmlns='http://www.w3.org/1999/xhtml'><p>test message 2</p></div>" 553 "<div xmlns='http://www.w3.org/1999/xhtml'><p>test message 2</p></div>"
530 "</title>" 554 "</title>"
531 ) 555 )
532 author_uri = str( 556 author_uri = str(
533 [e for e in items[0].entry.author.elements() if e.name == "uri"][0] 557 [e for e in items[0].entry.author.elements() if e.name == "uri"][0]
534 ) 558 )
535 assert author_uri == "xmpp:test_user\\40example.org@ap.test.example" 559 assert author_uri == "xmpp:test_user\\40example.org@ap.test.example"
536 assert str(items[0].entry.published) == "2021-12-16T17:27:03Z" 560 assert str(items[0].entry.published) == "2021-12-16T17:27:03Z"
537 561
538 assert items[1].entry.title.toXml() == ( 562 title_xhtml = self.getTitleXHTML(items[1])
563 assert title_xhtml.toXml() == (
539 "<title xmlns='http://www.w3.org/2005/Atom' type='xhtml'>" 564 "<title xmlns='http://www.w3.org/2005/Atom' type='xhtml'>"
540 "<div xmlns='http://www.w3.org/1999/xhtml'><p>test message 1</p></div>" 565 "<div xmlns='http://www.w3.org/1999/xhtml'><p>test message 1</p></div>"
541 "</title>" 566 "</title>"
542 ) 567 )
543 author_uri = str( 568 author_uri = str(
556 assert rsm_resp.index == 2 581 assert rsm_resp.index == 2
557 assert rsm_resp.first == "https://example.org/users/test_user/statuses/2" 582 assert rsm_resp.first == "https://example.org/users/test_user/statuses/2"
558 assert rsm_resp.last == "https://example.org/users/test_user/statuses/2" 583 assert rsm_resp.last == "https://example.org/users/test_user/statuses/2"
559 assert len(items) == 1 584 assert len(items) == 1
560 585
561 assert items[0].entry.title.toXml() == ( 586 title_xhtml = self.getTitleXHTML(items[0])
587 assert title_xhtml.toXml() == (
562 "<title xmlns='http://www.w3.org/2005/Atom' type='xhtml'>" 588 "<title xmlns='http://www.w3.org/2005/Atom' type='xhtml'>"
563 "<div xmlns='http://www.w3.org/1999/xhtml'><p>test message 2</p></div>" 589 "<div xmlns='http://www.w3.org/1999/xhtml'><p>test message 2</p></div>"
564 "</title>" 590 "</title>"
565 ) 591 )
566 assert str(items[0].entry.published) == "2021-12-16T17:27:03Z" 592 assert str(items[0].entry.published) == "2021-12-16T17:27:03Z"
573 assert rsm_resp.count == 4 599 assert rsm_resp.count == 4
574 assert rsm_resp.index == 1 600 assert rsm_resp.index == 1
575 assert rsm_resp.first == "https://example.org/users/test_user/statuses/3" 601 assert rsm_resp.first == "https://example.org/users/test_user/statuses/3"
576 assert rsm_resp.last == "https://example.org/users/test_user/statuses/1" 602 assert rsm_resp.last == "https://example.org/users/test_user/statuses/1"
577 assert len(items) == 3 603 assert len(items) == 3
578 assert items[0].entry.title.toXml() == ( 604 title_xhtml = self.getTitleXHTML(items[0])
605 assert title_xhtml.toXml() == (
579 "<title xmlns='http://www.w3.org/2005/Atom' type='xhtml'>" 606 "<title xmlns='http://www.w3.org/2005/Atom' type='xhtml'>"
580 "<div xmlns='http://www.w3.org/1999/xhtml'><p>test message 3</p></div>" 607 "<div xmlns='http://www.w3.org/1999/xhtml'><p>test message 3</p></div>"
581 "</title>" 608 "</title>"
582 ) 609 )
583 assert items[2].entry.title.toXml() == ( 610 title_xhtml = self.getTitleXHTML(items[2])
611 assert title_xhtml.toXml() == (
584 "<title xmlns='http://www.w3.org/2005/Atom' type='xhtml'>" 612 "<title xmlns='http://www.w3.org/2005/Atom' type='xhtml'>"
585 "<div xmlns='http://www.w3.org/1999/xhtml'><p>test message 1</p></div>" 613 "<div xmlns='http://www.w3.org/1999/xhtml'><p>test message 1</p></div>"
586 "</title>" 614 "</title>"
587 ) 615 )
588 616