Skip to content

Commit

Permalink
vs code snippet - update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wsmelton committed Mar 15, 2021
1 parent 6e73fa1 commit 50ec291
Showing 1 changed file with 98 additions and 41 deletions.
139 changes: 98 additions & 41 deletions .vscode/tss.code-snippets
Expand Up @@ -42,7 +42,7 @@
" process {",
" Write-Verbose \"Provided command parameters: \\$(. \\$GetInvocation \\$PSCmdlet.MyInvocation)\"",
" if (\\$tssNewParams.ContainsKey('TssSession') -and \\$TssSession.IsValidSession()) {",
" . \\$CheckVersion \\$TssSession '10.9.000000' \\$PSCmdlet.MyInvocation",
" . \\$CheckVersion \\$TssSession '10.9.000000' \\$PSCmdlet.MyInvocation",
" \\$restResponse = $null",
" \\$uri = \\$TssSession.ApiUrl, '${10:endpoint}' -join '/'",
" \\$invokeParams.Uri = \\$uri",
Expand Down Expand Up @@ -132,7 +132,7 @@
" process {",
" Write-Verbose \"Provided command parameters: \\$(. \\$GetInvocation \\$PSCmdlet.MyInvocation)\"",
" if (\\$tssParams.ContainsKey('TssSession') -and \\$TssSession.IsValidSession()) {",
" . \\$CheckVersion \\$TssSession '10.9.000000' \\$PSCmdlet.MyInvocation",
" . \\$CheckVersion \\$TssSession '10.9.000000' \\$PSCmdlet.MyInvocation",
" \\$restResponse = \\$null",
" \\$uri = \\$TssSession.ApiUrl, '${1}s' -join '/'",
" \\$uri = \\$uri, \"sortBy[0].direction=asc&sortBy[0].name=\\$SortBy&take=\\$(\\$TssSession.Take)\" -join '?'",
Expand Down Expand Up @@ -219,7 +219,7 @@
" process {",
" Write-Verbose \"Provided command parameters: \\$(. \\$GetInvocation \\$PSCmdlet.MyInvocation)\"",
" if (\\$tssParams.ContainsKey('TssSession') -and \\$TssSession.IsValidSession()) {",
" . \\$CheckVersion \\$TssSession '10.9.000000' \\$PSCmdlet.MyInvocation",
" . \\$CheckVersion \\$TssSession '10.9.000000' \\$PSCmdlet.MyInvocation",
" foreach (\\$${7} in \\$${4}) {",
" \\$restResponse = \\$null",
" \\$uri = \\$TssSession.ApiUrl, '${1}', \\$${8} -join '/'",
Expand Down Expand Up @@ -293,7 +293,7 @@
" process {",
" Write-Verbose \"Provided command parameters: \\$(. \\$GetInvocation \\$PSCmdlet.MyInvocation)\"",
" if (\\$tssParams.ContainsKey('TssSession') -and \\$TssSession.IsValidSession()) {",
" . \\$CheckVersion \\$TssSession '10.9.000000' \\$PSCmdlet.MyInvocation",
" . \\$CheckVersion \\$TssSession '10.9.000000' \\$PSCmdlet.MyInvocation",
" foreach (\\$${7} in \\$${4}) {",
" \\$restResponse = \\$null",
" \\$uri = \\$TssSession.ApiUrl, '${9}', \\$${7} -join '/'",
Expand Down Expand Up @@ -331,14 +331,14 @@
"function Set-${1:Noun} {",
" <#",
" .SYNOPSIS",
" Set various properties for a given ${1}",
" Set ${2}",
"",
" .DESCRIPTION",
" Set various properties for a given ${1}",
" Set ${2}",
"",
" .EXAMPLE",
" \\$session = New-TssSession -SecretServer https://alpha -Credential \\$ssCred",
" Remove-Tss${1} -TssSession \\$session -${2} ${3:some test value}",
" Set-Tss${1} -TssSession \\$session -${2} ${3:some test value}",
"",
" ${4:Add minimum example for each parameter}",
"",
Expand All @@ -357,9 +357,9 @@
"",
" # Folder Id to modify",
" [Parameter(Mandatory,ValueFromPipelineByPropertyName)]",
" [Alias(\"${1}Id\")]",
" [Alias(\"${5}Id\")]",
" [int[]]",
" \\$${2}},",
" \\$${2},",
"",
" )",
" begin {",
Expand All @@ -369,37 +369,37 @@
" process {",
" Write-Verbose \"Provided command parameters: \\$(. \\$GetInvocation \\$PSCmdlet.MyInvocation)\"",
" if (\\$setParams.ContainsKey('TssSession') -and \\$TssSession.IsValidSession()) {",
" . \\$CheckVersion \\$TssSession '10.9.000000' \\$PSCmdlet.MyInvocation",
" foreach (\\$${5} in \\$${2}) {",
" . \\$CheckVersion \\$TssSession '10.9.0000${6}' \\$PSCmdlet.MyInvocation",
" foreach (\\$${7} in \\$${2}) {",
" \\$restResponse = \\$null",
" \\$uri = \\$TssSession.ApiUrl, '${6}', \\$${7} -join '/'",
" \\$uri = \\$TssSession.ApiUrl, '${8}', \\$${7} -join '/'",
" \\$invokeParams.Uri = \\$uri",
" \\$invokeParams.Method = 'PATCH'",
"",
" \\$setBody = @{ data = @{ } }",
"",
" \\$whatIfProcessing = 0",
" switch (\\$setParams.Keys) {",
" '${8}' {",
" if (-not \\$PSCmdlet.ShouldProcess(\"${1]Id: \\$${5}\", \"Updating {8} to \\$(\\$${8})\")) {",
" '${9}' {",
" if (-not \\$PSCmdlet.ShouldProcess(\"${10] ID: \\$${11}\", \"Updating {12} to \\$(\\$${9})\")) {",
" \\$whatIfProcessing++",
" }",
" if ($setParams.ContainsKey('') -and $) {",
" Write-Warning \"Unable to update ${8} when enabling ${9}\"",
" if ($setParams.ContainsKey('') -and \\$) {",
" Write-Warning \"Unable to update ${9} when enabling ${10}\"",
" return",
" }",
" \\$setFolderBody.data.allowedTemplates = \\$${8}",
" \\$setBody.data.${9} = \\$${9}",
" }",
" }",
"",
" \\$invokeParams.Body = \\$setBody | ConvertTo-Json",
"",
" if (\\$PSCmdlet.ShouldProcess(\"${1}ID: \\${5}\", \"\\$(\\$invokeParams.Method) \\$uri with:`n\\$(\\$invokeParams.Body)`n\")) {",
" if (\\$PSCmdlet.ShouldProcess(\"${10} ID: \\${12}\", \"\\$(\\$invokeParams.Method) \\$uri with:`n\\$(\\$invokeParams.Body)`n\")) {",
" Write-Verbose \"Performing the operation \\$(\\$invokeParams.Method) \\$uri with:`n\\$(\\$invokeParams.Body)`n\"",
" try {",
" \\$restResponse = Invoke-TssRestApi @invokeParams",
" } catch {",
" Write-Warning \"Issue setting property on ${5} [\\$${5}]\"",
" Write-Warning \"Issue setting property on ${10} [\\$${7}]\"",
" \\$err = \\$_",
" . \\$ErrorHandling \\$err",
" }",
Expand All @@ -416,8 +416,7 @@
],
"description": "Create a Set command"
},
"Base Test": {
"scope": "powershell",
"New tests": {
"prefix": "tsstest",
"body": [
"BeforeDiscovery {",
Expand All @@ -426,12 +425,12 @@
"}",
"Describe \"\\$commandName verify parameters\" {",
" BeforeDiscovery {",
" [object[]]\\$knownParameters = 'TssSession'",
" [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 parameters\" -Foreach @{currentParams = \\$currentParams} {",
" Context \"Verify parameters\" -Foreach @{currentParams = \\$currentParams } {",
" It \"\\$commandName should contain <_> parameter\" -TestCases \\$knownParameters {",
" \\$_ -in \\$currentParams | Should -Be \\$true",
" }",
Expand All @@ -440,37 +439,95 @@
" }",
" }",
" Context \"Command specific details\" {",
" It \"\\$commandName should set OutputType to TssClassName\" -TestCases \\$commandDetails {",
" \\$_.OutputType.Name | Should -Be 'TssClassName'",
" It \"\\$commandName should set OutputType to ${1:Class name}\" -TestCases \\$commandDetails {",
" \\$_.OutputType.Name | Should -Be '${1}'",
" }",
" }",
"}",
"Describe \"\\$commandName works\" {",
" BeforeDiscovery {",
" \\$session = New-TssSession -SecretServer \\$ss -Credential \\$ssCred",
" \\$invokeParams = @{",
" Uri = \"\\$ss/api/v1/folders?take=\\$(\\$session.take)\"",
" ExpandProperty = 'records'",
" PersonalAccessToken = \\$session.AccessToken",
" }",
" \\$getFolders = Invoke-TssRestApi @invokeParams",
" \\$tssSecretFolder = \\$getFolders.Where({\\$_.folderPath -match ' ss_module_testing'})",
" # Prep work",
"Describe \"\\$commandName functions\" {",
" Context \"Checking\" {",
" BeforeAll {",
" \\$session = [pscustomobject]@{",
" ApiVersion = 'api/v1'",
" Take = 2147483647",
" SecretServer = 'http://alpha/'",
" ApiUrl = 'http://alpha/api/v1'",
" AccessToken = 'AgJf5YLFWtzw2UcBrM1s1KB2BGZ5Ufc4qLZ'",
" RefreshToken = '9oacYFZZ0YqgBNg0L7VNIF6-Z9ITE51Qplj'",
" TokenType = 'bearer'",
" ExpiresIn = 1199",
" }",
" Mock -Verifiable -CommandName Get-TssVersion -MockWith {",
" return @{",
" Version = '10.9.000033'",
" }",
" }",
"",
" \\$session.SessionExpire()",
" \\$props = 'Prop1', 'Prop2', 'Prop3'",
" Mock -Verifiable -CommandName Invoke-TssRestApi -ParameterFilter { \\$Uri -eq \"\\$(\\$session.ApiUrl)/${2:endpoint}\" } -MockWith {",
" return [pscustomobject]@{",
" # Object expected by REST API call",
" }",
" }",
" \\$object = ${2:Public Function name} -TssSession \\$session ${3:Parameters}",
" Assert-VerifiableMock",
" }",
" It \"Should not be empty\" {",
" \\$object | Should -Not -BeNullOrEmpty",
" }",
" It \"Should have property <_>\" -TestCases ${4:Property list (minimum 3)} {",
" \\$object[0].PSObject.Properties.Name | Should -Contain \\$_",
" }",
" It \"Should have property ${5:Single property to check} equal ${6:value}\" {",
" $object.${5} | Should -Be ${6}",
" }",
" }",
" Context \"Checking\" -Foreach @{object = \\$object} {",
"}"
],
"description": "New tests"
},
"Functional tests": {
"prefix": "tsstestfunctional",
"body": [
"Describe \"\\$commandName functions\" {",
" Context \"Checking\" {",
" BeforeAll {",
" \\$session = [pscustomobject]@{",
" ApiVersion = 'api/v1'",
" Take = 2147483647",
" SecretServer = 'http://alpha/'",
" ApiUrl = 'http://alpha/api/v1'",
" AccessToken = 'AgJf5YLFWtzw2UcBrM1s1KB2BGZ5Ufc4qLZ'",
" RefreshToken = '9oacYFZZ0YqgBNg0L7VNIF6-Z9ITE51Qplj'",
" TokenType = 'bearer'",
" ExpiresIn = 1199",
" }",
" Mock -Verifiable -CommandName Get-TssVersion -MockWith {",
" return @{",
" Version = '10.9.000033'",
" }",
" }",
"",
" Mock -Verifiable -CommandName Invoke-TssRestApi -ParameterFilter { \\$Uri -eq \"\\$(\\$session.ApiUrl)/${1:endpoint}\" } -MockWith {",
" return [pscustomobject]@{",
" # Object expected by REST API call",
" }",
" }",
" \\$object = ${3:Public Function name} -TssSession \\$session ${4:Parameters}",
" Assert-VerifiableMock",
" }",
" It \"Should not be empty\" {",
" \\$object | Should -Not -BeNullOrEmpty",
" }",
" It \"Should output <_> property\" -TestCases \\$props {",
" It \"Should have property <_>\" -TestCases ${5:Property list (minimum 3)} {",
" \\$object[0].PSObject.Properties.Name | Should -Contain \\$_",
" }",
" It \"Should have property ${6:Single property to check} equal ${7:value}\" {",
" $object.${6} | Should -Be ${6}",
" }",
" }",
"}"
],
"description": "template for tests with Thycotic.SecretServer"
"description": "New tests"
},
"Base Part": {
"scope": "powershell",
Expand Down

0 comments on commit 50ec291

Please sign in to comment.