|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +Import-Module "$psscriptroot\PSGetTestUtils.psm1" -Force |
| 5 | + |
| 6 | +Describe "Create PSCredentialInfo with VaultName and SecretName" -tags 'CI' { |
| 7 | + |
| 8 | + It "Verifies VaultName is not empty" { |
| 9 | + $randomSecret = [System.IO.Path]::GetRandomFileName() |
| 10 | + { New-Object Microsoft.PowerShell.PowerShellGet.UtilClasses.PSCredentialInfo ("", $randomSecret) } | Should -Throw -ErrorId "ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand" |
| 11 | + } |
| 12 | + |
| 13 | + It "Verifies SecretName is not empty" { |
| 14 | + { New-Object Microsoft.PowerShell.PowerShellGet.UtilClasses.PSCredentialInfo ("testvault", "") } | Should -Throw -ErrorId "ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand" |
| 15 | + } |
| 16 | + |
| 17 | + It "Creates PSCredentialInfo successfully if VaultName and SecretName are non-empty" { |
| 18 | + $randomSecret = [System.IO.Path]::GetRandomFileName() |
| 19 | + $credentialInfo = New-Object Microsoft.PowerShell.PowerShellGet.UtilClasses.PSCredentialInfo ("testvault", $randomSecret) |
| 20 | + $credentialInfo.VaultName | Should -Be "testvault" |
| 21 | + $credentialInfo.SecretName | Should -Be $randomSecret |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +Describe "Create PSCredentialInfo with VaultName, SecretName, and Credential" -tags 'CI' { |
| 26 | + |
| 27 | + It "Creates PSCredentialInfo successfully if Credential is null" { |
| 28 | + $randomSecret = [System.IO.Path]::GetRandomFileName() |
| 29 | + $credentialInfo = New-Object Microsoft.PowerShell.PowerShellGet.UtilClasses.PSCredentialInfo ("testvault", $randomSecret) |
| 30 | + |
| 31 | + $credentialInfo.VaultName | Should -Be "testvault" |
| 32 | + $credentialInfo.SecretName | Should -Be $randomSecret |
| 33 | + } |
| 34 | + |
| 35 | + It "Creates PSCredentialInfo successfully if Credential is non-null and of type PSCredential" { |
| 36 | + $randomSecret = [System.IO.Path]::GetRandomFileName() |
| 37 | + $randomPassword = [System.IO.Path]::GetRandomFileName() |
| 38 | + $credential = New-Object System.Management.Automation.PSCredential ("username", (ConvertTo-SecureString $randomPassword -AsPlainText -Force)) |
| 39 | + $credentialInfo = New-Object Microsoft.PowerShell.PowerShellGet.UtilClasses.PSCredentialInfo ("testvault", $randomSecret, $credential) |
| 40 | + |
| 41 | + $credentialInfo.VaultName | Should -Be "testvault" |
| 42 | + $credentialInfo.SecretName | Should -Be $randomSecret |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +Describe "Create PSCredentialInfo from a PSObject" -tags 'CI' { |
| 47 | + |
| 48 | + It "Throws if VaultName is null" { |
| 49 | + $customObject = New-Object PSObject |
| 50 | + { New-Object Microsoft.PowerShell.PowerShellGet.UtilClasses.PSCredentialInfo $customObject } | Should -Throw -ErrorId "ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand" |
| 51 | + } |
| 52 | + |
| 53 | + It "Throws if SecretName is null" { |
| 54 | + $customObject = New-Object PSObject |
| 55 | + $customObject | Add-Member -Name "VaultName" -Value "testvault" -MemberType NoteProperty |
| 56 | + { New-Object Microsoft.PowerShell.PowerShellGet.UtilClasses.PSCredentialInfo $customObject } | Should -Throw -ErrorId "ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand" |
| 57 | + } |
| 58 | + |
| 59 | + It "Creates PSCredentialInfo successfully from PSObject with VaultName and SecretName" { |
| 60 | + $randomSecret = [System.IO.Path]::GetRandomFileName() |
| 61 | + $properties = [PSCustomObject]@{ |
| 62 | + VaultName = "testvault" |
| 63 | + SecretName = $randomSecret |
| 64 | + } |
| 65 | + |
| 66 | + $credentialInfo = [Microsoft.PowerShell.PowerShellGet.UtilClasses.PSCredentialInfo] $properties |
| 67 | + |
| 68 | + $credentialInfo.VaultName | Should -Be "testvault" |
| 69 | + $credentialInfo.SecretName | Should -Be $randomSecret |
| 70 | + } |
| 71 | + |
| 72 | + It "Creates PSCredentialInfo successfully from PSObject with VaultName, SecretName and PSCredential Credential" { |
| 73 | + $randomSecret = [System.IO.Path]::GetRandomFileName() |
| 74 | + $randomPassword = [System.IO.Path]::GetRandomFileName() |
| 75 | + |
| 76 | + $credential = New-Object System.Management.Automation.PSCredential ("username", (ConvertTo-SecureString $randomPassword -AsPlainText -Force)) |
| 77 | + $properties = [PSCustomObject]@{ |
| 78 | + VaultName = "testvault" |
| 79 | + SecretName = $randomSecret |
| 80 | + Credential = [PSCredential] $credential |
| 81 | + } |
| 82 | + |
| 83 | + $credentialInfo = [Microsoft.PowerShell.PowerShellGet.UtilClasses.PSCredentialInfo] $properties |
| 84 | + |
| 85 | + $credentialInfo.VaultName | Should -Be "testvault" |
| 86 | + $credentialInfo.SecretName | Should -Be $randomSecret |
| 87 | + $credentialInfo.Credential.UserName | Should -Be "username" |
| 88 | + $credentialInfo.Credential.GetNetworkCredential().Password | Should -Be $randomPassword |
| 89 | + } |
| 90 | + |
| 91 | + It "Creates PSCredentialInfo successfully from PSObject with VaultName, SecretName and string Credential" { |
| 92 | + $randomSecret = [System.IO.Path]::GetRandomFileName() |
| 93 | + $randomPassword = [System.IO.Path]::GetRandomFileName() |
| 94 | + |
| 95 | + $properties = [PSCustomObject]@{ |
| 96 | + VaultName = "testvault" |
| 97 | + SecretName = $randomSecret |
| 98 | + Credential = $randomPassword |
| 99 | + } |
| 100 | + |
| 101 | + $credentialInfo = [Microsoft.PowerShell.PowerShellGet.UtilClasses.PSCredentialInfo] $properties |
| 102 | + |
| 103 | + $credentialInfo.VaultName | Should -Be "testvault" |
| 104 | + $credentialInfo.SecretName | Should -Be $randomSecret |
| 105 | + $credentialInfo.Credential.GetNetworkCredential().Password | Should -Be $randomPassword |
| 106 | + } |
| 107 | + |
| 108 | + It "Creates PSCredentialInfo successfully from PSObject with VaultName, SecretName and SecureString Credential" { |
| 109 | + $randomSecret = [System.IO.Path]::GetRandomFileName() |
| 110 | + $randomPassword = [System.IO.Path]::GetRandomFileName() |
| 111 | + |
| 112 | + $secureString = ConvertTo-SecureString $randomPassword -AsPlainText -Force |
| 113 | + $properties = [PSCustomObject]@{ |
| 114 | + VaultName = "testvault" |
| 115 | + SecretName = $randomSecret |
| 116 | + Credential = $secureString |
| 117 | + } |
| 118 | + |
| 119 | + $credentialInfo = [Microsoft.PowerShell.PowerShellGet.UtilClasses.PSCredentialInfo] $properties |
| 120 | + |
| 121 | + $credentialInfo.VaultName | Should -Be "testvault" |
| 122 | + $credentialInfo.SecretName | Should -Be $randomSecret |
| 123 | + $credentialInfo.Credential.GetNetworkCredential().Password | Should -Be $randomPassword |
| 124 | + } |
| 125 | +} |
0 commit comments