ps/Modules/Alkami.DevOps.SystemEngineering/Public/Disable-AlkamiDomainAccounts.tests.ps1
2023-05-30 22:51:22 -07:00

48 lines
2.5 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 "Disable-AlkamiDomainAccounts" {
Mock -CommandName Get-LogLeadName -ModuleName $moduleForMock -MockWith { return 'Disable-AlkamiDomainAccounts.tests' }
Mock -CommandName Disable-ActiveDirectoryAccount -ModuleName $moduleForMock -MockWith { }
Mock -CommandName Move-AccountToDisabledOU -ModuleName $moduleForMock -MockWith { }
Mock -CommandName Write-Warning -ModuleName $moduleForMock -MockWith { }
$fakeAccountName = "FakeyMcFakeAccount"
Context "User Permissions" {
It "Writes a Warning and Exits Early if the User Does Not Have Domain Admin Rights" {
Mock -CommandName Test-IsUserDomainAdmin -ModuleName $moduleForMock -MockWith { return $false }
Mock -CommandName Get-ActiveDirectoryAccount -ModuleName $moduleForMock -MockWith { }
Disable-AlkamiDomainAccounts @($fakeAccountName)
Assert-MockCalled -ModuleName $moduleForMock -CommandName Write-Warning `
-ParameterFilter { $Message -match "You must have domain administrative privileges" } -Times 1 -Exactly -Scope It
Assert-MockCalled -ModuleName $moduleForMock -CommandName Get-ActiveDirectoryAccount -Times 0 -Exactly -Scope It
}
}
Context "Parameter Validation and Manipulation" {
Mock Test-IsUserDomainAdmin -ModuleName $moduleForMock -MockWith { return $true }
Mock Get-ActiveDirectoryAccount -ModuleName $moduleForMock -MockWith { return $null }
It "Writes a Warning and Exits if the User Is Not Found" {
Mock Write-Warning -ModuleName $moduleForMock -MockWith { }
Disable-AlkamiDomainAccounts @($fakeAccountName)
Assert-MockCalled -ModuleName $moduleForMock -CommandName Write-Warning `
-ParameterFilter { $Message -match "Account named \[$fakeAccountName\] not found" } -Times 1 -Exactly -Scope It
Assert-MockCalled -ModuleName $moduleForMock -CommandName Disable-ActiveDirectoryAccount -Times 0 -Exactly -Scope It
Assert-MockCalled -ModuleName $moduleForMock -CommandName Move-AccountToDisabledOU -Times 0 -Exactly -Scope It
}
}
}