Test/MSFT_xExchExchangeServer.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
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
###NOTE: This test module requires use of credentials. The first run through of the tests will prompt for credentials from the logged on user.
###This module has the following additional requirements;
### * Requires that the ActiveDirectory module is installed

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

#Sets props retrieved by Get-ExchangeServer back to their default values
function PrepTestExchangeServer
{
    [CmdletBinding()]
    param()

    Import-Module ActiveDirectory
    ClearServerADProp("msExchProductID")
    ClearServerADProp("msExchCustomerFeedbackEnabled")
    ClearServerADProp("msExchInternetWebProxy")
    #ClearServerADProp("msExchShadowDisplayName")
}

#Used to null out the specified Active Directory property of an Exchange Server
function ClearServerADProp
{
    [CmdletBinding()]
    param($prop)

    Get-ADObject -SearchBase "$($Global:ExchangeServerDN)" -Filter {ObjectClass -eq "msExchExchangeServer"} | where {$_.ObjectClass -eq "msExchExchangeServer"} | Set-ADObject -Clear "$($prop)"
}

function VerifyServerPrepped
{
    [CmdletBinding()]
    param()

    Context "Server has had relevant properties nulled out for xExchExchangeServer tests" {
        [Hashtable]$getResult = Get-TargetResource @testParams -Verbose

        #It "CustomerFeedbackEnabled should be null" {
        # $getResult.CustomerFeedbackEnabled | Should Be $null
        #}

        It "InternetWebProxy should be empty" {
            [string]::IsNullOrEmpty($getResult.InternetWebProxy) | Should Be $true
        }

        It "ProductKey should be empty" {
            [string]::IsNullOrEmpty($getResult.ProductKey) | Should Be $true
        }
    }
}

$adModule = Get-Module -ListAvailable ActiveDirectory -ErrorAction SilentlyContinue

if ($adModule -ne $null)
{
    #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"
        }

        if ($Global:ExchangeServerDN -eq $null)
        {
            GetRemoteExchangeSession -Credential $Global:ShellCredentials -CommandsToLoad "Get-ExchangeServer"

            $server = Get-ExchangeServer -Identity $env:COMPUTERNAME

            if ($server -ne $null)
            {
                $Global:ExchangeServerDN = $server.DistinguishedName
            }

            if ($Global:ExchangeServerDN -eq $null)
            {
                throw "Failed to determine distinguishedName of Exchange Server object"
            }
        }

        #Get the product key to use for testing
        if ($Global:ProductKey -eq $null)
        {
            $Global:ProductKey = Read-Host -Prompt "Enter the product key to license Exchange with"
        }

        Describe "Test Setting Properties with xExchExchangeServer" {
            #Create out initial test params
            $testParams = @{
                Identity = $env:COMPUTERNAME
                Credential = $Global:ShellCredentials
            }

            #First prepare the server for tests
            PrepTestExchangeServer
            VerifyServerPrepped


            #Now do tests
            $testParams = @{
                Identity = $env:COMPUTERNAME
                Credential = $Global:ShellCredentials
                InternetWebProxy = "http://someproxy.local/"
                ProductKey = $Global:ProductKey
            }

            $expectedGetResults = @{
                Identity = $env:COMPUTERNAME
                InternetWebProxy = "http://someproxy.local/"
                ProductKey = "Licensed"
            }

            Test-AllTargetResourceFunctions -Params $testParams -ContextLabel "Standard xExchExchangeServer tests" -ExpectedGetResults $expectedGetResults
        }
    }
    else
    {
        Write-Verbose "Tests in this file require that Exchange is installed to be run."
    }
}
else
{
    Write-Verbose "Tests in this file require that the ActiveDirectory module is installed. Run: Add-WindowsFeature RSAT-ADDS"
}