test_apis_v2.ps1 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. # Precise analysis: get actual backend mapping list from running server
  2. # Use a smarter approach - test only the paths from the frontend API files
  3. # and properly map them to backend endpoints
  4. $companyUrl = "http://localhost:8006"
  5. # Login
  6. $loginBody = '{"tenantCode":"T202605253515","username":"admin","password":"admin123"}'
  7. $loginResp = Invoke-WebRequest -Uri "$companyUrl/login" -Method POST -ContentType "application/json" -Body $loginBody -UseBasicParsing
  8. $loginJson = $loginResp.Content | ConvertFrom-Json
  9. $token = $loginJson.token
  10. Write-Host "Token OK"
  11. $headers = @{ Authorization = "Bearer $token" }
  12. # Step 1: Get the ACTUAL registered mappings by testing key patterns
  13. # First, let's extract frontend API URLs properly from saasadminui
  14. $apiFiles = Get-ChildItem -Path "d:\ylrz\saasadminui\src\api" -Recurse -Filter "*.js"
  15. # Parse each API file to extract url + method pairs
  16. $frontendApis = @()
  17. foreach ($file in $apiFiles) {
  18. $content = Get-Content $file.FullName -Raw -Encoding UTF8
  19. # Match each API function with its url and method
  20. $funcBlocks = [regex]::Matches($content, "(?:export\s+(?:const|function)\s+\w+\s*=?\s*\([^)]*\)\s*=>\s*\{?\s*return\s+request\(\s*\{[^}]*\}\s*\)|request\(\s*\{[^}]*\}\s*\))")
  21. foreach ($block in $funcBlocks) {
  22. $blockText = $block.Value
  23. $url = ""
  24. $method = "GET"
  25. if ($blockText -match "url:\s*['`"]([^'`"]+)['`"]") {
  26. $url = $Matches[1]
  27. }
  28. if ($blockText -match "method:\s*['`"]([^'`"]+)['`"]") {
  29. $method = $Matches[1].ToUpper()
  30. }
  31. if ($url -ne "") {
  32. # Replace path variables
  33. $cleanUrl = $url -replace '\$\{[^}]+\}', '1'
  34. $frontendApis += @{ url = $cleanUrl; method = $method; file = $file.Name }
  35. }
  36. }
  37. }
  38. Write-Host "Frontend API calls: $($frontendApis.Count)"
  39. # Deduplicate by url+method
  40. $uniqueApis = @{}
  41. foreach ($api in $frontendApis) {
  42. $key = "$($api.method) $($api.url)"
  43. if (-not $uniqueApis.ContainsKey($key)) {
  44. $uniqueApis[$key] = $api
  45. }
  46. }
  47. Write-Host "Unique API calls: $($uniqueApis.Count)"
  48. # Step 2: Test each API with the CORRECT HTTP method
  49. Write-Host "`n=== Testing APIs with correct methods ==="
  50. $ok = [System.Collections.ArrayList]::new()
  51. $notFound = [System.Collections.ArrayList]::new()
  52. $serverErr = [System.Collections.ArrayList]::new()
  53. $forbidden = [System.Collections.ArrayList]::new()
  54. $other = [System.Collections.ArrayList]::new()
  55. $i = 0
  56. $total = $uniqueApis.Count
  57. foreach ($key in ($uniqueApis.Keys | Sort-Object)) {
  58. $i++
  59. $api = $uniqueApis[$key]
  60. $url = $api.url
  61. $method = $api.method
  62. $file = $api.file
  63. if ($i % 100 -eq 0) { Write-Host " Progress: $i / $total" }
  64. try {
  65. if ($method -eq "GET") {
  66. $resp = Invoke-WebRequest -Uri "$companyUrl$url" -Method GET -Headers $headers -UseBasicParsing -TimeoutSec 8
  67. } elseif ($method -eq "POST") {
  68. $resp = Invoke-WebRequest -Uri "$companyUrl$url" -Method POST -Headers $headers -ContentType "application/json" -Body "{}" -UseBasicParsing -TimeoutSec 8
  69. } elseif ($method -eq "PUT") {
  70. $resp = Invoke-WebRequest -Uri "$companyUrl$url" -Method PUT -Headers $headers -ContentType "application/json" -Body "{}" -UseBasicParsing -TimeoutSec 8
  71. } elseif ($method -eq "DELETE") {
  72. $resp = Invoke-WebRequest -Uri "$companyUrl$url" -Method DELETE -Headers $headers -UseBasicParsing -TimeoutSec 8
  73. } else {
  74. $resp = Invoke-WebRequest -Uri "$companyUrl$url" -Method GET -Headers $headers -UseBasicParsing -TimeoutSec 8
  75. }
  76. $body = $resp.Content | ConvertFrom-Json
  77. if ($body.code -and $body.code -ne 200) {
  78. $code = "$($body.code)"
  79. if ($code -eq "404") { [void]$notFound.Add("$method $url | $file") }
  80. elseif ($code -eq "500") { [void]$serverErr.Add("$method $url | $file | msg=$($body.msg)") }
  81. elseif ($code -eq "403") { [void]$forbidden.Add("$method $url | $file") }
  82. else { [void]$other.Add("$method $url | $file | code=$code msg=$($body.msg)") }
  83. } else {
  84. [void]$ok.Add("$method $url | $file")
  85. }
  86. } catch {
  87. $err = "$($_.Exception.Message)"
  88. if ($err -match "404") { [void]$notFound.Add("$method $url | $file") }
  89. elseif ($err -match "500") { [void]$serverErr.Add("$method $url | $file") }
  90. elseif ($err -match "403") { [void]$forbidden.Add("$method $url | $file") }
  91. else { [void]$other.Add("$method $url | $file | $err") }
  92. }
  93. }
  94. # Step 3: Summary
  95. Write-Host "`n========================================"
  96. Write-Host "=== API Test Results (Correct Methods) ==="
  97. Write-Host "========================================"
  98. Write-Host "200 OK: $($ok.Count)"
  99. Write-Host "404 Not Found: $($notFound.Count)"
  100. Write-Host "500 Server Error: $($serverErr.Count)"
  101. Write-Host "403 Forbidden: $($forbidden.Count)"
  102. Write-Host "OTHER: $($other.Count)"
  103. # Categorize 404 by module
  104. Write-Host "`n--- 404 by Module ---"
  105. $nfByMod = @{}
  106. foreach ($line in $notFound) {
  107. if ($line -match "\s+/([^/]+)/") {
  108. $mod = $Matches[1]
  109. if (-not $nfByMod.ContainsKey($mod)) { $nfByMod[$mod] = 0 }
  110. $nfByMod[$mod]++
  111. }
  112. }
  113. $nfByMod.GetEnumerator() | Sort-Object Value -Descending | ForEach-Object { Write-Host " $($_.Key): $($_.Value)" }
  114. # Categorize 500 by type
  115. Write-Host "`n--- 500 Error Types ---"
  116. $errByType = @{}
  117. foreach ($line in $serverErr) {
  118. if ($line -match "msg=(.+)") {
  119. $msg = $Matches[1].Substring(0, [Math]::Min(80, $Matches[1].Length))
  120. if (-not $errByType.ContainsKey($msg)) { $errByType[$msg] = 0 }
  121. $errByType[$msg]++
  122. }
  123. }
  124. $errByType.GetEnumerator() | Sort-Object Value -Descending | Select-Object -First 15 | ForEach-Object { Write-Host " [$($_.Value)] $($_.Key)" }
  125. # Show sample 404
  126. Write-Host "`n--- Sample 404 ---"
  127. $notFound | Sort-Object | Select-Object -First 30 | ForEach-Object { Write-Host " $_" }
  128. # Show sample 500
  129. Write-Host "`n--- Sample 500 ---"
  130. $serverErr | Sort-Object | Select-Object -First 30 | ForEach-Object { Write-Host " $_" }
  131. # Save
  132. $out = [System.Collections.ArrayList]::new()
  133. [void]$out.Add("API Test Results (Correct Methods) - $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')")
  134. [void]$out.Add("200 OK: $($ok.Count)")
  135. [void]$out.Add("404: $($notFound.Count)")
  136. [void]$out.Add("500: $($serverErr.Count)")
  137. [void]$out.Add("403: $($forbidden.Count)")
  138. [void]$out.Add("OTHER: $($other.Count)")
  139. [void]$out.Add("")
  140. [void]$out.Add("=== 404 ===")
  141. $notFound | Sort-Object | ForEach-Object { [void]$out.Add($_) }
  142. [void]$out.Add("")
  143. [void]$out.Add("=== 500 ===")
  144. $serverErr | Sort-Object | ForEach-Object { [void]$out.Add($_) }
  145. [void]$out.Add("")
  146. [void]$out.Add("=== 403 ===")
  147. $forbidden | Sort-Object | ForEach-Object { [void]$out.Add($_) }
  148. [void]$out.Add("")
  149. [void]$out.Add("=== OTHER ===")
  150. $other | Sort-Object | ForEach-Object { [void]$out.Add($_) }
  151. [System.IO.File]::WriteAllLines("d:\ylrz\saasadminui\api_test_v2_results.txt", $out, (New-Object System.Text.UTF8Encoding($false)))
  152. Write-Host "`nResults saved to api_test_v2_results.txt"