Tests/PSWebConfig/Get-WebConfig.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
. (Join-Path $PSScriptRoot '../Import-LocalModule.ps1')

$isVerbose=($VerbosePreference -eq 'Continue')

$webConfigFile = Join-Path $script:FixturePath 'web.config'

$webConfigSections = @(
    "appSettings"
    "connectionStrings"
)

$testInput = New-Object -TypeName PsObject -Property @{
    physicalPath = $webConfigFile
}

Describe "Web.config file test" {
    It "Test-file Should exists" {
        $webConfigFile | Should Exist
    }

    $xml = [xml](Get-Content $webConfigFile)
    It "Should be a valid XMLDocument" {
        $xml.GetType().Name | Should Be "XmlDocument"
    }

    foreach($section in $webConfigSections) {
        It "Should have '$section' configuration section" {
            $xml.configuration.$section | Should Not BeNullOrEmpty
        }
    }
}

Describe "Get-PSWebConfig" {
    Context "Parameters input" {
        It "should accept -Path" {
            Get-PSWebConfig -Path $webConfigFile -AsXml -Verbose:$isVerbose |
            Select-Object -ExpandProperty File |
            Should Be $webConfigFile
        }

        It "should accept Path at 0 position" {
            Get-PSWebConfig $webConfigFile -AsXml -Verbose:$isVerbose |
            Select-Object -ExpandProperty File |
            Should Be $webConfigFile
        }

        It "should accept -InputObject" {
            Get-PSWebConfig -InputObject $testInput -AsXml -Verbose:$isVerbose |
            Select-Object -ExpandProperty File |
            Should Be $testInput.physicalPath
        }
    }
    Context "Pipeline input" {
        It "should accept testInputObject" {
            $testInput |
            Get-PSWebConfig -AsXml -Verbose:$isVerbose |
            Select-Object -ExpandProperty File |
            Should Be $testInput.physicalPath
        }

        It "should accept FileInfo" {
            Get-Item $webConfigFile |
            Get-PSWebConfig -AsXml -Verbose:$isVerbose |
            Select-Object -ExpandProperty File |
            Should Be $testInput.physicalPath
        }
    }
}
Describe "Get-PSWebConfig" {
    Context "Invalid Paths" {
        It "Should not return anything" {
            Get-PSWebConfig -Path 'clearly:\invalid\path\to_fail' -Verbose:$isVerbose |
            Should BeNullOrEmpty
        }
    }

    Context "Default XML output" {
        It "should return the XML object by default" {
            $defaultConfig = Get-PSWebConfig -Path $webConfigFile -Verbose:$isVerbose
            $defaultConfig | Should Not BeNullOrEmpty
            $defaultConfig.GetType().Name | Should Be 'XmlDocument'
            #$defaultConfig.OuterXml | Set-Content ./default.txt -Enc UTF8

            $xmlConfig = Get-PSWebConfig -Path $webConfigFile -AsXml -Verbose:$isVerbose
            $xmlConfig | Should Not BeNullOrEmpty
            $xmlConfig.GetType().Name | Should Be 'XmlDocument'
            #$xmlConfig.OuterXml | Set-Content ./xml.txt -Enc UTF8

            $defaultConfig.OuterXml | Should Be $xmlConfig.OuterXml
        }

        It "should have PSWebConfig.WebConfig additional type" {
            $config = Get-PSWebConfig -Path $webConfigFile -Verbose:$isVerbose
            $config | Should Not BeNullOrEmpty
            $config.psobject.TypeNames -contains 'PsWebConfig.WebConfig' | Should Be $true
        }

        It "Should be a valid XML Configuration" {
            $config = Get-PSWebConfig -Path $webConfigFile -Verbose:$isVerbose
            $config.GetType().Name | Should Be 'XmlDocument'
            $config.configuration | Should Not BeNullOrEmpty
            $config.configuration.GetType().Name | Should Be 'XmlElement'
        }
    }

    Context "AsFileInfo output" {
        It "should match the source File" {
            $config = Get-PSWebConfig -Path $webConfigFile -AsFileInfo -Verbose:$isVerbose
            $config | Should Not BeNullOrEmpty
            $config | Should Be $webConfigFile
            $config.GetType().Name | Should Be 'FileInfo'
        }

        It "should find web.config in folders" {
            $config = Get-PSWebConfig -Path $script:FixturePath -AsFileInfo -Verbose:$isVerbose
            $config | Should Not BeNullOrEmpty
            $config.FullName | Should Be $webConfigFile
        }
    }

    Context "Unencrypted text output" {
        It "should be the same XML string as the source file" {
            $config = Get-PSWebConfig -Path $webConfigFile -AsText -Verbose:$isVerbose
            $config | Should Not BeNullOrEmpty
            $config.GetType().Name | Should Be 'String'

            $rawConfig = Get-Content -Path $webConfigFile

            #Convert both String to XML to strip unnessary whitespaces
            ([xml]$config).OuterXml  | Should Be ([xml]$rawConfig).OuterXml
        }
    }
}