ps/Modules/Alkami.PowerShell.Configuration/Public/New-WebTierHostFileEntries.tests.ps1

116 lines
6.4 KiB
PowerShell
Raw Normal View History

2023-05-30 22:51:22 -07:00
. $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 "New-WebTierHostFileEntries" {
Mock -CommandName Get-HostsFileContent -ModuleName $moduleForMock -MockWith {
return @"
127.0.0.1 thisexistsalready.fakeplace.com
#127.0.0.1 thisiscommentedout.fakeplace.com
# 127.0.0.1 thisiscommentedout.fakeplace.com
127.0.0.2 someonemisconfiguredthiswebhost.fakeplace.com
127-0-0-1 badbutalmostthesameipmatchingurl.fakeplace.com
"@
}
It "NewEntry_Calls_Add-HostsFileContent_And_DoesNotWarn" {
Mock -CommandName Get-LogLeadName -MockWith {return ""} -ModuleName $moduleForMock
Mock -CommandName Format-Url -MockWith {return $url} -ModuleName $moduleForMock
Mock -CommandName Add-HostsFileContent -MockWith {} -ModuleName $moduleForMock
Mock -CommandName Write-Warning -MockWith {} -ModuleName $moduleForMock
$newEntryUrl = "thisreallydoesnotexist.fakeplace.com"
$expectedWarning = (" : LOOPBACK Hosts File Entry for URL {0} Already Exists" -f $newEntryUrl)
$expectedHostsEntry = "127.0.0.1`t`t$newEntryUrl"
New-WebTierHostFileEntries -Url $newEntryUrl
Assert-MockCalled Write-Warning -Times 0 -Exactly -Scope It -ModuleName $moduleForMock -ParameterFilter {$Message -eq $expectedWarning}
Assert-MockCalled Add-HostsFileContent -Times 1 -Exactly -Scope It -ModuleName $moduleForMock -ParameterFilter {$contentToAdd -eq $expectedHostsEntry}
}
It "NewEntryThatIsSubstringOfExistingEntry_Calls_Add-HostsFileContent_And_DoesNotWarn" {
Mock -CommandName Get-LogLeadName -MockWith {return ""} -ModuleName $moduleForMock
Mock -CommandName Format-Url -MockWith {return $url} -ModuleName $moduleForMock
Mock -CommandName Add-HostsFileContent -MockWith {} -ModuleName $moduleForMock
Mock -CommandName Write-Warning -MockWith {} -ModuleName $moduleForMock
$newEntryUrl = "existsalready.fakeplace.com"
$expectedWarning = (" : LOOPBACK Hosts File Entry for URL {0} Already Exists" -f $newEntryUrl)
$expectedHostsEntry = "127.0.0.1`t`t$newEntryUrl"
New-WebTierHostFileEntries -Url $newEntryUrl
Assert-MockCalled Write-Warning -Times 0 -Exactly -Scope It -ModuleName $moduleForMock -ParameterFilter {$Message -eq $expectedWarning}
Assert-MockCalled Add-HostsFileContent -Times 1 -Exactly -Scope It -ModuleName $moduleForMock -ParameterFilter {$contentToAdd -eq $expectedHostsEntry}
}
It "ExistingEntry_WritesMessage_And_DoesNotCall_Add-HostsFileContent" {
Mock -CommandName Get-LogLeadName -MockWith {return ""} -ModuleName $moduleForMock
Mock -CommandName Format-Url -MockWith {return $url} -ModuleName $moduleForMock
Mock -CommandName Add-HostsFileContent -MockWith {} -ModuleName $moduleForMock
Mock -CommandName Write-Warning -MockWith {} -ModuleName $moduleForMock
$existingEntryUrl = "thisexistsalready.fakeplace.com"
$expectedWarning = (" : LOOPBACK Hosts File Entry for URL {0} Already Exists" -f $existingEntryUrl)
New-WebTierHostFileEntries -Url $existingEntryUrl
Assert-MockCalled Write-Warning -Times 1 -Exactly -Scope It -ModuleName $moduleForMock -ParameterFilter {$Message -eq $expectedWarning}
Assert-MockCalled Add-HostsFileContent -Times 0 -Exactly -Scope It -ModuleName $moduleForMock
}
It "CommentedEntry_Calls_Add-HostsFileContent_And_DoesNotWarn" {
Mock -CommandName Get-LogLeadName -MockWith {return ""} -ModuleName $moduleForMock
Mock -CommandName Format-Url -MockWith {return $url} -ModuleName $moduleForMock
Mock -CommandName Add-HostsFileContent -MockWith {} -ModuleName $moduleForMock
Mock -CommandName Write-Warning -MockWith {} -ModuleName $moduleForMock
$commentedEntryUrl = "thisiscommentedout.fakeplace.com"
$expectedWarning = (" : LOOPBACK Hosts File Entry for URL {0} Already Exists" -f $commentedEntryUrl)
$expectedHostsEntry = "127.0.0.1`t`t$commentedEntryUrl"
New-WebTierHostFileEntries -Url $commentedEntryUrl
Assert-MockCalled Write-Warning -Times 0 -Exactly -Scope It -ModuleName $moduleForMock -ParameterFilter {$Message -eq $expectedWarning}
Assert-MockCalled Add-HostsFileContent -Times 1 -Exactly -Scope It -ModuleName $moduleForMock -ParameterFilter {$contentToAdd -eq $expectedHostsEntry}
}
It "NonLoopback_MatchingUrl_Calls_Add-HostsFileContent_And_DoesNotWarn" {
Mock -CommandName Get-LogLeadName -MockWith {return ""} -ModuleName $moduleForMock
Mock -CommandName Format-Url -MockWith {return $url} -ModuleName $moduleForMock
Mock -CommandName Add-HostsFileContent -MockWith {} -ModuleName $moduleForMock
Mock -CommandName Write-Warning -MockWith {} -ModuleName $moduleForMock
$nonLoopbackMatchingEntryUrl = "someonemisconfiguredthiswebhost.fakeplace.com"
$expectedWarning = (" : LOOPBACK Hosts File Entry for URL {0} Already Exists" -f $nonLoopbackMatchingEntryUrl)
$expectedHostsEntry = "127.0.0.1`t`t$nonLoopbackMatchingEntryUrl"
New-WebTierHostFileEntries -Url $nonLoopbackMatchingEntryUrl
Assert-MockCalled Write-Warning -Times 0 -Exactly -Scope It -ModuleName $moduleForMock -ParameterFilter {$Message -eq $expectedWarning}
Assert-MockCalled Add-HostsFileContent -Times 1 -Exactly -Scope It -ModuleName $moduleForMock -ParameterFilter {$contentToAdd -eq $expectedHostsEntry}
}
It "SimilarIp_MatchingUrl_Calls_Add-HostsFileContent_And_DoesNotWarn" {
Mock -CommandName Get-LogLeadName -MockWith {return ""} -ModuleName $moduleForMock
Mock -CommandName Format-Url -MockWith {return $url} -ModuleName $moduleForMock
Mock -CommandName Add-HostsFileContent -MockWith {} -ModuleName $moduleForMock
Mock -CommandName Write-Warning -MockWith {} -ModuleName $moduleForMock
$similarIpUrl = "badbutalmostthesameipmatchingurl.fakeplace.com"
$expectedWarning = (" : LOOPBACK Hosts File Entry for URL {0} Already Exists" -f $similarIpUrl)
$expectedHostsEntry = "127.0.0.1`t`t$similarIpUrl"
New-WebTierHostFileEntries -Url $similarIpUrl
Assert-MockCalled Write-Warning -Times 0 -Exactly -Scope It -ModuleName $moduleForMock -ParameterFilter {$Message -eq $expectedWarning}
Assert-MockCalled Add-HostsFileContent -Times 1 -Exactly -Scope It -ModuleName $moduleForMock -ParameterFilter {$contentToAdd -eq $expectedHostsEntry}
}
}