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

    # Import the function to test
    . "$($PSScriptRoot)\Block-UntilExchangeOnlineGroupAvailable.ps1"

    # Declare external functions and mocks
    function Get-Group {
        param ($Identity)
    }
    function Get-DynamicDistributionGroup {
        param ($Identity)
    }

    context "when the group is immediately available" {
        # Declare mocks
        mock Get-Group {
            return [PSCustomObject]@{
                GroupPrincipalName = "group@domain.com"
            }
        }

        it "returns the distribution group after trying once" {
            # Call the function
            $output = Block-UntilExchangeOnlineGroupAvailable -Identity "group@domain.com" -WaitLimitSeconds 20 -WaitIntervalSeconds 1

            # Verify the mocks
            Assert-MockCalled Get-Group -Times 1 -Exactly -ParameterFilter {
                $Identity -eq "group@domain.com"
            } -Scope it

            # Verify the output
            $output.GroupPrincipalName | Should Be "group@domain.com"
        }

        # Declare mocks
        mock Get-DynamicDistributionGroup {
            return [PSCustomObject]@{
                GroupPrincipalName = "group@domain.com"
            }
        }

        it "returns the dynamic distribution group after trying once" {
            # Call the function
            $output = Block-UntilExchangeOnlineGroupAvailable -Identity "group@domain.com" -GroupType "DynamicDistributionGroup" -WaitLimitSeconds 20

            # Verify the mocks
            Assert-MockCalled Get-DynamicDistributionGroup -Times 1 -Exactly -ParameterFilter {
                $Identity -eq "group@domain.com"
            } -Scope it

            # Verify the output
            $output.GroupPrincipalName | Should Be "group@domain.com"
        }
    }

    context "when the group is available after several tries" {
        # Declare mocks
        $Global:numTries = 0
        mock Get-Group {
            ++$Global:numTries
            if ($Global:numTries -ge 5) {
                return [PSCustomObject]@{
                    GroupPrincipalName = "group@domain.com"
                }
            }
        }

        it "returns the group after trying several times" {
            # Call the function
            $output = Block-UntilExchangeOnlineGroupAvailable -Identity "group@domain.com" -WaitLimitSeconds 20

            # Verify the mocks
            Assert-MockCalled Get-Group -Times 5 -Exactly -ParameterFilter {
                $Identity -eq "group@domain.com"
            } -Scope it

            # Verify the output
            $output.GroupPrincipalName | Should Be "group@domain.com"
        }
    }

    context "when the group is not available" {
        # Declare mocks
        mock Get-Group {}

        it "returns null and outputs an error" {
            # Call the function
            $output = Block-UntilExchangeOnlineGroupAvailable -Identity "group@domain.com" -WaitLimitSeconds 5 `
                -ErrorAction SilentlyContinue -ErrorVariable errorVariable

            # Verify the output
            $errorVariable | Should Not BeNullOrEmpty
            $output | Should Be $null
        }
    }
}