|
- $ErrorActionPreference = 'Stop'
- $root = Split-Path -Parent $PSScriptRoot
- $work = Join-Path $root ('temp\campaign-flow-' + [guid]::NewGuid().ToString('N'))
- $templateRoot = Join-Path $work 'templates'
- $assets = Join-Path $templateRoot 'images'
- $dbPath = Join-Path $work 'test.mdb'
- $configPath = Join-Path $work 'test.ini'
- $requestPath = Join-Path $work 'request.json'
- $templatePath = Join-Path $templateRoot 'offer.html'
- $imagePath = Join-Path $assets 'logo.png'
- $pickup = Join-Path $work 'pickup'
- $scriptHost = Join-Path $env:SystemRoot 'SysWOW64\cscript.exe'
- $powerShellX86 = Join-Path $env:SystemRoot 'SysWOW64\WindowsPowerShell\v1.0\powershell.exe'
-
- function Save-Request($Object) {
- $Object | ConvertTo-Json -Compress | Set-Content -LiteralPath $requestPath -Encoding UTF8
- }
- function Expect-Failure([string]$ScriptPath, [string[]]$Arguments) {
- $ErrorActionPreference = 'Continue'
- $null = & $powerShellX86 -NoProfile -ExecutionPolicy Bypass -File $ScriptPath @Arguments 2>$null
- $failed = $LASTEXITCODE -ne 0
- $ErrorActionPreference = 'Stop'
- if (-not $failed) { throw "Expected failure from $ScriptPath" }
- }
-
- try {
- $null = New-Item -ItemType Directory -Path $assets -Force
- $null = New-Item -ItemType Directory -Path $pickup -Force
- $example = Get-Content -LiteralPath (Join-Path $root 'config.example.ini') -Raw
- $example.Replace('DB_PATH=data/MassMailer.mdb', ('DB_PATH=' + $dbPath)).Replace(
- 'TEMPLATE_ROOT=templates', ('TEMPLATE_ROOT=' + $templateRoot)).Replace(
- 'SMTP_TEST_TO=', 'SMTP_TEST_TO=owner@example.invalid').Replace(
- 'BUSINESS_POSTAL_ADDRESS=', 'BUSINESS_POSTAL_ADDRESS=123 Example Street, Test City, NY 10001') |
- Set-Content -LiteralPath $configPath -Encoding UTF8
- & $scriptHost //nologo (Join-Path $PSScriptRoot 'init-db.vbs') $configPath
- if ($LASTEXITCODE -ne 0) { throw 'MDB initialization failed' }
- & $powerShellX86 -NoProfile -ExecutionPolicy Bypass -File (Join-Path $PSScriptRoot 'test-campaign-db.ps1') -ConfigPath $configPath -Mode Seed
- if ($LASTEXITCODE -ne 0) { throw 'Contact fixture failed' }
-
- [System.IO.File]::WriteAllBytes($imagePath, [Convert]::FromBase64String(
- 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+jRZkAAAAASUVORK5CYII='))
- [System.IO.File]::WriteAllText($templatePath,
- '<html><body><h1>Postcard offer</h1><img src="images/logo.png" alt="Logo" /></body></html>',
- [System.Text.Encoding]::UTF8)
- & $powerShellX86 -NoProfile -ExecutionPolicy Bypass -File (Join-Path $PSScriptRoot 'import-template.ps1') -Path $templatePath -ConfigPath $configPath
- if ($LASTEXITCODE -ne 0) { throw 'Template fixture failed' }
- $request = @{ Name = 'Autumn postcard'; Subject = 'Advertising on our postcard';
- TemplateId = 1; ReviewToken = ''; TestReceived = $false }
- Save-Request $request
- $review = (& $powerShellX86 -NoProfile -ExecutionPolicy Bypass -File (Join-Path $PSScriptRoot 'review-campaign.ps1') -RequestPath $requestPath -ConfigPath $configPath -OutputRoot (Join-Path $work 'previews')) | ConvertFrom-Json
- if ($LASTEXITCODE -ne 0 -or $review.Count -ne 3 -or -not (Test-Path -LiteralPath $review.PreviewPath)) {
- throw 'Campaign review count or preview failed'
- }
- $previewHtml = Get-Content -LiteralPath $review.PreviewPath -Raw
- if ($previewHtml -notmatch '123 Example Street' -or $previewHtml -notmatch 'UNSUBSCRIBE' -or
- $previewHtml -notmatch 'image-1.png') { throw 'Full preview is missing footer or image' }
- $request.ReviewToken = $review.Token
- Save-Request $request
- Expect-Failure (Join-Path $PSScriptRoot 'approve-campaign.ps1') @('-RequestPath',$requestPath,'-ConfigPath',$configPath)
- & $powerShellX86 -NoProfile -ExecutionPolicy Bypass -File (Join-Path $PSScriptRoot 'test-campaign-db.ps1') -ConfigPath $configPath -Mode SuppressSecond
- if ($LASTEXITCODE -ne 0) { throw 'Suppression fixture failed' }
- Expect-Failure (Join-Path $PSScriptRoot 'send-campaign-test.ps1') @('-RequestPath',$requestPath,'-ConfigPath',$configPath,'-PickupDirectory',$pickup)
- $review = (& $powerShellX86 -NoProfile -ExecutionPolicy Bypass -File (Join-Path $PSScriptRoot 'review-campaign.ps1') -RequestPath $requestPath -ConfigPath $configPath -OutputRoot (Join-Path $work 'previews')) | ConvertFrom-Json
- if ($LASTEXITCODE -ne 0 -or $review.Count -ne 2 -or $review.Token -eq $request.ReviewToken) {
- throw 'Suppression did not invalidate the review token'
- }
- $request.ReviewToken = $review.Token
- Save-Request $request
- & $powerShellX86 -NoProfile -ExecutionPolicy Bypass -File (Join-Path $PSScriptRoot 'send-campaign-test.ps1') -RequestPath $requestPath -ConfigPath $configPath -PickupDirectory $pickup
- if ($LASTEXITCODE -ne 0) { throw 'Internal campaign pickup test failed' }
- $messages = @(Get-ChildItem -LiteralPath $pickup -Filter '*.eml' -File)
- if ($messages.Count -ne 1) { throw 'Expected exactly one internal test MIME message' }
- $mime = Get-Content -LiteralPath $messages[0].FullName -Raw
- if ($mime -notmatch 'multipart/related' -or $mime -notmatch 'Content-ID:' -or
- $mime -notmatch 'owner@example.invalid' -or $mime -match 'one@example.invalid|two@example.invalid|three@example.invalid') {
- throw 'Internal test MIME has wrong recipient or inline image'
- }
- $request.TestReceived = $true
- Save-Request $request
- & $powerShellX86 -NoProfile -ExecutionPolicy Bypass -File (Join-Path $PSScriptRoot 'approve-campaign.ps1') -RequestPath $requestPath -ConfigPath $configPath
- if ($LASTEXITCODE -ne 0) { throw 'Campaign approval failed' }
- & $powerShellX86 -NoProfile -ExecutionPolicy Bypass -File (Join-Path $PSScriptRoot 'send-approved-campaign-test.ps1') -CampaignId 1 -ConfigPath $configPath -PickupDirectory $pickup
- if ($LASTEXITCODE -ne 0) { throw 'Approved campaign test failed' }
- if (@(Get-ChildItem -LiteralPath $pickup -Filter '*.eml' -File).Count -ne 2) {
- throw 'Approved campaign test did not create exactly one more internal message'
- }
- & $powerShellX86 -NoProfile -ExecutionPolicy Bypass -File (Join-Path $PSScriptRoot 'test-campaign-db.ps1') -ConfigPath $configPath -Mode Check
- if ($LASTEXITCODE -ne 0) { throw 'Campaign delivery state failed' }
- Expect-Failure (Join-Path $PSScriptRoot 'approve-campaign.ps1') @('-RequestPath',$requestPath,'-ConfigPath',$configPath)
- Write-Output 'Campaign review, internal tests before and after approval, and paused queue passed'
- }
- finally {
- $resolvedWork = [System.IO.Path]::GetFullPath($work)
- $resolvedTemp = [System.IO.Path]::GetFullPath((Join-Path $root 'temp'))
- if ($resolvedWork.StartsWith($resolvedTemp + [System.IO.Path]::DirectorySeparatorChar,
- [StringComparison]::OrdinalIgnoreCase) -and (Test-Path -LiteralPath $resolvedWork)) {
- Remove-Item -LiteralPath $resolvedWork -Recurse -Force
- }
- }
|