Michael-Antony commited on
Commit
4a17f3c
Β·
1 Parent(s): 80b4a42

test: add test script for notifications register endpoint

Browse files

- Add comprehensive test suite for POST /notifications/register
- Test iOS and Android token registration
- Test token update functionality
- Test validation for invalid platform
- Test authentication requirement
- Include usage instructions and test summary

Files changed (1) hide show
  1. test_notifications.py +241 -0
test_notifications.py ADDED
@@ -0,0 +1,241 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Test script for POST /notifications/register endpoint.
3
+ Tests push token registration with JWT authentication.
4
+
5
+ Usage:
6
+ python test_notifications.py <JWT_TOKEN>
7
+ """
8
+ import sys
9
+ import asyncio
10
+ import httpx
11
+
12
+
13
+ BASE_URL = "http://localhost:8003"
14
+ ENDPOINT = "/tracker/notifications/register"
15
+
16
+
17
+ async def test_register_ios_token(token: str):
18
+ """Test registering iOS push token"""
19
+ print("\n" + "="*60)
20
+ print("Test 1: Register iOS Push Token")
21
+ print("="*60)
22
+
23
+ payload = {
24
+ "token": "ExponentPushToken[xxxxxxxxxxxxxxxxxxxxxx]",
25
+ "platform": "ios"
26
+ }
27
+
28
+ try:
29
+ async with httpx.AsyncClient() as client:
30
+ response = await client.post(
31
+ f"{BASE_URL}{ENDPOINT}",
32
+ json=payload,
33
+ headers={
34
+ "Authorization": f"Bearer {token}",
35
+ "Content-Type": "application/json"
36
+ },
37
+ timeout=10.0
38
+ )
39
+
40
+ print(f"Status Code: {response.status_code}")
41
+ print(f"Response: {response.json()}")
42
+
43
+ if response.status_code == 200:
44
+ print("βœ“ Test 1 PASSED")
45
+ return True
46
+ else:
47
+ print("βœ— Test 1 FAILED")
48
+ return False
49
+
50
+ except Exception as e:
51
+ print(f"βœ— Test 1 FAILED: {str(e)}")
52
+ return False
53
+
54
+
55
+ async def test_register_android_token(token: str):
56
+ """Test registering Android push token"""
57
+ print("\n" + "="*60)
58
+ print("Test 2: Register Android Push Token")
59
+ print("="*60)
60
+
61
+ payload = {
62
+ "token": "fcm_token_xxxxxxxxxxxxxxxxxxxxxxxxxx",
63
+ "platform": "android"
64
+ }
65
+
66
+ try:
67
+ async with httpx.AsyncClient() as client:
68
+ response = await client.post(
69
+ f"{BASE_URL}{ENDPOINT}",
70
+ json=payload,
71
+ headers={
72
+ "Authorization": f"Bearer {token}",
73
+ "Content-Type": "application/json"
74
+ },
75
+ timeout=10.0
76
+ )
77
+
78
+ print(f"Status Code: {response.status_code}")
79
+ print(f"Response: {response.json()}")
80
+
81
+ if response.status_code == 200:
82
+ print("βœ“ Test 2 PASSED")
83
+ return True
84
+ else:
85
+ print("βœ— Test 2 FAILED")
86
+ return False
87
+
88
+ except Exception as e:
89
+ print(f"βœ— Test 2 FAILED: {str(e)}")
90
+ return False
91
+
92
+
93
+ async def test_update_token(token: str):
94
+ """Test updating existing token"""
95
+ print("\n" + "="*60)
96
+ print("Test 3: Update Existing Token")
97
+ print("="*60)
98
+
99
+ payload = {
100
+ "token": "ExponentPushToken[yyyyyyyyyyyyyyyyyyyyyy]",
101
+ "platform": "ios"
102
+ }
103
+
104
+ try:
105
+ async with httpx.AsyncClient() as client:
106
+ response = await client.post(
107
+ f"{BASE_URL}{ENDPOINT}",
108
+ json=payload,
109
+ headers={
110
+ "Authorization": f"Bearer {token}",
111
+ "Content-Type": "application/json"
112
+ },
113
+ timeout=10.0
114
+ )
115
+
116
+ print(f"Status Code: {response.status_code}")
117
+ print(f"Response: {response.json()}")
118
+
119
+ if response.status_code == 200:
120
+ print("βœ“ Test 3 PASSED")
121
+ return True
122
+ else:
123
+ print("βœ— Test 3 FAILED")
124
+ return False
125
+
126
+ except Exception as e:
127
+ print(f"βœ— Test 3 FAILED: {str(e)}")
128
+ return False
129
+
130
+
131
+ async def test_invalid_platform(token: str):
132
+ """Test with invalid platform"""
133
+ print("\n" + "="*60)
134
+ print("Test 4: Invalid Platform (Should fail)")
135
+ print("="*60)
136
+
137
+ payload = {
138
+ "token": "ExponentPushToken[xxxxxxxxxxxxxxxxxxxxxx]",
139
+ "platform": "windows"
140
+ }
141
+
142
+ try:
143
+ async with httpx.AsyncClient() as client:
144
+ response = await client.post(
145
+ f"{BASE_URL}{ENDPOINT}",
146
+ json=payload,
147
+ headers={
148
+ "Authorization": f"Bearer {token}",
149
+ "Content-Type": "application/json"
150
+ },
151
+ timeout=10.0
152
+ )
153
+
154
+ print(f"Status Code: {response.status_code}")
155
+ print(f"Response: {response.text}")
156
+
157
+ if response.status_code == 422:
158
+ print("βœ“ Test 4 PASSED")
159
+ return True
160
+ else:
161
+ print("βœ— Test 4 FAILED")
162
+ return False
163
+
164
+ except Exception as e:
165
+ print(f"βœ— Test 4 FAILED: {str(e)}")
166
+ return False
167
+
168
+
169
+ async def test_missing_token_auth(token: str):
170
+ """Test without JWT token"""
171
+ print("\n" + "="*60)
172
+ print("Test 5: Missing JWT Token (Should fail)")
173
+ print("="*60)
174
+
175
+ payload = {
176
+ "token": "ExponentPushToken[xxxxxxxxxxxxxxxxxxxxxx]",
177
+ "platform": "ios"
178
+ }
179
+
180
+ try:
181
+ async with httpx.AsyncClient() as client:
182
+ response = await client.post(
183
+ f"{BASE_URL}{ENDPOINT}",
184
+ json=payload,
185
+ headers={"Content-Type": "application/json"},
186
+ timeout=10.0
187
+ )
188
+
189
+ print(f"Status Code: {response.status_code}")
190
+ print(f"Response: {response.text}")
191
+
192
+ if response.status_code in [401, 403]:
193
+ print("βœ“ Test 5 PASSED")
194
+ return True
195
+ else:
196
+ print("βœ— Test 5 FAILED")
197
+ return False
198
+
199
+ except Exception as e:
200
+ print(f"βœ— Test 5 FAILED: {str(e)}")
201
+ return False
202
+
203
+
204
+ async def run_all_tests(token: str):
205
+ """Run all test cases"""
206
+ print("="*60)
207
+ print("POST /notifications/register - Test Suite")
208
+ print("="*60)
209
+
210
+ results = []
211
+ results.append(await test_register_ios_token(token))
212
+ results.append(await test_register_android_token(token))
213
+ results.append(await test_update_token(token))
214
+ results.append(await test_invalid_platform(token))
215
+ results.append(await test_missing_token_auth(token))
216
+
217
+ print("\n" + "="*60)
218
+ print("Test Summary")
219
+ print("="*60)
220
+ passed = sum(results)
221
+ total = len(results)
222
+ print(f"Tests Passed: {passed}/{total}")
223
+
224
+ if passed == total:
225
+ print("βœ… All tests passed!")
226
+ else:
227
+ print("❌ Some tests failed")
228
+
229
+
230
+ def main():
231
+ if len(sys.argv) < 2:
232
+ print("Usage: python test_notifications.py <JWT_TOKEN>")
233
+ print("\nGenerate token with: python generate_test_token.py")
234
+ sys.exit(1)
235
+
236
+ token = sys.argv[1]
237
+ asyncio.run(run_all_tests(token))
238
+
239
+
240
+ if __name__ == "__main__":
241
+ main()