Files
SGU-CredentialProvider/tests/WelcomeWallpaper.Tests.ps1
T

175 lines
7.9 KiB
PowerShell

$repositoryRoot = (Resolve-Path (Join-Path $PSScriptRoot '..')).Path
$wallpaperScript = Join-Path $repositoryRoot 'scripts\Set-SguWelcomeWallpaper.ps1'
$source = Get-Content -LiteralPath $wallpaperScript -Raw
$tokens = $null
$parseErrors = $null
$ast = [Management.Automation.Language.Parser]::ParseFile($wallpaperScript, [ref]$tokens, [ref]$parseErrors)
if ($parseErrors.Count) { throw ($parseErrors -join [Environment]::NewLine) }
$lookup = $ast.Find({
param($node)
$node -is [Management.Automation.Language.FunctionDefinitionAst] -and
$node.Name -eq 'Get-DirectoryWelcomeMetadata'
}, $true)
function Invoke-WelcomeFixture {
param(
$DirectoryGender,
[string]$ExplicitGender,
[string]$DirectoryLocation = 'Sala de pruebas',
[string]$DirectoryOu = 'Laboratorio',
[string]$MachineLocation,
[string]$MachineOu
)
# Replace only the external directory lookup. Execute the actual script,
# including its validated parameters, metadata assignment and JPEG renderer.
$fixtureJson = [pscustomobject]@{
DisplayName = 'Usuario de prueba'
Gender = $DirectoryGender
Location = $DirectoryLocation
OrganizationalUnit = $DirectoryOu
} | ConvertTo-Json -Compress
$fixtureFunction = 'function Get-DirectoryWelcomeMetadata { param($UserName, $MachineName); ConvertFrom-Json ''' +
$fixtureJson.Replace("'", "''") + ''' }'
$fixtureSource = $source.Remove($lookup.Extent.StartOffset, $lookup.Extent.EndOffset - $lookup.Extent.StartOffset).
Insert($lookup.Extent.StartOffset, $fixtureFunction)
$testScript = Join-Path $TestDrive ('wallpaper-' + [Guid]::NewGuid().ToString('N') + '.ps1')
[IO.File]::WriteAllText($testScript, $fixtureSource, [Text.UTF8Encoding]::new($false))
$parameters = @{
BaseImagePath = Join-Path $repositoryRoot 'assets\branding\darkblue.jpg'
FontsPath = Join-Path $repositoryRoot 'assets\branding\fonts'
OutputPath = Join-Path $TestDrive ([IO.Path]::GetFileNameWithoutExtension($testScript) + '.jpg')
CanvasWidth = 640
CanvasHeight = 480
SkipApply = $true
}
if ($ExplicitGender) { $parameters.Gender = $ExplicitGender }
$previousLocalAppData = $env:LOCALAPPDATA
$previousMachineLocation = $env:SGU_WELCOME_LOCATION
$previousMachineOu = $env:SGU_WELCOME_ORGANIZATIONAL_UNIT
try {
$env:LOCALAPPDATA = $TestDrive
$env:SGU_WELCOME_LOCATION = $MachineLocation
$env:SGU_WELCOME_ORGANIZATIONAL_UNIT = $MachineOu
& $testScript @parameters
}
finally {
$env:LOCALAPPDATA = $previousLocalAppData
$env:SGU_WELCOME_LOCATION = $previousMachineLocation
$env:SGU_WELCOME_ORGANIZATIONAL_UNIT = $previousMachineOu
}
}
Describe 'Welcome wallpaper with AD metadata' {
It 'renders a neutral JPEG when AD has no gender' {
$result = Invoke-WelcomeFixture -DirectoryGender $null
$result.WelcomeHeading | Should Be 'Te damos la bienvenida,'
$result.Applied | Should Be $false
$bitmap = [Drawing.Image]::FromFile($result.OutputPath)
try { $bitmap.Width | Should Be 640; $bitmap.Height | Should Be 480 }
finally { $bitmap.Dispose() }
}
It 'uses neutral wording for empty or unrecognized metadata' {
foreach ($value in @('', 'Unknown')) {
(Invoke-WelcomeFixture -DirectoryGender $value).WelcomeHeading | Should Be 'Te damos la bienvenida,'
}
}
It 'keeps the gendered greetings for recognized directory values' {
(Invoke-WelcomeFixture -DirectoryGender 'Female').WelcomeHeading | Should Be 'Bienvenida,'
(Invoke-WelcomeFixture -DirectoryGender 'Male').WelcomeHeading | Should Be 'Bienvenido,'
}
It 'honors an explicit gender over directory metadata' {
(Invoke-WelcomeFixture -DirectoryGender 'Female' -ExplicitGender 'Male').WelcomeHeading | Should Be 'Bienvenido,'
}
It 'renders the flexible classroom and immediate OU as an access label' {
$result = Invoke-WelcomeFixture -DirectoryGender $null `
-DirectoryLocation 'Aula Flexible' `
-DirectoryOu 'Centro de Experiencia Digital'
$result.LocationText | Should Be "Acceso al Aula Flexible`ndel Centro de Experiencia Digital."
$result.LocationPrimaryText | Should Be 'Acceso al Aula Flexible'
$result.LocationSecondaryText | Should Be 'del Centro de Experiencia Digital.'
}
It 'uses the requested neutral, masculine and feminine location wording' {
$neutral = Invoke-WelcomeFixture -DirectoryGender $null `
-DirectoryLocation 'Sala de Aplicaciones' `
-DirectoryOu 'Laboratorio de Cómputo de Ingeniería'
$neutral.LocationPrimaryText | Should Be 'Acceso a la Sala de Aplicaciones'
$neutral.LocationSecondaryText | Should Be 'del Laboratorio de Cómputo de Ingeniería.'
$male = Invoke-WelcomeFixture -DirectoryGender 'Male' `
-DirectoryLocation 'Aula Flexible' `
-DirectoryOu 'Centro de Experiencia Digital'
$male.LocationPrimaryText | Should Be 'Estás ubicado en el Aula Flexible'
$male.LocationSecondaryText | Should Be 'del Centro de Experiencia Digital.'
$female = Invoke-WelcomeFixture -DirectoryGender 'Female' `
-DirectoryLocation 'Sala de Redes' `
-DirectoryOu 'Laboratorio de Cómputo de Ingeniería'
$female.LocationPrimaryText | Should Be 'Estás ubicada en la Sala de Redes'
$female.LocationSecondaryText | Should Be 'del Laboratorio de Cómputo de Ingeniería.'
}
It 'keeps machine location metadata when the GPO command has no location arguments' {
$result = Invoke-WelcomeFixture -DirectoryGender $null `
-DirectoryLocation 'Sala de pruebas' `
-DirectoryOu 'Laboratorio' `
-MachineLocation 'Aula Flexible' `
-MachineOu 'Centro de Experiencia Digital'
$result.LocationText | Should Be "Acceso al Aula Flexible`ndel Centro de Experiencia Digital."
}
It 'ships the logo at the lower right of the base image instead of compositing it at runtime' {
$source | Should Not Match 'lasalle-logo-blanco|DrawImage.*logo|composite.*logo'
$bitmap = [Drawing.Bitmap]::FromFile((Join-Path $repositoryRoot 'assets\branding\darkblue.jpg'))
try {
$upperLeftWhitePixels = 0
for ($x = 60; $x -lt 390; $x += 2) {
for ($y = 45; $y -lt 175; $y += 2) {
$pixel = $bitmap.GetPixel($x, $y)
if ($pixel.R -gt 220 -and $pixel.G -gt 220 -and $pixel.B -gt 220) {
$upperLeftWhitePixels++
}
}
}
$lowerRightWhitePixels = 0
for ($x = 1260; $x -lt 1510; $x += 2) {
for ($y = 740; $y -lt 860; $y += 2) {
$pixel = $bitmap.GetPixel($x, $y)
if ($pixel.R -gt 220 -and $pixel.G -gt 220 -and $pixel.B -gt 220) {
$lowerRightWhitePixels++
}
}
}
($upperLeftWhitePixels -lt 25) | Should Be $true
($lowerRightWhitePixels -gt 250) | Should Be $true
}
finally {
$bitmap.Dispose()
}
}
It 'keeps the lock screen base free of the desktop logo' {
$bitmap = [Drawing.Bitmap]::FromFile((Join-Path $repositoryRoot 'assets\branding\darkblue-lockscreen.jpg'))
try {
$whitePixels = 0
for ($x = 0; $x -lt $bitmap.Width; $x += 4) {
for ($y = 0; $y -lt $bitmap.Height; $y += 4) {
$pixel = $bitmap.GetPixel($x, $y)
if ($pixel.R -gt 220 -and $pixel.G -gt 220 -and $pixel.B -gt 220) {
$whitePixels++
}
}
}
($whitePixels -lt 10) | Should Be $true
}
finally {
$bitmap.Dispose()
}
}
}