Skip to content

Commit fe34aa2

Browse files
committed
add delete_user_preferences
1 parent e236bd5 commit fe34aa2

File tree

2 files changed

+96
-2
lines changed

2 files changed

+96
-2
lines changed

notificationapi_python_server_sdk/notificationapi.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def init(client_id, client_secret):
2121
__client_secret = client_secret
2222

2323

24-
async def request(method, uri, data=None, custom_auth=None):
24+
async def request(method, uri, data=None, custom_auth=None, queryStrings=None):
2525
api_url = "https://api.notificationapi.com/" + __client_id + "/" + uri
2626

2727
headers = {}
@@ -34,6 +34,7 @@ async def request(method, uri, data=None, custom_auth=None):
3434
response = await client.request(
3535
method,
3636
api_url,
37+
params=queryStrings,
3738
headers=headers,
3839
json=data,
3940
)
@@ -97,6 +98,17 @@ async def set_user_preferences(params):
9798
)
9899

99100

101+
async def delete_user_preferences(params):
102+
user_id = params.pop('id')
103+
104+
hashed_user_id = hashlib.sha256((__client_secret + user_id).encode()).digest()
105+
hashed_user_id_base64 = base64.b64encode(hashed_user_id).decode()
106+
107+
custom_auth = 'Basic ' + base64.b64encode(f'{__client_id}:{user_id}:{hashed_user_id_base64}'.encode()).decode()
108+
109+
await request('DELETE', f'users/{user_id}/preferences', None, custom_auth, params)
110+
111+
100112
async def identify_user(params):
101113
user_id = params.pop('id')
102114

@@ -114,7 +126,6 @@ async def query_logs(params):
114126

115127

116128
async def update_in_app_notification(user_id, params):
117-
118129
hashed_user_id = hashlib.sha256((__client_secret + user_id).encode()).digest()
119130
hashed_user_id_base64 = base64.b64encode(hashed_user_id).decode()
120131
custom_auth = 'Basic ' + base64.b64encode(f'{__client_id}:{user_id}:{hashed_user_id_base64}'.encode()).decode()
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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

Comments
 (0)