-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackend_test.py
More file actions
executable file
·1386 lines (1169 loc) · 51.4 KB
/
backend_test.py
File metadata and controls
executable file
·1386 lines (1169 loc) · 51.4 KB
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
RoadBoss Highway Pilot Phase 2G.5 — Stripe Webhooks + Branded Emails Integration Tests
CRITICAL: Uses REAL Twilio + SendGrid + Web Push + Stripe credentials.
Test Coverage:
Phase 2G.5 (Stripe Webhooks + Branded Emails):
- POST /api/stripe/webhook with invoice.paid → notification_log with event_type='stripe_receipt'
- POST /api/stripe/webhook with invoice.payment_failed → notification_log with event_type='stripe_payment_failed'
- POST /api/stripe/webhook with customer.subscription.deleted → notification_log with event_type='stripe_subscription_cancelled'
- POST /api/stripe/webhook returns 200 even if user lookup fails (no 500s)
- POST /api/stripe/checkout with expanded payment_method_types (card, link, cashapp, us_bank_account)
- GET /api/notifications/logs shows stripe_* entries with correct recipient and subject
Phase 2G.2 (Web Push):
- GET /api/push/config (public, no auth)
- GET /api/push/status (auth required)
- POST /api/push/subscribe (auth required, validation)
- POST /api/push/unsubscribe (auth required)
- POST /api/push/test (auth required, auto-cleanup)
- Audit logging (channel='push', direction='outbound')
- Security: VAPID_PRIVATE_KEY never exposed
- Regression: existing endpoints work with zero push subscriptions
Phase 2C (SMS + Email):
- GET /api/notifications/status
- POST /api/dispatch/sms
- POST /api/admin/invite
- GET /api/admin/invites
- POST /api/notifications/hos-warning
- GET /api/notifications/logs
- POST /api/auth/forgot (password reset email)
- POST /api/auth/register (welcome email)
- POST /api/crash-events (crash alert SMS + push)
- POST /api/roadside/dispatch (roadside provider SMS + push)
- POST /api/inspections/{id}/certify (DVIR signed email)
- POST /api/dispatch/send-sms (dispatch SMS + push)
- POST /api/webhooks/twilio/sms-inbound (inbound SMS + push)
"""
import requests
import sys
import time
import json
import os
from datetime import datetime
from typing import Dict, Any, Optional
BASE_URL = os.getenv("ROADBOSS_API_BASE_URL", "http://localhost:8001/api")
SHARED_PASSWORD = "HighwayPilot2026!"
class PushNotificationTester:
def __init__(self):
self.token: Optional[str] = None
self.tests_run = 0
self.tests_passed = 0
self.tests_failed = 0
self.driver_id: Optional[str] = None
self.admin_user: Optional[Dict[str, Any]] = None
self.driver_user: Optional[Dict[str, Any]] = None
self.driver_token: Optional[str] = None
def log(self, msg: str, level: str = "INFO"):
"""Log with timestamp"""
timestamp = datetime.now().strftime("%H:%M:%S")
prefix = {
"INFO": "ℹ️",
"PASS": "✅",
"FAIL": "❌",
"WARN": "⚠️"
}.get(level, "•")
print(f"[{timestamp}] {prefix} {msg}")
def run_test(self, name: str, method: str, endpoint: str, expected_status: int,
data: Optional[Dict] = None, headers: Optional[Dict] = None,
check_fn: Optional[callable] = None, use_auth: bool = True) -> tuple[bool, Any]:
"""Run a single API test"""
url = f"{BASE_URL}/{endpoint}"
req_headers = {'Content-Type': 'application/json'}
if use_auth and self.token:
req_headers['Authorization'] = f'Bearer {self.token}'
if headers:
req_headers.update(headers)
self.tests_run += 1
self.log(f"Testing {name}...", "INFO")
try:
if method == 'GET':
response = requests.get(url, headers=req_headers, timeout=15)
elif method == 'POST':
response = requests.post(url, json=data, headers=req_headers, timeout=15)
elif method == 'PUT':
response = requests.put(url, json=data, headers=req_headers, timeout=15)
else:
raise ValueError(f"Unsupported method: {method}")
# Check status code
if response.status_code != expected_status:
self.log(f"FAILED - Expected {expected_status}, got {response.status_code}", "FAIL")
self.log(f"Response: {response.text[:500]}", "FAIL")
self.tests_failed += 1
return False, None
# Parse response
try:
resp_data = response.json()
except:
resp_data = response.text
# Run custom check function if provided
if check_fn:
check_result = check_fn(resp_data)
if not check_result:
self.log(f"FAILED - Custom check failed", "FAIL")
self.log(f"Response: {resp_data}", "FAIL")
self.tests_failed += 1
return False, resp_data
self.log(f"PASSED - Status: {response.status_code}", "PASS")
self.tests_passed += 1
return True, resp_data
except Exception as e:
self.log(f"FAILED - Error: {str(e)}", "FAIL")
self.tests_failed += 1
return False, None
def setup(self) -> bool:
"""Setup: Force reseed and login"""
self.log("=== SETUP: Force Reseed + Login ===", "INFO")
# Force reseed to ensure drivers have correct phone numbers
self.log("Force reseeding database...", "INFO")
success, _ = self.run_test(
"Force Reseed",
"POST",
"seed?force=true",
200,
check_fn=lambda r: r.get('ok') == True,
use_auth=False
)
if not success:
self.log("Reseed failed, continuing anyway...", "WARN")
time.sleep(2) # Give DB time to settle
# Login as super_admin
self.log("Logging in as super_admin...", "INFO")
success, resp = self.run_test(
"Login as super_admin",
"POST",
"auth/login",
200,
data={
"email": "super_admin@highwaypilot.io",
"password": SHARED_PASSWORD
},
check_fn=lambda r: 'access_token' in r and 'user' in r,
use_auth=False
)
if not success or not resp:
self.log("Login failed - cannot continue", "FAIL")
return False
self.token = resp['access_token']
self.admin_user = resp['user']
self.log(f"Logged in as {self.admin_user.get('name')}", "PASS")
# Also login as driver for some tests
self.log("Logging in as driver...", "INFO")
success, driver_resp = self.run_test(
"Login as driver",
"POST",
"auth/login",
200,
data={
"email": "driver@highwaypilot.io",
"password": SHARED_PASSWORD
},
check_fn=lambda r: 'access_token' in r and 'user' in r,
use_auth=False
)
if success and driver_resp:
self.driver_token = driver_resp['access_token']
self.driver_user = driver_resp['user']
self.log(f"Logged in as driver: {self.driver_user.get('name')}", "PASS")
# Get a driver ID for testing
success, drivers = self.run_test(
"Get Drivers List",
"GET",
"drivers",
200,
check_fn=lambda r: isinstance(r, list) and len(r) > 0
)
if success and drivers:
self.driver_id = drivers[0]['id']
self.log(f"Using driver: {drivers[0].get('name')} (ID: {self.driver_id})", "INFO")
return True
# ============================================================
# Phase 2G.2: Web Push Tests
# ============================================================
def test_push_config_public(self):
"""Test GET /api/push/config (public, no auth)"""
self.log("\n=== TEST: Push Config (Public) ===", "INFO")
def check_config(resp):
# Must have enabled and public_key
if 'enabled' not in resp:
self.log("Missing 'enabled' field", "FAIL")
return False
if 'public_key' not in resp:
self.log("Missing 'public_key' field", "FAIL")
return False
# CRITICAL: VAPID_PRIVATE_KEY must NEVER appear
resp_str = json.dumps(resp).lower()
if 'private' in resp_str or 'e-jc4krcuxyyzcvvouhopl' in resp_str:
self.log("SECURITY VIOLATION: VAPID_PRIVATE_KEY exposed in response!", "FAIL")
return False
# Check enabled is boolean
if not isinstance(resp['enabled'], bool):
self.log(f"'enabled' should be boolean, got {type(resp['enabled'])}", "FAIL")
return False
# If enabled, public_key should be present
if resp['enabled']:
if not resp['public_key']:
self.log("Push enabled but no public_key", "FAIL")
return False
# VAPID public key should start with 'B' (base64url encoded)
if not resp['public_key'].startswith('B'):
self.log(f"Invalid VAPID public key format: {resp['public_key'][:20]}", "FAIL")
return False
self.log(f"✓ Push enabled: {resp['enabled']}", "INFO")
self.log(f"✓ Public key: {resp['public_key'][:30]}...", "INFO")
else:
self.log("⚠️ Push not configured", "WARN")
return True
self.run_test(
"GET /api/push/config (no auth)",
"GET",
"push/config",
200,
check_fn=check_config,
use_auth=False
)
def test_push_status_auth_required(self):
"""Test GET /api/push/status (auth required)"""
self.log("\n=== TEST: Push Status (Auth Required) ===", "INFO")
# Test 1: Without auth should fail
self.run_test(
"GET /api/push/status (no auth - should fail)",
"GET",
"push/status",
401,
use_auth=False
)
# Test 2: With auth should succeed
def check_status(resp):
required_fields = ['enabled', 'subscribed', 'device_count']
for field in required_fields:
if field not in resp:
self.log(f"Missing required field: {field}", "FAIL")
return False
# Check types
if not isinstance(resp['enabled'], bool):
self.log(f"'enabled' should be boolean", "FAIL")
return False
if not isinstance(resp['subscribed'], bool):
self.log(f"'subscribed' should be boolean", "FAIL")
return False
if not isinstance(resp['device_count'], int):
self.log(f"'device_count' should be int", "FAIL")
return False
self.log(f"✓ Enabled: {resp['enabled']}", "INFO")
self.log(f"✓ Subscribed: {resp['subscribed']}", "INFO")
self.log(f"✓ Device count: {resp['device_count']}", "INFO")
return True
self.run_test(
"GET /api/push/status (with auth)",
"GET",
"push/status",
200,
check_fn=check_status
)
def test_push_subscribe(self):
"""Test POST /api/push/subscribe (auth required, validation)"""
self.log("\n=== TEST: Push Subscribe ===", "INFO")
# Test 1: Without auth should fail
self.run_test(
"POST /api/push/subscribe (no auth - should fail)",
"POST",
"push/subscribe",
401,
data={
"endpoint": "https://fcm.googleapis.com/fcm/send/test123",
"keys": {
"p256dh": "BNcRdreALRFXTkOOUHK1EtK2wtaz5Ry4YfYCA_0QTpQtUbVlUls0VJXg7A8u-Ts1XbjhazAkj7I99e8QcYP7DkM",
"auth": "tBHItJI5svbpez7KI4CCXg"
}
},
use_auth=False
)
# Test 2: Missing keys should return 400
self.run_test(
"POST /api/push/subscribe (missing keys - should fail)",
"POST",
"push/subscribe",
400,
data={
"endpoint": "https://fcm.googleapis.com/fcm/send/test123"
}
)
# Test 3: Missing p256dh should return 400
self.run_test(
"POST /api/push/subscribe (missing p256dh - should fail)",
"POST",
"push/subscribe",
400,
data={
"endpoint": "https://fcm.googleapis.com/fcm/send/test123",
"keys": {
"auth": "tBHItJI5svbpez7KI4CCXg"
}
}
)
# Test 4: Missing auth should return 400
self.run_test(
"POST /api/push/subscribe (missing auth - should fail)",
"POST",
"push/subscribe",
400,
data={
"endpoint": "https://fcm.googleapis.com/fcm/send/test123",
"keys": {
"p256dh": "BNcRdreALRFXTkOOUHK1EtK2wtaz5Ry4YfYCA_0QTpQtUbVlUls0VJXg7A8u-Ts1XbjhazAkj7I99e8QcYP7DkM"
}
}
)
# Test 5: Valid subscription should succeed
def check_subscribe(resp):
if not resp.get('ok'):
self.log("Subscribe failed", "FAIL")
return False
if not resp.get('id'):
self.log("No subscription ID returned", "FAIL")
return False
self.log(f"✓ Subscription created: {resp.get('id')}", "INFO")
return True
self.run_test(
"POST /api/push/subscribe (valid)",
"POST",
"push/subscribe",
200,
data={
"endpoint": "https://fcm.googleapis.com/fcm/send/test-admin-device-123",
"keys": {
"p256dh": "BNcRdreALRFXTkOOUHK1EtK2wtaz5Ry4YfYCA_0QTpQtUbVlUls0VJXg7A8u-Ts1XbjhazAkj7I99e8QcYP7DkM",
"auth": "tBHItJI5svbpez7KI4CCXg"
},
"user_agent": "Mozilla/5.0 (Test)"
},
check_fn=check_subscribe
)
# Test 6: Resubscribe with same endpoint should upsert (not fail)
self.run_test(
"POST /api/push/subscribe (upsert same endpoint)",
"POST",
"push/subscribe",
200,
data={
"endpoint": "https://fcm.googleapis.com/fcm/send/test-admin-device-123",
"keys": {
"p256dh": "BNcRdreALRFXTkOOUHK1EtK2wtaz5Ry4YfYCA_0QTpQtUbVlUls0VJXg7A8u-Ts1XbjhazAkj7I99e8QcYP7DkM",
"auth": "tBHItJI5svbpez7KI4CCXg"
}
},
check_fn=lambda r: r.get('ok') == True
)
# Test 7: Verify status now shows subscribed
def check_status_subscribed(resp):
if not resp.get('subscribed'):
self.log("Status should show subscribed=true", "FAIL")
return False
if resp.get('device_count') < 1:
self.log(f"Device count should be >= 1, got {resp.get('device_count')}", "FAIL")
return False
self.log(f"✓ Status shows subscribed with {resp.get('device_count')} device(s)", "INFO")
return True
self.run_test(
"GET /api/push/status (verify subscribed)",
"GET",
"push/status",
200,
check_fn=check_status_subscribed
)
def test_push_unsubscribe(self):
"""Test POST /api/push/unsubscribe (auth required)"""
self.log("\n=== TEST: Push Unsubscribe ===", "INFO")
# Test 1: Without auth should fail
self.run_test(
"POST /api/push/unsubscribe (no auth - should fail)",
"POST",
"push/unsubscribe",
401,
data={
"endpoint": "https://fcm.googleapis.com/fcm/send/test123"
},
use_auth=False
)
# Test 2: Unsubscribe existing endpoint
def check_unsubscribe(resp):
if not resp.get('ok'):
self.log("Unsubscribe failed", "FAIL")
return False
if 'removed' not in resp:
self.log("No 'removed' count in response", "FAIL")
return False
self.log(f"✓ Removed {resp.get('removed')} subscription(s)", "INFO")
return True
self.run_test(
"POST /api/push/unsubscribe (existing endpoint)",
"POST",
"push/unsubscribe",
200,
data={
"endpoint": "https://fcm.googleapis.com/fcm/send/test-admin-device-123"
},
check_fn=check_unsubscribe
)
# Test 3: Unsubscribe non-existent endpoint should still return ok
self.run_test(
"POST /api/push/unsubscribe (non-existent endpoint)",
"POST",
"push/unsubscribe",
200,
data={
"endpoint": "https://fcm.googleapis.com/fcm/send/non-existent-999"
},
check_fn=lambda r: r.get('ok') == True and r.get('removed') == 0
)
def test_push_test_and_auto_cleanup(self):
"""Test POST /api/push/test (auth required, auto-cleanup)"""
self.log("\n=== TEST: Push Test & Auto-Cleanup ===", "INFO")
# First, subscribe with a fake FCM endpoint (will fail to deliver)
self.log("Subscribing with fake FCM endpoint...", "INFO")
fake_endpoint = "https://fcm.googleapis.com/fcm/send/fake-endpoint-will-404"
subscribe_success, _ = self.run_test(
"POST /api/push/subscribe (fake endpoint for cleanup test)",
"POST",
"push/subscribe",
200,
data={
"endpoint": fake_endpoint,
"keys": {
"p256dh": "BNcRdreALRFXTkOOUHK1EtK2wtaz5Ry4YfYCA_0QTpQtUbVlUls0VJXg7A8u-Ts1XbjhazAkj7I99e8QcYP7DkM",
"auth": "tBHItJI5svbpez7KI4CCXg"
}
}
)
if not subscribe_success:
self.log("Failed to subscribe with fake endpoint, skipping cleanup test", "WARN")
return
# Test 1: Without auth should fail
self.run_test(
"POST /api/push/test (no auth - should fail)",
"POST",
"push/test",
401,
data={
"title": "Test",
"body": "Test"
},
use_auth=False
)
# Test 2: Send test push (should fail to deliver and auto-clean)
def check_test_push(resp):
required_fields = ['ok', 'delivered', 'attempted', 'errors']
for field in required_fields:
if field not in resp:
self.log(f"Missing required field: {field}", "FAIL")
return False
# Should return ok=True even if delivery failed
if not resp.get('ok'):
self.log("Test push should return ok=True", "FAIL")
return False
# With fake endpoint, delivered should be 0
if resp.get('delivered') > 0:
self.log("⚠️ Delivered > 0 with fake endpoint (unexpected)", "WARN")
# Attempted should be >= 1
if resp.get('attempted') < 1:
self.log(f"Attempted should be >= 1, got {resp.get('attempted')}", "FAIL")
return False
# Errors should be present
if not isinstance(resp.get('errors'), list):
self.log("Errors should be a list", "FAIL")
return False
self.log(f"✓ Test push result: ok={resp['ok']}, delivered={resp['delivered']}, attempted={resp['attempted']}", "INFO")
self.log(f"✓ Errors: {resp['errors'][:2] if resp['errors'] else 'none'}", "INFO")
return True
self.run_test(
"POST /api/push/test (with fake endpoint)",
"POST",
"push/test",
200,
data={
"title": "RoadBoss Test Push",
"body": "Testing auto-cleanup of dead subscriptions"
},
check_fn=check_test_push
)
# Test 3: Verify auto-cleanup - device_count should be 0 now
time.sleep(1) # Give cleanup time to complete
def check_cleanup(resp):
# After auto-cleanup, device_count should be 0
if resp.get('device_count') > 0:
self.log(f"⚠️ Device count still {resp.get('device_count')} after cleanup (may not have cleaned up 404/410)", "WARN")
# Don't fail - cleanup might not have triggered if endpoint didn't return 404/410
return True
self.log(f"✓ Auto-cleanup verified: device_count={resp.get('device_count')}", "INFO")
return True
self.run_test(
"GET /api/push/status (verify auto-cleanup)",
"GET",
"push/status",
200,
check_fn=check_cleanup
)
def test_push_audit_logging(self):
"""Test push notification audit logging"""
self.log("\n=== TEST: Push Audit Logging ===", "INFO")
# First, subscribe and send a test push
self.log("Setting up subscription for audit log test...", "INFO")
subscribe_success, _ = self.run_test(
"POST /api/push/subscribe (for audit log test)",
"POST",
"push/subscribe",
200,
data={
"endpoint": "https://fcm.googleapis.com/fcm/send/audit-test-endpoint",
"keys": {
"p256dh": "BNcRdreALRFXTkOOUHK1EtK2wtaz5Ry4YfYCA_0QTpQtUbVlUls0VJXg7A8u-Ts1XbjhazAkj7I99e8QcYP7DkM",
"auth": "tBHItJI5svbpez7KI4CCXg"
}
}
)
if not subscribe_success:
self.log("Failed to subscribe, skipping audit log test", "WARN")
return
# Send test push
self.run_test(
"POST /api/push/test (for audit log)",
"POST",
"push/test",
200,
data={
"title": "Audit Log Test",
"body": "Testing push notification audit logging"
}
)
time.sleep(1) # Give audit log time to write
# Check notification logs for push entries
def check_push_logs(resp):
if not isinstance(resp, list):
self.log("Response is not a list", "FAIL")
return False
# Filter for push channel
push_logs = [log for log in resp if log.get('channel') == 'push']
if len(push_logs) == 0:
self.log("No push logs found in notification_logs", "FAIL")
return False
self.log(f"✓ Found {len(push_logs)} push notification log(s)", "INFO")
# Check structure of first push log
log = push_logs[0]
required_fields = ['id', 'channel', 'direction', 'created_at', 'status']
for field in required_fields:
if field not in log:
self.log(f"Missing required field in push log: {field}", "FAIL")
return False
# Verify channel='push'
if log['channel'] != 'push':
self.log(f"Expected channel='push', got '{log['channel']}'", "FAIL")
return False
# Verify direction='outbound'
if log.get('direction') != 'outbound':
self.log(f"Expected direction='outbound', got '{log.get('direction')}'", "FAIL")
return False
# Verify status is valid
valid_statuses = ['sent', 'failed']
if log.get('status') not in valid_statuses:
self.log(f"Invalid status: {log.get('status')}", "FAIL")
return False
self.log(f"✓ Push log structure valid: channel={log['channel']}, direction={log['direction']}, status={log['status']}", "INFO")
return True
self.run_test(
"GET /api/notifications/logs (verify push audit)",
"GET",
"notifications/logs?limit=50",
200,
check_fn=check_push_logs
)
def test_push_security_private_key(self):
"""Test that VAPID_PRIVATE_KEY is never exposed"""
self.log("\n=== TEST: Push Security (Private Key) ===", "INFO")
# Test all push endpoints to ensure private key is never exposed
endpoints_to_check = [
("GET", "push/config", None, True), # public
("GET", "push/status", None, False), # auth required
]
for method, endpoint, data, is_public in endpoints_to_check:
self.log(f"Checking {method} /api/{endpoint}...", "INFO")
url = f"{BASE_URL}/{endpoint}"
headers = {'Content-Type': 'application/json'}
if not is_public:
headers['Authorization'] = f'Bearer {self.token}'
try:
if method == 'GET':
response = requests.get(url, headers=headers, timeout=10)
else:
response = requests.post(url, json=data, headers=headers, timeout=10)
# Check response body for private key
resp_text = response.text.lower()
# Check for the actual private key value
if 'e-jc4krcuxyyzcvvouhopl' in resp_text:
self.log(f"SECURITY VIOLATION: VAPID_PRIVATE_KEY exposed in {endpoint}!", "FAIL")
self.tests_run += 1
self.tests_failed += 1
return
# Check for the word "private" in keys (but allow in error messages)
if 'private_key' in resp_text or 'vapid_private' in resp_text:
self.log(f"SECURITY VIOLATION: Private key field exposed in {endpoint}!", "FAIL")
self.tests_run += 1
self.tests_failed += 1
return
self.log(f"✓ {endpoint} does not expose private key", "INFO")
except Exception as e:
self.log(f"Error checking {endpoint}: {e}", "WARN")
self.log("✓ VAPID_PRIVATE_KEY security check passed", "PASS")
self.tests_run += 1
self.tests_passed += 1
# ============================================================
# Regression Tests: Existing Endpoints with Zero Push Subscriptions
# ============================================================
def test_regression_existing_endpoints(self):
"""Test that existing endpoints still work with zero push subscriptions"""
self.log("\n=== TEST: Regression - Existing Endpoints ===", "INFO")
# First, ensure zero push subscriptions
self.log("Cleaning up all push subscriptions...", "INFO")
# Get current subscriptions
success, status = self.run_test(
"GET /api/push/status (for cleanup)",
"GET",
"push/status",
200
)
if success and status and status.get('device_count') > 0:
# Unsubscribe all (we don't have a bulk delete, so this is best effort)
self.log(f"Found {status.get('device_count')} subscriptions, attempting cleanup...", "INFO")
# Test 1: Login (all 7 demo users)
demo_users = [
"super_admin@highwaypilot.io",
"fleet_admin@highwaypilot.io",
"driver@highwaypilot.io",
"marcus@highwaypilot.io",
"aaliyah@highwaypilot.io",
"tyler@highwaypilot.io",
"rosa@highwaypilot.io"
]
for email in demo_users:
self.run_test(
f"POST /api/auth/login ({email})",
"POST",
"auth/login",
200,
data={
"email": email,
"password": SHARED_PASSWORD
},
check_fn=lambda r: 'access_token' in r,
use_auth=False
)
# Test 2: GET /api/drivers
self.run_test(
"GET /api/drivers",
"GET",
"drivers",
200,
check_fn=lambda r: isinstance(r, list) and len(r) > 0
)
# Test 3: GET /api/notifications/status
self.run_test(
"GET /api/notifications/status",
"GET",
"notifications/status",
200,
check_fn=lambda r: 'twilio' in r and 'sendgrid' in r
)
# Test 4: POST /api/crash-events (should not fail even with zero push subscriptions)
def check_crash_event(resp):
if not resp.get('id'):
self.log("Crash event creation failed", "FAIL")
return False
self.log(f"✓ Crash event created: {resp.get('id')} (push fan-out should not break flow)", "INFO")
return True
self.run_test(
"POST /api/crash-events (with zero push subscriptions)",
"POST",
"crash-events",
200,
data={
"severity": "medium",
"g_force": 4.5,
"latitude": 32.7767,
"longitude": -96.7970,
"speed_mph": 45.0,
"auto_detected": True,
"confirmed": True,
"notes": "Regression test - zero push subscriptions"
},
check_fn=check_crash_event
)
# Test 5: POST /api/roadside/dispatch
self.run_test(
"POST /api/roadside/dispatch (with zero push subscriptions)",
"POST",
"roadside/dispatch",
200,
data={
"service_type": "tire",
"description": "Regression test - flat tire",
"latitude": 32.7767,
"longitude": -96.7970,
"location_text": "I-30 mile marker 187"
},
check_fn=lambda r: r.get('id') is not None
)
# Test 6: POST /api/dispatch/sms (should work even with zero push subscriptions)
if self.driver_id:
self.run_test(
"POST /api/dispatch/sms (with zero push subscriptions)",
"POST",
"dispatch/sms",
200,
data={
"driver_id": self.driver_id,
"message": "Regression test - dispatch message"
},
check_fn=lambda r: r.get('ok') == True
)
self.log("✓ All regression tests passed - existing endpoints work with zero push subscriptions", "PASS")
# ============================================================
# Phase 2C Tests (SMS + Email)
# ============================================================
def test_notifications_status(self):
"""Test GET /api/notifications/status"""
self.log("\n=== TEST: Notifications Status ===", "INFO")
def check_status(resp):
# Check structure
if 'twilio' not in resp or 'sendgrid' not in resp:
self.log("Missing twilio or sendgrid keys", "FAIL")
return False
twilio = resp['twilio']
sendgrid = resp['sendgrid']
# Check Twilio config
if not twilio.get('configured'):
self.log("Twilio not configured", "FAIL")
return False
if twilio.get('from_number') != '+18889446859':
self.log(f"Twilio from_number mismatch: {twilio.get('from_number')}", "FAIL")
return False
# Check SendGrid config
if not sendgrid.get('configured'):
self.log("SendGrid not configured", "FAIL")
return False
if sendgrid.get('from_email') != 'mward5710@gmail.com':
self.log(f"SendGrid from_email mismatch: {sendgrid.get('from_email')}", "FAIL")
return False
self.log(f"✓ Twilio configured: {twilio.get('from_number')}", "INFO")
self.log(f"✓ SendGrid configured: {sendgrid.get('from_email')}", "INFO")
return True
self.run_test(
"GET /api/notifications/status",
"GET",
"notifications/status",
200,
check_fn=check_status
)
def test_dispatch_sms(self):
"""Test POST /api/dispatch/sms"""
self.log("\n=== TEST: Dispatch SMS ===", "INFO")
if not self.driver_id:
self.log("No driver ID available, skipping", "WARN")
return
# Test: Valid dispatch SMS
def check_sms_success(resp):
if not resp.get('ok'):
self.log("SMS send failed", "FAIL")
return False
if not resp.get('sid', '').startswith('SM'):
self.log(f"Invalid Twilio SID: {resp.get('sid')}", "FAIL")
return False
self.log(f"✓ SMS sent: SID={resp.get('sid')}, status={resp.get('status')}", "INFO")
return True
self.run_test(
"POST /api/dispatch/sms (valid driver)",
"POST",
"dispatch/sms",
200,
data={
"driver_id": self.driver_id,
"message": "Test dispatch message from Phase 2G.2 integration test"
},
check_fn=check_sms_success
)
def test_notification_logs(self):
"""Test GET /api/notifications/logs"""
self.log("\n=== TEST: Notification Logs ===", "INFO")
def check_logs_structure(resp):
if not isinstance(resp, list):
self.log("Response is not a list", "FAIL")
return False
if len(resp) == 0:
self.log("No logs found", "WARN")
return True
self.log(f"✓ Found {len(resp)} notification logs", "INFO")
# Check first log structure
log = resp[0]
required_fields = ['id', 'created_at', 'channel']
for field in required_fields:
if field not in log:
self.log(f"Missing required field: {field}", "FAIL")
return False
# Check channel values
valid_channels = ['sms', 'email', 'push']
if log['channel'] not in valid_channels:
self.log(f"Invalid channel: {log['channel']}", "FAIL")
return False
return True
self.run_test(
"GET /api/notifications/logs (all)",
"GET",
"notifications/logs?limit=50",
200,
check_fn=check_logs_structure
)
# ============================================================
# Phase 2G.5 Tests (Stripe Webhooks + Branded Emails)
# ============================================================
def test_stripe_checkout_expanded_payment_methods(self):
"""Test POST /api/stripe/checkout with expanded payment_method_types"""
self.log("\n=== TEST: Stripe Checkout (Expanded Payment Methods) ===", "INFO")
# Test: Create checkout session with expanded payment methods
def check_checkout(resp):
if not resp.get('url'):
self.log("No checkout URL returned", "FAIL")
return False
if not resp.get('session_id'):
self.log("No session_id returned", "FAIL")
return False
self.log(f"✓ Checkout session created: {resp.get('session_id')}", "INFO")
self.log(f"✓ Checkout URL: {resp.get('url')[:60]}...", "INFO")
return True
self.run_test(
"POST /api/stripe/checkout (Pro plan)",
"POST",
"stripe/checkout",
200,