Files
baiyue-firstleg/scripts/phase1-onboarding-order-e2e.ps1
T
2026-07-30 18:01:36 +08:00

78 lines
5.5 KiB
PowerShell

param(
[string]$AuthUrl = 'http://127.0.0.1:9502',
[string]$IamUrl = 'http://127.0.0.1:9501',
[string]$FileUrl = 'http://127.0.0.1:9506',
[string]$OrderUrl = 'http://127.0.0.1:9504',
[string]$ImagePath,
[string]$PlatformPassword = 'ChangeMe123!'
)
$ErrorActionPreference = 'Stop'
function Invoke-Json($method, $url, $body = $null, $token = $null) {
$headers = @{}; if ($token) { $headers.Authorization = "Bearer $token" }
$args = @{ Method=$method; Uri=$url; Headers=$headers; TimeoutSec=20 }
if ($null -ne $body) { $args.ContentType='application/json'; $args.Body=$body | ConvertTo-Json -Depth 12 }
Invoke-RestMethod @args
}
function Invoke-Status($method, $url, $body, $token) {
try { Invoke-Json $method $url $body $token | Out-Null; return 200 }
catch { return [int]$_.Exception.Response.StatusCode }
}
function Assert-Equal($name, $actual, $expected) {
if ($actual -ne $expected) { throw "$name expected=$expected actual=$actual" }
}
$stamp = Get-Date -Format 'MMddHHmmss'
$username = "phase1_$stamp"; $password = 'Phase1Test123!'
$registration = Invoke-Json POST "$AuthUrl/api/auth/register" @{
username=$username; password=$password; displayName='Phase One Customer'; companyName="Phase One Enterprise $stamp"
legalPerson='Test Legal Person'; legalPhone='13800138000'; contactEmail="phase1_$stamp@example.com"
countryCode='CN'; address='No. 1 Test Road, Nanshan, Shenzhen'
}
Assert-Equal 'registration status' $registration.data.tenantStatus 'PROFILE_REQUIRED'
$tenantId = [long]$registration.data.tenantId
$merchantLogin = Invoke-Json POST "$AuthUrl/api/auth/login" @{username=$username;password=$password}
$merchantToken = $merchantLogin.data.accessToken
$order = @{
customerId=$tenantId; customerName="Phase One Enterprise $stamp"; transportMode='SEA'; tradeTerm='FOB'
destinationCountryCode='US'; destinationWarehouseId=1001; destinationWarehouseName='ONT8'
expectedShipDate='2026-08-15'; customerRemark='Phase one E2E'; pickupMethod='PICKUP'
receivingWarehouseId=2001; receivingWarehouseName='Shenzhen Receiving Warehouse'; channelCode='SEA-US-FBA'
channelName='US Sea Channel'; taxMode='BUY_DECLARATION'; containerMode='LCL'; customsInspection=$false
destinationType='FBA'; amazonWarehouseCode='ONT8'; shipmentId="FBA$stamp"; referenceId="REF$stamp"
sender=@{companyName="Phase One Enterprise $stamp";contactName='Test Sender';phone='13800138000';email="p$stamp@example.com";countryCode='CN';province='Guangdong';city='Shenzhen';district='Nanshan';addressLine='No. 1 Test Road';postalCode='518000'}
receiver=@{companyName='Amazon';contactName='ONT8';phone='1234567890';email='ont8@example.com';countryCode='US';province='CA';city='Moreno Valley';district='Riverside';addressLine='24300 Nandina Ave';postalCode='92551'}
cargoes=@(@{cargoName='Bluetooth Speaker';hsCode='8518210000';packageType='CARTON';packageCount=2;grossWeightKg=20.5;volumeCbm=0.2;lengthCm=50;widthCm=40;heightCm=30;declaredValue=200;currency='USD';dangerousGoods=$false;cargoRemark='Phase one test';skuNo="SKU$stamp";upcNo='123456789012';cargoNameEn='Bluetooth Speaker';customsHsCode='8518210000';unitsPerPackage=10;totalQuantity=20;netWeightKg=18;unitDeclaredValue=10;material='Plastic';usage='Audio';model='P1';brand='Demo'})
}
$preApprovalStatus = Invoke-Status POST "$OrderUrl/api/merchant/v1/orders/first-leg" $order $merchantToken
Assert-Equal 'pre-approval order status' $preApprovalStatus 403
$uploadJson = & curl.exe -sS -X POST "$FileUrl/api/merchant/v1/files/qualifications" `
-H "Authorization: Bearer $merchantToken" -F "file=@$ImagePath;type=image/png" `
-F "certificateNo=91310101$stamp"
if ($LASTEXITCODE -ne 0) { throw 'qualification upload transport failed' }
$upload = $uploadJson | ConvertFrom-Json
Assert-Equal 'upload API code' $upload.code '0'
Assert-Equal 'upload tenant status' $upload.data.tenantStatus 'PENDING'
$platformLogin = Invoke-Json POST "$AuthUrl/api/auth/login" @{username='platform_admin';password=$PlatformPassword}
$platformToken = $platformLogin.data.accessToken
$reviews = Invoke-Json GET "$IamUrl/api/platform/v1/iam/reviews" $null $platformToken
$review = @($reviews.data) | Where-Object { [long]$_.tenantId -eq $tenantId } | Select-Object -First 1
if ($null -eq $review) { throw 'pending review not found' }
$qualifications = Invoke-Json GET "$IamUrl/api/platform/v1/iam/tenants/$tenantId/qualifications" $null $platformToken
Assert-Equal 'qualification count' @($qualifications.data).Count 1
$fileName = Split-Path $upload.data.documentUrl -Leaf
$preview = Invoke-WebRequest -UseBasicParsing -Uri "$FileUrl/api/platform/v1/files/qualifications/$tenantId/$fileName" `
-Headers @{Authorization="Bearer $platformToken"} -TimeoutSec 20
Assert-Equal 'platform preview status' $preview.StatusCode 200
Invoke-Json POST "$IamUrl/api/platform/v1/iam/reviews/$($review.id)/approve" @{comment='Phase one E2E approved'} $platformToken | Out-Null
$created = Invoke-Json POST "$OrderUrl/api/merchant/v1/orders/first-leg" $order $merchantToken
$orderId = [long]$created.data
Invoke-Json POST "$OrderUrl/api/merchant/v1/orders/first-leg/$orderId/submit" $null $merchantToken | Out-Null
$detail = Invoke-Json GET "$OrderUrl/api/merchant/v1/orders/first-leg/$orderId" $null $merchantToken
Assert-Equal 'submitted order status' $detail.data.order.status 'SUBMITTED'
[pscustomobject]@{username=$username;tenantId=$tenantId;registerStatus='PROFILE_REQUIRED';preApprovalOrderHttp=$preApprovalStatus;uploadStatus='PENDING';reviewId=$review.id;previewHttp=200;orderId=$orderId;finalOrderStatus='SUBMITTED'} | ConvertTo-Json