tests/TMD.Credentials.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
BeforeAll {
# Define some variable to be used in all of the tests
[String]$CredentialPath = $ENV:HOMEDRIVE + $env:HOMEPATH + '\TMD_Files\Credentials'
[String]$CredentialName = 'TestCredential'
[SecureString]$SecurePassword = ConvertTo-SecureString 'TestPassword' -AsPlainText -Force
[PsCredential]$Credential = [System.Management.Automation.PSCredential]::new('TestUserName', $SecurePassword)

    # Import the module file
    if (Get-Module 'TMD.Common') {
        Remove-Module 'TMD.Common' -Force
    }
    Import-Module "TMD.Common" -Force
}

Describe 'Add-StoredCredential' {
    BeforeAll {
        # Run the function before all of the tests
        Add-StoredCredential -Name $CredentialName -Credential $Credential
    }

    It 'Credential is created and named properly' {
        Join-Path $CredentialPath ($CredentialName + '.credential') | Should -Exist
    }

    It 'File contains proper data' {
        $CredPath = Join-Path $CredentialPath ($CredentialName + '.credential')
        $ImportedCred = Import-Clixml -Path $CredPath
        $ImportedCred.UserName | Should -Be $Credential.UserName
        $ImportedCred | Should -BeOfType System.Management.Automation.PSCredential
    }

    It 'Error is thrown when no credentials are supplied' {
        $Global:TmdRunningAction = $true
        { Add-StoredCredential -Name $CredentialName -ErrorAction Stop} | Should -Throw
        $Global:TmdRunningAction = $false
    }

    # TODO: Add pipeline tests
    
    AfterAll {
        $CredFile = Join-Path $CredentialPath ($CredentialName + '.credential')
        if (Test-Path -Path $CredFile) {
            Remove-Item $CredFile -Force
        }
    }
}

Describe 'Remove-StoredCredential' {
    BeforeEach {
        # Create a credential before each of the tests
        Add-StoredCredential -Name $CredentialName -Credential $Credential
    }
    
    It 'File is actually removed' {
        Remove-StoredCredential -Name $CredentialName
        Join-Path $CredentialPath ($CredentialName + '.credential') | Should -Not -Exist
    }
    
    It 'Error is thrown when Name is incorrect' {
        {Remove-StoredCredential -Name 'IncorrectCredentialName' -ErrorAction Stop} | Should -Throw
    }
    
    # TODO: Add pipeline tests
    AfterAll {
        Remove-Item (Join-Path $CredentialPath ($CredentialName + '.credential')) -Force
    }
}

Describe 'Get-StoredCredential' {
    BeforeAll {
        1..5 | ForEach-Object {
            Add-StoredCredential -Name $_ -Credential $Credential
        }
    }
    
    It 'Only one parameter set can be used' {
        {Get-StoredCredential -Name $CredentialName -List} | Should -Throw
    }

    Context "Without Params" {
        It 'List of objects is returned' {
            $Creds = Get-StoredCredential
            $Creds.GetType().BaseType.ToString() | Should -Be 'System.Array'
            $Creds | Should -BeOfType PSCustomObject
        }
    }
    
    Context 'Using -List' {
        It 'List of strings is returned' {
            $CredList = Get-StoredCredential -List
            $CredList.GetType().BaseType.ToString() | Should -Be 'System.Array'
            $CredList | Should -BeOfType String
        }
        
        It '1-5 are in the list' {
            $CredList = Get-StoredCredential -List
            1..5 | ForEach-Object {
                $CredList | Should -Contain $_
            }
        }
    }
    # TODO: Add pipeline tests
    
    Context 'Specifying Name' {
        BeforeAll {
            # Run the function before all of the tests
            Add-StoredCredential -Name $CredentialName -Credential $Credential
        }
    
        It 'PSCredential should be returned' {
            $Cred = Get-StoredCredential -Name $CredentialName
            $Cred | Should -BeOfType PSCredential
        }

        It 'No error if Name is incorrect' {
            $Cred = Get-StoredCredential -Name 'IncorrectCredentialName'
            $Cred | Should -BeNullOrEmpty
        }


    }
    
    AfterAll {
        1..5 | ForEach-Object {
            Remove-Item (Join-Path $CredentialPath "$_.credential") -Force
        }
    }
}