Repository navigation
Expand file tree
/
Copy pathapi-tests.http
More file actions
570 lines (382 loc) · 14.5 KB
/
Copy pathapi-tests.http
File metadata and controls
570 lines (382 loc) · 14.5 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
# ============================================================
# Matatu ETA API — REST Client Tests
# VS Code extension: humao.rest-client
# ============================================================
# Usage:
# 1. Set @baseUrl and credentials in the Variables section.
# 2. Run each request with "Send Request" (click the link above each block).
# 3. Copy tokens from responses into the variables below.
# ============================================================
### ─── Variables ────────────────────────────────────────────
@baseUrl = http://localhost:3000/api
@adminToken = PASTE_ADMIN_ACCESS_TOKEN_HERE
@matAdminToken = PASTE_MAT_ADMIN_ACCESS_TOKEN_HERE
@passengerToken = PASTE_PASSENGER_ACCESS_TOKEN_HERE
@stageId = 1
@vehicleId = 1
@userId = 2
@resetUserId = 1
@resetToken = PASTE_RESET_TOKEN_HERE
@tempToken = PASTE_TEMP_2FA_TOKEN_HERE
# ╔══════════════════════════════════════════════════════════╗
# ║ AUTH ROUTES ║
# ╚══════════════════════════════════════════════════════════╝
### 1. Register a new user (first user becomes Admin)
# POST /api/auth/register
POST {{baseUrl}}/auth/register
Content-Type: application/json
{
"name": "Test User",
"email": "test@example.com",
"password": "password123"
}
###
### 2. Login — returns accessToken + sets refreshToken cookie
# POST /api/auth/login
POST {{baseUrl}}/auth/login
Content-Type: application/json
{
"email": "test@example.com",
"password": "password123"
}
###
### 2b. Get current profile (any role) — preferred over the role-specific routes below
GET {{baseUrl}}/auth/me
Authorization: Bearer {{passengerToken}}
###
### 3. Get current Admin profile
# GET /api/auth/me/admin (requires Admin role)
GET {{baseUrl}}/auth/me/admin
Authorization: Bearer {{adminToken}}
###
### 4. Get current Passenger profile
# GET /api/auth/me/passenger (requires Passenger role)
GET {{baseUrl}}/auth/me/passenger
Authorization: Bearer {{passengerToken}}
###
### 5. Get current MatAdmin profile
# GET /api/auth/me/mat-admin (requires MatAdmin role)
GET {{baseUrl}}/auth/me/mat-admin
Authorization: Bearer {{matAdminToken}}
###
### 6. Update profile (name and / or phone number)
# PUT /api/auth/me (any authenticated role)
PUT {{baseUrl}}/auth/me
Authorization: Bearer {{passengerToken}}
Content-Type: application/json
{
"name": "Updated Name",
"phoneNumber": "0712345678"
}
###
### 7. Refresh access token
# POST /api/auth/refresh-token
# Cookie refreshToken must be present (set automatically after login).
# The expired / current access token is still required in the header.
POST {{baseUrl}}/auth/refresh-token
Authorization: Bearer {{passengerToken}}
###
### 8. Logout
# POST /api/auth/logout (any authenticated role)
POST {{baseUrl}}/auth/logout
Authorization: Bearer {{passengerToken}}
###
### 9. Request password reset email
# POST /api/auth/requestPasswordReset
POST {{baseUrl}}/auth/requestPasswordReset
Content-Type: application/json
{
"email": "test@example.com"
}
###
### 10. Reset password using token from email
# POST /api/auth/resetpassword/:id/:token
POST {{baseUrl}}/auth/resetpassword/{{resetUserId}}/{{resetToken}}
Content-Type: application/json
{
"password": "newpassword123"
}
###
### 11. 2FA — Generate secret & QR code (step 1 of setup)
# POST /api/auth/2fa/setup
POST {{baseUrl}}/auth/2fa/setup
Authorization: Bearer {{passengerToken}}
###
### 12. 2FA — Verify TOTP token to enable 2FA (step 2 of setup)
# POST /api/auth/2fa/verify
POST {{baseUrl}}/auth/2fa/verify
Authorization: Bearer {{passengerToken}}
Content-Type: application/json
{
"token": "123456"
}
###
### 13. 2FA — Disable 2FA
# POST /api/auth/2fa/disable
POST {{baseUrl}}/auth/2fa/disable
Authorization: Bearer {{passengerToken}}
###
### 14. 2FA — Complete login with TOTP code
# POST /api/auth/2fa/login
# tempToken is returned from a normal /login when twoFactorRequired: true
POST {{baseUrl}}/auth/2fa/login
Content-Type: application/json
{
"tempToken": "{{tempToken}}",
"token": "123456"
}
###
### 15. Google OAuth — Initiate sign-in (opens browser redirect)
# GET /api/auth/google
GET {{baseUrl}}/auth/google
# ╔══════════════════════════════════════════════════════════╗
# ║ ETA ROUTES ║
# ╚══════════════════════════════════════════════════════════╝
### 16. Get ETA for a stage (any authenticated role)
# GET /api/eta/:stageId
GET {{baseUrl}}/eta/{{stageId}}
Authorization: Bearer {{passengerToken}}
### 17. Get ETA — stage not found (expect 404)
GET {{baseUrl}}/eta/99999
Authorization: Bearer {{passengerToken}}
# ╔══════════════════════════════════════════════════════════╗
# ║ STAGE ROUTES ║
# ╚══════════════════════════════════════════════════════════╝
### 18. Get all stages (any authenticated role)
# GET /api/stages
GET {{baseUrl}}/stages
Authorization: Bearer {{passengerToken}}
###
### 19. Get subscription status for a stage
# GET /api/stages/:stageId/subscription
GET {{baseUrl}}/stages/{{stageId}}/subscription
Authorization: Bearer {{passengerToken}}
###
### 20. Subscribe to SMS alert for a stage
# POST /api/stages/:stageId/subscribe
POST {{baseUrl}}/stages/{{stageId}}/subscribe
Authorization: Bearer {{passengerToken}}
###
### 21. Unsubscribe from SMS alert for a stage
# POST /api/stages/:stageId/unsubscribe
POST {{baseUrl}}/stages/{{stageId}}/unsubscribe
Authorization: Bearer {{passengerToken}}
# ╔══════════════════════════════════════════════════════════╗
# ║ ADMIN ROUTES ║
# ║ All endpoints require Admin role ║
# ╚══════════════════════════════════════════════════════════╝
### 22. Admin — Get live vehicle dashboard data
# GET /api/admin/dashboard-data
GET {{baseUrl}}/admin/dashboard-data
Authorization: Bearer {{adminToken}}
###
### 23. Admin — Get all users (supports search and role filter)
# GET /api/admin/users
GET {{baseUrl}}/admin/users?q=&role=&limit=25&offset=0
Authorization: Bearer {{adminToken}}
###
### 24. Admin — Create a new Admin or MatAdmin user
# POST /api/admin/users
POST {{baseUrl}}/admin/users
Authorization: Bearer {{adminToken}}
Content-Type: application/json
{
"name": "New Mat Admin",
"email": "matadmin@example.com",
"password": "password123",
"role": "MatAdmin"
}
###
### 25. Admin — Block / unblock a user
# PUT /api/admin/users/:id
PUT {{baseUrl}}/admin/users/{{userId}}
Authorization: Bearer {{adminToken}}
Content-Type: application/json
{
"disabled": true
}
###
### 26. Admin — Delete a user
# DELETE /api/admin/users/:id
DELETE {{baseUrl}}/admin/users/{{userId}}
Authorization: Bearer {{adminToken}}
###
### 27. Admin — Get all feedback (paginated)
# GET /api/admin/feedback
GET {{baseUrl}}/admin/feedback?limit=10&offset=0
Authorization: Bearer {{adminToken}}
###
### 28. Admin — Get all MatAdmins (for dropdown)
# GET /api/admin/mat-admins
GET {{baseUrl}}/admin/mat-admins
Authorization: Bearer {{adminToken}}
###
### 29. Admin — Get all stages (paginated)
# GET /api/admin/stages
GET {{baseUrl}}/admin/stages?limit=25&offset=0
Authorization: Bearer {{adminToken}}
###
### 30. Admin — Create a stage
# POST /api/admin/stages
POST {{baseUrl}}/admin/stages
Authorization: Bearer {{adminToken}}
Content-Type: application/json
{
"name": "Westlands Stage",
"latitude": -1.2672,
"longitude": 36.8062
}
###
### 31. Admin — Get all vehicles (paginated)
# GET /api/admin/vehicles
GET {{baseUrl}}/admin/vehicles?limit=25&offset=0
Authorization: Bearer {{adminToken}}
###
### 32. Admin — Create a vehicle
# POST /api/admin/vehicles
POST {{baseUrl}}/admin/vehicles
Authorization: Bearer {{adminToken}}
Content-Type: application/json
{
"carId": "CAR_001",
"plateNumber": "KBA 001A",
"ownerId": 2
}
###
### 33. Admin — Delete a vehicle
# DELETE /api/admin/vehicles/:id
DELETE {{baseUrl}}/admin/vehicles/{{vehicleId}}
Authorization: Bearer {{adminToken}}
###
### 34. Admin — Assign vehicle to a stage
# POST /api/admin/stages/assign-vehicle
POST {{baseUrl}}/admin/stages/assign-vehicle
Authorization: Bearer {{adminToken}}
Content-Type: application/json
{
"stageId": 1,
"vehicleId": 1
}
###
### 35. Admin — Remove vehicle from a stage
# DELETE /api/admin/stages/:stageId/vehicles/:vehicleId
DELETE {{baseUrl}}/admin/stages/1/vehicles/1
Authorization: Bearer {{adminToken}}
# ╔══════════════════════════════════════════════════════════╗
# ║ FEEDBACK ROUTES ║
# ║ All endpoints require Passenger role ║
# ╚══════════════════════════════════════════════════════════╝
### 36. Passenger — Get all vehicles (for feedback form dropdown)
# GET /api/feedback/vehicles
GET {{baseUrl}}/feedback/vehicles
Authorization: Bearer {{passengerToken}}
###
### 37. Passenger — Submit feedback for a vehicle
# POST /api/feedback
POST {{baseUrl}}/feedback
Authorization: Bearer {{passengerToken}}
Content-Type: application/json
{
"vehicleId": 1,
"rating": 4,
"comment": "Punctual and clean matatu."
}
###
### 38. Feedback — validation error (rating out of range, expect 400)
POST {{baseUrl}}/feedback
Authorization: Bearer {{passengerToken}}
Content-Type: application/json
{
"vehicleId": 1,
"rating": 6,
"comment": "This should fail."
}
# ╔══════════════════════════════════════════════════════════╗
# ║ MAT-ADMIN ROUTES ║
# ║ All endpoints require MatAdmin role ║
# ╚══════════════════════════════════════════════════════════╝
### 39. MatAdmin — Dashboard summary (totals for today)
# GET /api/mat-admin/dashboard-summary
GET {{baseUrl}}/mat-admin/dashboard-summary
Authorization: Bearer {{matAdminToken}}
###
### 40. MatAdmin — Live vehicle status dashboard
# GET /api/mat-admin/dashboard-data
GET {{baseUrl}}/mat-admin/dashboard-data
Authorization: Bearer {{matAdminToken}}
###
### 41. MatAdmin — Vehicle stats with daily distance, trips & feedback
# GET /api/mat-admin/vehicle-stats
GET {{baseUrl}}/mat-admin/vehicle-stats
Authorization: Bearer {{matAdminToken}}
###
### 42. MatAdmin — Fleet aggregate chart data (daily)
# GET /api/mat-admin/fleet-aggregate-data?period=daily|weekly|monthly
GET {{baseUrl}}/mat-admin/fleet-aggregate-data?period=daily
Authorization: Bearer {{matAdminToken}}
###
### 43. MatAdmin — Fleet aggregate chart data (weekly)
GET {{baseUrl}}/mat-admin/fleet-aggregate-data?period=weekly
Authorization: Bearer {{matAdminToken}}
###
### 44. MatAdmin — Fleet aggregate chart data (monthly)
GET {{baseUrl}}/mat-admin/fleet-aggregate-data?period=monthly
Authorization: Bearer {{matAdminToken}}
###
### 45. MatAdmin — Chart data for a specific vehicle (daily)
# GET /api/mat-admin/vehicles/:vehicleId/chart-data?period=daily|weekly|monthly
GET {{baseUrl}}/mat-admin/vehicles/{{vehicleId}}/chart-data?period=daily
Authorization: Bearer {{matAdminToken}}
###
### 46. MatAdmin — Chart data for a specific vehicle (weekly)
GET {{baseUrl}}/mat-admin/vehicles/{{vehicleId}}/chart-data?period=weekly
Authorization: Bearer {{matAdminToken}}
###
### 47. MatAdmin — Chart data for a specific vehicle (monthly)
GET {{baseUrl}}/mat-admin/vehicles/{{vehicleId}}/chart-data?period=monthly
Authorization: Bearer {{matAdminToken}}
###
### 48. MatAdmin — Create a vehicle
# POST /api/mat-admin/vehicles
POST {{baseUrl}}/mat-admin/vehicles
Authorization: Bearer {{matAdminToken}}
Content-Type: application/json
{
"carId": "CAR_MAT_001",
"plateNumber": "KCB 123X"
}
###
### 49. MatAdmin — Get own vehicles
# GET /api/mat-admin/vehicles
GET {{baseUrl}}/mat-admin/vehicles
Authorization: Bearer {{matAdminToken}}
# ╔══════════════════════════════════════════════════════════╗
# ║ ERROR / EDGE CASE TESTS ║
# ╚══════════════════════════════════════════════════════════╝
### 50. No token — expect 401
GET {{baseUrl}}/stages
###
### 51. Wrong role — Passenger hitting Admin endpoint (expect 403)
GET {{baseUrl}}/admin/dashboard-data
Authorization: Bearer {{passengerToken}}
###
### 52. Malformed JSON body — expect 400
POST {{baseUrl}}/auth/login
Content-Type: application/json
{ "email": "bad@example.com", "password":
###
### 53. Invalid token format — expect 400
GET {{baseUrl}}/stages
Authorization: Token invalid_format_here
###
### 54. Expired / tampered token — expect 401
GET {{baseUrl}}/stages
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.tampered.signature
###
### 55. Health check (database reachability) — no auth
GET {{baseUrl}}/health
###
### 56. Run one ETA cycle (what Vercel Cron / a scheduler calls) — needs CRON_SECRET
@cronSecret = PASTE_CRON_SECRET_HERE
GET {{baseUrl}}/cron/eta-cycle
Authorization: Bearer {{cronSecret}}