Mercurial > libervia-backend
comparison tests/unit/test_ap-gateway.py @ 3981:acc9dfc8ba8d
component AP gateway: parse body immediately on `POST` request:
the body is parsed immediately during a `POST` request: this avoids duplication of code,
and allows to check the body data before continuing (will be used to filter some requests
in a future patch).
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 15 Nov 2022 18:07:34 +0100 |
parents | d43b197735d1 |
children | 69d970f974ff |
comparison
equal
deleted
inserted
replaced
3980:9b5092225e46 | 3981:acc9dfc8ba8d |
---|---|
627 def ap_request_params( | 627 def ap_request_params( |
628 self, | 628 self, |
629 ap_gateway, | 629 ap_gateway, |
630 type_: Optional[str] = None, | 630 type_: Optional[str] = None, |
631 url: Optional[str] = None, | 631 url: Optional[str] = None, |
632 doc: Optional[Any] = None, | 632 data: Optional[dict] = None, |
633 query_data: Optional[dict] = None, | 633 query_data: Optional[dict] = None, |
634 signing_actor: Optional[str] = None, | 634 signing_actor: Optional[str] = None, |
635 ) -> Dict[str, Any]: | 635 ) -> Dict[str, Any]: |
636 """Generate parameters for HTTPAPGServer's AP*Request | 636 """Generate parameters for HTTPAPGServer's AP*Request |
637 | 637 |
638 @param type_: one of the AP query type (e.g. "outbox") | 638 @param type_: one of the AP query type (e.g. "outbox") |
639 @param url: URL to query (mutually exclusif with type_) | 639 @param url: URL to query (mutually exclusif with type_) |
640 @param doc: if set, a document to embed in content of the request (will be | 640 @param data: object used as body of the request |
641 converted to JSON) | |
642 @param query_data: query data as returned by parse.parse_qs | 641 @param query_data: query data as returned by parse.parse_qs |
643 @return dict with kw params to use | 642 @return dict with kw params to use |
644 """ | 643 """ |
645 assert type_ and url is None or url and type_ is None | 644 assert type_ and url is None or url and type_ is None |
646 if type_ is not None: | 645 if type_ is not None: |
659 | 658 |
660 test_jid = jid.JID("some_user@test.example") | 659 test_jid = jid.JID("some_user@test.example") |
661 request = Request(MagicMock()) | 660 request = Request(MagicMock()) |
662 request.path = path.encode() | 661 request.path = path.encode() |
663 request.uri = uri.encode() | 662 request.uri = uri.encode() |
664 if doc is not None: | |
665 request.content = io.BytesIO(json.dumps(doc).encode()) | |
666 | 663 |
667 ap_url = parse.urljoin(f"https://{ap_gateway.public_url}", path) | 664 ap_url = parse.urljoin(f"https://{ap_gateway.public_url}", path) |
668 kwargs = { | 665 kwargs = { |
669 "request": request, | 666 "request": request, |
667 "data": data, | |
670 "account_jid": test_jid, | 668 "account_jid": test_jid, |
671 "node": None, | 669 "node": None, |
672 "ap_account": test_jid.full(), | 670 "ap_account": test_jid.full(), |
673 "ap_url": ap_url, | 671 "ap_url": ap_url, |
674 "signing_actor": signing_actor, | 672 "signing_actor": signing_actor, |
1508 with patch.object(ap_gateway.host.memory.storage, "getItems") as getItems: | 1506 with patch.object(ap_gateway.host.memory.storage, "getItems") as getItems: |
1509 getItems.return_value = ([], {}) | 1507 getItems.return_value = ([], {}) |
1510 with patch.object(ap_gateway._p, "sendItems") as sendItems: | 1508 with patch.object(ap_gateway._p, "sendItems") as sendItems: |
1511 await ap_gateway.server.resource.APInboxRequest( | 1509 await ap_gateway.server.resource.APInboxRequest( |
1512 **self.ap_request_params( | 1510 **self.ap_request_params( |
1513 ap_gateway, "inbox", doc=like, signing_actor=TEST_AP_ACTOR_ID | 1511 ap_gateway, "inbox", data=like, signing_actor=TEST_AP_ACTOR_ID |
1514 ) | 1512 ) |
1515 ) | 1513 ) |
1516 | 1514 |
1517 assert sendItems.called | 1515 assert sendItems.called |
1518 si_client, si_service, si_node, [si_item] = sendItems.call_args.args | 1516 si_client, si_service, si_node, [si_item] = sendItems.call_args.args |
1612 with patch.object(ap_gateway.host.memory.storage, "getItems") as getItems: | 1610 with patch.object(ap_gateway.host.memory.storage, "getItems") as getItems: |
1613 getItems.return_value = ([], {}) | 1611 getItems.return_value = ([], {}) |
1614 with patch.object(ap_gateway._p, "sendItems") as sendItems: | 1612 with patch.object(ap_gateway._p, "sendItems") as sendItems: |
1615 await ap_gateway.server.resource.APInboxRequest( | 1613 await ap_gateway.server.resource.APInboxRequest( |
1616 **self.ap_request_params( | 1614 **self.ap_request_params( |
1617 ap_gateway, "inbox", doc=like, signing_actor=TEST_AP_ACTOR_ID | 1615 ap_gateway, "inbox", data=like, signing_actor=TEST_AP_ACTOR_ID |
1618 ) | 1616 ) |
1619 ) | 1617 ) |
1620 | 1618 |
1621 assert sendItems.called | 1619 assert sendItems.called |
1622 si_client, si_service, si_node, [si_item] = sendItems.call_args.args | 1620 si_client, si_service, si_node, [si_item] = sendItems.call_args.args |