noid-privacy/Tests/Unit/EdgeHardening.Tests.ps1
NexusOne23 da9f937ee8 release: v2.2.3 - Fix Restore Mode module selection crash
CHANGELOG:
- Fixed: Restore Mode manual module selection crash (Critical)
- Root cause: .Split(',', ';', ' ') triggered wrong .NET overload
- Fix: Replaced with native PowerShell -split '[,; ]' operator
- Reported by: KatCat2

VERSION BUMP: 2.2.2 -> 2.2.3
- Updated 48 files with new version number
- CHANGELOG.md: Added v2.2.3 release notes
- README.md: Updated badge, module table, project status
2026-01-07 18:46:14 +01:00

158 lines
5.6 KiB
PowerShell

<#
.SYNOPSIS
Unit tests for EdgeHardening module
.DESCRIPTION
Pester v5 tests for the EdgeHardening module functionality.
Tests return values, DryRun behavior, and configuration handling.
.NOTES
Author: NexusOne23
Version: 2.2.3
Requires: Pester 5.0+
#>
BeforeAll {
# Import the module being tested
$modulePath = Join-Path (Split-Path (Split-Path $PSScriptRoot -Parent) -Parent) "Modules\EdgeHardening\EdgeHardening.psm1"
if (Test-Path $modulePath) {
Import-Module $modulePath -Force
}
else {
throw "Module not found: $modulePath"
}
# Import Core modules for testing
$coreModules = @("Logger.ps1", "Config.ps1", "Validator.ps1", "Rollback.ps1")
$corePath = Join-Path (Split-Path (Split-Path $PSScriptRoot -Parent) -Parent) "Core"
foreach ($module in $coreModules) {
$moduleFile = Join-Path $corePath $module
if (Test-Path $moduleFile) {
. $moduleFile
}
}
# Import Utils modules
$utilsModules = @("Registry.ps1")
$utilsPath = Join-Path (Split-Path (Split-Path $PSScriptRoot -Parent) -Parent) "Utils"
foreach ($module in $utilsModules) {
$moduleFile = Join-Path $utilsPath $module
if (Test-Path $moduleFile) {
. $moduleFile
}
}
# Initialize logging (silent for tests)
if (Get-Command Initialize-Logger -ErrorAction SilentlyContinue) {
Initialize-Logger -EnableConsole $false
}
# Initialize config
if (Get-Command Initialize-Config -ErrorAction SilentlyContinue) {
$configPath = Join-Path (Split-Path (Split-Path $PSScriptRoot -Parent) -Parent) "config.json"
Initialize-Config -ConfigPath $configPath
}
}
Describe "EdgeHardening Module" {
Context "Module Structure" {
It "Should export Invoke-EdgeHardening function" {
$command = Get-Command -Name Invoke-EdgeHardening -ErrorAction SilentlyContinue
$command | Should -Not -BeNullOrEmpty
}
It "Should export Test-EdgeHardening function" {
$command = Get-Command -Name Test-EdgeHardening -ErrorAction SilentlyContinue
$command | Should -Not -BeNullOrEmpty
}
It "Should have correct function type" {
$command = Get-Command -Name Invoke-EdgeHardening
$command.CommandType | Should -Be 'Function'
}
It "Should have CmdletBinding attribute" {
$command = Get-Command -Name Invoke-EdgeHardening
$command.CmdletBinding | Should -Be $true
}
}
Context "Function Parameters" {
It "Should have DryRun parameter" {
$command = Get-Command -Name Invoke-EdgeHardening
$command.Parameters.ContainsKey('DryRun') | Should -Be $true
}
It "DryRun parameter should be a switch" {
$command = Get-Command -Name Invoke-EdgeHardening
$command.Parameters['DryRun'].ParameterType.Name | Should -Be 'SwitchParameter'
}
It "Should have AllowExtensions parameter" {
$command = Get-Command -Name Invoke-EdgeHardening
$command.Parameters.ContainsKey('AllowExtensions') | Should -Be $true
}
It "AllowExtensions parameter should be a switch" {
$command = Get-Command -Name Invoke-EdgeHardening
$command.Parameters['AllowExtensions'].ParameterType.Name | Should -Be 'SwitchParameter'
}
}
Context "Configuration" {
It "EdgePolicies.json should exist" {
$configPath = Join-Path (Split-Path (Split-Path $PSScriptRoot -Parent) -Parent) "Modules\EdgeHardening\Config\EdgePolicies.json"
Test-Path $configPath | Should -Be $true
}
It "EdgePolicies.json should be valid JSON" {
$configPath = Join-Path (Split-Path (Split-Path $PSScriptRoot -Parent) -Parent) "Modules\EdgeHardening\Config\EdgePolicies.json"
{ Get-Content $configPath -Raw | ConvertFrom-Json } | Should -Not -Throw
}
It "EdgePolicies.json should contain policies" {
$configPath = Join-Path (Split-Path (Split-Path $PSScriptRoot -Parent) -Parent) "Modules\EdgeHardening\Config\EdgePolicies.json"
$config = Get-Content $configPath -Raw | ConvertFrom-Json
$config.Policies | Should -Not -BeNullOrEmpty
$config.Policies.Count | Should -BeGreaterThan 0
}
It "All policies should be valid objects" {
$configPath = Join-Path (Split-Path (Split-Path $PSScriptRoot -Parent) -Parent) "Modules\EdgeHardening\Config\EdgePolicies.json"
$config = Get-Content $configPath -Raw | ConvertFrom-Json
$config.Policies | Should -Not -BeNullOrEmpty
}
}
Context "DryRun Behavior" -Skip:$true {
# These tests require Core modules and admin rights - skipped on CI
It "Should accept DryRun parameter without errors" -Tag 'Interactive' {
{ Invoke-EdgeHardening -DryRun -ErrorAction Stop } | Should -Not -Throw
}
}
Context "Test-EdgeHardening Function" {
It "Should run Test-EdgeHardening without errors" {
{ Test-EdgeHardening -ErrorAction Stop } | Should -Not -Throw
}
It "Should return compliance results" {
$result = Test-EdgeHardening -ErrorAction SilentlyContinue
$result | Should -Not -BeNullOrEmpty
}
}
}
AfterAll {
# Cleanup
Remove-Module EdgeHardening -ErrorAction SilentlyContinue
}