-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcrud-post-write-check.ps1
More file actions
299 lines (256 loc) · 10.6 KB
/
crud-post-write-check.ps1
File metadata and controls
299 lines (256 loc) · 10.6 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
param(
[string]$BaseUrl,
[string]$AzdEnvironment = $(if ($env:AZURE_ENV_NAME) { $env:AZURE_ENV_NAME } else { '' }),
[string]$BearerToken = $env:CRUD_BEARER_TOKEN,
[int]$TimeoutSec = 30,
[switch]$UsePortForward = $false,
[string]$ResourceGroup,
[string]$AksName
)
$ErrorActionPreference = "Stop"
if ([string]::IsNullOrWhiteSpace($BaseUrl) -and [string]::IsNullOrWhiteSpace($AzdEnvironment)) {
throw "Provide -BaseUrl or set -AzdEnvironment/AZURE_ENV_NAME so the script can resolve APIM_GATEWAY_URL."
}
function Resolve-BaseUrl {
param([string]$Provided, [string]$EnvName)
if (-not [string]::IsNullOrWhiteSpace($Provided)) {
return $Provided.TrimEnd('/')
}
$envLines = azd env get-values -e $EnvName 2>$null
if (-not $envLines) {
throw "BaseUrl not provided and could not load azd env '$EnvName'."
}
foreach ($line in $envLines) {
if ($line -match '^APIM_GATEWAY_URL=') {
$value = $line.Substring($line.IndexOf('=') + 1).Trim('"')
if (-not [string]::IsNullOrWhiteSpace($value)) {
return $value.TrimEnd('/')
}
}
}
throw "APIM_GATEWAY_URL not found in azd env '$EnvName'. Provide -BaseUrl explicitly."
}
function Get-AzdEnvMap {
param([string]$EnvName)
$map = @{}
$envLines = azd env get-values -e $EnvName 2>$null
if (-not $envLines) {
return $map
}
foreach ($line in $envLines) {
if ($line -match '^[A-Za-z_][A-Za-z0-9_]*=') {
$idx = $line.IndexOf('=')
$key = $line.Substring(0, $idx)
$val = $line.Substring($idx + 1).Trim('"')
$map[$key] = $val
}
}
return $map
}
function Invoke-WriteCheck {
param(
[string]$Name,
[string]$Path,
[hashtable]$Body,
[int[]]$ExpectedStatuses,
[bool]$AuthRequired = $false,
[hashtable]$ExtraHeaders = @{}
)
$headers = @{
"Content-Type" = "application/json"
}
foreach ($k in $ExtraHeaders.Keys) {
$headers[$k] = $ExtraHeaders[$k]
}
if ($AuthRequired) {
if ([string]::IsNullOrWhiteSpace($script:Token)) {
return [pscustomobject]@{
Name = $Name
Path = $Path
StatusCode = "SKIP"
Outcome = "SKIPPED"
Detail = "Bearer token required"
}
}
$headers["Authorization"] = "Bearer $script:Token"
}
$url = "$script:ResolvedBaseUrl$Path"
try {
$jsonBody = if ($null -eq $Body) { "{}" } else { $Body | ConvertTo-Json -Depth 10 }
$response = Invoke-WebRequest -Uri $url -Method Post -Headers $headers -Body $jsonBody -TimeoutSec $script:TimeoutSec
$status = [int]$response.StatusCode
$content = $response.Content
}
catch {
$status = -1
$content = $_.Exception.Message
if ($_.Exception.Response) {
try {
$status = [int]$_.Exception.Response.StatusCode.value__
}
catch {
$status = -1
}
if ($_.ErrorDetails -and $_.ErrorDetails.Message) {
$content = $_.ErrorDetails.Message
}
}
}
$ok = $ExpectedStatuses -contains $status
$outcome = if ($ok) { "PASS" } else { "FAIL" }
return [pscustomobject]@{
Name = $Name
Path = $Path
StatusCode = $status
Outcome = $outcome
Detail = ($content | Out-String).Trim()
}
}
$azdMap = Get-AzdEnvMap -EnvName $AzdEnvironment
$pfProcess = $null
if ($UsePortForward) {
if ([string]::IsNullOrWhiteSpace($ResourceGroup)) {
$ResourceGroup = $azdMap["AZURE_RESOURCE_GROUP"]
if ([string]::IsNullOrWhiteSpace($ResourceGroup)) {
$ResourceGroup = $azdMap["resourceGroupName"]
}
}
if ([string]::IsNullOrWhiteSpace($AksName)) {
$AksName = $azdMap["AKS_CLUSTER_NAME"]
if ([string]::IsNullOrWhiteSpace($AksName)) {
$AksName = $azdMap["AZURE_AKS_CLUSTER_NAME"]
}
}
if ([string]::IsNullOrWhiteSpace($ResourceGroup) -or [string]::IsNullOrWhiteSpace($AksName)) {
throw "UsePortForward requires AKS identifiers. Provide -ResourceGroup and -AksName or ensure azd env exposes AZURE_RESOURCE_GROUP/resourceGroupName and AKS_CLUSTER_NAME."
}
az aks get-credentials --resource-group $ResourceGroup --name $AksName --overwrite-existing | Out-Null
$pfProcess = Start-Process -FilePath "kubectl" -ArgumentList @("port-forward", "svc/crud-service", "18080:8000", "-n", "holiday-peak") -PassThru -WindowStyle Hidden
Start-Sleep -Seconds 4
$script:ResolvedBaseUrl = "http://127.0.0.1:18080"
}
else {
$script:ResolvedBaseUrl = Resolve-BaseUrl -Provided $BaseUrl -EnvName $AzdEnvironment
}
$script:Token = $BearerToken
$script:TimeoutSec = $TimeoutSec
Write-Host "CRUD POST write check"
Write-Host "Base URL: $script:ResolvedBaseUrl"
Write-Host "Auth token: $([string]::IsNullOrWhiteSpace($script:Token) ? 'not provided (auth routes will be skipped)' : 'provided')"
try {
# Resolve one product for dependent write endpoints.
$productId = "prd-electronics-001"
try {
$productResp = Invoke-RestMethod -Uri "$script:ResolvedBaseUrl/api/products?limit=1" -Method Get -TimeoutSec $TimeoutSec
if ($productResp -and $productResp[0] -and $productResp[0].id) {
$productId = [string]$productResp[0].id
}
}
catch {
Write-Host "Could not read /api/products for dynamic seed context. Falling back to $productId"
}
$results = New-Object System.Collections.Generic.List[object]
# Non-auth POST endpoints.
$results.Add((Invoke-WriteCheck -Name "schemas-upsert" -Path "/api/schemas" -ExpectedStatuses @(200, 201) -Body @{
category_id = "cat-write-check"
category_name = "Write Check"
version = "v1"
fields = @(@{ name = "material"; type = "string"; required = $false; description = "Material" })
}))
$results.Add((Invoke-WriteCheck -Name "connector-replay-bulk" -Path "/webhooks/connectors/replay" -ExpectedStatuses @(200, 503) -Body @{ limit = 1 }))
$results.Add((Invoke-WriteCheck -Name "connector-replay-single" -Path "/webhooks/connectors/replay/dead-letter-write-check" -ExpectedStatuses @(200, 404, 503) -Body @{}))
$results.Add((Invoke-WriteCheck -Name "connector-webhook" -Path "/webhooks/connectors/shopify" -ExpectedStatuses @(202, 400, 503) -Body @{
event_type = "product.updated"
object_id = "prd-write-check"
payload = @{ id = "prd-write-check"; title = "Write Check Product" }
}))
$results.Add((Invoke-WriteCheck -Name "stripe-webhook" -Path "/webhooks/stripe" -ExpectedStatuses @(200, 400, 503) -Body @{} -ExtraHeaders @{ "stripe-signature" = "write-check-signature" }))
# Auth-required POST endpoints.
$results.Add((Invoke-WriteCheck -Name "auth-logout" -Path "/api/auth/logout" -ExpectedStatuses @(200, 401) -Body @{} -AuthRequired $true))
$results.Add((Invoke-WriteCheck -Name "cart-add-item" -Path "/api/cart/items" -ExpectedStatuses @(200, 401, 404, 409, 422) -Body @{ product_id = $productId; quantity = 1 } -AuthRequired $true))
$results.Add((Invoke-WriteCheck -Name "checkout-validate" -Path "/api/checkout/validate" -ExpectedStatuses @(200, 400, 401, 422) -Body @{} -AuthRequired $true))
$orderResult = Invoke-WriteCheck -Name "orders-create" -Path "/api/orders" -ExpectedStatuses @(200, 400, 401, 422) -Body @{
shipping_address_id = "addr-write-check"
payment_method_id = "pm-write-check"
} -AuthRequired $true
$results.Add($orderResult)
$orderId = $null
if ($orderResult.Outcome -eq "PASS" -and $orderResult.StatusCode -eq 200 -and $orderResult.Detail) {
try {
$obj = $orderResult.Detail | ConvertFrom-Json
$orderId = $obj.id
}
catch {}
}
if (-not $orderId) {
$orderId = "order-write-check"
}
$results.Add((Invoke-WriteCheck -Name "reviews-create" -Path "/api/reviews" -ExpectedStatuses @(200, 401, 404, 422) -Body @{
product_id = $productId
rating = 5
title = "Write check"
comment = "Validated via CRUD POST write-check script"
} -AuthRequired $true))
$results.Add((Invoke-WriteCheck -Name "payments-intent" -Path "/api/payments/intent" -ExpectedStatuses @(200, 401, 402, 404, 422, 502, 503) -Body @{
order_id = $orderId
amount = 19.99
currency = "usd"
} -AuthRequired $true))
$results.Add((Invoke-WriteCheck -Name "payments-process" -Path "/api/payments" -ExpectedStatuses @(200, 401, 402, 404, 422, 503) -Body @{
order_id = $orderId
payment_method_id = "pm_card_visa"
amount = 19.99
} -AuthRequired $true))
$delegateResult = Invoke-WriteCheck -Name "acp-payments-delegate" -Path "/acp/payments/delegate" -ExpectedStatuses @(200, 401, 422) -Body @{
payment_method_id = "pm_write_check"
allowance = @{ amount = 100.00; currency = "USD" }
} -AuthRequired $true
$results.Add($delegateResult)
$paymentToken = $null
if ($delegateResult.Outcome -eq "PASS" -and $delegateResult.StatusCode -eq 200 -and $delegateResult.Detail) {
try {
$obj = $delegateResult.Detail | ConvertFrom-Json
$paymentToken = $obj.token
}
catch {}
}
if (-not $paymentToken) {
$paymentToken = "token-write-check"
}
$acpSessionResult = Invoke-WriteCheck -Name "acp-checkout-session-create" -Path "/acp/checkout/sessions" -ExpectedStatuses @(200, 400, 401, 422) -Body @{
items = @(@{ sku = $productId; quantity = 1; unit_price = 19.99; currency = "USD" })
currency = "USD"
} -AuthRequired $true
$results.Add($acpSessionResult)
$sessionId = $null
if ($acpSessionResult.Outcome -eq "PASS" -and $acpSessionResult.StatusCode -eq 200 -and $acpSessionResult.Detail) {
try {
$obj = $acpSessionResult.Detail | ConvertFrom-Json
$sessionId = $obj.id
}
catch {}
}
if (-not $sessionId) {
$sessionId = "session-write-check"
}
$results.Add((Invoke-WriteCheck -Name "acp-checkout-session-complete" -Path "/acp/checkout/sessions/$sessionId/complete" -ExpectedStatuses @(200, 400, 401, 404, 422) -Body @{ payment_token = $paymentToken } -AuthRequired $true))
$results | Format-Table -AutoSize Name, StatusCode, Outcome, Path
$failed = $results | Where-Object { $_.Outcome -eq "FAIL" }
if ($failed.Count -gt 0) {
Write-Host "\nFailures:" -ForegroundColor Red
foreach ($f in $failed) {
Write-Host "- $($f.Name) [$($f.StatusCode)] $($f.Path)"
if ($f.Detail) {
Write-Host " $($f.Detail.Substring(0, [Math]::Min($f.Detail.Length, 240)))"
}
}
exit 1
}
Write-Host "\nAll POST endpoint checks passed or were intentionally skipped." -ForegroundColor Green
exit 0
}
finally {
if ($pfProcess -and -not $pfProcess.HasExited) {
Stop-Process -Id $pfProcess.Id -Force -ErrorAction SilentlyContinue
}
}