From 2483576337d851f34b0a6ad3b28f3c2266216a25 Mon Sep 17 00:00:00 2001 From: mbasadi Date: Fri, 1 Mar 2024 11:19:34 -0500 Subject: [PATCH 1/3] Chore: update_schedule and delete_schedule --- .../notificationapi.py | 15 +++ tests/test_notificationapi_deleteSchedule.py | 71 ++++++++++++++ tests/test_notificationapi_updateSchedule.py | 98 +++++++++++++++++++ 3 files changed, 184 insertions(+) create mode 100644 tests/test_notificationapi_deleteSchedule.py create mode 100644 tests/test_notificationapi_updateSchedule.py diff --git a/notificationapi_python_server_sdk/notificationapi.py b/notificationapi_python_server_sdk/notificationapi.py index 260aa53..e7397f6 100644 --- a/notificationapi_python_server_sdk/notificationapi.py +++ b/notificationapi_python_server_sdk/notificationapi.py @@ -71,6 +71,21 @@ async def delete_sub_notification(params): % (params["notification_id"], params["sub_notification_id"]), ) +async def update_schedule(params): + await request( + "PATCH", + "schedule/%s" + % (params["tracking_id"]), + params["send_request"], + ) + + +async def delete_schedule(params): + await request( + "DELETE", + "schedule/%s" + % (params["tracking_id"]), + ) async def set_user_preferences(params): await request( diff --git a/tests/test_notificationapi_deleteSchedule.py b/tests/test_notificationapi_deleteSchedule.py new file mode 100644 index 0000000..a115584 --- /dev/null +++ b/tests/test_notificationapi_deleteSchedule.py @@ -0,0 +1,71 @@ +#!/usr/bin/env python + +"""Tests for `notificationapi_python_server_sdk` package.""" + +import pytest +from httpx import Response +from notificationapi_python_server_sdk import notificationapi + +client_id = "client_id" +client_secret = "client_secret" +tracking_id = "tracking_id" +api_paths = { + "delete_schedule": f"https://api.notificationapi.com/{client_id}/schedule/{tracking_id}" +} + + +@pytest.mark.asyncio +@pytest.mark.parametrize( + "func,params", + [ + ( + "delete_schedule", + { + "tracking_id": tracking_id, + }, + ), + ], +) +async def test_makes_one_delete_api_call(respx_mock, func, params): + route = respx_mock.delete(api_paths[func]).mock(return_value=Response(200)) + notificationapi.init(client_id, client_secret) + await getattr(notificationapi, func)(params) + assert route.called + + +@pytest.mark.asyncio +@pytest.mark.parametrize( + "func,params", + [ + ( + "delete_schedule", + { + "tracking_id": tracking_id, + }, + ), + ], +) +async def test_uses_basic_authorization(respx_mock, func, params): + route = respx_mock.delete(api_paths[func]).mock(return_value=Response(200)) + notificationapi.init(client_id, client_secret) + await getattr(notificationapi, func)(params) + assert route.calls.last.request.headers["Authorization"] == "Basic Y2xpZW50X2lkOmNsaWVudF9zZWNyZXQ=" + + +@pytest.mark.asyncio +@pytest.mark.parametrize( + "func,params", + [ + ( + "delete_schedule", + { + "tracking_id": tracking_id, + }, + ), + ], +) +async def test_logs_and_throws_on_500(respx_mock, caplog, func, params): + respx_mock.delete(api_paths[func]).mock(return_value=Response(500, text="big oof 500")) + notificationapi.init(client_id, client_secret) + await getattr(notificationapi, func)(params) + assert "NotificationAPI request failed. Response: big oof 500" in caplog.text diff --git a/tests/test_notificationapi_updateSchedule.py b/tests/test_notificationapi_updateSchedule.py new file mode 100644 index 0000000..e23d5e1 --- /dev/null +++ b/tests/test_notificationapi_updateSchedule.py @@ -0,0 +1,98 @@ +#!/usr/bin/env python + +"""Tests for `notificationapi_python_server_sdk` package.""" + +import pytest +import json +from httpx import Response +from notificationapi_python_server_sdk import notificationapi + +client_id = "client_id" +client_secret = "client_secret" +tracking_id = "tracking_id" +send_request={ + 'notificationId':'notification_id' +} +api_paths = { + "update_schedule": f"https://api.notificationapi.com/{client_id}/schedule/{tracking_id}", +} + + +@pytest.mark.asyncio +@pytest.mark.parametrize( + "func,params", + [ + ( + "update_schedule", + { + "tracking_id": tracking_id, + "send_request": send_request, + }, + ), + ], +) +async def test_makes_one_patch_api_call(respx_mock, func, params): + route = respx_mock.patch(api_paths[func]).mock(return_value=Response(200)) + notificationapi.init(client_id, client_secret) + await getattr(notificationapi, func)(params) + assert route.called + + +@pytest.mark.asyncio +@pytest.mark.parametrize( + "func,params", + [ + ( + "update_schedule", + { + "tracking_id": tracking_id, + "send_request": send_request, + }, + ), + ], +) +async def test_uses_basic_authorization(respx_mock, func, params): + route = respx_mock.patch(api_paths[func]).mock(return_value=Response(200)) + notificationapi.init(client_id, client_secret) + await getattr(notificationapi, func)(params) + assert route.calls.last.request.headers["Authorization"] == "Basic Y2xpZW50X2lkOmNsaWVudF9zZWNyZXQ=" + + +@pytest.mark.asyncio +@pytest.mark.parametrize( + "func,params", + [ + ( + "update_schedule", + { + "tracking_id": tracking_id, + "send_request": send_request, + }, + ), + ], +) +async def test_passes_send_request_as_json_body(respx_mock, func, params): + route = respx_mock.patch(api_paths[func]).mock(return_value=Response(200)) + notificationapi.init(client_id, client_secret) + await getattr(notificationapi, func)(params) + assert json.loads(route.calls.last.request.content) == params["send_request"] + + +@pytest.mark.asyncio +@pytest.mark.parametrize( + "func,params", + [ + ( + "update_schedule", + { + "tracking_id": tracking_id, + "send_request": send_request, + }, + ), + ], +) +async def test_logs_and_throws_on_500(respx_mock, caplog, func, params): + respx_mock.patch(api_paths[func]).mock(return_value=Response(500, text="big oof 500")) + notificationapi.init(client_id, client_secret) + await getattr(notificationapi, func)(params) + assert "NotificationAPI request failed. Response: big oof 500" in caplog.text From 92e1c5ce723d7e89ef54c561b9c799a0119d32d4 Mon Sep 17 00:00:00 2001 From: mbasadi Date: Fri, 1 Mar 2024 12:22:08 -0500 Subject: [PATCH 2/3] fix: format --- notificationapi_python_server_sdk/notificationapi.py | 2 ++ tests/test_notificationapi_updateSchedule.py | 6 +++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/notificationapi_python_server_sdk/notificationapi.py b/notificationapi_python_server_sdk/notificationapi.py index e7397f6..50dd73c 100644 --- a/notificationapi_python_server_sdk/notificationapi.py +++ b/notificationapi_python_server_sdk/notificationapi.py @@ -71,6 +71,7 @@ async def delete_sub_notification(params): % (params["notification_id"], params["sub_notification_id"]), ) + async def update_schedule(params): await request( "PATCH", @@ -87,6 +88,7 @@ async def delete_schedule(params): % (params["tracking_id"]), ) + async def set_user_preferences(params): await request( "POST", diff --git a/tests/test_notificationapi_updateSchedule.py b/tests/test_notificationapi_updateSchedule.py index e23d5e1..d854777 100644 --- a/tests/test_notificationapi_updateSchedule.py +++ b/tests/test_notificationapi_updateSchedule.py @@ -10,8 +10,8 @@ client_id = "client_id" client_secret = "client_secret" tracking_id = "tracking_id" -send_request={ - 'notificationId':'notification_id' +send_request = { + 'notificationId': 'notification_id' } api_paths = { "update_schedule": f"https://api.notificationapi.com/{client_id}/schedule/{tracking_id}", @@ -75,7 +75,7 @@ async def test_passes_send_request_as_json_body(respx_mock, func, params): route = respx_mock.patch(api_paths[func]).mock(return_value=Response(200)) notificationapi.init(client_id, client_secret) await getattr(notificationapi, func)(params) - assert json.loads(route.calls.last.request.content) == params["send_request"] + assert json.loads(route.calls.last.request.content) == params["send_request"] @pytest.mark.asyncio From d0e7b57ea7f8f66eba9944ccee3b478bb4fc0571 Mon Sep 17 00:00:00 2001 From: mbasadi Date: Fri, 1 Mar 2024 12:33:39 -0500 Subject: [PATCH 3/3] update version --- notificationapi_python_server_sdk/__init__.py | 2 +- setup.cfg | 2 +- setup.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/notificationapi_python_server_sdk/__init__.py b/notificationapi_python_server_sdk/__init__.py index 3b1b1f8..64befc5 100644 --- a/notificationapi_python_server_sdk/__init__.py +++ b/notificationapi_python_server_sdk/__init__.py @@ -2,4 +2,4 @@ __author__ = """Sahand Seifi""" __email__ = "sahand.seifi@gmail.com" -__version__ = "1.0.1" +__version__ = "1.1.0" diff --git a/setup.cfg b/setup.cfg index 71dd4d5..0e89946 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,5 +1,5 @@ [bumpversion] -current_version = 1.0.1 +current_version = 1.1.0 commit = True tag = True diff --git a/setup.py b/setup.py index e92f6dc..7a462c9 100644 --- a/setup.py +++ b/setup.py @@ -52,6 +52,6 @@ test_suite="tests", tests_require=test_requirements, url="https://github.com/notificationapi-com/notificationapi_python_server_sdk", - version="1.0.1", + version="1.1.0", zip_safe=False, )