Skip to content

Commit

Permalink
Create: Test-TssVersion #22
Browse files Browse the repository at this point in the history
Adjusted Get-TssVersion moved API call to parts to manage easier
  • Loading branch information
wsmelton committed Dec 27, 2020
1 parent adf0611 commit cfdb07c
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 36 deletions.
24 changes: 2 additions & 22 deletions src/functions/Get-TssVersion.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
Requires TssSession object returned by New-TssSession
#>
[cmdletbinding()]
[OutputType('System.String')]
[OutputType('TssVersion')]
param(
# TssSession object passed for auth info
[Parameter(Mandatory,
Expand All @@ -36,31 +36,11 @@
)
begin {
$tssParams = . $GetParams $PSBoundParameters 'Get-TssVersion'
$invokeParams = @{ }
}

process {
if ($tssParams.Contains('TssSession') -and $TssSession.IsValidSession()) {
$uri = $TssSession.SecretServer + ($TssSession.ApiVersion, "version" -join '/')
$invokeParams.Uri = $Uri
$invokeParams.Method = 'GET'
$invokeParams.PersonalAccessToken = $TssSession.AccessToken

try {
$restResponse = Invoke-TssRestApi @invokeParams
} catch {
Write-Warning "Issue reading version, verify Hide Secret Server Version Numbers is disabled in Secret Server"
$err = $_.ErrorDetails.Message
Write-Error $err
}

if ($tssParams['Raw']) {
$restResponse
}
if ($restResponse) {
. $GetTssVersionObject $restResponse
}

. $GetTssVersionObject -TssSession $TssSession -Raw:$Raw
} else {
Write-Warning "No valid session found"
}
Expand Down
38 changes: 38 additions & 0 deletions src/functions/Test-TssVersion.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
function Test-TssVersion {
<#
.SYNOPSIS
Test Secret Server version
.DESCRIPTION
Tests whether Secret Server version returned by Get-TssVersion is the latest
.EXAMPLE
PS C:\> $session = New-TssSession -SecretServer https://alpha -Credential $ssCred
PS C:\> Test-TssVersion -TssSession $session
Pulls version of Secret Server and queries for latest version, returning object with details
.NOTES
Requires TssSession object returned by New-TssSession
#>
[CmdletBinding()]
[OutputType('TssVersion')]
param (
# TssSession object created by New-TssSession for auth
[Parameter(Mandatory,
ValueFromPipeline,
Position = 0)]
[TssSession]$TssSession
)
begin {
$tssParams = . $GetParams $PSBoundParameters 'Get-TssVersion'
}

process {
if ($tssParams.Contains('TssSession') -and $TssSession.IsValidSession()) {
. $GetTssVersionObject -TssSession $TssSession
} else {
Write-Warning "No valid session found"
}
}
}
59 changes: 50 additions & 9 deletions src/parts/GetTssVersionObject.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,68 @@
Creates an instance of the TssVersion class to output based on the calling command
#>
param(
[pscustomobject]$VersionRecord
[TssSession]
$TssSession,

[switch]
$Raw
)

begin {
$versionParams = $PSBoundParameters + @{}
$invokeParams = @{ }
}

process {
$source = $PSCmdlet.MyInvocation.MyCommand

switch ($source) {
'Get-TssVersion' {
$versionProperties = $VersionRecord.model.PSObject.Properties.Name
$VersionRecord = $VersionRecord.model
$returnProps = 'Version'
}
$uri = $TssSession.SecretServer + ($TssSession.ApiVersion, "version" -join '/')
$invokeParams.Uri = $Uri
$invokeParams.Method = 'GET'
$invokeParams.PersonalAccessToken = $TssSession.AccessToken

try {
$restResponse = Invoke-TssRestApi @invokeParams
} catch {
Write-Warning "Issue reading version, verify Hide Secret Server Version Numbers is disabled in Secret Server"
$err = $_.ErrorDetails.Message
Write-Error $err
}
}

process {
if ($versionParams['Raw']) {
return $restResponse
}

$versionProperties = $restResponse.model.PSObject.Properties.Name
$VersionRecord = $restResponse.model

foreach ($v in $VersionRecord) {
$outVersion = [TssVersion]::new()
foreach ($vProp in $versionProperties) {
$outVersion.$vProp = $v.$vProp
}
}

switch ($source) {
'Get-TssVersion' {
$returnProps = 'Version'
}
'Test-TssVersion' {
$getLatestUrl = "https://updates.thycotic.net/secretserver/LatestVersion.aspx?v=$($outVersion.Version.ToString())"
Write-Verbose "Accessing $getLatestUrl to validate latest version"
try {
$latest = Invoke-RestMethod -Uri $getLatestUrl -UseBasicParsing
} catch {
Write-Warning "Issue getting latest version information"
$err = $_.ErrorDetails.Message
Write-Error $err
}

$outVersion.LatestVersion = $latest
$outVersion.IsLatest = if ($outVersion.Version -ge $latest) { $true }

$returnProps = 'Version','LatestVersion','IsLatest'
}
}
Select-Object -InputObject $outVersion -Property $returnProps
}
11 changes: 6 additions & 5 deletions tests/functions/Get-TssVersion.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Describe "$commandName verify parameters" {
BeforeDiscovery {
[object[]]$knownParameters = 'TssSession', 'Raw'
[object[]]$currentParams = ([Management.Automation.CommandMetaData]$ExecutionContext.SessionState.InvokeCommand.GetCommand($commandName, 'Function')).Parameters.Keys
[object[]]$commandDetails = [System.Management.Automation.CommandInfo]$ExecutionContext.SessionState.InvokeCommand.GetCommand($commandName,'Function')
$unknownParameters = Compare-Object -ReferenceObject $knownParameters -DifferenceObject $currentParams -PassThru
}
Context "Verify parameters" -Foreach @{currentParams = $currentParams} {
Expand All @@ -16,6 +17,11 @@ Describe "$commandName verify parameters" {
$_ | Should -BeNullOrEmpty
}
}
Context "Command specific details" {
It "$commandName should set OutputType to TssVersion" -TestCases $commandDetails {
$_.OutputType.Name | Should -Be 'TssVersion'
}
}
}

Describe "$commandName works" {
Expand All @@ -28,13 +34,8 @@ Describe "$commandName works" {
It "Should not be empty" {
$version | Should -Not -BeNullOrEmpty
}
It "$commandName should have output type of TssVersion" {
<# This is because the command does a Select-Object the type is represented as Selected.<TypeName> #>
$version.PSTypeNames | Should -Contain 'Selected.TssVersion'
}
It "$commandName should get property <_> at minimum" -TestCases 'Version' {
$version.PSObject.Properties.Name | Should -Contain $_
}

}
}
40 changes: 40 additions & 0 deletions tests/functions/Test-TssVersion.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
BeforeDiscovery {
$commandName = Split-Path ($PSCommandPath.Replace('.Tests.ps1','')) -Leaf
. ([IO.Path]::Combine([string]$PSScriptRoot, '..', 'constants.ps1'))
}
Describe "$commandName verify parameters" {
BeforeDiscovery {
[object[]]$knownParameters = 'TssSession'
[object[]]$currentParams = ([Management.Automation.CommandMetaData]$ExecutionContext.SessionState.InvokeCommand.GetCommand($commandName, 'Function')).Parameters.Keys
[object[]]$commandDetails = [System.Management.Automation.CommandInfo]$ExecutionContext.SessionState.InvokeCommand.GetCommand($commandName,'Function')
$unknownParameters = Compare-Object -ReferenceObject $knownParameters -DifferenceObject $currentParams -PassThru
}
Context "Verify parmaeters" -Foreach @{currentParams = $currentParams} {
It "$commandName should contain <_> parameter" -TestCases $knownParameters {
$_ -in $currentParams | Should -Be $true
}
It "$commandName should not contain parameter: <_>" -TestCases $unknownParameters {
$_ | Should -BeNullOrEmpty
}
}
Context "Command specific details" {
It "$commandName should set OutputType to TssVersion" -TestCases $commandDetails {
$_.OutputType.Name | Should -Be 'TssVersion'
}
}
}
Describe "$commandName works" {
BeforeDiscovery {
$session = New-TssSession -SecretServer $ss -Credential $ssCred
$object = Test-TssVersion $session
$session.SessionExpire()
}
Context "Checking" -Foreach @{object = $object} {
It "Should not be empty" {
$object | Should -Not -BeNullOrEmpty
}
It "Should contain property <_>" -TestCases 'Version','LatestVersion','IsLatest' {
$object.PSObject.Properties.Name | Should -Contain $_
}
}
}

0 comments on commit cfdb07c

Please sign in to comment.