diff --git a/run-integration-tests.ps1 b/run-integration-tests.ps1 index fc26a25..f1a6e7e 100644 --- a/run-integration-tests.ps1 +++ b/run-integration-tests.ps1 @@ -67,7 +67,8 @@ function Invoke-Api { -Headers $headers ` -ErrorAction Stop return $response - } catch { + } + catch { $status = $_.Exception.Response.StatusCode.value__ throw "HTTP $status bei $Method $Path" } @@ -106,7 +107,8 @@ function Receive-WsMessages([System.Net.WebSockets.ClientWebSocket]$ws, [int]$wa $text = [System.Text.Encoding]::UTF8.GetString($buffer, 0, $result.Count) try { $messages += $text | ConvertFrom-Json } catch { } } - } catch { + } + catch { break } } @@ -124,24 +126,27 @@ function Close-WebSocket([System.Net.WebSockets.ClientWebSocket]$ws) { Section "Szenario 1: Auth-Flow" $aliceTokens = $null -$bobTokens = $null +$bobTokens = $null try { $aliceTokens = Invoke-Api -Method POST -Path "/api/auth/login" -Body @{ username = $AliceUser; password = $AlicePassword } Pass "Auth: Alice login (userId=$($aliceTokens.userId))" -} catch { Fail "Auth: Alice login - $_" } +} +catch { Fail "Auth: Alice login - $_" } try { $bobTokens = Invoke-Api -Method POST -Path "/api/auth/login" -Body @{ username = $BobUser; password = $BobPassword } Pass "Auth: Bob login (userId=$($bobTokens.userId))" -} catch { Fail "Auth: Bob login - $_" } +} +catch { Fail "Auth: Bob login - $_" } # Geschuetzten Endpoint mit Token aufrufen if ($aliceTokens) { try { $inv = Invoke-Api -Method GET -Path "/api/inventory" -Token $aliceTokens.accessToken Pass "Auth: Alice-Token fuer geschuetzten Endpoint gueltig" - } catch { Fail "Auth: Alice-Token ungueltig - $_" } + } + catch { Fail "Auth: Alice-Token ungueltig - $_" } } # --------------------------------------------------------------------------- @@ -155,36 +160,39 @@ $testInventory = @{ categories = @(@{ id = 1; name = "Lebensmittel" }) locations = @(@{ id = 1; name = "Keller" }) items = @(@{ - id = $itemId - name = "Testkonserve" - categoryId = 1 - quantity = 12.0 - unit = "Stueck" - unitPrice = 1.5 - kcalPerKg = $null - expiryDate = "2027-12-31" - locationId = 1 - notes = "Integrationstest" - lastUpdated = [DateTimeOffset]::UtcNow.ToUnixTimeMilliseconds() - }) - settings = @() + id = $itemId + name = "Testkonserve" + categoryId = 1 + quantity = 12.0 + unit = "Stueck" + unitPrice = 1.5 + kcalPerKg = $null + expiryDate = "2027-12-31" + locationId = 1 + notes = "Integrationstest" + lastUpdated = [DateTimeOffset]::UtcNow.ToUnixTimeMilliseconds() + }) + settings = @() } if ($aliceTokens) { try { $uploaded = Invoke-Api -Method PUT -Path "/api/inventory" -Body $testInventory -Token $aliceTokens.accessToken Pass "Inventory Sync: Alice laedt Inventar hoch ($($uploaded.items.Count) Item(s))" - } catch { Fail "Inventory Sync: Upload fehlgeschlagen - $_" } + } + catch { Fail "Inventory Sync: Upload fehlgeschlagen - $_" } try { $fetched = Invoke-Api -Method GET -Path "/api/inventory" -Token $aliceTokens.accessToken $item = $fetched.items | Where-Object { $_.id -eq $itemId } if ($item -and $item.name -eq "Testkonserve" -and $item.quantity -eq 12.0) { Pass "Inventory Sync: Abgerufenes Inventar konsistent (name=$($item.name), qty=$($item.quantity))" - } else { + } + else { Fail "Inventory Sync: Abgerufenes Item stimmt nicht ueberein" } - } catch { Fail "Inventory Sync: GET fehlgeschlagen - $_" } + } + catch { Fail "Inventory Sync: GET fehlgeschlagen - $_" } # PATCH eines einzelnen Items try { @@ -199,10 +207,12 @@ if ($aliceTokens) { $patched = $fetched2.items | Where-Object { $_.id -eq $itemId } if ($patched -and $patched.quantity -eq 24.0) { Pass "Inventory Sync: PATCH item erfolgreich (qty=$($patched.quantity))" - } else { + } + else { Fail "Inventory Sync: PATCH-Ergebnis nicht korrekt" } - } catch { Fail "Inventory Sync: PATCH fehlgeschlagen - $_" } + } + catch { Fail "Inventory Sync: PATCH fehlgeschlagen - $_" } } # --------------------------------------------------------------------------- @@ -212,9 +222,9 @@ Section "Szenario 3: Messaging + Offline Delivery" if ($aliceTokens -and $bobTokens) { - $bobId = $bobTokens.userId + $bobId = $bobTokens.userId $aliceId = $aliceTokens.userId - $now = [DateTimeOffset]::UtcNow.ToUnixTimeMilliseconds() + $now = [DateTimeOffset]::UtcNow.ToUnixTimeMilliseconds() # Alice sendet 3 Nachrichten an Bob (Bob hat keine aktive WS-Verbindung) $sentIds = @() @@ -227,7 +237,8 @@ if ($aliceTokens -and $bobTokens) { sentAt = $now + $i } $sentIds += $msg.id - } catch { + } + catch { Fail "Messaging: Alice sendet Nachricht $i - $_" $allSent = $false } @@ -245,10 +256,12 @@ if ($aliceTokens -and $bobTokens) { $newMsgs = $received | Where-Object { $_.type -eq "new_message" } if ($newMsgs.Count -ge 3) { Pass "Messaging: Bob empfaengt $($newMsgs.Count) Nachrichten nach WS-Connect (Offline-Delivery)" - } else { + } + else { Fail "Messaging: Bob empfaengt nur $($newMsgs.Count)/3 Nachrichten (erwartet 3)" } - } catch { Fail "Messaging: Bob WebSocket - $_" } + } + catch { Fail "Messaging: Bob WebSocket - $_" } # Bob antwortet 2 Nachrichten (Alice ist jetzt offline) $replySent = $true @@ -259,7 +272,8 @@ if ($aliceTokens -and $bobTokens) { body = "Antwort $i von Bob" sentAt = $now + 100 + $i } | Out-Null - } catch { + } + catch { Fail "Messaging: Bob sendet Antwort $i - $_" $replySent = $false } @@ -276,20 +290,24 @@ if ($aliceTokens -and $bobTokens) { $aliceMsgs = $aliceReceived | Where-Object { $_.type -eq "new_message" } if ($aliceMsgs.Count -ge 2) { Pass "Messaging: Alice empfaengt $($aliceMsgs.Count) Offline-Nachrichten von Bob nach Reconnect" - } else { + } + else { Fail "Messaging: Alice empfaengt nur $($aliceMsgs.Count)/2 Nachrichten (erwartet 2)" } - } catch { Fail "Messaging: Alice WebSocket Reconnect - $_" } + } + catch { Fail "Messaging: Alice WebSocket Reconnect - $_" } # Konversations-Verlauf per HTTP try { $conv = Invoke-Api -Method GET -Path "/api/messages/$bobId" -Token $aliceTokens.accessToken if ($conv.Count -ge 5) { Pass "Messaging: Konversationsverlauf enthaelt $($conv.Count) Nachrichten" - } else { + } + else { Fail "Messaging: Konversation enthaelt nur $($conv.Count) Nachrichten (erwartet >=5)" } - } catch { Fail "Messaging: Konversationsverlauf abruf - $_" } + } + catch { Fail "Messaging: Konversationsverlauf abruf - $_" } } # --------------------------------------------------------------------------- @@ -305,10 +323,12 @@ if ($aliceTokens) { # Alten Token sollte unbrauchbar sein nicht zwingend pruefbar; neuer Token muss funktionieren $inv = Invoke-Api -Method GET -Path "/api/inventory" -Token $refreshed.accessToken Pass "JWT Refresh: Neuer Access-Token fuer geschuetzten Endpoint gueltig" - } else { + } + else { Fail "JWT Refresh: Kein neuer Token erhalten" } - } catch { Fail "JWT Refresh: $_" } + } + catch { Fail "JWT Refresh: $_" } } # --------------------------------------------------------------------------- @@ -327,7 +347,8 @@ if ($aliceTokens -and $bobTokens) { if ($ws1.State -eq "Open" -and $ws2.State -eq "Open") { Pass "Parallele Sessions: Beide Verbindungen geoeffnet" - } else { + } + else { Fail "Parallele Sessions: Nicht beide Verbindungen offen" } @@ -358,11 +379,13 @@ if ($aliceTokens -and $bobTokens) { $undeliveredCount = ($pending | Where-Object { $_.type -eq "new_message" }).Count if ($undeliveredCount -eq 0) { Pass "Parallele Sessions: Server hat Alice korrekt als online erkannt - Nachricht als delivered markiert (nicht in Offline-Queue)" - } else { + } + else { Fail "Parallele Sessions: Server hat $undeliveredCount Nachricht(en) als undelivered gespeichert, obwohl Alice online war" } - } catch { Fail "Parallele Sessions: $_" } + } + catch { Fail "Parallele Sessions: $_" } finally { if ($ws1) { Close-WebSocket -ws $ws1 } if ($ws2) { Close-WebSocket -ws $ws2 }