Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

100 рядки
6.8KB

  1. $ErrorActionPreference = 'Stop'
  2. $root = Split-Path -Parent $PSScriptRoot
  3. $work = Join-Path $root ('temp\campaign-flow-' + [guid]::NewGuid().ToString('N'))
  4. $templateRoot = Join-Path $work 'templates'
  5. $assets = Join-Path $templateRoot 'images'
  6. $dbPath = Join-Path $work 'test.mdb'
  7. $configPath = Join-Path $work 'test.ini'
  8. $requestPath = Join-Path $work 'request.json'
  9. $templatePath = Join-Path $templateRoot 'offer.html'
  10. $imagePath = Join-Path $assets 'logo.png'
  11. $pickup = Join-Path $work 'pickup'
  12. $scriptHost = Join-Path $env:SystemRoot 'SysWOW64\cscript.exe'
  13. $powerShellX86 = Join-Path $env:SystemRoot 'SysWOW64\WindowsPowerShell\v1.0\powershell.exe'
  14. function Save-Request($Object) {
  15. $Object | ConvertTo-Json -Compress | Set-Content -LiteralPath $requestPath -Encoding UTF8
  16. }
  17. function Expect-Failure([string]$ScriptPath, [string[]]$Arguments) {
  18. $ErrorActionPreference = 'Continue'
  19. $null = & $powerShellX86 -NoProfile -ExecutionPolicy Bypass -File $ScriptPath @Arguments 2>$null
  20. $failed = $LASTEXITCODE -ne 0
  21. $ErrorActionPreference = 'Stop'
  22. if (-not $failed) { throw "Expected failure from $ScriptPath" }
  23. }
  24. try {
  25. $null = New-Item -ItemType Directory -Path $assets -Force
  26. $null = New-Item -ItemType Directory -Path $pickup -Force
  27. $example = Get-Content -LiteralPath (Join-Path $root 'config.example.ini') -Raw
  28. $example.Replace('DB_PATH=data/MassMailer.mdb', ('DB_PATH=' + $dbPath)).Replace(
  29. 'TEMPLATE_ROOT=templates', ('TEMPLATE_ROOT=' + $templateRoot)).Replace(
  30. 'SMTP_TEST_TO=', 'SMTP_TEST_TO=owner@example.invalid').Replace(
  31. 'BUSINESS_POSTAL_ADDRESS=', 'BUSINESS_POSTAL_ADDRESS=123 Example Street, Test City, NY 10001') |
  32. Set-Content -LiteralPath $configPath -Encoding UTF8
  33. & $scriptHost //nologo (Join-Path $PSScriptRoot 'init-db.vbs') $configPath
  34. if ($LASTEXITCODE -ne 0) { throw 'MDB initialization failed' }
  35. & $powerShellX86 -NoProfile -ExecutionPolicy Bypass -File (Join-Path $PSScriptRoot 'test-campaign-db.ps1') -ConfigPath $configPath -Mode Seed
  36. if ($LASTEXITCODE -ne 0) { throw 'Contact fixture failed' }
  37. [System.IO.File]::WriteAllBytes($imagePath, [Convert]::FromBase64String(
  38. 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+jRZkAAAAASUVORK5CYII='))
  39. [System.IO.File]::WriteAllText($templatePath,
  40. '<html><body><h1>Postcard offer</h1><img src="images/logo.png" alt="Logo" /></body></html>',
  41. [System.Text.Encoding]::UTF8)
  42. & $powerShellX86 -NoProfile -ExecutionPolicy Bypass -File (Join-Path $PSScriptRoot 'import-template.ps1') -Path $templatePath -ConfigPath $configPath
  43. if ($LASTEXITCODE -ne 0) { throw 'Template fixture failed' }
  44. $request = @{ Name = 'Autumn postcard'; Subject = 'Advertising on our postcard';
  45. TemplateId = 1; ReviewToken = ''; TestReceived = $false }
  46. Save-Request $request
  47. $review = (& $powerShellX86 -NoProfile -ExecutionPolicy Bypass -File (Join-Path $PSScriptRoot 'review-campaign.ps1') -RequestPath $requestPath -ConfigPath $configPath -OutputRoot (Join-Path $work 'previews')) | ConvertFrom-Json
  48. if ($LASTEXITCODE -ne 0 -or $review.Count -ne 3 -or -not (Test-Path -LiteralPath $review.PreviewPath)) {
  49. throw 'Campaign review count or preview failed'
  50. }
  51. $previewHtml = Get-Content -LiteralPath $review.PreviewPath -Raw
  52. if ($previewHtml -notmatch '123 Example Street' -or $previewHtml -notmatch 'UNSUBSCRIBE' -or
  53. $previewHtml -notmatch 'image-1.png') { throw 'Full preview is missing footer or image' }
  54. $request.ReviewToken = $review.Token
  55. Save-Request $request
  56. Expect-Failure (Join-Path $PSScriptRoot 'approve-campaign.ps1') @('-RequestPath',$requestPath,'-ConfigPath',$configPath)
  57. & $powerShellX86 -NoProfile -ExecutionPolicy Bypass -File (Join-Path $PSScriptRoot 'test-campaign-db.ps1') -ConfigPath $configPath -Mode SuppressSecond
  58. if ($LASTEXITCODE -ne 0) { throw 'Suppression fixture failed' }
  59. Expect-Failure (Join-Path $PSScriptRoot 'send-campaign-test.ps1') @('-RequestPath',$requestPath,'-ConfigPath',$configPath,'-PickupDirectory',$pickup)
  60. $review = (& $powerShellX86 -NoProfile -ExecutionPolicy Bypass -File (Join-Path $PSScriptRoot 'review-campaign.ps1') -RequestPath $requestPath -ConfigPath $configPath -OutputRoot (Join-Path $work 'previews')) | ConvertFrom-Json
  61. if ($LASTEXITCODE -ne 0 -or $review.Count -ne 2 -or $review.Token -eq $request.ReviewToken) {
  62. throw 'Suppression did not invalidate the review token'
  63. }
  64. $request.ReviewToken = $review.Token
  65. Save-Request $request
  66. & $powerShellX86 -NoProfile -ExecutionPolicy Bypass -File (Join-Path $PSScriptRoot 'send-campaign-test.ps1') -RequestPath $requestPath -ConfigPath $configPath -PickupDirectory $pickup
  67. if ($LASTEXITCODE -ne 0) { throw 'Internal campaign pickup test failed' }
  68. $messages = @(Get-ChildItem -LiteralPath $pickup -Filter '*.eml' -File)
  69. if ($messages.Count -ne 1) { throw 'Expected exactly one internal test MIME message' }
  70. $mime = Get-Content -LiteralPath $messages[0].FullName -Raw
  71. if ($mime -notmatch 'multipart/related' -or $mime -notmatch 'Content-ID:' -or
  72. $mime -notmatch 'owner@example.invalid' -or $mime -match 'one@example.invalid|two@example.invalid|three@example.invalid') {
  73. throw 'Internal test MIME has wrong recipient or inline image'
  74. }
  75. $request.TestReceived = $true
  76. Save-Request $request
  77. & $powerShellX86 -NoProfile -ExecutionPolicy Bypass -File (Join-Path $PSScriptRoot 'approve-campaign.ps1') -RequestPath $requestPath -ConfigPath $configPath
  78. if ($LASTEXITCODE -ne 0) { throw 'Campaign approval failed' }
  79. & $powerShellX86 -NoProfile -ExecutionPolicy Bypass -File (Join-Path $PSScriptRoot 'send-approved-campaign-test.ps1') -CampaignId 1 -ConfigPath $configPath -PickupDirectory $pickup
  80. if ($LASTEXITCODE -ne 0) { throw 'Approved campaign test failed' }
  81. if (@(Get-ChildItem -LiteralPath $pickup -Filter '*.eml' -File).Count -ne 2) {
  82. throw 'Approved campaign test did not create exactly one more internal message'
  83. }
  84. & $powerShellX86 -NoProfile -ExecutionPolicy Bypass -File (Join-Path $PSScriptRoot 'test-campaign-db.ps1') -ConfigPath $configPath -Mode Check
  85. if ($LASTEXITCODE -ne 0) { throw 'Campaign delivery state failed' }
  86. Expect-Failure (Join-Path $PSScriptRoot 'approve-campaign.ps1') @('-RequestPath',$requestPath,'-ConfigPath',$configPath)
  87. Write-Output 'Campaign review, internal tests before and after approval, and paused queue passed'
  88. }
  89. finally {
  90. $resolvedWork = [System.IO.Path]::GetFullPath($work)
  91. $resolvedTemp = [System.IO.Path]::GetFullPath((Join-Path $root 'temp'))
  92. if ($resolvedWork.StartsWith($resolvedTemp + [System.IO.Path]::DirectorySeparatorChar,
  93. [StringComparison]::OrdinalIgnoreCase) -and (Test-Path -LiteralPath $resolvedWork)) {
  94. Remove-Item -LiteralPath $resolvedWork -Recurse -Force
  95. }
  96. }

Powered by TurnKey Linux.