|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +"""Tests for `notificationapi_python_server_sdk` package.""" |
| 4 | + |
| 5 | +import pytest |
| 6 | +import urllib.parse |
| 7 | +import hashlib |
| 8 | +import base64 |
| 9 | +from httpx import Response |
| 10 | +from notificationapi_python_server_sdk import notificationapi |
| 11 | + |
| 12 | +client_id = "client_id" |
| 13 | +client_secret = "client_secret" |
| 14 | +user_id = "userId" |
| 15 | +notification_id = "notification_id" |
| 16 | + |
| 17 | +api_paths = { |
| 18 | + "delete_user_preferences": |
| 19 | + f"https://api.notificationapi.com/{client_id}/users/{urllib.parse.quote(user_id)}/preferences?notificationId={notification_id}", |
| 20 | +} |
| 21 | + |
| 22 | +delete_user_preferences_params = { |
| 23 | + "id": user_id, |
| 24 | + "notificationId": notification_id |
| 25 | +} |
| 26 | + |
| 27 | + |
| 28 | +@pytest.mark.asyncio |
| 29 | +@pytest.mark.parametrize( |
| 30 | + "func,params", |
| 31 | + [ |
| 32 | + ("delete_user_preferences", delete_user_preferences_params), |
| 33 | + ], |
| 34 | +) |
| 35 | +async def test_makes_one_delete_api_call(respx_mock, func, params): |
| 36 | + route = respx_mock.delete(api_paths[func]).mock(return_value=Response(200)) |
| 37 | + notificationapi.init(client_id, client_secret) |
| 38 | + await getattr(notificationapi, func)(params) |
| 39 | + assert route.called |
| 40 | + |
| 41 | + |
| 42 | +delete_user_preferences_params = { |
| 43 | + "id": user_id, |
| 44 | + "notificationId": notification_id |
| 45 | +} |
| 46 | +@pytest.mark.asyncio |
| 47 | +@pytest.mark.parametrize( |
| 48 | + "func,params", |
| 49 | + [ |
| 50 | + ("delete_user_preferences", delete_user_preferences_params), |
| 51 | + ], |
| 52 | +) |
| 53 | +async def test_uses_custom_authorization(respx_mock, func, params): |
| 54 | + route = respx_mock.delete(api_paths[func]).mock(return_value=Response(200)) |
| 55 | + hashed_user_id = hashlib.sha256((client_secret + user_id).encode()).digest() |
| 56 | + hashed_user_id_base64 = base64.b64encode(hashed_user_id).decode() |
| 57 | + |
| 58 | + # Create custom authorization header |
| 59 | + custom_auth = 'Basic ' + base64.b64encode(f'{client_id}:{user_id}:{hashed_user_id_base64}'.encode()).decode() |
| 60 | + notificationapi.init(client_id, client_secret) |
| 61 | + await getattr(notificationapi, func)(params) |
| 62 | + assert "Authorization" in route.calls.last.request.headers |
| 63 | + assert route.calls.last.request.headers["Authorization"] == custom_auth |
| 64 | + |
| 65 | + |
| 66 | +delete_user_preferences_params = { |
| 67 | + "id": user_id, |
| 68 | + "notificationId": notification_id |
| 69 | +} |
| 70 | + |
| 71 | + |
| 72 | +@pytest.mark.asyncio |
| 73 | +@pytest.mark.parametrize( |
| 74 | + "func,params", |
| 75 | + [ |
| 76 | + ("delete_user_preferences", delete_user_preferences_params), |
| 77 | + ], |
| 78 | +) |
| 79 | +async def test_logs_and_throws_on_500(respx_mock, caplog, func, params): |
| 80 | + respx_mock.delete(api_paths[func]).mock(return_value=Response(500, text="big oof 500")) |
| 81 | + notificationapi.init(client_id, client_secret) |
| 82 | + await getattr(notificationapi, func)(params) |
| 83 | + assert "NotificationAPI request failed. Response: big oof 500" in caplog.text |
0 commit comments