ps/Modules/Alkami.PowerShell.Configuration/Public/Set-AppSetting.xml.tests.ps1
2023-05-30 22:51:22 -07:00

289 lines
11 KiB
PowerShell

. $PSScriptRoot\..\..\Load-PesterModules.ps1
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.xml\.tests\.', '.'
$functionPath = Join-Path -Path $here -ChildPath $sut
Write-Host "Overriding SUT: $functionPath"
Get-Module $sut | Remove-Module -Force
# Have to do this to use the private function for Set-AppSettingPrivate
# Write-Host "Overriding SUT: $functionPath"
# Import-Module $functionPath -Force
$moduleForMock = "Alkami.PowerShell.Configuration"
$moduleForMock2 = "Alkami.PowerShell.Common"
Describe "Set-AppSetting - xml" {
#TODO: Refactor - mock Get-AppSetting, mock Read-XmlFile, mock Save-XmlFile
# Mock all the things - See "Get-AppSetting" tests for faking Read-XmlFile results
# Temp file to write content to
$tempFile = [System.IO.Path]::GetTempFileName()
$tempPath = $tempFile.Split(".") | Select-Object -First 1
$fakeConfigFile = Join-Path $tempPath "fake.config"
New-Item -ItemType Directory $tempPath -ErrorAction SilentlyContinue | Out-Null
Write-Host ("Using temp path: $tempPath for tests")
Mock -ModuleName $moduleForMock2 -CommandName Write-Host -MockWith {}
Context "Reports an error when two appSettings sections are present" {
$appSettingsTestContents = @"
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location path="." inheritInChildApplications="false">
<appSettings>
<add key="EnableMobileSite" value="true"/>
<add key="ForceAlwaysMobile" value="false"/>
<add key="EnableRegistrationErrorMessage" value="false"/>
<add key="NewRelic.AppName" value="Alkami Test Web"/>
<add key="AssetServerHost" value=""/>
<add key="IgnoreSecureConnectionRequirement" value="false"/>
<add key="ForceUseOfProductionAssetsInDebug" value="true"/>
<add key="ShouldDisplayUncaughtExceptions" value="false"/>
<add key="AlkamiClientMaxConnections" value="50"/>
<add key="aspnet:MaxJsonDeserializerMembers" value="50000" />
</appSettings>
</location>
<appSettings>
<add key="EagleEyeURL" value="eagleeye.dev.alkamitech.com" />
<add key="SubscriptionServiceMachine" value="localhost" />
<add key="NonDatabaseMicroServiceAccount" value="CORP\dev.micro$" />
<add key="MetalsmithURL" value="metalsmith.dev.alkamitech.com" />
<add key="Broadcasters" value="" />
<add key="DatabaseMicroServiceAccount" value="CORP\dev.dbms$" />
<add key="aspnet:MaxJsonDeserializerMembers" value="60000" />
</appSettings>
</configuration>
"@
Mock -ModuleName $moduleForMock -CommandName Write-Error -MockWith {}
Mock -ModuleName $moduleForMock -CommandName Write-Host -MockWith {}
Mock -ModuleName $moduleForMock -CommandName Write-Warning -MockWith {}
$appSettingsTestContents | Out-File $fakeConfigFile -Force
Set-AppSetting -Path $fakeConfigFile -Key "aspnet:MaxJsonDeserializerMembers" -Value 50000
$value = Get-AppSetting -Path $fakeConfigFile -key "aspnet:MaxJsonDeserializerMembers"
It "still got the right value" {
$value | Should Be "50000"
}
It "did throw an error" {
# Once in Get-AppSetting and once in Set-AppSetting because of the duplicate block
Assert-MockCalled -ModuleName $moduleForMock -CommandName Write-Error -Exactly -Times 2 -Scope Context
}
It "wrote two warnings to the console" {
Assert-MockCalled -ModuleName $moduleForMock -CommandName Write-Warning -Exactly -Times 3 -Scope Context
}
It "wrote four found paths" {
Assert-MockCalled -ModuleName $moduleForMock -CommandName Write-Host -Exactly -Times 8 -Scope Context
}
}
Context "Writes a warning and updates all values if more than one setting exists" {
$appSettingsTestContents = @"
<configuration>
<appSettings>
<add key="FakeSetting" value="FakeValueOriginal" />
<add key="FakeSetting" value="FakeValueDupe" />
</appSettings>
</configuration>
"@
Mock -ModuleName $moduleForMock -CommandName Write-Error -MockWith {}
Mock -ModuleName $moduleForMock -CommandName Write-Host -MockWith {}
Mock -ModuleName $moduleForMock -CommandName Write-Warning -MockWith {}
$appSettingsTestContents | Out-File $fakeConfigFile -Force
Set-AppSetting -Path $fakeConfigFile -Key "FakeSetting" -Value "DuplicateResolved"
It "the file looks accurate" {
## The reason for this joins shenanigans is because Get-Content returns an array of lines, and I don't want a wrapping construct here. This is fine. thisisfine.gif
((Get-Content $fakeConfigFile) -join ' ') | Should -Be '<?xml version="1.0" encoding="utf-8"?> <configuration> <appSettings> <add key="FakeSetting" value="DuplicateResolved" /> <add key="FakeSetting" value="DuplicateResolved" /> </appSettings> </configuration>'
}
$value = Get-AppSetting -Path $fakeConfigFile -key "FakeSetting"
It "still got the right value" {
$value | Should -Be "DuplicateResolved"
}
It "did not throw an error" {
# Once in Get-AppSetting and once in Set-AppSetting because of the duplicate block
Assert-MockCalled -ModuleName $moduleForMock -CommandName Write-Error -Exactly -Times 2 -Scope Context
}
It "wrote a warning to the console" {
Assert-MockCalled -ModuleName $moduleForMock -CommandName Write-Warning -Exactly -Times 1 -Scope Context
}
It "wrote two paths" {
Assert-MockCalled -ModuleName $moduleForMock -CommandName Write-Host -Exactly -Times 4 -Scope Context
}
}
Context "Writes a Warning and Returns Null if the appSettings Node Doesn't Exist" {
$appSettingsTestContents = @"
<configuration>
<appSettings2>
<add key="FakeSetting" value="FakeValue" />
</appSettings2>
</configuration>
"@
Mock -ModuleName $moduleForMock -CommandName Write-Error -MockWith {}
Mock -ModuleName $moduleForMock -CommandName Write-Host -MockWith {}
Mock -ModuleName $moduleForMock -CommandName Write-Warning -MockWith {}
$appSettingsTestContents | Out-File $fakeConfigFile -Force
Set-AppSetting -Path $fakeConfigFile -Key "FakeSetting" -Value 'test'
$value = Get-AppSetting -Path $fakeConfigFile -key "FakeSetting"
It "still got the right value" {
$value | Should -Be 'test'
}
It "did not throw an error" {
Assert-MockCalled -ModuleName $moduleForMock -CommandName Write-Error -Exactly -Times 0 -Scope Context
}
It "wrote a warning to the console" {
Assert-MockCalled -ModuleName $moduleForMock -CommandName Write-Warning -Exactly -Times 1 -Scope Context
}
It "did not output useless information" {
Assert-MockCalled -ModuleName $moduleForMock -CommandName Write-Host -Exactly -Times 1 -Scope Context
}
}
Context "App Setting Key Does Not Exist" {
$appSettingsTestContents = @"
<configuration>
<appSettings>
</appSettings>
</configuration>
"@
Mock -ModuleName $moduleForMock -CommandName Write-Error -MockWith {}
Mock -ModuleName $moduleForMock -CommandName Write-Host -MockWith {}
Mock -ModuleName $moduleForMock -CommandName Write-Warning -MockWith {}
$appSettingsTestContents | Out-File $fakeConfigFile -Force
Set-AppSetting -Path $fakeConfigFile -Key "FakeSetting" -Value 'test'
$value = Get-AppSetting -Path $fakeConfigFile -key "FakeSetting"
It "creates a missing AppSetting key." {
$value | Should -Be 'test'
}
It "did not throw an error" {
Assert-MockCalled -ModuleName $moduleForMock -CommandName Write-Error -Exactly -Times 0 -Scope Context
}
It "wrote a warning to the console" {
Assert-MockCalled -ModuleName $moduleForMock -CommandName Write-Warning -Exactly -Times 0 -Scope Context
}
It "did not output useless information" {
Assert-MockCalled -ModuleName $moduleForMock -CommandName Write-Host -Exactly -Times 1 -Scope Context
}
}
Context "App Setting Key Does Not Exist with -UpdateOnly" {
$appSettingsTestContents = @"
<configuration>
<appSettings>
<add key="FakeSetting" value="FakeValue" />
</appSettings>
</configuration>
"@
Mock -ModuleName $moduleForMock -CommandName Write-Error -MockWith {}
Mock -ModuleName $moduleForMock -CommandName Write-Host -MockWith {}
Mock -ModuleName $moduleForMock -CommandName Write-Warning -MockWith {}
$appSettingsTestContents | Out-File $fakeConfigFile -Force
Set-AppSetting -Path $fakeConfigFile -Key "MissingSetting" -Value 'test' -UpdateOnly
$value = Get-AppSetting -Path $fakeConfigFile -key "MissingSetting"
It "does not create a missing AppSetting key with -UpdateOnly" {
$value | Should -Be $null
}
It "did not throw an error" {
Assert-MockCalled -ModuleName $moduleForMock -CommandName Write-Error -Exactly -Times 0 -Scope Context
}
It "wrote a warning to the console" {
# The one warning is from Get-AppSetting and not Set-AppSetting
Assert-MockCalled -ModuleName $moduleForMock -CommandName Write-Warning -Exactly -Times 1 -Scope Context
}
It "did not output useless information" {
Assert-MockCalled -ModuleName $moduleForMock -CommandName Write-Host -Exactly -Times 1 -Scope Context
}
}
Context "Does a happy path" {
$appSettingsTestContents = @"
<configuration>
<appSettings>
<add key="FakeSetting" value="FakeValue" />
</appSettings>
</configuration>
"@
Mock -ModuleName $moduleForMock -CommandName Write-Error -MockWith {}
Mock -ModuleName $moduleForMock -CommandName Write-Host -MockWith {}
Mock -ModuleName $moduleForMock -CommandName Write-Warning -MockWith {}
$appSettingsTestContents | Out-File $fakeConfigFile -Force
Set-AppSetting -Path $fakeConfigFile -Key "FakeSetting" -Value 'test'
$value = Get-AppSetting -Path $fakeConfigFile -key "FakeSetting"
It "still got the right value" {
$value | Should -Be 'test'
}
It "did not throw an error" {
Assert-MockCalled -ModuleName $moduleForMock -CommandName Write-Error -Exactly -Times 0 -Scope Context
}
It "wrote a warning to the console" {
Assert-MockCalled -ModuleName $moduleForMock -CommandName Write-Warning -Exactly -Times 0 -Scope Context
}
It "did not output useless information" {
Assert-MockCalled -ModuleName $moduleForMock -CommandName Write-Host -Exactly -Times 1 -Scope Context
}
}
Remove-Item $fakeConfigFile -Force
Remove-Item $tempPath -Force
}