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

. "$PSScriptRoot\..\src\Initialize-CsStore.ps1"
. "$PSScriptRoot\..\src\Get-CsEntry.ps1"
. "$PSScriptRoot\..\src\Test-CsEntryName.ps1"
. "$PSScriptRoot\..\src\Set-CsEntry.ps1"

Describe Get-CsEntry {
    $filePath = $(New-TemporaryFile).FullName
    Remove-Item $filePath
    Initialize-CsStore $filePath

    @("User1", "User2", "Other", "Last") | ForEach-Object {
        $cred = New-Object PSCredential($_, $($_| ConvertTo-SecureString -AsPlainText -Force))
        Set-CsEntry -FilePath $filePath -Name $_ -Credential $cred
    }

    Context "Get all the Entries" {
        $result = Get-CsEntry -FilePath $filePath

        It "should get all the entries" {
            $result | Should Not Be $null
            $result.Length | Should Be 4

            $result[0].Name | Should Be "User1"
            $result[1].Name | Should Be "User2"
            $result[2].Name | Should Be "Other"
            $result[3].Name | Should Be "Last"
        }
    }

    Context "Getting entries by Name" {
        It "should be able to get an entry by its exact name" {
            $result = Get-CsEntry -FilePath $filePath -Name User1
            $result.Name | Should Be "User1"
        }

        It "should be able to get an entries by regex pattern" {
            $result = Get-CsEntry -FilePath $filePath -Name User*
            $result.Length | Should Be 2
            $result[0].Name | Should Be "User1"
            $result[1].Name | Should Be "User2"
        }

        It "should be able to get an entries by any regex pattern in name array" {
            $result = Get-CsEntry -FilePath $filePath -Name User*,*st
            $result.Length | Should Be 3
            $result[0].Name | Should Be "User1"
            $result[1].Name | Should Be "User2"
            $result[2].Name | Should Be "Last"
        }
    }

    Context "CredentialStore file does not exist" {
        It "should throw a validation exception" {
            { Get-CsEntry -Name User1 -FilePath unknown.json } | Should Throw "The path 'unknown.json' does not exist."
        }
    }

    Context "CredentialStore is for a different user" {
        $content = Get-Content -Raw -Path $filePath | ConvertFrom-Json
        $content.UserName = "other"
        $content | ConvertTo-Json |Out-File -FilePath $filePath

        It "should throw a exception" {
            { Get-CsEntry -Name User1 -FilePath $filePath } | Should Throw "Cannot access CredentialStore, it is encrypted for"
        }
    }

    Context "CredentialStore is for a different computer" {
        $content = Get-Content -Raw -Path $filePath | ConvertFrom-Json
        $content.ComputerName = "other"
        $content | ConvertTo-Json | Out-File -FilePath $filePath

        It "should throw a exception" {
            { Get-CsEntry -Name User1 -FilePath $filePath } | Should Throw "Cannot access CredentialStore, it is encrypted for"
        }
    }

    Remove-Item $filePath
}