Functions/Get-AllExchangeOnlineUsers.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
describe "BitTitan.Runbooks.ExchangeOnline/Get-AllExchangeOnlineUsers" -Tag "module", "unit" {

    # Import the function to test
    . "$($PSScriptRoot)\Get-AllExchangeOnlineUsers.ps1"

    # Declare external functions and mocks
    function Get-BT_RunbookEnvironment {}
    function Get-Office365TestEnvironmentAvailableUsers {
        param ($Domain, [Switch]$ExchangeOnline)
    }
    function Get-User {
        param ($ResultSize)
    }

    context "when the environment is set to testing" {
        # Declare mocks
        mock Get-BT_RunbookEnvironment {
            return @{
                IsTestEnvironment = $true
            }
        }
        mock Get-Office365TestEnvironmentAvailableUsers {}
        $Global:ExchangeOnlineUsername = "admin@domain.com"

        it "returns the test environment users" {
            # Call the function
            Get-AllExchangeOnlineUsers

            # Verify the mocks
            Assert-MockCalled Get-Office365TestEnvironmentAvailableUsers -Times 1 -Exactly -ParameterFilter {
                $Domain -eq "domain.com" -and $ExchangeOnline
            } -Scope it
        }
    }

    context "when the environment is not set to testing" {
        # Declare mocks
        mock Get-BT_RunbookEnvironment {
            return @{
                IsTestEnvironment = $false
            }
        }
        mock Get-User {}
        $Global:ExchangeOnlineUsername = "admin@domain.com"

        it "returns the Exchange Online users" {
            # Call the function
            Get-AllExchangeOnlineUsers

            # Verify the mocks
            Assert-MockCalled Get-User -Times 1 -Exactly -ParameterFilter {
                $ResultSize -eq "unlimited"
            } -Scope it
        }
    }
}