Skip to content
This repository was archived by the owner on Apr 27, 2021. It is now read-only.
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
param(
[string] $targetFilePath = "$PSScriptRoot\..\Install-Identity-Utilities.psm1"
)

Write-Host $targetFilePath
# Force re-import to pick up latest changes
Import-Module $targetFilePath -Force

Describe 'Confirm-DiscoveryConfig Unit Tests'{
InModuleScope Install-Identity-Utilities{
It 'Should validate valid discovery config settings'{
# Arrange
$discoveryConfig = @{ appName = "DiscoveryService"; appPoolName = "DiscoveryService"; siteName = "Default Web Site"}
$commonConfig = @{ sqlServerAddress = "localhost"; metadataDbName = "EDWAdmin"; webServerDomain = "host.domain.local"; clientEnvironment = "dev" }

# Act/Assert
{ Confirm-DiscoveryConfig -discoveryConfig $discoveryConfig -commonConfig $commonConfig } | Should -Not -Throw
}
It 'Should throw if there is an invalid setting'{
# Arrange
$discoveryConfig = @{}
$commonConfig = @{}

# Act/Assert
{ Confirm-DiscoveryConfig -discoveryConfig $discoveryConfig -commonConfig $commonConfig } | Should -Throw
}
}
}
48 changes: 48 additions & 0 deletions Fabric.Identity.API/scripts/tests/Get-AppInsightsKey.tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
param(
[string] $targetFilePath = "$PSScriptRoot\..\Install-Identity-Utilities.psm1"
)

Write-Host $targetFilePath
# Force re-import to pick up latest changes
Import-Module $targetFilePath -Force

$Global:testInstallFile = "install.config"
$Global:testInstallFileLoc = "$PSScriptRoot\$testInstallFile"

