|
| 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 | +api_paths = { |
| 13 | + "query_logs": f"https://api.notificationapi.com/{client_id}/logs/query", |
| 14 | +} |
| 15 | + |
| 16 | +query_logs_params = { |
| 17 | + "dateRangeFilter": { |
| 18 | + "startTime": 1719600830559, |
| 19 | + "endTime": 1719600840559 |
| 20 | + }, |
| 21 | + "notificationFilter": ["order_tracking"], |
| 22 | + "channelFilter": ["EMAIL"], |
| 23 | + "userFilter": ["abcd-1234"], |
| 24 | + "statusFilter": ["SUCCESS"], |
| 25 | + "trackingIds": ["172cf2f4-18cd-4f1f-b2ac-e50c7d71891c"], |
| 26 | + "requestFilter": ['request.mergeTags.item="Krabby Patty Burger"'], |
| 27 | + "envIdFilter": ["6ok6imq9unr2budgiebjdaa6oi"] |
| 28 | +} |
| 29 | + |
| 30 | + |
| 31 | +@pytest.mark.asyncio |
| 32 | +@pytest.mark.parametrize( |
| 33 | + "func,params", |
| 34 | + [ |
| 35 | + ("query_logs", query_logs_params), |
| 36 | + ], |
| 37 | +) |
| 38 | +async def test_makes_one_post_api_call(respx_mock, func, params): |
| 39 | + route = respx_mock.post(api_paths[func]).mock(return_value=Response(200)) |
| 40 | + notificationapi.init(client_id, client_secret) |
| 41 | + await getattr(notificationapi, func)(params) |
| 42 | + assert route.called |
| 43 | + |
| 44 | + |
| 45 | +@pytest.mark.asyncio |
| 46 | +@pytest.mark.parametrize( |
| 47 | + "func,params", |
| 48 | + [ |
| 49 | + ("query_logs", query_logs_params), |
| 50 | + ], |
| 51 | +) |
| 52 | +async def test_passes_params_as_json_body(respx_mock, func, params): |
| 53 | + route = respx_mock.post(api_paths[func]).mock(return_value=Response(200)) |
| 54 | + notificationapi.init(client_id, client_secret) |
| 55 | + await getattr(notificationapi, func)(params) |
| 56 | + assert json.loads(route.calls.last.request.content) == params |
| 57 | + |
| 58 | + |
| 59 | +@pytest.mark.asyncio |
| 60 | +@pytest.mark.parametrize( |
| 61 | + "func,params", |
| 62 | + [ |
| 63 | + ("query_logs", query_logs_params), |
| 64 | + ], |
| 65 | +) |
| 66 | +async def test_logs_on_202(respx_mock, caplog, func, params): |
| 67 | + respx_mock.post(api_paths[func]).mock(return_value=Response(202)) |
| 68 | + notificationapi.init(client_id, client_secret) |
| 69 | + await getattr(notificationapi, func)(params) |
| 70 | + assert "NotificationAPI request ignored." in caplog.text |
| 71 | + |
| 72 | + |
| 73 | +@pytest.mark.asyncio |
| 74 | +@pytest.mark.parametrize( |
| 75 | + "func,params", |
| 76 | + [ |
| 77 | + ("query_logs", query_logs_params), |
| 78 | + ], |
| 79 | +) |
| 80 | +async def test_logs_and_throws_on_500(respx_mock, caplog, func, params): |
| 81 | + respx_mock.post(api_paths[func]).mock(return_value=Response(500, text="big oof 500")) |
| 82 | + notificationapi.init(client_id, client_secret) |
| 83 | + await getattr(notificationapi, func)(params) |
| 84 | + assert "NotificationAPI request failed. Response: big oof 500" in caplog.text |
0 commit comments