|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +"""Tests for `notificationapi_python_server_sdk` package.""" |
| 4 | + |
| 5 | +import pytest |
| 6 | +import json |
| 7 | +from httpx import Response |
| 8 | +from notificationapi_python_server_sdk import notificationapi |
| 9 | + |
| 10 | +client_id = "client_id" |
| 11 | +client_secret = "client_secret" |
| 12 | +tracking_id = "tracking_id" |
| 13 | +send_request = { |
| 14 | + 'notificationId': 'notification_id' |
| 15 | +} |
| 16 | +api_paths = { |
| 17 | + "update_schedule": f"https://api.notificationapi.com/{client_id}/schedule/{tracking_id}", |
| 18 | +} |
| 19 | + |
| 20 | + |
| 21 | +@pytest.mark.asyncio |
| 22 | +@pytest.mark.parametrize( |
| 23 | + "func,params", |
| 24 | + [ |
| 25 | + ( |
| 26 | + "update_schedule", |
| 27 | + { |
| 28 | + "tracking_id": tracking_id, |
| 29 | + "send_request": send_request, |
| 30 | + }, |
| 31 | + ), |
| 32 | + ], |
| 33 | +) |
| 34 | +async def test_makes_one_patch_api_call(respx_mock, func, params): |
| 35 | + route = respx_mock.patch(api_paths[func]).mock(return_value=Response(200)) |
| 36 | + notificationapi.init(client_id, client_secret) |
| 37 | + await getattr(notificationapi, func)(params) |
| 38 | + assert route.called |
| 39 | + |
| 40 | + |
| 41 | +@pytest.mark.asyncio |
| 42 | +@pytest.mark.parametrize( |
| 43 | + "func,params", |
| 44 | + [ |
| 45 | + ( |
| 46 | + "update_schedule", |
| 47 | + { |
| 48 | + "tracking_id": tracking_id, |
| 49 | + "send_request": send_request, |
| 50 | + }, |
| 51 | + ), |
| 52 | + ], |
| 53 | +) |
| 54 | +async def test_uses_basic_authorization(respx_mock, func, params): |
| 55 | + route = respx_mock.patch(api_paths[func]).mock(return_value=Response(200)) |
| 56 | + notificationapi.init(client_id, client_secret) |
| 57 | + await getattr(notificationapi, func)(params) |
| 58 | + assert route.calls.last.request.headers["Authorization"] == "Basic Y2xpZW50X2lkOmNsaWVudF9zZWNyZXQ=" |
| 59 | + |
| 60 | + |
| 61 | +@pytest.mark.asyncio |
| 62 | +@pytest.mark.parametrize( |
| 63 | + "func,params", |
| 64 | + [ |
| 65 | + ( |
| 66 | + "update_schedule", |
| 67 | + { |
| 68 | + "tracking_id": tracking_id, |
| 69 | + "send_request": send_request, |
| 70 | + }, |
| 71 | + ), |
| 72 | + ], |
| 73 | +) |
| 74 | +async def test_passes_send_request_as_json_body(respx_mock, func, params): |
| 75 | + route = respx_mock.patch(api_paths[func]).mock(return_value=Response(200)) |
| 76 | + notificationapi.init(client_id, client_secret) |
| 77 | + await getattr(notificationapi, func)(params) |
| 78 | + assert json.loads(route.calls.last.request.content) == params["send_request"] |
| 79 | + |
| 80 | + |
| 81 | +@pytest.mark.asyncio |
| 82 | +@pytest.mark.parametrize( |
| 83 | + "func,params", |
| 84 | + [ |
| 85 | + ( |
| 86 | + "update_schedule", |
| 87 | + { |
| 88 | + "tracking_id": tracking_id, |
| 89 | + "send_request": send_request, |
| 90 | + }, |
| 91 | + ), |
| 92 | + ], |
| 93 | +) |
| 94 | +async def test_logs_and_throws_on_500(respx_mock, caplog, func, params): |
| 95 | + respx_mock.patch(api_paths[func]).mock(return_value=Response(500, text="big oof 500")) |
| 96 | + notificationapi.init(client_id, client_secret) |
| 97 | + await getattr(notificationapi, func)(params) |
| 98 | + assert "NotificationAPI request failed. Response: big oof 500" in caplog.text |
0 commit comments