Repository navigation
Push: add APNs broadcast channels and Live Activity admin API #732
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ttypic
wants to merge
1
commit into
main
Choose a base branch
from
live-activities-admin-methods
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| class PushApnsBroadcast: | ||
| """An APNs broadcast channel created for an iOS Live Activity. | ||
|
|
||
| Experimental: this is a preview feature and may change in a future non-major release. | ||
| """ | ||
|
|
||
| def __init__(self, id, apns_channel_id): | ||
| self.__id = id | ||
| self.__apns_channel_id = apns_channel_id | ||
|
|
||
| @property | ||
| def id(self): | ||
| """The opaque Ably broadcast id, passed to the `push.admin.live_activity` methods.""" | ||
| return self.__id | ||
|
|
||
| @property | ||
| def apns_channel_id(self): | ||
| """The Apple-assigned channel id.""" | ||
| return self.__apns_channel_id | ||
|
|
||
| @classmethod | ||
| def from_dict(cls, obj): | ||
| return cls(id=obj.get('id'), apns_channel_id=obj.get('apnsChannelId')) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import httpx | ||
| import pytest | ||
|
|
||
| from ably import AblyRest | ||
| from ably.types.testoptions import TestOptions | ||
| from ably.util.exceptions import AblyException | ||
|
|
||
|
|
||
| class RecordingTransport(httpx.AsyncBaseTransport): | ||
| def __init__(self): | ||
| self.requests = [] | ||
|
|
||
| async def handle_async_request(self, request): | ||
| self.requests.append(request) | ||
| return httpx.Response(200, json={}) | ||
|
|
||
|
|
||
| # RSH1e1a: the recipient must name exactly one of channels and deviceId, | ||
| # and is rejected before any request is made | ||
| @pytest.mark.parametrize('recipient', [ | ||
| {}, | ||
| {'channels': []}, | ||
| {'channels': ['a'], 'deviceId': 'device-1'}, | ||
| ]) | ||
| async def test_live_activity_start_rejects_invalid_recipient(recipient): | ||
| transport = RecordingTransport() | ||
| ably = AblyRest(key='appId.keyId:keySecret', _test_options=TestOptions(http_transport=transport)) | ||
| try: | ||
| with pytest.raises(AblyException) as excinfo: | ||
| await ably.push.admin.live_activity.start( | ||
| recipient=recipient, | ||
| apns_broadcast='broadcast-1', | ||
| apns={'aps': {'event': 'start'}}, | ||
| ) | ||
| finally: | ||
| await ably.close() | ||
|
|
||
| assert excinfo.value.code == 40000 | ||
| assert excinfo.value.status_code == 400 | ||
| assert transport.requests == [] |
110 changes: 110 additions & 0 deletions
110
test/uts/rest/unit/push/push_admin_apns_broadcast_test.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| """Derived from uts/rest/unit/push/push_admin_apns_broadcast.md in ably/specification. | ||
|
|
||
| Spec points: RSH1, RSH1d | ||
| """ | ||
|
|
||
| import msgpack | ||
| import pytest | ||
|
|
||
| from ably.types.apnsbroadcast import PushApnsBroadcast | ||
| from ably.util.exceptions import AblyException | ||
| from test.uts.helpers.client import rest_client | ||
| from test.uts.helpers.mock_http import MockHttpClient | ||
|
|
||
| BROADCAST_RESPONSE = {'id': 'broadcast-1', 'apnsChannelId': 'apns-1'} | ||
|
|
||
|
|
||
| def capture_and_respond(captured_requests, status=201, body=None): | ||
| def on_request(request): | ||
| captured_requests.append(request) | ||
| request.respond_with(status, body if body is not None else BROADCAST_RESPONSE) | ||
|
|
||
| return on_request | ||
|
|
||
|
|
||
| # UTS: rest/unit/RSH1d/create-apns-broadcast-post-0 | ||
| async def test_rsh1d_create_apns_broadcast_post(): | ||
| captured_requests = [] | ||
| mock_http = MockHttpClient( | ||
| on_connection_attempt=lambda conn: conn.respond_with_success(), | ||
| on_request=capture_and_respond(captured_requests), | ||
| ) | ||
| client = rest_client(mock_http) | ||
|
|
||
| await client.push.admin.create_apns_broadcast(message_storage_policy=1) | ||
|
|
||
| assert len(captured_requests) == 1 | ||
|
|
||
| request = captured_requests[0] | ||
| assert request.method == 'POST' | ||
| assert request.url.path == '/push/apnsBroadcastChannels' | ||
|
|
||
|
|
||
| # UTS: rest/unit/RSH1d/message-storage-policy-1 | ||
| async def test_rsh1d_message_storage_policy(): | ||
| captured_requests = [] | ||
| mock_http = MockHttpClient( | ||
| on_connection_attempt=lambda conn: conn.respond_with_success(), | ||
| on_request=capture_and_respond(captured_requests), | ||
| ) | ||
| client = rest_client(mock_http) | ||
|
|
||
| await client.push.admin.create_apns_broadcast(message_storage_policy=0) | ||
|
|
||
| assert len(captured_requests) == 1 | ||
|
|
||
| body = msgpack.unpackb(captured_requests[0].body) | ||
| assert body['messageStoragePolicy'] == 0 | ||
|
|
||
|
|
||
| # UTS: rest/unit/RSH1d/returns-ids-2 | ||
| async def test_rsh1d_returns_ids(): | ||
| mock_http = MockHttpClient( | ||
| on_connection_attempt=lambda conn: conn.respond_with_success(), | ||
| on_request=lambda request: request.respond_with(201, { | ||
| 'id': 'broadcast-xyz', | ||
| 'apnsChannelId': 'apple-channel-abc', | ||
| }), | ||
| ) | ||
| client = rest_client(mock_http) | ||
|
|
||
| result = await client.push.admin.create_apns_broadcast(message_storage_policy=1) | ||
|
|
||
| assert isinstance(result, PushApnsBroadcast) | ||
| assert result.id == 'broadcast-xyz' | ||
| assert result.apns_channel_id == 'apple-channel-abc' | ||
|
|
||
|
|
||
| # UTS: rest/unit/RSH1d/auth-header-3 | ||
| async def test_rsh1d_auth_header(): | ||
| captured_requests = [] | ||
| mock_http = MockHttpClient( | ||
| on_connection_attempt=lambda conn: conn.respond_with_success(), | ||
| on_request=capture_and_respond(captured_requests), | ||
| ) | ||
| client = rest_client(mock_http) | ||
|
|
||
| await client.push.admin.create_apns_broadcast(message_storage_policy=1) | ||
|
|
||
| assert len(captured_requests) == 1 | ||
| assert captured_requests[0].headers['Authorization'].startswith('Basic ') | ||
|
|
||
|
|
||
| # UTS: rest/unit/RSH1d/server-error-4 | ||
| async def test_rsh1d_server_error(): | ||
| mock_http = MockHttpClient( | ||
| on_connection_attempt=lambda conn: conn.respond_with_success(), | ||
| on_request=lambda request: request.respond_with(400, { | ||
| 'error': { | ||
| 'code': 40000, | ||
| 'statusCode': 400, | ||
| 'message': 'Invalid request', | ||
| }, | ||
| }), | ||
| ) | ||
| client = rest_client(mock_http) | ||
|
|
||
| with pytest.raises(AblyException) as excinfo: | ||
| await client.push.admin.create_apns_broadcast(message_storage_policy=1) | ||
|
|
||
| assert excinfo.value.code == 40000 |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
validation is quite weak here - at the very least we should check that it's an array. not sure if the spec says anything about how we should validate