|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +"""Tests for `notificationapi_python_server_sdk` package.""" |
| 4 | + |
| 5 | +import pytest |
| 6 | +import hashlib |
| 7 | +import base64 |
| 8 | +import urllib.parse |
| 9 | +import json |
| 10 | +from httpx import Response |
| 11 | +from notificationapi_python_server_sdk import notificationapi |
| 12 | + |
| 13 | +client_id = "client_id" |
| 14 | +client_secret = "client_secret" |
| 15 | +user_id = "userId" |
| 16 | + |
| 17 | +api_paths = { |
| 18 | + "update_in_app_notification": f"https://api.notificationapi.com/{client_id}/users/{urllib.parse.quote(user_id)}/notifications/INAPP_WEB", |
| 19 | +} |
| 20 | + |
| 21 | + |
| 22 | +@pytest.mark.asyncio |
| 23 | +@pytest.mark.parametrize( |
| 24 | + "func,params", |
| 25 | + [ |
| 26 | + ( |
| 27 | + "update_in_app_notification", |
| 28 | + ( |
| 29 | + user_id, |
| 30 | + { |
| 31 | + "trackingIds": ["sampleTrackingId"], |
| 32 | + "opened": "1970-01-01T00:00:00.000Z", |
| 33 | + "clicked": "1970-01-01T00:00:00.000Z", |
| 34 | + "archived": "1970-01-01T00:00:00.000Z", |
| 35 | + "actioned1": "1970-01-01T00:00:00.000Z", |
| 36 | + "actioned2": "1970-01-01T00:00:00.000Z", |
| 37 | + "reply": {"date": "1970-01-01T00:00:00.000Z", "message": "nice!"} |
| 38 | + } |
| 39 | + ), |
| 40 | + ), |
| 41 | + ], |
| 42 | +) |
| 43 | +async def test_makes_one_patch_api_call(respx_mock, func, params): |
| 44 | + route = respx_mock.patch(api_paths[func]).mock(return_value=Response(200)) |
| 45 | + notificationapi.init(client_id, client_secret) |
| 46 | + await getattr(notificationapi, func)(*params) |
| 47 | + assert route.called |
| 48 | + |
| 49 | + |
| 50 | +@pytest.mark.parametrize( |
| 51 | + "func,params", |
| 52 | + [ |
| 53 | + ( |
| 54 | + "update_in_app_notification", |
| 55 | + ( |
| 56 | + user_id, |
| 57 | + { |
| 58 | + "trackingIds": ["sampleTrackingId"], |
| 59 | + "opened": "1970-01-01T00:00:00.000Z", |
| 60 | + "clicked": "1970-01-01T00:00:00.000Z", |
| 61 | + "archived": "1970-01-01T00:00:00.000Z", |
| 62 | + "actioned1": "1970-01-01T00:00:00.000Z", |
| 63 | + "actioned2": "1970-01-01T00:00:00.000Z", |
| 64 | + "reply": {"date": "1970-01-01T00:00:00.000Z", "message": "nice!"} |
| 65 | + } |
| 66 | + ), |
| 67 | + ), |
| 68 | + ], |
| 69 | +) |
| 70 | +async def test_uses_custom_authorization(respx_mock, func, params): |
| 71 | + route = respx_mock.patch(api_paths[func]).mock(return_value=Response(200)) |
| 72 | + hashed_user_id = hashlib.sha256((client_secret + user_id).encode()).digest() |
| 73 | + hashed_user_id_base64 = base64.b64encode(hashed_user_id).decode() |
| 74 | + |
| 75 | + # Create custom authorization header |
| 76 | + custom_auth = 'Basic ' + base64.b64encode(f'{client_id}:{user_id}:{hashed_user_id_base64}'.encode()).decode() |
| 77 | + notificationapi.init(client_id, client_secret) |
| 78 | + await getattr(notificationapi, func)(*params) |
| 79 | + assert "Authorization" in route.calls.last.request.headers |
| 80 | + assert route.calls.last.request.headers["Authorization"] == custom_auth |
| 81 | + |
| 82 | + |
| 83 | +@pytest.mark.parametrize( |
| 84 | + "func,params", |
| 85 | + [ |
| 86 | + ( |
| 87 | + "update_in_app_notification", |
| 88 | + ( |
| 89 | + user_id, |
| 90 | + { |
| 91 | + "trackingIds": ["sampleTrackingId"], |
| 92 | + "opened": "1970-01-01T00:00:00.000Z", |
| 93 | + "clicked": "1970-01-01T00:00:00.000Z", |
| 94 | + "archived": "1970-01-01T00:00:00.000Z", |
| 95 | + "actioned1": "1970-01-01T00:00:00.000Z", |
| 96 | + "actioned2": "1970-01-01T00:00:00.000Z", |
| 97 | + "reply": {"date": "1970-01-01T00:00:00.000Z", "message": "nice!"} |
| 98 | + } |
| 99 | + ), |
| 100 | + ), |
| 101 | + ], |
| 102 | +) |
| 103 | +async def test_passes_data_as_json_body(respx_mock, func, params): |
| 104 | + route = respx_mock.patch(api_paths[func]).mock(return_value=Response(200)) |
| 105 | + notificationapi.init(client_id, client_secret) |
| 106 | + await getattr(notificationapi, func)(*params) |
| 107 | + sent_data = json.loads(route.calls.last.request.content) |
| 108 | + assert sent_data == { |
| 109 | + "trackingIds": ["sampleTrackingId"], |
| 110 | + "opened": "1970-01-01T00:00:00.000Z", |
| 111 | + "clicked": "1970-01-01T00:00:00.000Z", |
| 112 | + "archived": "1970-01-01T00:00:00.000Z", |
| 113 | + "actioned1": "1970-01-01T00:00:00.000Z", |
| 114 | + "actioned2": "1970-01-01T00:00:00.000Z", |
| 115 | + "reply": {"date": "1970-01-01T00:00:00.000Z", "message": "nice!"} |
| 116 | + } |
| 117 | + |
| 118 | + |
| 119 | +@pytest.mark.parametrize( |
| 120 | + "func,params", |
| 121 | + [ |
| 122 | + ( |
| 123 | + "update_in_app_notification", |
| 124 | + ( |
| 125 | + user_id, |
| 126 | + { |
| 127 | + "trackingIds": ["sampleTrackingId"], |
| 128 | + "opened": "1970-01-01T00:00:00.000Z", |
| 129 | + "clicked": "1970-01-01T00:00:00.000Z", |
| 130 | + "archived": "1970-01-01T00:00:00.000Z", |
| 131 | + "actioned1": "1970-01-01T00:00:00.000Z", |
| 132 | + "actioned2": "1970-01-01T00:00:00.000Z", |
| 133 | + "reply": {"date": "1970-01-01T00:00:00.000Z", "message": "nice!"} |
| 134 | + } |
| 135 | + ), |
| 136 | + ), |
| 137 | + ], |
| 138 | +) |
| 139 | +async def test_logs_and_throws_on_500(respx_mock, caplog, func, params): |
| 140 | + respx_mock.patch(api_paths[func]).mock(return_value=Response(500, text="big oof 500")) |
| 141 | + notificationapi.init(client_id, client_secret) |
| 142 | + await getattr(notificationapi, func)(*params) |
| 143 | + assert "NotificationAPI request failed. Response: big oof 500" in caplog.text |
0 commit comments