From 5bbc096b3260da31a7083aa59c65313dee45d852 Mon Sep 17 00:00:00 2001 From: Sebastian Molenda Date: Tue, 11 Feb 2025 21:33:39 +0100 Subject: [PATCH 1/2] Fix grant token type hints --- pubnub/endpoints/access/grant_token.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/pubnub/endpoints/access/grant_token.py b/pubnub/endpoints/access/grant_token.py index 8f2da50d..c21dc1f6 100644 --- a/pubnub/endpoints/access/grant_token.py +++ b/pubnub/endpoints/access/grant_token.py @@ -6,6 +6,11 @@ from pubnub.enums import HttpMethod, PNOperationType from pubnub.models.consumer.common import PNStatus from pubnub.models.consumer.v3.access_manager import PNGrantTokenResult +from pubnub.models.consumer.v3.channel import Channel +from pubnub.models.consumer.v3.group import Group +from pubnub.models.consumer.v3.space import Space +from pubnub.models.consumer.v3.user import User +from pubnub.models.consumer.v3.uuid import UUID from pubnub.structures import Envelope @@ -55,23 +60,23 @@ def authorized_user(self, user) -> 'GrantToken': self._authorized_uuid = user return self - def spaces(self, spaces: Union[str, List[str]]) -> 'GrantToken': + def spaces(self, spaces: List[Space]) -> 'GrantToken': self._channels = spaces return self - def users(self, users: Union[str, List[str]]) -> 'GrantToken': + def users(self, users: List[User]) -> 'GrantToken': self._uuids = users return self - def channels(self, channels: Union[str, List[str]]) -> 'GrantToken': + def channels(self, channels: List[Channel]) -> 'GrantToken': self._channels = channels return self - def groups(self, groups: Union[str, List[str]]) -> 'GrantToken': + def groups(self, groups: List[Group]) -> 'GrantToken': self._groups = groups return self - def uuids(self, uuids: Union[str, List[str]]) -> 'GrantToken': + def uuids(self, uuids: List[UUID]) -> 'GrantToken': self._uuids = uuids return self From 67033038d148caf224fd37c243d9c995209b9d60 Mon Sep 17 00:00:00 2001 From: Sebastian Molenda Date: Tue, 11 Feb 2025 21:55:10 +0100 Subject: [PATCH 2/2] expanded test as example --- .../pam/grant_token_user_space.json | 72 +++++++++++++++++++ .../pam/grant_token_user_space.yaml | 46 ------------ .../grant_token_with_uuid_and_channels.json | 72 +++++++++++++++++++ .../grant_token_with_uuid_and_channels.yaml | 46 ------------ .../native_sync/test_grant_token.py | 30 +++++--- 5 files changed, 164 insertions(+), 102 deletions(-) create mode 100644 tests/integrational/fixtures/native_sync/pam/grant_token_user_space.json delete mode 100644 tests/integrational/fixtures/native_sync/pam/grant_token_user_space.yaml create mode 100644 tests/integrational/fixtures/native_sync/pam/grant_token_with_uuid_and_channels.json delete mode 100644 tests/integrational/fixtures/native_sync/pam/grant_token_with_uuid_and_channels.yaml diff --git a/tests/integrational/fixtures/native_sync/pam/grant_token_user_space.json b/tests/integrational/fixtures/native_sync/pam/grant_token_user_space.json new file mode 100644 index 00000000..29369f42 --- /dev/null +++ b/tests/integrational/fixtures/native_sync/pam/grant_token_user_space.json @@ -0,0 +1,72 @@ +{ + "version": 1, + "interactions": [ + { + "request": { + "method": "POST", + "uri": "https://ps.pndsn.com/v3/pam/{PN_KEY_PAM_SUBSCRIBE}/grant", + "body": { + "pickle": "gASVKgEAAAAAAABYIwEAAHsidHRsIjogNjAsICJwZXJtaXNzaW9ucyI6IHsicmVzb3VyY2VzIjogeyJjaGFubmVscyI6IHsic29tZV9zcGFjZV9pZCI6IDN9LCAiZ3JvdXBzIjoge30sICJ1dWlkcyI6IHt9LCAidXNlcnMiOiB7fSwgInNwYWNlcyI6IHsic29tZV9zcGFjZV9pZCI6IDN9fSwgInBhdHRlcm5zIjogeyJjaGFubmVscyI6IHsic29tZV8qIjogM30sICJncm91cHMiOiB7fSwgInV1aWRzIjoge30sICJ1c2VycyI6IHt9LCAic3BhY2VzIjogeyJzb21lXyoiOiAzfX0sICJtZXRhIjoge30sICJ1dWlkIjogInNvbWVfdXNlcl9pZCJ9fZQu" + }, + "headers": { + "host": [ + "ps.pndsn.com" + ], + "accept": [ + "*/*" + ], + "accept-encoding": [ + "gzip, deflate" + ], + "connection": [ + "keep-alive" + ], + "user-agent": [ + "PubNub-Python/10.1.0" + ], + "content-type": [ + "application/json" + ], + "content-length": [ + "291" + ] + } + }, + "response": { + "status": { + "code": 200, + "message": "OK" + }, + "headers": { + "Date": [ + "Tue, 11 Feb 2025 20:46:28 GMT" + ], + "Content-Type": [ + "text/javascript; charset=UTF-8" + ], + "Transfer-Encoding": [ + "chunked" + ], + "Connection": [ + "keep-alive" + ], + "cache-control": [ + "no-cache, no-store, must-revalidate" + ], + "content-encoding": [ + "gzip" + ], + "Access-Control-Allow-Credentials": [ + "true" + ], + "Access-Control-Expose-Headers": [ + "*" + ] + }, + "body": { + "pickle": "gASVKQEAAAAAAAB9lIwGc3RyaW5nlEIWAQAAH4sIAAAAAAAA/1WQTY+CMBiE/8qmZzcBzBrlRuTD1VCzJfJ12ZRScWmhSosKxv++dTcePL2ZeWcyyXMDJVYY2DfQUClxRYENop4QLcAEKMFoq52T51sO842goZ0akFuukAzcq0uaeDzuUI0DvxeJORaWydMpvGQJ5KnFmWPAlrTL6uvhzXxFrIXK48VIAr/W95g70C1TOAgPmWXCmfB0Lo04CqDIktkhb+G5SOL9poJuPl1f9F93ty/+ax+pPEWHpy5W/zmd4YTrPd3HSZl9rjfzGDrSDDtzNhfZ9jpEYv8dLwvxXgbFLmTVh0GYS+tTA+4TIGl3/iEPNs4fmrcQt5pVpxFJhVUvgW0Zxv0XP0RQKE0BAACUcy4=" + } + } + } + ] +} diff --git a/tests/integrational/fixtures/native_sync/pam/grant_token_user_space.yaml b/tests/integrational/fixtures/native_sync/pam/grant_token_user_space.yaml deleted file mode 100644 index 179e1e1a..00000000 --- a/tests/integrational/fixtures/native_sync/pam/grant_token_user_space.yaml +++ /dev/null @@ -1,46 +0,0 @@ -interactions: -- request: - body: '{"ttl": 60, "permissions": {"resources": {"channels": {"some_space_id": - 3}, "groups": {}, "uuids": {}, "users": {}, "spaces": {"some_space_id": 3}}, - "patterns": {"channels": {"some_*": 3}, "groups": {}, "uuids": {}, "users": - {}, "spaces": {"some_*": 3}}, "meta": {}, "uuid": "some_user_id"}}' - headers: - Accept: - - '*/*' - Accept-Encoding: - - gzip, deflate - Connection: - - keep-alive - Content-Length: - - '291' - Content-type: - - application/json - User-Agent: - - PubNub-Python/6.4.1 - method: POST - uri: https://ps.pndsn.com/v3/pam/sub-c-7ba2ac4c-4836-11e6-85a4-0619f8945a4f/grant - response: - body: - string: !!binary | - H4sIAAAAAAAA/1WQzW6DMBCEX6XyOUj8VChwQwGcpq1pjYqBS2UMgQQDaWySQpR3r9Oqh5xWMzuz - K30XUFJJgXsBXSUErSvggnhkTAmwAHJoq145X0Foem2ow+5lKzvkl2ssoP/tsy6ZDx94T2E4DsSY - C9PgqYXOGUE8NXnr6ahn/ap+v3l2KJnpyDxxZgbDvZqH3FO3UjQNATZKwtshULk05hiiISN2k/fo - VJBk+1wjP7c2Z7VX3ejOv+9jmae4+dfF+i+nMpxx9U/1KSmzJ2+KlqRZ6sxsZ2gHO+qgjRbE2rQi - kkf6G88ePy2NYhgV4LoAojqeduzGxvtF8/BKe8XqqBAJSeUogGvq+vUH2oM+x00BAAA= - headers: - Connection: - - keep-alive - Content-Type: - - text/javascript; charset=UTF-8 - Date: - - Tue, 26 Jul 2022 09:39:47 GMT - Transfer-Encoding: - - chunked - cache-control: - - no-cache, no-store, must-revalidate - content-encoding: - - gzip - status: - code: 200 - message: OK -version: 1 diff --git a/tests/integrational/fixtures/native_sync/pam/grant_token_with_uuid_and_channels.json b/tests/integrational/fixtures/native_sync/pam/grant_token_with_uuid_and_channels.json new file mode 100644 index 00000000..c0e18baa --- /dev/null +++ b/tests/integrational/fixtures/native_sync/pam/grant_token_with_uuid_and_channels.json @@ -0,0 +1,72 @@ +{ + "version": 1, + "interactions": [ + { + "request": { + "method": "POST", + "uri": "https://ps.pndsn.com/v3/pam/{PN_KEY_PAM_SUBSCRIBE}/grant", + "body": { + "pickle": "gASVhAEAAAAAAABYfQEAAHsidHRsIjogNjAsICJwZXJtaXNzaW9ucyI6IHsicmVzb3VyY2VzIjogeyJjaGFubmVscyI6IHsic29tZV9jaGFubmVsX2lkIjogMjM5fSwgImdyb3VwcyI6IHsic29tZV9ncm91cF9pZCI6IDV9LCAidXVpZHMiOiB7InNvbWVfdXVpZCI6IDEwNH0sICJ1c2VycyI6IHsic29tZV91dWlkIjogMTA0fSwgInNwYWNlcyI6IHsic29tZV9jaGFubmVsX2lkIjogMjM5fX0sICJwYXR0ZXJucyI6IHsiY2hhbm5lbHMiOiB7InNvbWVfKiI6IDd9LCAiZ3JvdXBzIjogeyJzb21lXyoiOiAxfSwgInV1aWRzIjogeyJzb21lXyoiOiAzMn0sICJ1c2VycyI6IHsic29tZV8qIjogMzJ9LCAic3BhY2VzIjogeyJzb21lXyoiOiA3fX0sICJtZXRhIjoge30sICJ1dWlkIjogInNvbWVfdXVpZCJ9fZQu" + }, + "headers": { + "host": [ + "ps.pndsn.com" + ], + "accept": [ + "*/*" + ], + "accept-encoding": [ + "gzip, deflate" + ], + "connection": [ + "keep-alive" + ], + "user-agent": [ + "PubNub-Python/10.1.0" + ], + "content-type": [ + "application/json" + ], + "content-length": [ + "381" + ] + } + }, + "response": { + "status": { + "code": 200, + "message": "OK" + }, + "headers": { + "Date": [ + "Tue, 11 Feb 2025 20:46:03 GMT" + ], + "Content-Type": [ + "text/javascript; charset=UTF-8" + ], + "Transfer-Encoding": [ + "chunked" + ], + "Connection": [ + "keep-alive" + ], + "cache-control": [ + "no-cache, no-store, must-revalidate" + ], + "content-encoding": [ + "gzip" + ], + "Access-Control-Allow-Credentials": [ + "true" + ], + "Access-Control-Expose-Headers": [ + "*" + ] + }, + "body": { + "pickle": "gASVVwEAAAAAAAB9lIwGc3RyaW5nlEJEAQAAH4sIAAAAAAAA/2WRyW6DMBRFf6XyOgsGNS2RugglOE1UKkBgYBOZyaQMTmIDGZR/rwlIVdWl77PPebq+gRRzDBY3UGeMYZKBBXDbJBEHMAOcllkjkuPKVJalKcE6O/GeG+naYdA4G0ntXw+e842h2VKkXWNFrgLFoiF6biN0zjGywxfJapLmvYgDq4uRn0fqpksDfZiZtmr14dzsEkXjka89OHHts0CpSvilGWlgXSiqRq7qywMPrxw5RVX5PxeswK0cOPjnRdSMvi1JDeHsKYrG+xeqT9569L4edcmSE3X7+6YgxFkPXPdvBuUqhXCaOYdp78c+gnmIZEL63NppHtEVT2IfuudT7O6L1rR3utGsN6zOSmnZbu2wyNkbuM8Ay07dPhl6Xz5qf/rEjfiHk6ifccxbBhaKJN1/AJLBBPipAQAAlHMu" + } + } + } + ] +} diff --git a/tests/integrational/fixtures/native_sync/pam/grant_token_with_uuid_and_channels.yaml b/tests/integrational/fixtures/native_sync/pam/grant_token_with_uuid_and_channels.yaml deleted file mode 100644 index 838070fa..00000000 --- a/tests/integrational/fixtures/native_sync/pam/grant_token_with_uuid_and_channels.yaml +++ /dev/null @@ -1,46 +0,0 @@ -interactions: -- request: - body: '{"ttl": 60, "permissions": {"resources": {"channels": {"some_channel_id": - 3}, "groups": {}, "uuids": {}, "users": {}, "spaces": {"some_channel_id": 3}}, - "patterns": {"channels": {"some_*": 3}, "groups": {}, "uuids": {}, "users": - {}, "spaces": {"some_*": 3}}, "meta": {}, "uuid": "some_uuid"}}' - headers: - Accept: - - '*/*' - Accept-Encoding: - - gzip, deflate - Connection: - - keep-alive - Content-Length: - - '292' - Content-type: - - application/json - User-Agent: - - PubNub-Python/6.4.1 - method: POST - uri: https://ps.pndsn.com/v3/pam/sub-c-7ba2ac4c-4836-11e6-85a4-0619f8945a4f/grant - response: - body: - string: !!binary | - H4sIAAAAAAAA/3WQT2+CMADFv8rSs4dS1GwkHmSFurGRwDKqXJZa/gQEKrSgYvzuw7lk08Tje/m9 - d/gdQcQUA8YRlLGULI2BAT5azocARkCJTVwNTW3ZaL6xISnfElW6OFr4kuA95mXQbz/9nBG7FfSp - XyOtWCJXrOikDek+YdTDHooO3DEx1838LqMHPa9NK1oG2/DMEhv+/YaXzUHcfP3rr/fWmgZwxX4z - KS6cHmiM+pijoqLPuOF5NyV44nTOzsvqafmyyPjj+FX7onnq5K7ujlOhhbBLdrMZOI2AjJsu42c/ - 8x89D++sGnw1gyapmGolMBCEp28eF9ZaUQEAAA== - headers: - Connection: - - keep-alive - Content-Type: - - text/javascript; charset=UTF-8 - Date: - - Tue, 26 Jul 2022 09:39:47 GMT - Transfer-Encoding: - - chunked - cache-control: - - no-cache, no-store, must-revalidate - content-encoding: - - gzip - status: - code: 200 - message: OK -version: 1 diff --git a/tests/integrational/native_sync/test_grant_token.py b/tests/integrational/native_sync/test_grant_token.py index 2b8e1e49..bd39e0c4 100644 --- a/tests/integrational/native_sync/test_grant_token.py +++ b/tests/integrational/native_sync/test_grant_token.py @@ -2,33 +2,43 @@ from pubnub.pubnub import PubNub from pubnub.models.consumer.v3.access_manager import PNGrantTokenResult from pubnub.models.consumer.v3.channel import Channel +from pubnub.models.consumer.v3.group import Group +from pubnub.models.consumer.v3.uuid import UUID from pubnub.models.consumer.v3.space import Space -from tests.helper import pnconf_pam_copy +from tests.helper import pnconf_pam_env_copy from tests.integrational.vcr_helper import pn_vcr -pubnub = PubNub(pnconf_pam_copy()) +pubnub = PubNub(pnconf_pam_env_copy()) pubnub.config.uuid = "test_grant" @pn_vcr.use_cassette( - 'tests/integrational/fixtures/native_sync/pam/grant_token_with_uuid_and_channels.yaml', + 'tests/integrational/fixtures/native_sync/pam/grant_token_with_uuid_and_channels.json', serializer='pn_json', filter_query_parameters=['uuid', 'seqn', 'pnsdk', 'timestamp', 'signature'] ) def test_grant_auth_key_with_uuid_and_channels(): - envelope = pubnub.grant_token()\ - .ttl(60)\ - .authorized_uuid('some_uuid')\ + envelope = pubnub.grant_token() \ + .ttl(60) \ + .authorized_uuid('some_uuid') \ .channels([ - Channel().id('some_channel_id').read().write(), - Channel().pattern('some_*').read().write() - ])\ + Channel().id('some_channel_id').read().write().manage().delete().get().update().join(), + Channel().pattern('some_*').read().write().manage() + ]) \ + .groups([ + Group().id('some_group_id').read().manage(), + Group().pattern('some_*').read(), + ]) \ + .uuids([ + UUID().id('some_uuid').get().update().delete(), + UUID().pattern('some_*').get() + ]) \ .sync() assert isinstance(envelope.result, PNGrantTokenResult) assert envelope.result.token @pn_vcr.use_cassette( - 'tests/integrational/fixtures/native_sync/pam/grant_token_user_space.yaml', + 'tests/integrational/fixtures/native_sync/pam/grant_token_user_space.json', serializer='pn_json', filter_query_parameters=['uuid', 'seqn', 'pnsdk', 'timestamp', 'signature'] ) def test_grant_auth_key_with_user_id_and_spaces():