Skip to content

Commit edfe33a

Browse files
committed
chore: Enhance tests for Get-CIPPAlertGlobalAdminAllowList with error handling and additional scenarios
1 parent bf2c2c0 commit edfe33a

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

Tests/Alerts/Get-CIPPAlertGlobalAdminAllowList.Tests.ps1

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ BeforeAll {
99
function New-GraphGetRequest { param($uri, $tenantid, $AsApp) }
1010
function Write-AlertTrace { param($cmdletName, $tenantFilter, $data) }
1111
function Write-AlertMessage { param($tenant, $message) }
12+
function Get-NormalizedError { param($message) $message }
1213

1314
. $AlertPath
1415
}
@@ -17,6 +18,7 @@ Describe 'Get-CIPPAlertGlobalAdminAllowList' {
1718
BeforeEach {
1819
$script:CapturedData = $null
1920
$script:CapturedTenant = $null
21+
$script:CapturedErrorMessage = $null
2022

2123
Mock -CommandName New-GraphGetRequest -MockWith {
2224
@(
@@ -41,7 +43,10 @@ Describe 'Get-CIPPAlertGlobalAdminAllowList' {
4143
$script:CapturedTenant = $tenantFilter
4244
}
4345

44-
Mock -CommandName Write-AlertMessage {}
46+
Mock -CommandName Write-AlertMessage -MockWith {
47+
param($tenant, $message)
48+
$script:CapturedErrorMessage = $message
49+
}
4550
}
4651

4752
It 'emits per-admin alerts when AlertEachAdmin is true' {
@@ -64,6 +69,17 @@ Describe 'Get-CIPPAlertGlobalAdminAllowList' {
6469
$CapturedData[0].NonCompliantUsers | Should -Not -Contain '[email protected]'
6570
}
6671

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+
6783
It 'suppresses alert when UPN prefix is approved (comma separated list)' {
6884
$allowInput = @{ ApprovedGlobalAdmins = 'breakglass,otheradmin'; AlertEachAdmin = $true }
6985
Get-CIPPAlertGlobalAdminAllowList -TenantFilter 'contoso.onmicrosoft.com' -InputValue $allowInput
@@ -77,4 +93,14 @@ Describe 'Get-CIPPAlertGlobalAdminAllowList' {
7793

7894
$CapturedData | Should -BeNullOrEmpty
7995
}
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+
}
80106
}

0 commit comments

Comments
 (0)