. $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-ADUserProfileList' { Mock -CommandName Get-LogLeadName -ModuleName $moduleForMock -MockWith { return 'Get-ADUserProfileList.tests' } Mock -CommandName Get-Command -ModuleName $moduleForMock -MockWith { return $true } Mock -CommandName Compare-StringToLocalMachineIdentifiers -ModuleName $moduleForMock -MockWith { return $true } Mock -CommandName Test-IsCollectionNullOrEmpty -ModuleName $moduleForMock -MockWith { return $false } Mock -CommandName Write-Warning -ModuleName $moduleForMock -MockWith {} Mock -CommandName Write-Verbose -ModuleName $moduleForMock -MockWith {} Mock -CommandName Test-StringIsNullOrWhitespace -ModuleName $moduleForMock -MockWith { return $true } Mock -CommandName Get-CimInstance -ModuleName $moduleForMock -MockWith { $result = @() $result += New-Object PSObject @{ SID = "$($_DomainSidPrefix['CORP'])-12345" } $result += New-Object PSObject @{ SID = "$($_DomainSidPrefix['CORP'])-54321" } $result += New-Object PSObject @{ SID = "$($_DomainSidPrefix['FH'])-12345" } $result += New-Object PSObject @{ SID = '1-2-3-4-5-12345' } return $result } Context 'Input Validation' { It 'Throws if Domain list contains invalid value' { { Get-ADUserProfileList -Domains @('Test') } | Should -Throw } } Context 'Logic' { It 'Writes warning if Get-CimInstance is unavailable' { Mock -CommandName Get-Command -ModuleName $moduleForMock -MockWith { return $null } Get-ADUserProfileList | Out-Null Assert-MockCalled -ModuleName $inScopeModuleForAssert -CommandName Write-Warning -Times 1 -Exactly -Scope It ` -ParameterFilter { $Message -match "Can't CimInstance on this host; aborting." } Mock -CommandName Get-Command -ModuleName $moduleForMock -MockWith { return $true } } It 'Returns empty list and aborts if Get-CimInstance is unavailable' { Mock -CommandName Get-Command -ModuleName $moduleForMock -MockWith { return $null } $result = Get-ADUserProfileList $result | Should -HaveCount 0 Assert-MockCalled -ModuleName $inScopeModuleForAssert -CommandName Get-CimInstance -Times 0 -Exactly -Scope It Mock -CommandName Get-Command -ModuleName $moduleForMock -MockWith { return $true } } It 'Performs command on localhost by default' { Get-ADUserProfileList | Out-Null Assert-MockCalled -ModuleName $inScopeModuleForAssert -CommandName Test-StringIsNullOrWhitespace -Times 1 -Exactly -Scope It Assert-MockCalled -ModuleName $inScopeModuleForAssert -CommandName Compare-StringToLocalMachineIdentifiers -Times 0 -Exactly -Scope It Assert-MockCalled -ModuleName $inScopeModuleForAssert -CommandName Write-Warning -Times 0 -Exactly -Scope It Assert-MockCalled -ModuleName $inScopeModuleForAssert -CommandName Get-CimInstance -Times 1 -Exactly -Scope It ` -ParameterFilter { ( -not $PSBoundParameters.ContainsKey('ComputerName')) } } It 'Performs command on localhost if ComputerName matches current server' { Mock -CommandName Test-StringIsNullOrWhitespace -ModuleName $moduleForMock -MockWith { return $false } $test = 'server1.test.local' Get-ADUserProfileList -ComputerName $test | Out-Null Assert-MockCalled -ModuleName $inScopeModuleForAssert -CommandName Test-StringIsNullOrWhitespace -Times 1 -Exactly -Scope It ` -ParameterFilter { $Value -eq $test } Assert-MockCalled -ModuleName $inScopeModuleForAssert -CommandName Compare-StringToLocalMachineIdentifiers -Times 1 -Exactly -Scope It ` -ParameterFilter { $stringToCheck -eq $test } Assert-MockCalled -ModuleName $inScopeModuleForAssert -CommandName Get-CimInstance -Times 1 -Exactly -Scope It ` -ParameterFilter { ( -not $PSBoundParameters.ContainsKey('ComputerName')) } Mock -CommandName Test-StringIsNullOrWhitespace -ModuleName $moduleForMock -MockWith { return $true } } It 'Performs command on remote server if ComputerName does not match current server' { $test = 'server2.test.local' Mock -CommandName Test-StringIsNullOrWhitespace -ModuleName $moduleForMock -MockWith { return $false } Mock -CommandName Compare-StringToLocalMachineIdentifiers -ModuleName $moduleForMock -MockWith { return $false } Get-ADUserProfileList -ComputerName $test | Out-Null Assert-MockCalled -ModuleName $inScopeModuleForAssert -CommandName Test-StringIsNullOrWhitespace -Times 1 -Exactly -Scope It ` -ParameterFilter { $Value -eq $test } Assert-MockCalled -ModuleName $inScopeModuleForAssert -CommandName Compare-StringToLocalMachineIdentifiers -Times 1 -Exactly -Scope It ` -ParameterFilter { $stringToCheck -eq $test } Assert-MockCalled -ModuleName $inScopeModuleForAssert -CommandName Get-CimInstance -Times 1 -Exactly -Scope It ` -ParameterFilter { $ComputerName -eq $test } Mock -CommandName Test-StringIsNullOrWhitespace -ModuleName $moduleForMock -MockWith { return $true } Mock -CommandName Compare-StringToLocalMachineIdentifiers -ModuleName $moduleForMock -MockWith { return $true } } It 'Filters out non-Active Directory profiles' { $results = Get-ADUserProfileList foreach ( $result in $results ) { ($result.SID.StartsWith($_DomainSidPrefix['CORP']) -or $result.SID.StartsWith($_DomainSidPrefix['FH'])) | Should -BeTrue } } It 'Applies Domain filter if specified' { $results = Get-ADUserProfileList -Domains @('CORP') foreach ( $result in $results ) { $result.SID.StartsWith($_DomainSidPrefix['CORP']) | Should -BeTrue } } } } }