tests/Initialize-CsStore.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\..\src\Initialize-CsStore.ps1"

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

    Context "Create a new CredentialStore file" {
        Initialize-CsStore -FilePath $filePath

        It "should create a new CredentialStore file" {
            $filePath | Should Exist
        }

        It "should contain an empty credentials collection" {
            $content = Get-Content -Raw -Path $filePath | ConvertFrom-Json
            $content.credentials.Length | Should Be 0
        }

        Remove-Item $filePath
    }

    Context "File already exists exist" {
        New-Item $filePath -Type file

        It "should throw an exception" {
            { Initialize-CsStore -FilePath $filePath } | Should Throw "File already exists, cannot overwrite"
        }

        Remove-Item $filePath
    }

    Context "New CredentialStore should set file meta data" {
        Initialize-CsStore -FilePath $filePath
        $content = Get-Content -Raw -Path $filePath | ConvertFrom-Json

        It "should set the username" {
            $content.UserName | Should Not BeNullOrEmpty
        }

        It "should set the computer name" {
            $content.ComputerName | Should Not BeNullOrEmpty
        }
        
        It "should set the created date" {
            $content.CreatedDate | Should Not BeNullOrEmpty
        }

        Remove-Item $filePath
    }
}