-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
346 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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 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 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 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
Empty file.
This file contains 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,8 @@ | ||
from pathlib import Path | ||
|
||
import pytest | ||
|
||
|
||
@pytest.fixture() | ||
def auth_key_filepath() -> Path: | ||
return Path(__file__).parent / "dummy.p8" |
This file contains 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,5 @@ | ||
-----BEGIN PRIVATE KEY----- | ||
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgrcxc3odhJh+wcUbY | ||
FQRIACPBtRdkAw2RkkoLEU//vVWhRANCAAS3E4nNyDsuBLssEHUDu/Eck4pUCKge | ||
M/WbLSx83MmMWyv4ynD3z3xqjRptG1cGLGWuCXFZZ4+qPKKXixqShjaE | ||
-----END PRIVATE KEY----- |
This file contains 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 @@ | ||
from kalyke import LiveActivityClient | ||
|
||
|
||
def test_initialize_with_pathlib(auth_key_filepath): | ||
client = LiveActivityClient( | ||
use_sandbox=True, | ||
team_id="DUMMY_TEAM_ID", | ||
auth_key_id="DUMMY", | ||
auth_key_filepath=auth_key_filepath, | ||
) | ||
|
||
assert isinstance(client, LiveActivityClient) | ||
|
||
|
||
def test_initialize_with_str(auth_key_filepath): | ||
client = LiveActivityClient( | ||
use_sandbox=True, | ||
team_id="DUMMY_TEAM_ID", | ||
auth_key_id="DUMMY", | ||
auth_key_filepath=str(auth_key_filepath), | ||
) | ||
|
||
assert isinstance(client, LiveActivityClient) |
This file contains 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,20 @@ | ||
from httpx import AsyncClient | ||
|
||
from kalyke import LiveActivityApnsConfig, LiveActivityClient | ||
|
||
|
||
def test_exist_authorization_header(auth_key_filepath): | ||
client = LiveActivityClient( | ||
use_sandbox=True, | ||
team_id="DUMMY_TEAM_ID", | ||
auth_key_id="DUMMY", | ||
auth_key_filepath=auth_key_filepath, | ||
) | ||
actual_client = client._init_client( | ||
apns_config=LiveActivityApnsConfig( | ||
topic="com.example.App.push-type.liveactivity", | ||
), | ||
) | ||
|
||
assert isinstance(actual_client, AsyncClient) | ||
assert actual_client.headers["authorization"].startswith("bearer ey") |
This file contains 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,53 @@ | ||
import datetime | ||
from pathlib import Path | ||
from unittest.mock import MagicMock | ||
|
||
import jwt | ||
import pytest | ||
|
||
from kalyke import LiveActivityClient | ||
|
||
|
||
def test_success(auth_key_filepath): | ||
datetime_mock = MagicMock(wraps=datetime.datetime) | ||
datetime_mock.now.return_value = datetime.datetime(2022, 1, 10, 23, 6, 34) | ||
|
||
client = LiveActivityClient( | ||
use_sandbox=True, | ||
team_id="DUMMY_TEAM_ID", | ||
auth_key_id="DUMMY", | ||
auth_key_filepath=auth_key_filepath, | ||
) | ||
token = client._make_authorization() | ||
|
||
expect = jwt.encode( | ||
payload={ | ||
"iss": "DUMMY_TEAM_ID", | ||
"iat": str(int(datetime.datetime.now().timestamp())), | ||
}, | ||
key=auth_key_filepath.read_text(), | ||
algorithm="ES256", | ||
headers={ | ||
"alg": "ES256", | ||
"kid": "DUMMY", | ||
}, | ||
) | ||
|
||
actual_payload = jwt.decode(jwt=token, options={"verify_signature": False}) | ||
expect_payload = jwt.decode(jwt=expect, options={"verify_signature": False}) | ||
assert actual_payload == expect_payload | ||
assert jwt.get_unverified_header(jwt=token) == jwt.get_unverified_header(jwt=expect) | ||
|
||
|
||
def test_file_not_found_error(): | ||
client = LiveActivityClient( | ||
use_sandbox=True, | ||
team_id="DUMMY_TEAM_ID", | ||
auth_key_id="DUMMY", | ||
auth_key_filepath=Path("/") / "no_exist.p8", | ||
) | ||
|
||
with pytest.raises(FileNotFoundError) as e: | ||
_ = client._make_authorization() | ||
|
||
assert str(e.value) == "[Errno 2] No such file or directory: '/no_exist.p8'" |
This file contains 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,86 @@ | ||
import pytest | ||
|
||
from kalyke import LiveActivityApnsConfig, LiveActivityClient | ||
from kalyke.exceptions import BadDeviceToken | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_success(httpx_mock, auth_key_filepath): | ||
httpx_mock.add_response( | ||
status_code=200, | ||
http_version="HTTP/2.0", | ||
headers={ | ||
"apns-id": "stub-apns-id", | ||
}, | ||
json={}, | ||
) | ||
|
||
client = LiveActivityClient( | ||
use_sandbox=True, | ||
team_id="DUMMY_TEAM", | ||
auth_key_id="DUMMY", | ||
auth_key_filepath=auth_key_filepath, | ||
) | ||
apns_id = await client.send_message( | ||
device_token="stub_device_token", | ||
payload={ | ||
"alert": "test alert", | ||
}, | ||
apns_config=LiveActivityApnsConfig( | ||
topic="com.example.App.push-type.liveactivity", | ||
), | ||
) | ||
|
||
assert apns_id == "stub-apns-id" | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_bad_device_token(httpx_mock, auth_key_filepath): | ||
httpx_mock.add_response( | ||
status_code=400, | ||
http_version="HTTP/2.0", | ||
json={ | ||
"reason": "BadDeviceToken", | ||
}, | ||
) | ||
|
||
client = LiveActivityClient( | ||
use_sandbox=True, | ||
team_id="DUMMY_TEAM_ID", | ||
auth_key_id="DUMMY", | ||
auth_key_filepath=auth_key_filepath, | ||
) | ||
|
||
with pytest.raises(BadDeviceToken) as e: | ||
await client.send_message( | ||
device_token="stub_device_token", | ||
payload={ | ||
"alert": "test alert", | ||
}, | ||
apns_config=LiveActivityApnsConfig( | ||
topic="com.example.App.push-type.liveactivity", | ||
), | ||
) | ||
|
||
assert str(e.value) == str(BadDeviceToken(error={})) | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_value_error(auth_key_filepath): | ||
client = LiveActivityClient( | ||
use_sandbox=True, | ||
team_id="DUMMY_TEAM_ID", | ||
auth_key_id="DUMMY", | ||
auth_key_filepath=auth_key_filepath, | ||
) | ||
|
||
with pytest.raises(ValueError) as e: | ||
await client.send_message( | ||
device_token="stub_device_token", | ||
payload=["test alert"], | ||
apns_config=LiveActivityApnsConfig( | ||
topic="com.example.App.push-type.liveactivity", | ||
), | ||
) | ||
|
||
assert str(e.value) == "Type of 'payload' must be specified by Payload or Dict[str, Any]." |
Empty file.
Oops, something went wrong.