Test/MSFT_xExchExchangeCertificate.Integration.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
###NOTE: This test module requires use of credentials. The first run through of the tests will prompt for credentials from the logged on user.

Import-Module $PSScriptRoot\..\DSCResources\MSFT_xExchExchangeCertificate\MSFT_xExchExchangeCertificate.psm1
Import-Module $PSScriptRoot\..\Misc\xExchangeCommon.psm1 -Verbose:0
Import-Module $PSScriptRoot\xExchange.Tests.Common.psm1 -Verbose:0

function Test-CertificateServices
{
    [CmdletBinding()]
    param([Hashtable]$TestParams, [string]$ContextLabel)

    Context $ContextLabel {
        [Hashtable]$getResult = Get-TargetResource @TestParams

        It 'Certificate Services Check' {
            CompareCertServices -ServicesActual $getResult.Services -ServicesDesired $TestParams.Services -AllowExtraServices $TestParams.AllowExtraServices | Should Be $true
        }
    }
}

#Check if Exchange is installed on this machine. If not, we can't run tests
[bool]$exchangeInstalled = IsSetupComplete

if ($exchangeInstalled)
{
    #Get required credentials to use for the test
    if ($Global:ShellCredentials -eq $null)
    {
        [PSCredential]$Global:ShellCredentials = Get-Credential -Message "Enter credentials for connecting a Remote PowerShell session to Exchange"
    }

    #Get required credentials to use for the test
    $certPassword = ConvertTo-SecureString "Password1" -AsPlainText -Force
    $certCredentials = New-Object System.Management.Automation.PSCredential ("admin", $certPassword)

    [string]$testCertThumbprint1 = "766358855A7361C6D99D4FB58903AB0833296B2A"
    [string]$testCertThumbprint2 = "4C14890860F4126A18560779B8AF8B818B900F5A"
    [string]$testCertPath1 = "$($PSScriptRoot)\Data\TestCert1.pfx"
    [string]$testCertPath2 = "$($PSScriptRoot)\Data\TestCert2.pfx"

    Describe "Test Installing, Enabling, and Removing Exchange Certificates" {
        #Test installing and enabling test cert 1
        $testParams = @{
            Thumbprint = $testCertThumbprint1
            Credential = $Global:ShellCredentials
            Ensure = "Present"
            AllowExtraServices = $true
            CertCreds = $certCredentials
            CertFilePath = $testCertPath1
            Services = "IIS","POP","IMAP","SMTP"
        }

        $expectedGetResults = @{
            Thumbprint = $testCertThumbprint1
        }

        Test-AllTargetResourceFunctions -Params $testParams -ContextLabel "Install and Enable Test Certificate 1" -ExpectedGetResults $expectedGetResults
        Test-CertificateServices -TestParams $testParams -ContextLabel 'Verify Services on Test Certificate 1'


        #Test installing and enabling test cert2
        $testParams.Thumbprint = $testCertThumbprint2
        $testParams.CertFilePath = $testCertPath2
        $expectedGetResults.Thumbprint = $testCertThumbprint2

        Test-AllTargetResourceFunctions -Params $testParams -ContextLabel "Install and Enable Test Certificate 2" -ExpectedGetResults $expectedGetResults
        Test-CertificateServices -TestParams $testParams -ContextLabel 'Verify Services on Test Certificate 2'


        #Test removing test cert 1
        $testParams.Thumbprint = $testCertThumbprint1
        $testParams.Ensure = "Absent"
        $expectedGetResults = $null
        
        Test-AllTargetResourceFunctions -Params $testParams -ContextLabel "Remove Test Certificate 1" -ExpectedGetResults $expectedGetResults        
    }
}
else
{
    Write-Verbose "Tests in this file require that Exchange is installed to be run."
}