tests/Import-CsKeyVaultStore.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
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingConvertToSecureStringWithPlainText", "")]
param()

. "$PSScriptRoot\..\..\CredentialStore\src\Test-CsEntryName.ps1"
. "$PSScriptRoot\..\..\CredentialStore\src\Initialize-CsStore.ps1"
. "$PSScriptRoot\..\..\CredentialStore\src\Set-CsEntry.ps1"
. "$PSScriptRoot\..\src\Get-CsKeyVaultEntry.ps1"
. "$PSScriptRoot\..\src\Import-CsKeyVaultStore.ps1"

Describe Import-CsKeyVaultStore {
    $filePath = $(New-TemporaryFile).FullName

    Mock Initialize-CsStore
    Mock Set-CsEntry
    Mock Get-CsKeyVaultEntry -MockWith {
        @(
            [PSCustomObject]@{
                Name        = 'name1'
                Credential  = New-Object PSCredential("user1", $("pass" | ConvertTo-SecureString -AsPlainText -Force))
                Description = 'desc1'
            },
            [PSCustomObject]@{
                Name        = 'name2'
                Credential  = New-Object PSCredential("user2", $("pass" | ConvertTo-SecureString -AsPlainText -Force))
                Description = 'desc2'
            }
        )
    }

    Context "Import a CredentialStore from Vault to File" {
        Import-CsKeyVaultStore -VaultName vault1 -FilePath $filePath

        It "Should Initialise a local CredentialStore" {
            Assert-MockCalled Initialize-CsStore -ParameterFilter { 
                $FilePath -eq $filePath
            }
        }

        It "Should add the entries from the vault" {
            Assert-MockCalled Set-CsEntry -Exactly 2
            Assert-MockCalled Set-CsEntry -ParameterFilter {
                $Name -eq 'name1'
            }

            Assert-MockCalled Set-CsEntry -ParameterFilter {
                $Name -eq 'name2'
            }
        }
    }

    Remove-Item $filePath 
}