comparison tests/unit/test_ap-gateway.py @ 3908:d43b197735d1

tests (unit/AP gateway): add tests for events: rel 372
author Goffi <goffi@goffi.org>
date Thu, 22 Sep 2022 00:01:48 +0200
parents 755920bd30da
children acc9dfc8ba8d
comparison
equal deleted inserted replaced
3907:755920bd30da 3908:d43b197735d1
684 """Pubsub nodes are converted to AP collections""" 684 """Pubsub nodes are converted to AP collections"""
685 monkeypatch.setattr(ap_gateway._p, "getItems", mock_getItems) 685 monkeypatch.setattr(ap_gateway._p, "getItems", mock_getItems)
686 outbox = await ap_gateway.server.resource.APOutboxRequest( 686 outbox = await ap_gateway.server.resource.APOutboxRequest(
687 **self.ap_request_params(ap_gateway, "outbox") 687 **self.ap_request_params(ap_gateway, "outbox")
688 ) 688 )
689 assert outbox["@context"] == ["https://www.w3.org/ns/activitystreams"] 689 assert outbox["@context"] == ["https://www.w3.org/ns/activitystreams"]
690 assert outbox["id"] == "https://test.example/_ap/outbox/some_user%40test.example" 690 assert outbox["id"] == "https://test.example/_ap/outbox/some_user%40test.example"
691 assert outbox["totalItems"] == len(XMPP_ITEMS) 691 assert outbox["totalItems"] == len(XMPP_ITEMS)
692 assert outbox["type"] == "OrderedCollection" 692 assert outbox["type"] == "OrderedCollection"
693 assert outbox["first"] 693 assert outbox["first"]
694 assert outbox["last"] 694 assert outbox["last"]
791 ap_gateway._p, "getItems", partial(mock_getItems, ret_items=subscriptions) 791 ap_gateway._p, "getItems", partial(mock_getItems, ret_items=subscriptions)
792 ) 792 )
793 following = await ap_gateway.server.resource.APFollowingRequest( 793 following = await ap_gateway.server.resource.APFollowingRequest(
794 **self.ap_request_params(ap_gateway, "following") 794 **self.ap_request_params(ap_gateway, "following")
795 ) 795 )
796 assert following["@context"] == ["https://www.w3.org/ns/activitystreams"] 796 assert following["@context"] == ["https://www.w3.org/ns/activitystreams"]
797 assert ( 797 assert (
798 following["id"] 798 following["id"]
799 == "https://test.example/_ap/following/some_user%40test.example" 799 == "https://test.example/_ap/following/some_user%40test.example"
800 ) 800 )
801 assert following["totalItems"] == len(subscriptions) 801 assert following["totalItems"] == len(subscriptions)
829 ap_gateway._p, "getItems", partial(mock_getItems, ret_items=subscribers) 829 ap_gateway._p, "getItems", partial(mock_getItems, ret_items=subscribers)
830 ) 830 )
831 followers = await ap_gateway.server.resource.APFollowersRequest( 831 followers = await ap_gateway.server.resource.APFollowersRequest(
832 **self.ap_request_params(ap_gateway, "followers") 832 **self.ap_request_params(ap_gateway, "followers")
833 ) 833 )
834 assert followers["@context"] == ["https://www.w3.org/ns/activitystreams"] 834 assert followers["@context"] == ["https://www.w3.org/ns/activitystreams"]
835 assert ( 835 assert (
836 followers["id"] 836 followers["id"]
837 == "https://test.example/_ap/followers/some_user%40test.example" 837 == "https://test.example/_ap/followers/some_user%40test.example"
838 ) 838 )
839 assert followers["totalItems"] == len(subscribers) 839 assert followers["totalItems"] == len(subscribers)
863 await ap_gateway.onMessage(ap_gateway.client, mess_data) 863 await ap_gateway.onMessage(ap_gateway.client, mess_data)
864 url, actor_id, doc = signAndPost.call_args[0] 864 url, actor_id, doc = signAndPost.call_args[0]
865 assert url == "https://example.org/users/test_user/inbox" 865 assert url == "https://example.org/users/test_user/inbox"
866 assert actor_id == "https://test.example/_ap/actor/some_user%40test.example" 866 assert actor_id == "https://test.example/_ap/actor/some_user%40test.example"
867 obj = doc["object"] 867 obj = doc["object"]
868 assert doc["@context"] == ["https://www.w3.org/ns/activitystreams"] 868 assert doc["@context"] == ["https://www.w3.org/ns/activitystreams"]
869 assert doc["actor"] == "https://test.example/_ap/actor/some_user%40test.example" 869 assert doc["actor"] == "https://test.example/_ap/actor/some_user%40test.example"
870 assert obj["type"] == "Note" 870 assert obj["type"] == "Note"
871 assert obj["content"] == "This is a test message." 871 assert obj["content"] == "This is a test message."
872 assert obj["attributedTo"] == ( 872 assert obj["attributedTo"] == (
873 "https://test.example/_ap/actor/some_user%40test.example" 873 "https://test.example/_ap/actor/some_user%40test.example"
1627 ) 1627 )
1628 [parsed_item] = ap_gateway._pa.items2attachmentData(si_client, [si_item]) 1628 [parsed_item] = ap_gateway._pa.items2attachmentData(si_client, [si_item])
1629 assert parsed_item["from"] == si_client.jid.full() 1629 assert parsed_item["from"] == si_client.jid.full()
1630 assert "reactions" in parsed_item 1630 assert "reactions" in parsed_item
1631 assert parsed_item["reactions"]["reactions"] == ["🐅"] 1631 assert parsed_item["reactions"]["reactions"] == ["🐅"]
1632
1633 @ed
1634 async def test_xmpp_event_2_ap_event(self, ap_gateway):
1635 """XMPP events are converted to AP events"""
1636 # we use internal event data
1637 event_data = {
1638 "id": "event_123",
1639 "name": {"": "test event"},
1640 "start": 1664222400.0,
1641 "end": 1664226000.0,
1642 "head-picture": {
1643 "sources": [{"url": "https://example.org/head_picture.jpg"}]
1644 },
1645 "descriptions": [{"description": "meeting for test", "type": "text"}],
1646 "categories": [{"term": "test", "wikidata_id": "Q1003030"}],
1647 "locations": [{"description": "somewhere in the world"}],
1648 "rsvp": [
1649 {
1650 "fields": [
1651 {
1652 "type": "list-single",
1653 "name": "attending",
1654 "label": "Attending",
1655 "options": [
1656 {"value": "maybe", "label": "maybe"},
1657 {"value": "yes", "label": "yes"},
1658 {"value": "no", "label": "no"},
1659 ],
1660 }
1661 ],
1662 "namespace": "urn:xmpp:events:rsvp:0",
1663 }
1664 ],
1665 "extra": {
1666 "status": "confirmed",
1667 },
1668 }
1669
1670 ap_item = await ap_gateway.ap_events.event_data_2_ap_item(event_data, TEST_JID)
1671 ap_object = ap_item["object"]
1672 actor_id = (
1673 "https://test.example/_ap/actor/"
1674 "___urn.3Axmpp.3Aevents.3A0---some_user%40test.example"
1675 )
1676 event_id = (
1677 "https://test.example/_ap/item/"
1678 "___urn.3Axmpp.3Aevents.3A0---some_user%40test.example/event_123"
1679 )
1680 assert ap_object["name"] == "test event"
1681 assert ap_object["actor"] == actor_id
1682 assert ap_object["attributedTo"] == actor_id
1683 assert ap_object["type"] == "Event"
1684 assert ap_object["startTime"] == "2022-09-26T20:00:00Z"
1685 assert ap_object["endTime"] == "2022-09-26T21:00:00Z"
1686 assert ap_object["id"] == event_id
1687 assert ap_object["url"] == event_id
1688 assert ap_object["attachment"] == [
1689 {
1690 "name": "Banner",
1691 "type": "Document",
1692 "mediaType": "image/jpeg",
1693 "url": "https://example.org/head_picture.jpg",
1694 }
1695 ]
1696 assert ap_object["content"] == "<p>meeting for test</p>"
1697 assert ap_object["tag"] == [{"name": "#test", "type": "Hashtag"}]
1698 assert ap_object["location"] == {"name": "somewhere in the world"}
1699 assert ap_object["ical:status"] == "CONFIRMED"
1700 assert ap_item["type"] == "Create"
1701
1702 @ed
1703 async def test_ap_event_2_xmpp_event(self, ap_gateway):
1704 """AP events are converted to XMPP events"""
1705 test_actor = "___urn.3Axmpp.3Aevents.3A0---some_user%40test.example"
1706 ap_object = {
1707 "actor": f"https://test.example/_ap/actor/{test_actor}",
1708 "attachment": [
1709 {
1710 "mediaType": "image/jpeg",
1711 "name": "Banner",
1712 "type": "Document",
1713 "url": "https://example.org/head_picture.jpg",
1714 }
1715 ],
1716 "attributedTo": f"https://test.example/_ap/actor/{test_actor}",
1717 "content": "<p>meeting for test</p>",
1718 "endTime": "2022-09-26T21:00:00Z",
1719 "ical:status": "CONFIRMED",
1720 "id": f"https://test.example/_ap/item/{test_actor}/event_123",
1721 "location": {"name": "somewhere in the world"},
1722 "name": "test event",
1723 "startTime": "2022-09-26T20:00:00Z",
1724 "tag": [{"name": "#test", "type": "Hashtag"}],
1725 "to": ["https://www.w3.org/ns/activitystreams#Public"],
1726 "type": "Event",
1727 "url": f"https://test.example/_ap/item/{test_actor}/event_123",
1728 }
1729
1730 event_data = await ap_gateway.ap_events.ap_item_2_event_data(ap_object)
1731
1732 assert event_data["id"] == ap_object["id"]
1733 assert event_data["name"] == {"": "test event"}
1734 assert event_data["start"] == 1664222400
1735 assert event_data["end"] == 1664226000
1736 assert event_data["head-picture"] == {
1737 "sources": [{"url": "https://example.org/head_picture.jpg"}]
1738 }
1739 assert event_data["descriptions"] == [{
1740 "description": "<p>meeting for test</p>", "type": "xhtml"
1741 }]
1742 assert event_data["categories"] == [{"term": "test"}]
1743 assert event_data["locations"] == [{"description": "somewhere in the world"}]
1744 assert event_data["rsvp"] == [
1745 {
1746 "fields": [
1747 {
1748 "label": "Attending",
1749 "name": "attending",
1750 "options": [
1751 {"label": "yes", "value": "yes"},
1752 {"label": "no", "value": "no"},
1753 ],
1754 "required": True,
1755 "type": "list-single",
1756 }
1757 ]
1758 }
1759 ]
1760 assert event_data["extra"] == {"status": "confirmed"}