Describe 'Get-AppInsightsKey'{
Context 'Quiet Mode'{
InModuleScope Install-Identity-Utilities{
It 'Should return stored app insights key'{
# Arrange
Mock -ModuleName Install-Identity-Utilities -CommandName Read-Host -MockWith { }
Mock -ModuleName Install-Identity-Utilities -CommandName Add-InstallationSetting -MockWith {}

# Act
$appInsightsKey = Get-AppInsightsKey -appInsightsInstrumentationKey "123456" -installConfigPath $testInstallFileLoc -scope "identity" -quiet $true

# Assert
$appInsightsKey | Should -Be "123456"
Assert-MockCalled -ModuleName Install-Identity-Utilities -CommandName Read-Host -Times 0 -Exactly
Assert-MockCalled -ModuleName Install-Identity-Utilities -CommandName Add-InstallationSetting -Times 2 -Exactly
}
}
}

Context 'Interactive Mode'{
InModuleScope Install-Identity-Utilities{
It 'Should return user entered app insights key'{
# Arrange
Mock -ModuleName Install-Identity-Utilities -CommandName Read-Host -MockWith { return "567890" }
Mock -ModuleName Install-Identity-Utilities -CommandName Add-InstallationSetting -MockWith {}

# Act
$appInsightsKey = Get-AppInsightsKey -appInsightsInstrumentationKey "123456" -installConfigPath $testInstallFileLoc -scope "identity" -quiet $false

# Assert
$appInsightsKey | Should -Be "567890"
Assert-MockCalled -ModuleName Install-Identity-Utilities -CommandName Read-Host -Times 1 -Exactly
Assert-MockCalled -ModuleName Install-Identity-Utilities -CommandName Add-InstallationSetting -Times 2 -Exactly
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
param(
[string] $targetFilePath = "$PSScriptRoot\..\Install-Identity-Utilities.psm1"
)

Write-Host $targetFilePath
# Force re-import to pick up latest changes
Import-Module $targetFilePath -Force

$Global:testInstallFile = "install.config"
$Global:testInstallFileLoc = "$PSScriptRoot\$testInstallFile"

Describe 'Get-ApplicationEndpoint'{
Context 'Quiet Mode'{
InModuleScope Install-Identity-Utilities{
It 'Should return stored ApplicationEndpoint'{
# Arrange
Mock -ModuleName Install-Identity-Utilities -CommandName Get-FullyQualifiedMachineName -MockWith { }
Mock -ModuleName Install-Identity-Utilities -CommandName Read-Host -MockWith { }
Mock -ModuleName Install-Identity-Utilities -CommandName Add-InstallationSetting -MockWith {}

# Act
$appEndpoint = Get-ApplicationEndpoint -appName "identity" -applicationEndpoint "https://host.fabric.local/identity" -installConfigPath $testInstallFileLoc -scope "identity" -quiet $true

# Assert
$appEndpoint | Should -Be "https://host.fabric.local/identity"
Assert-MockCalled -ModuleName Install-Identity-Utilities -CommandName Read-Host -Times 0 -Exactly
Assert-MockCalled -ModuleName Install-Identity-Utilities -CommandName Get-FullyQualifiedMachineName -Times 0 -Exactly
Assert-MockCalled -ModuleName Install-Identity-Utilities -CommandName Add-InstallationSetting -Times 2 -Exactly
}
}
}

Context 'Interactive Mode'{
InModuleScope Install-Identity-Utilities{
It 'Should return user DiscoveryService URL'{
# Arrange
Mock -ModuleName Install-Identity-Utilities -CommandName Get-FullyQualifiedMachineName -MockWith { }
Mock -ModuleName Install-Identity-Utilities -CommandName Read-Host -MockWith { "https://otherhost.fabric.local/identity" }
Mock -ModuleName Install-Identity-Utilities -CommandName Add-InstallationSetting -MockWith {}

# Act
$appEndpoint = Get-ApplicationEndpoint -appName "identity" -applicationEndpoint "https://host.fabric.local/identity" -installConfigPath $testInstallFileLoc -scope "identity" -quiet $false

# Assert
$appEndpoint | Should -Be "https://otherhost.fabric.local/identity"
Assert-MockCalled -ModuleName Install-Identity-Utilities -CommandName Read-Host -Times 1 -Exactly
Assert-MockCalled -ModuleName Install-Identity-Utilities -CommandName Get-FullyQualifiedMachineName -Times 0 -Exactly
Assert-MockCalled -ModuleName Install-Identity-Utilities -CommandName Add-InstallationSetting -Times 2 -Exactly
}
}
}
}
104 changes: 104 additions & 0 deletions Fabric.Identity.API/scripts/tests/Get-Certificates.tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
param(
[string] $targetFilePath = "$PSScriptRoot\..\Install-Identity-Utilities.psm1"
)

Write-Host $targetFilePath
# Force re-import to pick up latest changes
Import-Module $targetFilePath -Force

$Global:testInstallFile = "install.config"
$Global:testInstallFileLoc = "$PSScriptRoot\$testInstallFile"

Describe 'Get-Certificates' -Tag 'Unit'{
Context 'Quiet Mode' {
InModuleScope Install-Identity-Utilities {
It 'Should return certificates without prompt'{
Mock -ModuleName Install-Identity-Utilities -CommandName Test-ShouldShowCertMenu -MockWith { $false }
Mock -ModuleName Install-Identity-Utilities -CommandName Get-Certificate -MockWith { return @{Thumbprint = 123456; Subject = "CN=server.domain.local"}}
Mock -ModuleName Install-Identity-Utilities -CommandName Add-InstallationSetting -MockWith {}
Mock -CommandName Read-Host -MockWith { }
$certs = Get-Certificates -primarySigningCertificateThumbprint "123456" -encryptionCertificateThumbprint "123456" -installConfigPath $testInstallFileLoc -scope "identity" -quiet $true
Assert-MockCalled -ModuleName Install-Identity-Utilities -CommandName Get-Certificate -Times 2 -Exactly
Assert-MockCalled -ModuleName Install-Identity-Utilities -CommandName Add-InstallationSetting -Times 3 -Exactly
Assert-MockCalled -ModuleName Install-Identity-Utilities -CommandName Read-Host -Times 0 -Exactly
$certs.SigningCertificate.Thumbprint | Should -Be "123456"
$certs.EncryptionCertificate.Thumbprint | Should -Be "123456"
}

It 'Should throw an exception if we cannot read the certificate'{
Mock -ModuleName Install-Identity-Utilities -CommandName Test-ShouldShowCertMenu -MockWith { $false }
Mock -ModuleName Install-Identity-Utilities -CommandName Get-Certificate -MockWith { throw }
Mock -ModuleName Install-Identity-Utilities -CommandName Add-InstallationSetting -MockWith {}
{Get-Certificates -primarySigningCertificateThumbprint "123456" -encryptionCertificateThumbprint "123456" -installConfigPath $testInstallFileLoc -scope "identity" -quiet $true } | Should -Throw
}
}
}

Context 'Interactive Mode'{
InModuleScope Install-Identity-Utilities{
It 'Should prompt and return certificates'{
# Arrange
$testBeforeDate = Get-Date
$testAfterDate = Get-Date
$cert1 = New-Object -TypeName psobject -Property @{Thumbprint = 678901; Subject = "CN=server.domain.local"; NotBefore = $testBeforeDate.AddDays(-10); NotAfter = $testAfterDate.AddDays(10)}
$cert2 = New-Object -TypeName psobject -Property @{Thumbprint = 123456; Subject = "CN=server.domain.local"; NotBefore = $testBeforeDate.AddDays(-10); NotAfter = $testAfterDate.AddDays(10)}
Mock -ModuleName Install-Identity-Utilities -CommandName Get-CertsFromLocation -MockWith { return @($cert1, $cert2)}
Mock -ModuleName Install-Identity-Utilities -CommandName Test-ShouldShowCertMenu -MockWith { $true }
Mock -ModuleName Install-Identity-Utilities -CommandName Get-Certificate -MockWith { return @{Thumbprint = 123456; Subject = "CN=server.domain.local"}}
Mock -ModuleName Install-Identity-Utilities -CommandName Add-InstallationSetting -MockWith {}
Mock -CommandName Read-Host -MockWith { 2 }

# Act
$certs = Get-Certificates -primarySigningCertificateThumbprint "123456" -encryptionCertificateThumbprint "123456" -installConfigPath $testInstallFileLoc -scope "identity" -quiet $true

# Assert
Assert-MockCalled -ModuleName Install-Identity-Utilities -CommandName Get-Certificate -Times 2 -Exactly
Assert-MockCalled -ModuleName Install-Identity-Utilities -CommandName Add-InstallationSetting -Times 3 -Exactly
Assert-MockCalled -ModuleName Install-Identity-Utilities -CommandName Read-Host -Times 1 -Exactly
$certs.SigningCertificate.Thumbprint | Should -Be "123456"
$certs.EncryptionCertificate.Thumbprint | Should -Be "123456"
}

It 'Should throw an exception if no selection is made'{
# Arrange
$cert1 = New-Object -TypeName psobject -Property @{Thumbprint = 678901; Subject = "CN=server.domain.local"}
$cert2 = New-Object -TypeName psobject -Property @{Thumbprint = 123456; Subject = "CN=server.domain.local"}
Mock -ModuleName Install-Identity-Utilities -CommandName Get-CertsFromLocation -MockWith { return @($cert1, $cert2)}
Mock -ModuleName Install-Identity-Utilities -CommandName Test-ShouldShowCertMenu -MockWith { $true }
Mock -ModuleName Install-Identity-Utilities -CommandName Get-Certificate -MockWith { return @{Thumbprint = 123456; Subject = "CN=server.domain.local"}}
Mock -ModuleName Install-Identity-Utilities -CommandName Add-InstallationSetting -MockWith {}
Mock -ModuleName Install-Identity-Utilities -CommandName Write-DosMessage -MockWith { } -ParameterFilter { $Level -and $Level -eq "Information" -and $Message -eq "You must select a certificate so Fabric.Identity can sign access and identity tokens." }
Mock -CommandName Read-Host -MockWith { $null }

# Act/Assert
{Get-Certificates -primarySigningCertificateThumbprint "123456" -encryptionCertificateThumbprint "123456" -installConfigPath $testInstallFileLoc -scope "identity" -quiet $false} | Should -Throw
Assert-MockCalled -ModuleName Install-Identity-Utilities -CommandName Write-DosMessage -ParameterFilter { $Level -and $Level -eq "Information" -and $Message -eq "You must select a certificate so Fabric.Identity can sign access and identity tokens." } -Times 10 -Exactly
}

It 'Should throw an exception if a bad selection is made'{
# Arrange
$cert1 = New-Object -TypeName psobject -Property @{Thumbprint = 678901; Subject = "CN=server.domain.local"}
$cert2 = New-Object -TypeName psobject -Property @{Thumbprint = 123456; Subject = "CN=server.domain.local"}
Mock -ModuleName Install-Identity-Utilities -CommandName Get-CertsFromLocation -MockWith { return @($cert1, $cert2)}
Mock -ModuleName Install-Identity-Utilities -CommandName Test-ShouldShowCertMenu -MockWith { $true }
Mock -ModuleName Install-Identity-Utilities -CommandName Get-Certificate -MockWith { return @{Thumbprint = 123456; Subject = "CN=server.domain.local"}}
Mock -ModuleName Install-Identity-Utilities -CommandName Add-InstallationSetting -MockWith {}
Mock -ModuleName Install-Identity-Utilities -CommandName Write-DosMessage -MockWith { } -ParameterFilter { $Level -and $Level -eq "Information" -and $Message.StartsWith("Please select a certificate with index between 1 and") }
Mock -CommandName Read-Host -MockWith { 3 }

# Act/Assert
{Get-Certificates -primarySigningCertificateThumbprint "123456" -encryptionCertificateThumbprint "123456" -installConfigPath $testInstallFileLoc -scope "identity" -quiet $false} | Should -Throw
Assert-MockCalled -ModuleName Install-Identity-Utilities -CommandName Write-DosMessage -ParameterFilter { $Level -and $Level -eq "Information" -and $Message.StartsWith("Please select a certificate with index between 1 and") } -Times 10 -Exactly
}

It 'Should throw an exception if certs cannot be retreived from certificate store'{
# Arrange
Mock -ModuleName Install-Identity-Utilities -CommandName Get-CertsFromLocation -MockWith { throw }
Mock -ModuleName Install-Identity-Utilities -CommandName Test-ShouldShowCertMenu -MockWith { $true }

# Act/Assert
{Get-Certificates -primarySigningCertificateThumbprint "123456" -encryptionCertificateThumbprint "123456" -installConfigPath $testInstallFileLoc -scope "identity" -quiet $false} | Should -Throw
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
param(
[string] $targetFilePath = "$PSScriptRoot\..\Install-Identity-Utilities.psm1"
)

Write-Host $targetFilePath
# Force re-import to pick up latest changes
Import-Module $targetFilePath -Force

$Global:testInstallFile = "install.config"
$Global:testInstallFileLoc = "$PSScriptRoot\$testInstallFile"

Describe 'Get-ClientSettingsFromInstallConfig' -Tag 'Unit' {
InModuleScope Install-Identity-Utilities{
Context 'Valid config path' {
It 'Should return a list of client settings' {
$mockXml = [xml]'<?xml version="1.0" encoding="utf-8"?><installation><settings><scope name="identity"><variable name="fabricInstallerSecret" value="" /><variable name="discoveryService" value="" /> <registeredApplications><variable appName="testApp" tenantId="tenant1" secret="secret1" clientid="clientid1" /><variable appName="testApp" tenantId="tenant2" secret="secret2" clientid="clientid2" /></registeredApplications></scope></settings></installation>'

Mock -CommandName Get-Content { return $mockXml }
$result = Get-ClientSettingsFromInstallConfig -installConfigPath $testInstallFileLoc -appName "testApp"
$result.length | Should -Be 2
$firstApp = $result[0]
$secondApp = $result[1]

$firstApp.clientId | Should -Be "clientid1"
$firstApp.tenantId | Should -Be "tenant1"
$firstApp.clientSecret | Should -Be "secret1"

$secondApp.clientId | Should -Be "clientid2"
$secondApp.tenantId | Should -Be "tenant2"
$secondApp.clientSecret | Should -Be "secret2"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
param(
[string] $targetFilePath = "$PSScriptRoot\..\Install-Identity-Utilities.psm1"
)

Write-Host $targetFilePath
# Force re-import to pick up latest changes
Import-Module $targetFilePath -Force

Describe 'Get-DefaultApplicationEndpoint'{
Context 'Gets stored app endpoint'{
InModuleScope Install-Identity-Utilities{
It 'Should return the stored app endpoint'{
# Arrange
Mock -ModuleName Install-Identity-Utilities -CommandName Get-FullyQualifiedMachineName -MockWith {}

# Act
$appEndpoint = Get-DefaultApplicationEndpoint -appName "identity" -appEndpoint "https://host.fabric.local/identity"

# Assert
$appEndpoint | Should -Be "https://host.fabric.local/identity"
Assert-MockCalled -ModuleName Install-Identity-Utilities -CommandName Get-FullyQualifiedMachineName -Times 0 -Exactly
}
}
}
Context 'Gets calculated app endpoint'{
InModuleScope Install-Identity-Utilities{
It 'Should return the calculated app endpoint'{
# Arrange
Mock -ModuleName Install-Identity-Utilities -CommandName Get-FullyQualifiedMachineName -MockWith { "https://host.fabric.local" }

# Act
$appEndpoint = Get-DefaultApplicationEndpoint -appName "identity" -appEndpoint $null

# Assert
$appEndpoint | Should -Be "https://host.fabric.local/identity"
Assert-MockCalled -ModuleName Install-Identity-Utilities -CommandName Get-FullyQualifiedMachineName -Times 1 -Exactly
}
}
}
}
Loading