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