|
| 1 | +# ------------------------------------------------------------------------------ |
| 2 | +# Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information. |
| 3 | +# ------------------------------------------------------------------------------ |
| 4 | + |
| 5 | +BeforeAll { |
| 6 | + if((Get-Module -Name Microsoft.Entra.Beta.SignIns) -eq $null){ |
| 7 | + Import-Module Microsoft.Entra.Beta.SignIns |
| 8 | + } |
| 9 | + Import-Module (Join-Path $PSScriptRoot "..\..\Common-Functions.ps1") -Force |
| 10 | + |
| 11 | + $scriptblock = { |
| 12 | + return @( |
| 13 | + [PSCustomObject]@{ |
| 14 | + "Id" = "bbbbbbbb-1111-2222-3333-cccccccccc55" |
| 15 | + "InviteRedeemUrl" = "https://contoso.com/redeem?rd=https%3a%2f%2finvitations.microsoft.com%2fredeem%2f%3ftenant%aaaabbbb-0000-cccc-1111-dddd2222eeee" |
| 16 | + } |
| 17 | + ) |
| 18 | + } |
| 19 | + Mock -CommandName New-MgBetaInvitation -MockWith $scriptblock -ModuleName Microsoft.Entra.Beta.SignIns |
| 20 | +} |
| 21 | + |
| 22 | +Describe "New-EntraBetaInvitation" { |
| 23 | + Context "Test for New-EntraBetaInvitation" { |
| 24 | + It "Should invite a new external user to your directory." { |
| 25 | + |
| 26 | + $result = New-EntraBetaInvitation -InvitedUserEmailAddress 'someexternaluser@externaldomain.com' -SendInvitationMessage $True -InviteRedirectUrl 'https://myapps.contoso.com' |
| 27 | + $result | Should -Not -BeNullOrEmpty |
| 28 | + $result.Id | should -Be "bbbbbbbb-1111-2222-3333-cccccccccc55" |
| 29 | + |
| 30 | + Should -Invoke -CommandName New-MgBetaInvitation -ModuleName Microsoft.Entra.Beta.SignIns -Times 1 |
| 31 | + } |
| 32 | + |
| 33 | + It "Should reset a redemption for an external user." { |
| 34 | + |
| 35 | + $result = New-EntraBetaInvitation -InvitedUserEmailAddress 'someexternaluser@externaldomain.com' -SendInvitationMessage $True -InviteRedirectUrl 'https://myapps.contoso.com' -ResetRedemption |
| 36 | + $result | Should -Not -BeNullOrEmpty |
| 37 | + $result.Id | should -Be "bbbbbbbb-1111-2222-3333-cccccccccc55" |
| 38 | + |
| 39 | + Should -Invoke -CommandName New-MgBetaInvitation -ModuleName Microsoft.Entra.Beta.SignIns -Times 1 |
| 40 | + } |
| 41 | + |
| 42 | + It "Should fail when InvitedUserEmailAddress is empty" { |
| 43 | + { New-EntraBetaInvitation -InvitedUserEmailAddress -SendInvitationMessage $True -InviteRedirectUrl 'https://myapps.contoso.com' } | Should -Throw "Missing an argument for parameter 'InvitedUserEmailAddress'*" |
| 44 | + } |
| 45 | + |
| 46 | + It "Should fail when InvitedUserEmailAddress is empty string" { |
| 47 | + { New-EntraBetaInvitation -InvitedUserEmailAddress '' -SendInvitationMessage $True -InviteRedirectUrl 'https://myapps.contoso.com' } | Should -Throw "Cannot bind argument to parameter 'InvitedUserEmailAddress' because it is an empty string." |
| 48 | + } |
| 49 | + |
| 50 | + It "Should contain 'User-Agent' header" { |
| 51 | + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion New-EntraBetaFeatureRolloutPolicy" |
| 52 | + |
| 53 | + $result = New-EntraBetaInvitation -InvitedUserEmailAddress 'someexternaluser@externaldomain.com' -SendInvitationMessage $True -InviteRedirectUrl 'https://myapps.contoso.com' |
| 54 | + $result | Should -Not -BeNullOrEmpty |
| 55 | + |
| 56 | + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion New-EntraBetaInvitation" |
| 57 | + |
| 58 | + Should -Invoke -CommandName New-MgBetaInvitation -ModuleName Microsoft.Entra.Beta.SignIns -Times 1 -ParameterFilter { |
| 59 | + $Headers.'User-Agent' | Should -Be $userAgentHeaderValue |
| 60 | + $true |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + It "Should execute successfully without throwing an error " { |
| 65 | + # Disable confirmation prompts |
| 66 | + $originalDebugPreference = $DebugPreference |
| 67 | + $DebugPreference = 'Continue' |
| 68 | + |
| 69 | + try { |
| 70 | + # Act & Assert: Ensure the function doesn't throw an exception |
| 71 | + { $result = New-EntraBetaInvitation -InvitedUserEmailAddress 'someexternaluser@externaldomain.com' -SendInvitationMessage $True -InviteRedirectUrl 'https://myapps.contoso.com' -Debug } | Should -Not -Throw |
| 72 | + } finally { |
| 73 | + # Restore original confirmation preference |
| 74 | + $DebugPreference = $originalDebugPreference |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | + |
0 commit comments