|
| 1 | +# Pester tests for Get-CIPPAlertGlobalAdminAllowList |
| 2 | +# Verifies prefix-based allow list handling and alert emission |
| 3 | + |
| 4 | +BeforeAll { |
| 5 | + $RepoRoot = Split-Path -Parent (Split-Path -Parent (Split-Path -Parent $PSCommandPath)) |
| 6 | + $AlertPath = Join-Path $RepoRoot 'Modules/CIPPCore/Public/Alerts/Get-CIPPAlertGlobalAdminAllowList.ps1' |
| 7 | + |
| 8 | + # Provide minimal stubs so Mock has commands to replace during tests |
| 9 | + function New-GraphGetRequest { param($uri, $tenantid, $AsApp) } |
| 10 | + function Write-AlertTrace { param($cmdletName, $tenantFilter, $data) } |
| 11 | + function Write-AlertMessage { param($tenant, $message) } |
| 12 | + function Get-NormalizedError { param($message) $message } |
| 13 | + |
| 14 | + . $AlertPath |
| 15 | +} |
| 16 | + |
| 17 | +Describe 'Get-CIPPAlertGlobalAdminAllowList' { |
| 18 | + BeforeEach { |
| 19 | + $script:CapturedData = $null |
| 20 | + $script:CapturedTenant = $null |
| 21 | + $script:CapturedErrorMessage = $null |
| 22 | + |
| 23 | + Mock -CommandName New-GraphGetRequest -MockWith { |
| 24 | + @( |
| 25 | + [pscustomobject]@{ |
| 26 | + '@odata.type' = '#microsoft.graph.user' |
| 27 | + displayName = 'Allowed Admin' |
| 28 | + userPrincipalName = '[email protected]' |
| 29 | + id = 'id-allowed' |
| 30 | + }, |
| 31 | + [pscustomobject]@{ |
| 32 | + '@odata.type' = '#microsoft.graph.user' |
| 33 | + displayName = 'Unapproved Admin' |
| 34 | + userPrincipalName = '[email protected]' |
| 35 | + id = 'id-unapproved' |
| 36 | + } |
| 37 | + ) |
| 38 | + } |
| 39 | + |
| 40 | + Mock -CommandName Write-AlertTrace -MockWith { |
| 41 | + param($cmdletName, $tenantFilter, $data) |
| 42 | + $script:CapturedData = $data |
| 43 | + $script:CapturedTenant = $tenantFilter |
| 44 | + } |
| 45 | + |
| 46 | + Mock -CommandName Write-AlertMessage -MockWith { |
| 47 | + param($tenant, $message) |
| 48 | + $script:CapturedErrorMessage = $message |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + It 'emits per-admin alerts when AlertEachAdmin is true' { |
| 53 | + $allowInput = @{ ApprovedGlobalAdmins = 'breakglass'; AlertEachAdmin = $true } |
| 54 | + |
| 55 | + Get-CIPPAlertGlobalAdminAllowList -TenantFilter 'contoso.onmicrosoft.com' -InputValue $allowInput |
| 56 | + |
| 57 | + $CapturedData | Should -Not -BeNullOrEmpty |
| 58 | + $CapturedData.UserPrincipalName | Should -Contain '[email protected]' |
| 59 | + $CapturedData.UserPrincipalName | Should -Not -Contain '[email protected]' |
| 60 | + $CapturedTenant | Should -Be 'contoso.onmicrosoft.com' |
| 61 | + } |
| 62 | + |
| 63 | + It 'emits single aggregated alert when AlertEachAdmin is false (default)' { |
| 64 | + Get-CIPPAlertGlobalAdminAllowList -TenantFilter 'contoso.onmicrosoft.com' -InputValue 'breakglass' |
| 65 | + |
| 66 | + $CapturedData | Should -Not -BeNullOrEmpty |
| 67 | + $CapturedData.Count | Should -Be 1 |
| 68 | + $CapturedData[ 0].NonCompliantUsers | Should -Contain '[email protected]' |
| 69 | + $CapturedData[ 0].NonCompliantUsers | Should -Not -Contain '[email protected]' |
| 70 | + } |
| 71 | + |
| 72 | + It 'emits single aggregated alert when AlertEachAdmin is explicitly false via input object' { |
| 73 | + $allowInput = @{ ApprovedGlobalAdmins = 'breakglass'; AlertEachAdmin = $false } |
| 74 | + |
| 75 | + Get-CIPPAlertGlobalAdminAllowList -TenantFilter 'contoso.onmicrosoft.com' -InputValue $allowInput |
| 76 | + |
| 77 | + $CapturedData | Should -Not -BeNullOrEmpty |
| 78 | + $CapturedData.Count | Should -Be 1 |
| 79 | + $CapturedData[ 0].NonCompliantUsers | Should -Contain '[email protected]' |
| 80 | + $CapturedData[ 0].NonCompliantUsers | Should -Not -Contain '[email protected]' |
| 81 | + } |
| 82 | + |
| 83 | + It 'suppresses alert when UPN prefix is approved (comma separated list)' { |
| 84 | + $allowInput = @{ ApprovedGlobalAdmins = 'breakglass,otheradmin'; AlertEachAdmin = $true } |
| 85 | + Get-CIPPAlertGlobalAdminAllowList -TenantFilter 'contoso.onmicrosoft.com' -InputValue $allowInput |
| 86 | + |
| 87 | + $CapturedData | Should -BeNullOrEmpty |
| 88 | + } |
| 89 | + |
| 90 | + It 'accepts ApprovedGlobalAdmins property when provided as hashtable' { |
| 91 | + $allowInput = @{ ApprovedGlobalAdmins = 'breakglass,otheradmin' } |
| 92 | + Get-CIPPAlertGlobalAdminAllowList -TenantFilter 'contoso.onmicrosoft.com' -InputValue $allowInput |
| 93 | + |
| 94 | + $CapturedData | Should -BeNullOrEmpty |
| 95 | + } |
| 96 | + |
| 97 | + It 'writes alert message when Graph call fails' { |
| 98 | + Mock -CommandName New-GraphGetRequest -MockWith { throw 'Graph failure' } -Verifiable |
| 99 | + |
| 100 | + Get-CIPPAlertGlobalAdminAllowList -TenantFilter 'contoso.onmicrosoft.com' -InputValue 'breakglass' |
| 101 | + |
| 102 | + $CapturedData | Should -BeNullOrEmpty |
| 103 | + $CapturedErrorMessage | Should -Match 'Failed to check approved Global Admins' |
| 104 | + $CapturedErrorMessage | Should -Match 'Graph failure' |
| 105 | + } |
| 106 | +} |
0 commit comments