Functions/Connect-AzureRmsAdminAccount.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
describe "BitTitan.Runbooks.AzureRMS/Connect-AzureRmsAdminAccount" -Tags "module", "unit" {

    # Import the function to test
    . "$($PSScriptRoot)\Connect-AzureRmsAdminAccount.ps1"

    # Declare our own Get-CredentialFromMSPCompleteEndpoint
    # If we don't do this the mock will not work
    function Get-CredentialFromMSPCompleteEndpoint {
        param ($endpoint)
        return [PSCredential]::new("username", ("password" | ConvertTo-SecureString -AsPlainText -Force))
    }

    # Declare our own Connect-AadrmService
    # If we don't do this the mock will not work
    function Connect-AadrmService {
        param ([PSCredential]$Credential)
    }

    context "when there are no issues" {
        # Mock Get-CredentialFromMSPCompleteEndpoint
        mock Get-CredentialFromMSPCompleteEndpoint {
            return [PSCredential]::new("username", ("password" | ConvertTo-SecureString -AsPlainText -Force))
        }

        # Mock Connect-AadrmService
        mock Connect-AadrmService {}

        it "connects to Azure RMS with the provided username and password" {
            # Call the function
            $output = Connect-AzureRmsAdminAccount -Username "username" -Password ("password" | ConvertTo-SecureString -AsPlainText -Force)

            # Verify the mocks
            Assert-MockCalled Get-CredentialFromMSPCompleteEndpoint -Times 0 -Exactly -Scope it
            Assert-MockCalled Connect-AadrmService -Times 1 -Exactly -ParameterFilter {
                $Credential.Username -eq "username" -and $Credential.GetNetworkCredential().Password -eq "password"
            } -Scope it

            # Verify the output
            $output | Should Be $true
        }

        it "connects to Azure RMS with the provided endpoint" {
            # Mock the endpoint
            $endpoint = "endpoint"

            # Call the function
            $output = Connect-AzureRmsAdminAccount -Endpoint $endpoint

            # Verify the mocks
            Assert-MockCalled Get-CredentialFromMSPCompleteEndpoint -Times 1 -Exactly -ParameterFilter {
                $Endpoint -eq "endpoint"
            } -Scope it
            Assert-MockCalled Connect-AadrmService -Times 1 -Exactly -ParameterFilter {
                $Credential.Username -eq "username" -and $Credential.GetNetworkCredential().Password -eq "password"
            } -Scope it

            # Verify the output
            $output | Should Be $true
        }
    }

    context "when there is an exception while connecting to Azure RMS" {
        # Mock Get-CredentialFromMSPCompleteEndpoint
        mock Get-CredentialFromMSPCompleteEndpoint {
            return [PSCredential]::new("username", ("password" | ConvertTo-SecureString -AsPlainText -Force))
        }

        # Mock Connect-AadrmService
        mock Connect-AadrmService {
            throw "throws exception"
        }

        it "fails to connect to Azure RMS and outputs an error message" {
            # Call the function
            $output = Connect-AzureRmsAdminAccount -Username "username" -Password ("password" | ConvertTo-SecureString -AsPlainText -Force) `
                -ErrorAction SilentlyContinue -ErrorVariable errorVariable

            # Verify the mocks
            Assert-MockCalled Get-CredentialFromMSPCompleteEndpoint -Times 0 -Exactly -Scope it
            Assert-MockCalled Connect-AadrmService -Times 1 -Exactly -ParameterFilter {
                $Credential.Username -eq "username" -and $Credential.GetNetworkCredential().Password -eq "password"
            } -Scope it

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