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

    # Import the function to test
    . "$($PSScriptRoot)\Add-ManagerEmailAddressProperty.ps1"

    # Declare our own Get-User
    function Get-User {
        param ($Identity)
    }

    context "when the user has a manager" {
        # Mock Get-User
        mock Get-User {
            return [PSCustomObject]@{
                UserPrincipalName = "manager@domain.com"
            }
        }

        it "returns the user object with the manager email address" {
            # Mock the user object
            $user = [PSCustomObject]@{
                Manager = "Manager Name"
            }

            # Call the function
            $output = Add-ManagerEmailAddressProperty -User $user

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

            # Verify the output
            $output.ManagerEmailAddress | Should Be "manager@domain.com"
        }
    }

    context "when the user does not have a manager" {
        # Mock Get-User
        mock Get-User {
            return [PSCustomObject]@{
                UserPrincipalName = "manager@domain.com"
            }
        }

        it "returns the user object without the manager email address" {
            # Mock the user object
            $user = [PSCustomObject]@{}

            # Call the function
            $output = Add-ManagerEmailAddressProperty -User $user

            # Verify the mocks
            Assert-MockCalled Get-User -Times 0 -Exactly -Scope it

            # Verify the output
            $output.ManagerEmailAddress | Should Be $null
        }
    }
}