86 lines
4.4 KiB
PowerShell
86 lines
4.4 KiB
PowerShell
|
. $PSScriptRoot\..\..\Load-PesterModules.ps1
|
||
|
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
|
||
|
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.tests\.', '.'
|
||
|
$functionPath = Join-Path -Path $here -ChildPath $sut
|
||
|
Write-Host "Overriding SUT: $functionPath"
|
||
|
Import-Module $functionPath -Force
|
||
|
$moduleForMock = ''
|
||
|
|
||
|
Describe 'Get-UsernamesWithProcesses' {
|
||
|
|
||
|
$testServer = 'server.test.local'
|
||
|
|
||
|
Mock -CommandName Test-IsAdmin -ModuleName $moduleForMock -MockWith { return $true }
|
||
|
Mock -CommandName Compare-StringToLocalMachineIdentifiers -ModuleName $moduleForMock -MockWith { return $true }
|
||
|
Mock -CommandName Test-StringIsNullOrWhitespace -ModuleName $moduleForMock -MockWith { return [string]::IsNullOrWhiteSpace($Value) }
|
||
|
Mock -CommandName Invoke-Command -ModuleName $moduleForMock -MockWith { return $ScriptBlock.Invoke() }
|
||
|
|
||
|
Mock -CommandName Get-Process -ModuleName $moduleForMock -MockWith {
|
||
|
return @(
|
||
|
@{ Username = 'Test\Username' },
|
||
|
@{ Username = 'Test\Username' },
|
||
|
@{ Username = $null },
|
||
|
@{ Username = '' },
|
||
|
@{ Username = 'Test2\Username' },
|
||
|
@{ Username = 'Test2\Username2' }
|
||
|
)
|
||
|
}
|
||
|
|
||
|
Context 'Logic' {
|
||
|
|
||
|
It 'Performs command on localhost by default' {
|
||
|
|
||
|
Mock -CommandName Test-StringIsNullOrWhitespace -ModuleName $moduleForMock -MockWith { return $true }
|
||
|
|
||
|
Get-UsernamesWithProcesses | Out-Null
|
||
|
|
||
|
Assert-MockCalled -ModuleName $moduleForMock -CommandName Test-StringIsNullOrWhitespace -Times 1 -Scope It
|
||
|
Assert-MockCalled -ModuleName $moduleForMock -CommandName Compare-StringToLocalMachineIdentifiers -Times 0 -Exactly -Scope It
|
||
|
Assert-MockCalled -ModuleName $moduleForMock -CommandName Get-Process -Times 1 -Exactly -Scope It
|
||
|
Assert-MockCalled -ModuleName $moduleForMock -CommandName Invoke-Command -Times 1 -Exactly -Scope It `
|
||
|
-ParameterFilter { ( -not $PSBoundParameters.ContainsKey('ComputerName')) }
|
||
|
|
||
|
Mock -CommandName Test-StringIsNullOrWhitespace -ModuleName $moduleForMock -MockWith { return [string]::IsNullOrWhiteSpace($Value) }
|
||
|
}
|
||
|
|
||
|
It 'Performs command on localhost if ComputerName matches current server' {
|
||
|
|
||
|
Get-UsernamesWithProcesses -ComputerName $testServer | Out-Null
|
||
|
|
||
|
Assert-MockCalled -ModuleName $moduleForMock -CommandName Test-StringIsNullOrWhitespace -Times 1 -Scope It
|
||
|
Assert-MockCalled -ModuleName $moduleForMock -CommandName Get-Process -Times 1 -Exactly -Scope It
|
||
|
Assert-MockCalled -ModuleName $moduleForMock -CommandName Compare-StringToLocalMachineIdentifiers -Times 1 -Exactly -Scope It `
|
||
|
-ParameterFilter { $stringToCheck -eq $testServer }
|
||
|
Assert-MockCalled -ModuleName $moduleForMock -CommandName Invoke-Command -Times 1 -Exactly -Scope It `
|
||
|
-ParameterFilter { ( -not $PSBoundParameters.ContainsKey('ComputerName')) }
|
||
|
}
|
||
|
|
||
|
It 'Performs command on remote server if ComputerName does not match current server' {
|
||
|
|
||
|
Mock -CommandName Compare-StringToLocalMachineIdentifiers -ModuleName $moduleForMock -MockWith { return $false }
|
||
|
|
||
|
$testServer2 = 'server2.test.local'
|
||
|
|
||
|
Get-UsernamesWithProcesses -ComputerName $testServer2 | Out-Null
|
||
|
|
||
|
Assert-MockCalled -ModuleName $moduleForMock -CommandName Test-StringIsNullOrWhitespace -Times 1 -Scope It
|
||
|
Assert-MockCalled -ModuleName $moduleForMock -CommandName Get-Process -Times 1 -Exactly -Scope It
|
||
|
Assert-MockCalled -ModuleName $moduleForMock -CommandName Compare-StringToLocalMachineIdentifiers -Times 1 -Exactly -Scope It `
|
||
|
-ParameterFilter { $stringToCheck -eq $testServer2 }
|
||
|
Assert-MockCalled -ModuleName $moduleForMock -CommandName Invoke-Command -Times 1 -Exactly -Scope It `
|
||
|
-ParameterFilter { $ComputerName -eq $testServer2 }
|
||
|
|
||
|
Mock -CommandName Compare-StringToLocalMachineIdentifiers -ModuleName $moduleForMock -MockWith { return $true }
|
||
|
}
|
||
|
|
||
|
It 'Returns unique usernames only' {
|
||
|
|
||
|
$result = Get-UsernamesWithProcesses
|
||
|
$result | Should -HaveCount 2
|
||
|
$result | Should -Contain 'Username'
|
||
|
$result | Should -Contain 'Username2'
|
||
|
|
||
|
Assert-MockCalled -ModuleName $moduleForMock -CommandName Get-Process -Times 1 -Exactly -Scope It
|
||
|
}
|
||
|
}
|
||
|
}
|