tests/Set-CsEntry.Tests.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingConvertToSecureStringWithPlainText", "")] param() . "$PSScriptRoot\..\src\Test-CsEntryName.ps1" . "$PSScriptRoot\..\src\Initialize-CsStore.ps1" . "$PSScriptRoot\..\src\Set-CsEntry.ps1" Describe Set-CsEntry { $filePath = $(New-TemporaryFile).FullName Remove-Item $filePath Context "Adding a new Credential without description" { Initialize-CsStore $filePath $cred = New-Object PSCredential("user", $("pass" | ConvertTo-SecureString -AsPlainText -Force)) Set-CsEntry -Name NewCred -Credential $cred -FilePath $filePath $content = Get-Content -Raw -Path $filePath | ConvertFrom-Json It "should add the new credential" { $content.Credentials.Length | Should Be 1 $content.Credentials[0].Name | Should Be "NewCred" } It "should save the username" { $content.Credentials[0].UserName | Should Be "user" } It "should encrypt and save the password" { $content.Credentials[0].Password | Should Not Be NullOrEmpty $content.Credentials[0].Password | Should Not Be "pass" } It "should have empty description" { $content.Credentials[0].Description | Should BeNullOrEmpty } Remove-Item $filePath } Context "Adding a new Credential with description" { Initialize-CsStore $filePath $cred = New-Object PSCredential("user", $("pass" | ConvertTo-SecureString -AsPlainText -Force)) Set-CsEntry -Name NewCred -Description Test -Credential $cred -FilePath $filePath $content = Get-Content -Raw -Path $filePath | ConvertFrom-Json It "should set the description" { $content.Credentials[0].Description | Should Be 'Test' } Remove-Item $filePath } Context "Updating an existing entry without description" { Initialize-CsStore $filePath $content = Get-Content -Raw -Path $filePath | ConvertFrom-Json $content.Credentials += @{ Name = "Existing" Description = "oldDesc" Username = "olduser" Password = "encOldPass" } $content | ConvertTo-Json |Out-File -FilePath $filePath $cred = New-Object PSCredential("user", $("pass" | ConvertTo-SecureString -AsPlainText -Force)) Set-CsEntry -Name Existing -Credential $cred -FilePath $filePath $content = Get-Content -Raw -Path $filePath | ConvertFrom-Json It "should not add a the new credential" { $content.Credentials.Length | Should Be 1 $content.Credentials[0].Name | Should Be "Existing" } It "should update the username" { $content.Credentials[0].UserName | Should Be "user" } It "should encrypt and update the password" { $content.Credentials[0].Password | Should Not Be NullOrEmpty $content.Credentials[0].Password | Should Not Be "encOldPass" $content.Credentials[0].Password | Should Not Be "pass" } It "should have empty description" { $content.Credentials[0].Description | Should BeNullOrEmpty } Remove-Item $filePath } Context "Pipeline support" { Initialize-CsStore $filePath $content = Get-Content -Raw -Path $filePath | ConvertFrom-Json $content.Credentials += @{ Name = "Existing" Description = "oldDesc" Username = "olduser" Password = "encOldPass" } $content | ConvertTo-Json | Out-File -FilePath $filePath $cred = New-Object PSCredential("user", $("pass" | ConvertTo-SecureString -AsPlainText -Force)) $entries = @( [PSCustomObject] @{Name = "Name1"; Credential = $cred} [PSCustomObject] @{Name = "Name2"; Credential = $cred} [PSCustomObject] @{Name = "Existing"; Credential = $cred} ) $entries | Set-CsEntry -FilePath $filePath $content = Get-Content -Raw -Path $filePath | ConvertFrom-Json It "should update the existing credential" { $content.Credentials[0].UserName | Should Be "user" $content.Credentials[0].Password | Should Not Be "encOldPass" } It "should add the new credential" { $content.Credentials.Length | Should Be 3 $content.Credentials[1].Name | Should Be "Name1" $content.Credentials[2].Name | Should Be "Name2" } Remove-Item $filePath } Context "CredentialStore file does not exist" { It "should throw a validation exception" { $cred = New-Object PSCredential("user", $("pass" | ConvertTo-SecureString -AsPlainText -Force)) { Set-CsEntry -Name User1 -Credential $cred -FilePath unknown.json } | Should Throw "The path 'unknown.json' does not exist." } } Context "CredentialStore is for a different user" { Initialize-CsStore $filePath $content = Get-Content -Raw -Path $filePath | ConvertFrom-Json $content.UserName = "other" $content | ConvertTo-Json | Out-File -FilePath $filePath It "should throw a exception" { $cred = New-Object PSCredential("user", $("pass" | ConvertTo-SecureString -AsPlainText -Force)) { Set-CsEntry -Name User1 -Credential $cred -FilePath $filePath } | Should Throw "Cannot access CredentialStore, it is encrypted for" } Remove-Item $filePath } Context "CredentialStore is for a different computer" { Initialize-CsStore $filePath $content = Get-Content -Raw -Path $filePath | ConvertFrom-Json $content.ComputerName = "other" $content | ConvertTo-Json | Out-File -FilePath $filePath It "should throw a exception" { $cred = New-Object PSCredential("user", $("pass" | ConvertTo-SecureString -AsPlainText -Force)) { Set-CsEntry -Name User1 -Credential $cred -FilePath $filePath } | Should Throw "Cannot access CredentialStore, it is encrypted for" } Remove-Item $filePath } } |