Functions/Block-UntilMSOnlineUserAvailable.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
describe "BitTitan.Runbooks.MSOnline/Block-UntilMSOnlineUserAvailable" -Tags "module", "unit" {

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

    # Declare our own Get-MsolUser
    # If we don't do this the mock will not work
    function Get-MsolUser {
        param ($SearchString)
    }

    # Declare our own Start-Sleep so that the function doesn't delay
    function Start-Sleep {
        param ($Seconds)
    }

    context "when the user is immediately available" {
        # Mock Get-MsolUser
        mock Get-MsolUser {
            return [PSCustomObject]@{
                UserPrincipalName = "user@domain.com"
            }
        }

        # Mock Start-Sleep
        mock Start-Sleep {}

        it "returns the user after trying once" {
            # Call the function
            $output = Block-UntilMSOnlineUserAvailable -UserPrincipalName "user@domain.com"

            # Verify the mocks
            Assert-MockCalled Get-MsolUser -Times 1 -Exactly -ParameterFilter {
                $SearchString -eq "user@domain.com"
            } -Scope it
            Assert-MockCalled Start-Sleep -Times 0 -Exactly -Scope it

            # Verify the output
            $output.UserPrincipalName | Should Be "user@domain.com"
        }
    }

    context "when the user is available after 10 tries" {
        # Mock Get-MsolUser
        $Global:numTries = 0
        mock Get-MsolUser {
            ++$Global:numTries
            if ($Global:numTries -ge 10) {
                return [PSCustomObject]@{
                    UserPrincipalName = "user@domain.com"
                }
            }
        }

        # Mock Start-Sleep
        mock Start-Sleep {}

        it "returns the user after trying 10 times" {
            # Call the function
            $output = Block-UntilMSOnlineUserAvailable -UserPrincipalName "user@domain.com" -WaitLimit 20

            # Verify the mocks
            Assert-MockCalled Get-MsolUser -Times 10 -Exactly -ParameterFilter {
                $SearchString -eq "user@domain.com"
            } -Scope it
            Assert-MockCalled Start-Sleep -Times 9 -Exactly -Scope it

            # Verify the output
            $output.UserPrincipalName | Should Be "user@domain.com"
        }
    }

    context "when the user is not available" {
        # Mock Get-MsolUser
        mock Get-MsolUser {}

        # Mock Start-Sleep
        mock Start-Sleep {}

        it "returns null after trying the maximum number of times and outputs an error" {
            # Call the function
            $output = Block-UntilMSOnlineUserAvailable -UserPrincipalName "user@domain.com" -WaitLimit 20 `
                -ErrorAction SilentlyContinue -ErrorVariable errorVariable

            # Verify the mocks
            Assert-MockCalled Get-MsolUser -Times 20 -Exactly -ParameterFilter {
                $SearchString -eq "user@domain.com"
            } -Scope it
            Assert-MockCalled Start-Sleep -Times 20 -Exactly -Scope it

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

}