Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

126 řádky
8.0KB

  1. $ErrorActionPreference = 'Stop'
  2. $root = Split-Path -Parent $PSScriptRoot
  3. $work = Join-Path $root ('temp\dispatch-flow-' + [guid]::NewGuid().ToString('N'))
  4. $templateRoot = Join-Path $work 'templates'
  5. $dbPath = Join-Path $work 'test.mdb'
  6. $configPath = Join-Path $work 'test.ini'
  7. $requestPath = Join-Path $work 'request.json'
  8. $pickup = Join-Path $work 'pickup'
  9. $scriptHost = Join-Path $env:SystemRoot 'SysWOW64\cscript.exe'
  10. $powerShellX86 = Join-Path $env:SystemRoot 'SysWOW64\WindowsPowerShell\v1.0\powershell.exe'
  11. function Call-X86([string]$File, [string[]]$Arguments) {
  12. $result = & $powerShellX86 -NoProfile -ExecutionPolicy Bypass -File (Join-Path $PSScriptRoot $File) @Arguments
  13. if ($LASTEXITCODE -ne 0) { throw "$File failed" }
  14. return $result
  15. }
  16. function Count-Messages {
  17. return @(Get-ChildItem -LiteralPath $pickup -Filter '*.eml' -File).Count
  18. }
  19. function Expect-Failure([string]$File, [string[]]$Arguments) {
  20. $ErrorActionPreference = 'Continue'
  21. $null = & $powerShellX86 -NoProfile -ExecutionPolicy Bypass -File (Join-Path $PSScriptRoot $File) @Arguments 2>$null
  22. $failed = $LASTEXITCODE -ne 0
  23. $ErrorActionPreference = 'Stop'
  24. if (-not $failed) { throw "Expected $File to reject the action" }
  25. }
  26. try {
  27. $null = New-Item -ItemType Directory -Path $templateRoot -Force
  28. $null = New-Item -ItemType Directory -Path $pickup -Force
  29. $example = Get-Content -LiteralPath (Join-Path $root 'config.example.ini') -Raw
  30. $example.Replace('DB_PATH=data/MassMailer.mdb', ('DB_PATH=' + $dbPath)).Replace(
  31. 'TEMPLATE_ROOT=templates', ('TEMPLATE_ROOT=' + $templateRoot)).Replace(
  32. 'SMTP_TEST_TO=', 'SMTP_TEST_TO=owner@example.invalid').Replace(
  33. 'BUSINESS_POSTAL_ADDRESS=', 'BUSINESS_POSTAL_ADDRESS=123 Example Street, Test City, NY 10001') |
  34. Set-Content -LiteralPath $configPath -Encoding UTF8
  35. & $scriptHost //nologo (Join-Path $PSScriptRoot 'init-db.vbs') $configPath
  36. if ($LASTEXITCODE -ne 0) { throw 'MDB setup failed' }
  37. $null = Call-X86 'test-campaign-db.ps1' @('-ConfigPath',$configPath,'-Mode','Seed')
  38. $templatePath = Join-Path $templateRoot 'offer.html'
  39. [System.IO.File]::WriteAllText($templatePath, '<html><body><h1>Postcard offer</h1></body></html>',
  40. [System.Text.Encoding]::UTF8)
  41. $null = Call-X86 'import-template.ps1' @('-Path',$templatePath,'-ConfigPath',$configPath)
  42. $request = @{ Name = 'Dispatch fixture'; Subject = 'Postcard advertising';
  43. TemplateId = 1; ReviewToken = ''; TestReceived = $false }
  44. $request | ConvertTo-Json -Compress | Set-Content -LiteralPath $requestPath -Encoding UTF8
  45. $review = (Call-X86 'review-campaign.ps1' @('-RequestPath',$requestPath,'-ConfigPath',$configPath,
  46. '-OutputRoot',(Join-Path $work 'previews'))) | ConvertFrom-Json
  47. if ($review.Count -ne 3) { throw 'Expected three eligible contacts' }
  48. $request.ReviewToken = $review.Token
  49. $request | ConvertTo-Json -Compress | Set-Content -LiteralPath $requestPath -Encoding UTF8
  50. $null = Call-X86 'send-campaign-test.ps1' @('-RequestPath',$requestPath,'-ConfigPath',$configPath,'-PickupDirectory',$pickup)
  51. $request.TestReceived = $true
  52. $request | ConvertTo-Json -Compress | Set-Content -LiteralPath $requestPath -Encoding UTF8
  53. $null = Call-X86 'approve-campaign.ps1' @('-RequestPath',$requestPath,'-ConfigPath',$configPath)
  54. $null = Get-ChildItem -LiteralPath $pickup -File | Remove-Item -Force
  55. $null = Call-X86 'dispatch-control.ps1' @('-Action','Recover','-ConfigPath',$configPath)
  56. $null = Call-X86 'dispatch-control.ps1' @('-Action','ReviewOptOut','-ConfigPath',$configPath)
  57. $null = Call-X86 'dispatch-control.ps1' @('-Action','Resume','-CampaignId','1','-ConfigPath',$configPath)
  58. $t0 = [DateTime]::UtcNow.AddSeconds(5)
  59. Expect-Failure 'dispatch-once.ps1' @('-ConfigPath',$configPath,'-Send','-SessionPid','999999','-SessionToken','00000000000000000000000000000000')
  60. $first = Call-X86 'dispatch-once.ps1' @('-ConfigPath',$configPath,'-PickupDirectory',$pickup,
  61. '-TestNowUtc',$t0.ToString('o'))
  62. if ($first -notmatch 'accepted' -or (Count-Messages) -ne 1) { throw 'First queued attempt failed' }
  63. $settings = Get-Content -LiteralPath $configPath -Raw
  64. $settings.Replace('QUEUE_INTERVAL=300', 'QUEUE_INTERVAL=600') |
  65. Set-Content -LiteralPath $configPath -Encoding UTF8
  66. $early = Call-X86 'dispatch-once.ps1' @('-ConfigPath',$configPath,'-PickupDirectory',$pickup,
  67. '-TestNowUtc',$t0.AddSeconds(300).ToString('o'))
  68. if ($early -notmatch 'Next attempt allowed' -or (Count-Messages) -ne 1) {
  69. throw 'Longer interval did not protect the reserved slot'
  70. }
  71. $null = Call-X86 'test-campaign-db.ps1' @('-ConfigPath',$configPath,'-Mode','SuppressSecond')
  72. $null = Call-X86 'dispatch-control.ps1' @('-Action','Recover','-ConfigPath',$configPath)
  73. $closed = Call-X86 'dispatch-once.ps1' @('-ConfigPath',$configPath,'-PickupDirectory',$pickup,
  74. '-TestNowUtc',$t0.AddSeconds(1200).ToString('o'))
  75. if ($closed -notmatch 'paused' -or (Count-Messages) -ne 1) {
  76. throw 'Recovery failed to pause dispatch after reopen'
  77. }
  78. $null = Call-X86 'dispatch-control.ps1' @('-Action','Resume','-CampaignId','1','-ConfigPath',$configPath)
  79. $second = Call-X86 'dispatch-once.ps1' @('-ConfigPath',$configPath,'-PickupDirectory',$pickup,
  80. '-TestNowUtc',$t0.AddSeconds(1200).ToString('o'),'-TestSimulateUnknownAfterSend')
  81. if ($second -notmatch 'uncertain' -or (Count-Messages) -ne 2) {
  82. throw 'Injected uncertain response did not pause after the second due attempt'
  83. }
  84. $sameSlot = Call-X86 'dispatch-once.ps1' @('-ConfigPath',$configPath,'-PickupDirectory',$pickup,
  85. '-TestNowUtc',$t0.AddSeconds(1200).ToString('o'))
  86. if ($sameSlot -notmatch 'paused' -or (Count-Messages) -ne 2) {
  87. throw 'Uncertain outcome did not stop further sends'
  88. }
  89. Expect-Failure 'dispatch-control.ps1' @('-Action','Resume','-CampaignId','1','-ConfigPath',$configPath)
  90. $status = (Call-X86 'dispatch-control.ps1' @('-Action','Status','-ConfigPath',$configPath)) | ConvertFrom-Json
  91. if ($status.Counts.accepted -ne 1 -or $status.Counts.suppressed -ne 1 -or
  92. $status.Counts.uncertain -ne 1) { throw 'Uncertain delivery counts are wrong' }
  93. $null = Call-X86 'dispatch-control.ps1' @('-Action','ResolveAccepted','-DeliveryId','3','-ConfigPath',$configPath)
  94. $status = (Call-X86 'dispatch-control.ps1' @('-Action','Status','-ConfigPath',$configPath)) | ConvertFrom-Json
  95. if ($status.Counts.accepted -ne 2 -or $status.Counts.uncertain -ne 0) {
  96. throw 'Manual uncertain reconciliation failed'
  97. }
  98. $reportFolder = Join-Path $work 'reports'
  99. $null = Call-X86 'export-report.ps1' @('-CampaignId','1','-ConfigPath',$configPath,'-OutputRoot',$reportFolder)
  100. $deliveryReport = @(Get-ChildItem -LiteralPath $reportFolder -Filter '*-deliveries.csv' -File)
  101. $attemptReport = @(Get-ChildItem -LiteralPath $reportFolder -Filter '*-attempts.csv' -File)
  102. if ($deliveryReport.Count -ne 1 -or $attemptReport.Count -ne 1) {
  103. throw 'Campaign report files were not created'
  104. }
  105. $deliveryRows = @(Import-Csv -LiteralPath $deliveryReport[0].FullName)
  106. $attemptRows = @(Import-Csv -LiteralPath $attemptReport[0].FullName)
  107. if ($deliveryRows.Count -ne 3 -or $attemptRows.Count -ne 2 -or
  108. @($deliveryRows | Where-Object Status -eq 'suppressed').Count -ne 1 -or
  109. @($attemptRows | Where-Object Outcome -eq 'uncertain').Count -ne 1) {
  110. throw 'Campaign report content is wrong'
  111. }
  112. $null = Call-X86 'dispatch-control.ps1' @('-Action','PauseAll','-ConfigPath',$configPath)
  113. Write-Output 'Session guard, recovery, pacing, suppression, uncertainty, and reports passed'
  114. }
  115. finally {
  116. $resolvedWork = [System.IO.Path]::GetFullPath($work)
  117. $resolvedTemp = [System.IO.Path]::GetFullPath((Join-Path $root 'temp'))
  118. if ($resolvedWork.StartsWith($resolvedTemp + [System.IO.Path]::DirectorySeparatorChar,
  119. [StringComparison]::OrdinalIgnoreCase) -and (Test-Path -LiteralPath $resolvedWork)) {
  120. Remove-Item -LiteralPath $resolvedWork -Recurse -Force
  121. }
  122. }

Powered by TurnKey Linux.