Files
easyai/scripts/Test-SecurityOrigin.ps1
T

37 lines
2.0 KiB
PowerShell

$ErrorActionPreference = 'Stop'
. (Join-Path $PSScriptRoot 'Initialize-SecurityOrigin.ps1')
$tempDir = Join-Path ([System.IO.Path]::GetTempPath()) ("easyai-origin-" + [guid]::NewGuid().ToString('N'))
New-Item -ItemType Directory -Path $tempDir | Out-Null
try {
$path = Join-Path $tempDir 'test.env'
[System.IO.File]::WriteAllText($path, "CONFIG_PUBLIC_API_BASE_URL=https://zaowua.com/api`n")
$before = Get-Content $path -Raw
Initialize-SecurityOrigin -Path $path
$content = Get-Content $path -Raw
if ((Get-PublicEnvValue $content 'CONFIG_SECURITY_ORIGIN')) { throw 'Missing origin was unexpectedly initialized' }
if ($content -ne $before) { throw 'Missing origin changed the environment' }
$before = $content
Initialize-SecurityOrigin -Path $path
if ((Get-Content $path -Raw) -ne $before) { throw 'Repeated initialization changed the environment' }
[System.IO.File]::WriteAllText($path, "CONFIG_PUBLIC_API_BASE_URL=https://zaowua.com/api`nCONFIG_SECURITY_ORIGIN=http://127.0.0.1,http://localhost`n")
Initialize-SecurityOrigin -Path $path
if ((Get-PublicEnvValue (Get-Content $path -Raw) 'CONFIG_SECURITY_ORIGIN')) { throw 'Sample origin was not cleared' }
[System.IO.File]::WriteAllText($path, "CONFIG_PUBLIC_API_BASE_URL=https://zaowua.com/api`nCONFIG_SECURITY_ORIGIN=https://zaowua.com,https://www.zaowua.com`n")
$before = Get-Content $path -Raw
Initialize-SecurityOrigin -Path $path
if ((Get-Content $path -Raw) -ne $before) { throw 'Custom origins were not preserved' }
foreach ($invalid in @('*', 'https://zaowua.com/api', 'https://zaowua.com,', 'https://user:pass@zaowua.com')) {
[System.IO.File]::WriteAllText($path, "CONFIG_PUBLIC_API_BASE_URL=https://zaowua.com/api`nCONFIG_SECURITY_ORIGIN=$invalid`n")
$rejected = $false
try { Initialize-SecurityOrigin -Path $path } catch { $rejected = $true }
if (-not $rejected) { throw "Accepted invalid origin: $invalid" }
}
Write-Host 'Security origin PowerShell tests passed'
} finally {
Remove-Item -Recurse -Force $tempDir
}