Skip to content

Commit

Permalink
Secret Tests - reworked
Browse files Browse the repository at this point in the history
  • Loading branch information
wsmelton committed Apr 4, 2021
1 parent 78638ed commit ee188be
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 84 deletions.
Expand Up @@ -31,18 +31,20 @@ Describe "$commandName functions" {
TokenType = 'bearer'
ExpiresIn = 1199
}
Mock -Verifiable -CommandName Get-TssVersion -MockWith {
Mock -Verifiable -CommandName Invoke-RestMethod -ParameterFilter { $Uri -match '/version' } -MockWith {
return @{
Version = '10.9.000033'
model = [pscustomobject]@{
Version = '10.9.000033'
}
}
}

$secretId = Get-Random -Maximum 12
$secretId = 42
$notes = (New-Guid).Guid
Mock -Verifiable -CommandName Invoke-TssRestApi -ParameterFilter { $Uri -eq "$($session.ApiUrl)/secrets/$secretId/fields/notes" } -MockWith {
Mock -Verifiable -CommandName Invoke-RestMethod -ParameterFilter { $Uri -match "/secrets/$secretId/fields/notes" } -MockWith {
return $notes
}
$object = Set-TssSecretField -TssSession $session -Id $secretId -Slug notes -Value $notes
$object = Set-SecretField -TssSession $session -Id $secretId -Slug notes -Value $notes
Assert-VerifiableMock
}
It "Should be empty" {
Expand Down
67 changes: 67 additions & 0 deletions tests/secrets/Start-SecretChangePassword.Tests.ps1
@@ -0,0 +1,67 @@
BeforeDiscovery {
$commandName = Split-Path ($PSCommandPath.Replace('.Tests.ps1','')) -Leaf
. ([IO.Path]::Combine([string]$PSScriptRoot, '..', 'constants.ps1'))
}
Describe "$commandName verify parameters" {
BeforeDiscovery {
[object[]]$knownParameters = 'TssSession', 'Id', 'Type', 'NextPassword'
[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} {
It "$commandName should contain <_> parameter" -TestCases $knownParameters {
$_ -in $currentParams | Should -Be $true
}
It "$commandName should not contain parameter: <_>" -TestCases $unknownParameters {
$_ | Should -BeNullOrEmpty
}
}
}
Describe "$commandName functions" {
Context "Checking" {
BeforeAll {
$session = [pscustomobject]@{
ApiVersion = 'api/v1'
Take = 2147483647
SecretServer = 'http://alpha/'
ApiUrl = 'http://alpha/api/v1'
AccessToken = 'AgJf5YLChrisPine312UcBrM1s1KB2BGZ5Ufc4qLZ'
RefreshToken = '9oacYeah0YqgBNg0L7VinDiesel6-Z9ITE51Humus'
TokenType = 'bearer'
ExpiresIn = 1199
}
Mock -Verifiable -CommandName Invoke-RestMethod -ParameterFilter { $Uri -match '/version' } -MockWith {
return @{
model = [pscustomobject]@{
Version = '10.9.000033'
}
}
}

$secretIdRandom = 42
Mock -Verifiable -CommandName Invoke-RestMethod -ParameterFilter { $Uri -match "/internals/secret-detail/$secretIdRandom/change-password-now" } -MockWith {
return [pscustomobject]@{
success = $true
}
}

$secretIdManual = 43
Mock -Verifiable -CommandName Invoke-RestMethod -ParameterFilter { $Uri -match "/internals/secret-detail/$secretIdManual/change-password-now" } -MockWith {
return [pscustomobject]@{
success = $true
}
}

$objectRandom = Start-SecretChangePassword -TssSession $session -Id $secretIdRandom -Type Random
$objectManual = Start-SecretChangePassword -TssSession $session -Id $secretIdManual -Type Manual -NextPassword (ConvertTo-SecureString 'password' -AsPlainText -Force)
Assert-VerifiableMock
}
It "Should be empty" {
$objectRandom | Should -BeNullOrEmpty
}
It "Should have called Invoke-RestMethod 3 times" {
Assert-MockCalled -CommandName Invoke-RestMethod -Times 3 -Scope Describe
}
}
}
20 changes: 0 additions & 20 deletions tests/secrets/Start-TssSecretChangePassword.Tests.ps1

This file was deleted.

74 changes: 74 additions & 0 deletions tests/secrets/Stop-SecretChangePassword.Tests.ps1
@@ -0,0 +1,74 @@
BeforeDiscovery {
$commandName = Split-Path ($PSCommandPath.Replace('.Tests.ps1','')) -Leaf
. ([IO.Path]::Combine([string]$PSScriptRoot, '..', 'constants.ps1'))
}
Describe "$commandName verify parameters" {
BeforeDiscovery {
[object[]]$knownParameters = 'TssSession','Id'
[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} {
It "$commandName should contain <_> parameter" -TestCases $knownParameters {
$_ -in $currentParams | Should -Be $true
}
It "$commandName should not contain parameter: <_>" -TestCases $unknownParameters {
$_ | Should -BeNullOrEmpty
}
}
}
Describe "$commandName functions" {
Context "Checking" {
BeforeAll {
$session = [pscustomobject]@{
ApiVersion = 'api/v1'
Take = 2147483647
SecretServer = 'http://alpha/'
ApiUrl = 'http://alpha/api/v1'
AccessToken = 'AgJf5YLChrisPine312UcBrM1s1KB2BGZ5Ufc4qLZ'
RefreshToken = '9oacYeah0YqgBNg0L7VinDiesel6-Z9ITE51Humus'
TokenType = 'bearer'
ExpiresIn = 1199
}
Mock -Verifiable -CommandName Invoke-RestMethod -ParameterFilter { $Uri -match '/version' } -MockWith {
return @{
model = [pscustomobject]@{
Version = '10.9.000033'
}
}
}

$secretIdTrue = 42
Mock -Verifiable -CommandName Invoke-RestMethod -ParameterFilter { $Uri -match "/secrets/$secretIdTrue/stop-password-change" } -MockWith {
return [pscustomobject]@{
success = $true
}
}
$secretIdFalse = 43
Mock -Verifiable -CommandName Invoke-RestMethod -ParameterFilter { $Uri -match "/secrets/$secretIdFalse/stop-password-change" } -MockWith {
return [pscustomobject]@{
success = $false
}
}
$objectTrue = Stop-SecretChangePassword -TssSession $session -Id $secretIdTrue
$objectFalse = Stop-SecretChangePassword -TssSession $session -Id $secretIdFalse
Assert-VerifiableMock
}
It "Should not be empty when true" {
$objectTrue | Should -Not -BeNullOrEmpty
}
It "Should have property <_> when True" -TestCases 'SecretId', 'Status' {
$objectTrue[0].PSObject.Properties.Name | Should -Contain $_
}
It "Should have property SecretId with a value when True" {
$objectTrue.SecretId | Should -Not -BeNullOrEmpty
}
It "Should be empty when False" {
$objectFalse | Should -BeNullOrEmpty
}
It "Should have called Invoke-RestMethod 3 times" {
Assert-MockCalled -CommandName Invoke-RestMethod -Times 3 -Scope Describe
}
}
}
59 changes: 0 additions & 59 deletions tests/secrets/Stop-TssSecretChangePassword.Tests.ps1

This file was deleted.

0 comments on commit ee188be

Please sign in to comment.