. $PSScriptRoot\..\..\Load-PesterModules.ps1 $here = Split-Path -Parent $MyInvocation.MyCommand.Path $sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.tests\.', '.' $global:functionPath = Join-Path -Path $here -ChildPath $sut Write-Host "Overriding SUT: $functionPath" InModuleScope -ModuleName Alkami.DevOps.Operations -ScriptBlock { Write-Host "InModuleScope - Overriding SUT: $($global:functionPath)" Import-Module $global:functionPath -Force $moduleForMock = '' $inScopeModuleForAssert = 'Alkami.DevOps.Operations' Describe 'Get-ADUserProfileListToRemove' { Mock -CommandName Get-ADUserProfileList -ModuleName $moduleForMock -MockWith { $result = @() $result += New-Object PSObject @{ LocalPath = 'TestDrive:\Test\test1$' } $result += New-Object PSObject @{ LocalPath = 'TestDrive:\Test\test2' } $result += New-Object PSObject @{ LocalPath = 'TestDrive:\Test\test3' } return $result } Mock -CommandName Get-UsernamesWithProcesses -ModuleName $moduleForMock -MockWith { return @('test2', 'test4', 'test5') } Context 'Input Validation' { It 'Throws if Domain list contains invalid value' { { Get-ADUserProfileListToRemove -Domains @('Test') } | Should -Throw } } Context 'Logic' { It 'Performs command on localhost by default' { Get-ADUserProfileListToRemove | Out-Null Assert-MockCalled -ModuleName $inScopeModuleForAssert -CommandName Get-ADUserProfileList -Times 1 -Exactly -Scope It ` -ParameterFilter { [string]::IsNullOrWhiteSpace($ComputerName) } Assert-MockCalled -ModuleName $inScopeModuleForAssert -CommandName Get-UsernamesWithProcesses -Times 1 -Exactly -Scope It ` -ParameterFilter { [string]::IsNullOrWhiteSpace($ComputerName) } } It 'Uses ComputerName parameter if provided' { $test = 'server1.test.local' Get-ADUserProfileListToRemove -ComputerName $test | Out-Null Assert-MockCalled -ModuleName $inScopeModuleForAssert -CommandName Get-ADUserProfileList -Times 1 -Exactly -Scope It ` -ParameterFilter { $ComputerName -eq $test } Assert-MockCalled -ModuleName $inScopeModuleForAssert -CommandName Get-UsernamesWithProcesses -Times 1 -Exactly -Scope It ` -ParameterFilter { $ComputerName -eq $test } } It 'Uses Domain parameter if provided' { $test = @('CORP') Get-ADUserProfileListToRemove -Domains $test | Out-Null Assert-MockCalled -ModuleName $inScopeModuleForAssert -CommandName Get-ADUserProfileList -Times 1 -Exactly -Scope It ` -ParameterFilter { $null -eq (Compare-Object $Domains $test ) } } It 'Filters out users with running processes' { $results = Get-ADUserProfileListToRemove foreach ( $result in $results ) { $result.LocalPath | Should -Not -Match 'test2' } } It 'Filters out user profiles with a local path containing $' { $results = Get-ADUserProfileListToRemove foreach ( $result in $results ) { $result.LocalPath.Contains('$') | Should -BeFalse } } } } }