-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest-evowner-booking-integration.ps1
More file actions
339 lines (306 loc) · 13.8 KB
/
Copy pathtest-evowner-booking-integration.ps1
File metadata and controls
339 lines (306 loc) · 13.8 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
# Comprehensive EV Owner-Booking Integration Test Script
$baseUrl = "http://localhost:5067"
Write-Host "🚗⚡ Testing EV Owner-Booking Integration" -ForegroundColor Green
Write-Host "=====================================`n" -ForegroundColor Green
# Global variables to store tokens and IDs
$evOwnerToken = ""
$systemUserToken = ""
$stationId = ""
$bookingId = ""
# Function to make API calls with error handling
function Invoke-ApiCall {
param(
[string]$Method,
[string]$Uri,
[string]$Body = "",
[hashtable]$Headers = @{}
)
try {
if ($Body -ne "") {
$Headers["Content-Type"] = "application/json"
$response = Invoke-RestMethod -Uri $Uri -Method $Method -Body $Body -Headers $Headers
} else {
$response = Invoke-RestMethod -Uri $Uri -Method $Method -Headers $Headers
}
return @{ Success = $true; Data = $response; Error = $null }
}
catch {
$errorMessage = $_.Exception.Message
if ($_.ErrorDetails) {
$errorMessage += " - " + $_.ErrorDetails.Message
}
return @{ Success = $false; Data = $null; Error = $errorMessage }
}
}
# Test 1: Initialize Database
Write-Host "1️⃣ Initializing Database..." -ForegroundColor Yellow
$result = Invoke-ApiCall -Method "POST" -Uri "$baseUrl/api/Database/initialize" -Body "{}"
if ($result.Success) {
Write-Host "✅ Database initialized successfully!" -ForegroundColor Green
} else {
Write-Host "❌ Database initialization failed: $($result.Error)" -ForegroundColor Red
exit 1
}
# Test 2: Create a Charging Station (as system admin)
Write-Host "`n2️⃣ Creating Test Charging Station..." -ForegroundColor Yellow
$newStation = @{
name = "Integration Test Station"
location = "Test City, Sri Lanka"
type = "Fast DC"
availableSlots = 5
isActive = $true
} | ConvertTo-Json
$result = Invoke-ApiCall -Method "POST" -Uri "$baseUrl/api/ChargingStations" -Body $newStation
if ($result.Success) {
$stationId = $result.Data.id
Write-Host "✅ Charging station created! ID: $stationId" -ForegroundColor Green
} else {
Write-Host "❌ Failed to create charging station: $($result.Error)" -ForegroundColor Red
exit 1
}
# Test 3: Register EV Owner
Write-Host "`n3️⃣ Registering EV Owner..." -ForegroundColor Yellow
$evOwnerRegistration = @{
nic = "199812345678"
firstName = "Integration"
lastName = "Test"
email = "integration.test@example.com"
password = "testpassword123"
phoneNumber = "+94777888999"
address = "123 Test Street, Colombo"
dateOfBirth = "1998-05-15T00:00:00Z"
licenseNumber = "DL19981234"
vehicleDetails = @(
@{
vehicleNumber = "TEST-2024"
vehicleModel = "Tesla Model 3"
vehicleBrand = "Tesla"
batteryCapacity = 75.0
chargingType = "Both"
isActive = $true
},
@{
vehicleNumber = "LEAF-2023"
vehicleModel = "Nissan Leaf"
vehicleBrand = "Nissan"
batteryCapacity = 62.0
chargingType = "AC"
isActive = $true
}
)
} | ConvertTo-Json -Depth 10
$result = Invoke-ApiCall -Method "POST" -Uri "$baseUrl/api/owners/auth/register" -Body $evOwnerRegistration
if ($result.Success) {
$evOwnerToken = $result.Data.token
Write-Host "✅ EV Owner registered! Token received." -ForegroundColor Green
Write-Host " NIC: $($result.Data.nic)" -ForegroundColor Cyan
Write-Host " Name: $($result.Data.fullName)" -ForegroundColor Cyan
} else {
Write-Host "❌ EV Owner registration failed: $($result.Error)" -ForegroundColor Red
exit 1
}
# Test 4: Login EV Owner (alternative flow)
Write-Host "`n4️⃣ Testing EV Owner Login..." -ForegroundColor Yellow
$loginRequest = @{
nic = "199812345678"
password = "testpassword123"
} | ConvertTo-Json
$result = Invoke-ApiCall -Method "POST" -Uri "$baseUrl/api/owners/auth/login" -Body $loginRequest
if ($result.Success) {
Write-Host "✅ EV Owner login successful!" -ForegroundColor Green
Write-Host " Alternative token received: $($result.Data.token.Substring(0,20))..." -ForegroundColor Cyan
} else {
Write-Host "❌ EV Owner login failed: $($result.Error)" -ForegroundColor Red
}
# Test 5: Get EV Owner Profile
Write-Host "`n5️⃣ Getting EV Owner Profile..." -ForegroundColor Yellow
$headers = @{ "Authorization" = "Bearer $evOwnerToken" }
$result = Invoke-ApiCall -Method "GET" -Uri "$baseUrl/api/owners/auth/me" -Headers $headers
if ($result.Success) {
Write-Host "✅ Profile retrieved successfully!" -ForegroundColor Green
Write-Host " Vehicles: $($result.Data.vehicleDetails.Count)" -ForegroundColor Cyan
} else {
Write-Host "❌ Failed to get profile: $($result.Error)" -ForegroundColor Red
}
# Test 6: Create Booking (EV Owner)
Write-Host "`n6️⃣ Creating Booking as EV Owner..." -ForegroundColor Yellow
$bookingRequest = @{
stationId = $stationId
reservationDateTime = (Get-Date).AddDays(2).ToString("yyyy-MM-ddTHH:mm:ss.fffZ")
vehicleNumber = "TEST-2024"
} | ConvertTo-Json
$result = Invoke-ApiCall -Method "POST" -Uri "$baseUrl/api/owners/me/bookings" -Body $bookingRequest -Headers $headers
if ($result.Success) {
$bookingId = $result.Data.id
Write-Host "✅ Booking created successfully!" -ForegroundColor Green
Write-Host " Booking ID: $bookingId" -ForegroundColor Cyan
Write-Host " Status: $($result.Data.status)" -ForegroundColor Cyan
Write-Host " EV Owner NIC: $($result.Data.evOwnerNIC)" -ForegroundColor Cyan
} else {
Write-Host "❌ Booking creation failed: $($result.Error)" -ForegroundColor Red
}
# Test 7: Get My Bookings
Write-Host "`n7️⃣ Getting EV Owner Bookings..." -ForegroundColor Yellow
$result = Invoke-ApiCall -Method "GET" -Uri "$baseUrl/api/owners/me/bookings" -Headers $headers
if ($result.Success) {
Write-Host "✅ Bookings retrieved!" -ForegroundColor Green
Write-Host " Active bookings: $($result.Data.Count)" -ForegroundColor Cyan
if ($result.Data.Count -gt 0) {
Write-Host " Latest booking: $($result.Data[0].reservationDateTime)" -ForegroundColor Cyan
}
} else {
Write-Host "❌ Failed to get bookings: $($result.Error)" -ForegroundColor Red
}
# Test 8: Get Specific Booking
Write-Host "`n8️⃣ Getting Specific Booking..." -ForegroundColor Yellow
if ($bookingId -ne "") {
$result = Invoke-ApiCall -Method "GET" -Uri "$baseUrl/api/owners/me/bookings/$bookingId" -Headers $headers
if ($result.Success) {
Write-Host "✅ Specific booking retrieved!" -ForegroundColor Green
Write-Host " Station ID: $($result.Data.stationId)" -ForegroundColor Cyan
Write-Host " Status: $($result.Data.status)" -ForegroundColor Cyan
} else {
Write-Host "❌ Failed to get specific booking: $($result.Error)" -ForegroundColor Red
}
} else {
Write-Host "⚠️ No booking ID available to test" -ForegroundColor Orange
}
# Test 9: Update Booking
Write-Host "`n9️⃣ Updating Booking..." -ForegroundColor Yellow
if ($bookingId -ne "") {
$updateRequest = @{
reservationDateTime = (Get-Date).AddDays(3).ToString("yyyy-MM-ddTHH:mm:ss.fffZ")
} | ConvertTo-Json
$result = Invoke-ApiCall -Method "PUT" -Uri "$baseUrl/api/owners/me/bookings/$bookingId" -Body $updateRequest -Headers $headers
if ($result.Success) {
Write-Host "✅ Booking updated successfully!" -ForegroundColor Green
Write-Host " New date: $($result.Data.reservationDateTime)" -ForegroundColor Cyan
} else {
Write-Host "❌ Booking update failed: $($result.Error)" -ForegroundColor Red
}
} else {
Write-Host "⚠️ No booking ID available to test" -ForegroundColor Orange
}
# Test 10: Test Vehicle Compatibility (Create incompatible booking)
Write-Host "`n🔟 Testing Vehicle Compatibility (Should Fail)..." -ForegroundColor Yellow
# Create an AC-only station first
$acStation = @{
name = "AC Only Test Station"
location = "Test City, Sri Lanka"
type = "AC"
availableSlots = 3
isActive = $true
} | ConvertTo-Json
$result = Invoke-ApiCall -Method "POST" -Uri "$baseUrl/api/ChargingStations" -Body $acStation
if ($result.Success) {
$acStationId = $result.Data.id
# Try to book with EV Owner who has both AC and "Both" compatible vehicles
# This should work since Nissan Leaf (AC) is compatible with AC station
$compatibleBooking = @{
stationId = $acStationId
reservationDateTime = (Get-Date).AddDays(1).ToString("yyyy-MM-ddTHH:mm:ss.fffZ")
vehicleNumber = "LEAF-2023"
} | ConvertTo-Json
$result = Invoke-ApiCall -Method "POST" -Uri "$baseUrl/api/owners/me/bookings" -Body $compatibleBooking -Headers $headers
if ($result.Success) {
Write-Host "✅ Compatible vehicle booking worked!" -ForegroundColor Green
Write-Host " Nissan Leaf (AC) → AC Station = Compatible ✅" -ForegroundColor Cyan
} else {
Write-Host "❌ Compatible booking failed: $($result.Error)" -ForegroundColor Red
}
} else {
Write-Host "❌ Failed to create AC station for compatibility test" -ForegroundColor Red
}
# Test 11: Test Business Rules (Try to book in the past - should fail)
Write-Host "`n1️⃣1️⃣ Testing Business Rules (Past Date - Should Fail)..." -ForegroundColor Yellow
$pastBooking = @{
stationId = $stationId
reservationDateTime = (Get-Date).AddDays(-1).ToString("yyyy-MM-ddTHH:mm:ss.fffZ")
} | ConvertTo-Json
$result = Invoke-ApiCall -Method "POST" -Uri "$baseUrl/api/owners/me/bookings" -Body $pastBooking -Headers $headers
if (-not $result.Success) {
Write-Host "✅ Past date validation working correctly!" -ForegroundColor Green
Write-Host " Error (Expected): $($result.Error)" -ForegroundColor Cyan
} else {
Write-Host "❌ Past date validation failed - booking was created!" -ForegroundColor Red
}
# Test 12: Test Authorization (Try to access another user's booking)
Write-Host "`n1️⃣2️⃣ Testing Authorization..." -ForegroundColor Yellow
# Create another EV Owner
$anotherOwner = @{
nic = "199712345679"
firstName = "Another"
lastName = "Owner"
email = "another.owner@example.com"
password = "password123"
phoneNumber = "+94777555444"
address = "456 Another Street, Kandy"
dateOfBirth = "1997-03-10T00:00:00Z"
licenseNumber = "DL19970310"
vehicleDetails = @()
} | ConvertTo-Json -Depth 10
$result = Invoke-ApiCall -Method "POST" -Uri "$baseUrl/api/owners/auth/register" -Body $anotherOwner
if ($result.Success) {
$anotherToken = $result.Data.token
$anotherHeaders = @{ "Authorization" = "Bearer $anotherToken" }
# Try to access first owner's booking with second owner's token
if ($bookingId -ne "") {
$result = Invoke-ApiCall -Method "GET" -Uri "$baseUrl/api/owners/me/bookings/$bookingId" -Headers $anotherHeaders
if (-not $result.Success) {
Write-Host "✅ Authorization working correctly!" -ForegroundColor Green
Write-Host " Error (Expected): $($result.Error)" -ForegroundColor Cyan
} else {
Write-Host "❌ Authorization failed - unauthorized access allowed!" -ForegroundColor Red
}
}
} else {
Write-Host "⚠️ Could not create second owner for authorization test" -ForegroundColor Orange
}
# Test 13: Cancel Booking
Write-Host "`n1️⃣3️⃣ Canceling Booking..." -ForegroundColor Yellow
if ($bookingId -ne "") {
$result = Invoke-ApiCall -Method "DELETE" -Uri "$baseUrl/api/owners/me/bookings/$bookingId" -Headers $headers
if ($result.Success) {
Write-Host "✅ Booking canceled successfully!" -ForegroundColor Green
Write-Host " Message: $($result.Data.message)" -ForegroundColor Cyan
} else {
Write-Host "❌ Booking cancellation failed: $($result.Error)" -ForegroundColor Red
}
} else {
Write-Host "⚠️ No booking ID available to cancel" -ForegroundColor Orange
}
# Test 14: Verify Integration - Check All Data
Write-Host "`n1️⃣4️⃣ Final Integration Verification..." -ForegroundColor Yellow
# Get all bookings (system view)
$result = Invoke-ApiCall -Method "GET" -Uri "$baseUrl/api/Booking/user/199812345678"
Write-Host "📊 System View - User Bookings:" -ForegroundColor Cyan
if ($result.Success) {
Write-Host " Found $($result.Data.Count) bookings in system" -ForegroundColor Gray
} else {
Write-Host " No bookings found in system view" -ForegroundColor Gray
}
# Get EV Owner view
$result = Invoke-ApiCall -Method "GET" -Uri "$baseUrl/api/owners/me/bookings?includeHistory=true" -Headers $headers
Write-Host "📊 EV Owner View - My Bookings:" -ForegroundColor Cyan
if ($result.Success) {
Write-Host " Found $($result.Data.Count) bookings in EV Owner view" -ForegroundColor Gray
foreach ($booking in $result.Data) {
Write-Host " - Booking: $($booking.id)" -ForegroundColor Gray
Write-Host " Status: $($booking.status)" -ForegroundColor Gray
Write-Host " EV Owner NIC: $($booking.evOwnerNIC)" -ForegroundColor Gray
Write-Host " Station ID: $($booking.stationId)" -ForegroundColor Gray
}
} else {
Write-Host " No bookings found in EV Owner view" -ForegroundColor Gray
}
# Summary
Write-Host "`n🎉 Integration Test Complete!" -ForegroundColor Green
Write-Host "=============================" -ForegroundColor Green
Write-Host "✅ EV Owner Registration & Authentication" -ForegroundColor Green
Write-Host "✅ Profile Management" -ForegroundColor Green
Write-Host "✅ Booking Creation & Management" -ForegroundColor Green
Write-Host "✅ Vehicle Compatibility Validation" -ForegroundColor Green
Write-Host "✅ Business Rules Enforcement" -ForegroundColor Green
Write-Host "✅ Authorization & Security" -ForegroundColor Green
Write-Host "✅ Data Consistency Between Views" -ForegroundColor Green
Write-Host "`n🚀 EV Owner-Booking Integration is fully functional!" -ForegroundColor Magenta