SecretManagement.Chromium.Extension/Tests/Get-SecretInfo.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
#requires -modules @{ModuleName="Pester"; ModuleVersion="5.1.0"}
Describe 'Get-SecretInfo' {
    
    BeforeEach {
        . $PSScriptRoot/Shared.ps1
        PrepareTestEnvironment
        Register-SecretVault @registerVaultParams
    }
    AfterEach {
        TeardownTestEnvironment
        $testVault | Microsoft.Powershell.SecretManagement\Unregister-SecretVault -ErrorAction SilentlyContinue
    }
    It 'All secrets' {
        $secretInfo = SecretManagement.Chromium.Extension\Get-SecretInfo @defaultVaultParams
        $secretInfo | Should -HaveCount 8
    }

    It 'Secret by fully qualified name' {
        $secretName = 'pester|https://twitter.com/'
        $secretInfo = SecretManagement.Chromium.Extension\Get-SecretInfo @defaultVaultParams -Filter $secretName
        $secretInfo | Should -HaveCount 1
        $secretInfo.Name | Should -Be "pester${D}https://twitter.com/${D}2"
        $secretInfo.VaultName | Should -be '__PESTER'
        $secretInfo.Type | Should -Be 'PSCredential'
    }
    
    $SCRIPT:D = "`u{2502}"
    $secretSearchTestCases = @(
        @{
            #Fully Qualified
            searchTerm = "pester|https://twitter.com/"
            expectedResultCount = 1
            expectedNames = "pester${D}https://twitter.com/${D}2"
        }
        @{
            #Explicit Domain
            searchTerm = 'https://twitter.com/'
            expectedResultCount = 3
            expectedNames = @(
                "pester${D}https://twitter.com/${D}2"
                "pester2${D}https://twitter.com/${D}3"
                "pester3${D}https://twitter.com/${D}4"
            )
        }
        @{
            #Wildcard Domain
            searchTerm = '*twit*'
            expectedResultCount = 3
            expectedNames = @(
                "pester${D}https://twitter.com/${D}2"
                "pester2${D}https://twitter.com/${D}3"
                "pester3${D}https://twitter.com/${D}4"
            )
        }
        @{
            #Wildcard Domain w/ mistake
            searchTerm = '*twit*xxxmistyped*'
            expectedResultCount = 0
        }
        @{
            #Explicit Username
            searchTerm = 'pester2|'
            expectedResultCount = 2
            expectedNames = @(
                "pester2${D}https://twitter.com/${D}3"
                "pester2${D}https://www.facebook.com/${D}7"
            )
        }
        @{
            #Wildcard Username
            searchTerm = 'pester*|'
            expectedResultCount = 6
            expectedNames = @(
                "pester${D}https://twitter.com/${D}2"
                "pester2${D}https://twitter.com/${D}3"
                "pester3${D}https://twitter.com/${D}4"
                "pester1${D}https://www.facebook.com/${D}6"
                "pester2${D}https://www.facebook.com/${D}7"
                "pester3${D}https://www.facebook.com/${D}8"
            )
        }
        @{
            #Double Wildcard Username
            searchTerm = '*pes*|'
            expectedResultCount = 6
            expectedNames = @(
                "pester${D}https://twitter.com/${D}2"
                "pester2${D}https://twitter.com/${D}3"
                "pester3${D}https://twitter.com/${D}4"
                "pester1${D}https://www.facebook.com/${D}6"
                "pester2${D}https://www.facebook.com/${D}7"
                "pester3${D}https://www.facebook.com/${D}8"
            )
        }
        @{
            #Intermediate wildcard
            searchTerm = '*p*s*|'
            expectedResultCount = 6
            expectedNames = @(
                "pester${D}https://twitter.com/${D}2"
                "pester2${D}https://twitter.com/${D}3"
                "pester3${D}https://twitter.com/${D}4"
                "pester1${D}https://www.facebook.com/${D}6"
                "pester2${D}https://www.facebook.com/${D}7"
                "pester3${D}https://www.facebook.com/${D}8"
            )
        }
        @{
            #Intermediate wildcard with wrong term
            searchTerm = '*p*xxxmistyped*|'
            expectedResultCount = 0
        }
        @{
            #Combined Intermediate wildcard
            searchTerm = 'pester*|*twitter*'
            expectedResultCount = 3
            expectedNames = @(
                "pester${D}https://twitter.com/${D}2"
                "pester2${D}https://twitter.com/${D}3"
                "pester3${D}https://twitter.com/${D}4"
            )
        }
        @{
            #Http and Https
            searchTerm = 'pester*|*twitter*'
            expectedResultCount = 3
            expectedNames = @(
                "pester${D}https://twitter.com/${D}2"
                "pester2${D}https://twitter.com/${D}3"
                "pester3${D}https://twitter.com/${D}4"
            )
        }
    )
    It 'Secret Search <searchTerm>' -TestCases $secretSearchTestCases {
        $secretInfo = SecretManagement.Chromium.Extension\Get-SecretInfo @defaultVaultParams -Filter $searchTerm
        $secretInfo | Should -HaveCount $expectedResultCount
        $secretInfo.Name.foreach{
            $PSItem | Should -BeIn $ExpectedNames
        }
        $secretInfo.VaultName.foreach{
            $PSItem | Should -be '__PESTER'
        } 
        $secretInfo.Type.foreach{
            $PSItem | Should -Be 'PSCredential'
        } 
    }
}