|
| 1 | +# Pester tests for Get-CIPPSharedMailboxAccountEnabledReport |
| 2 | +# Verifies the cached Mailboxes + Users join, accountEnabled filtering, payload shape, and AllTenants fan-out |
| 3 | + |
| 4 | +BeforeAll { |
| 5 | + $RepoRoot = Split-Path -Parent (Split-Path -Parent (Split-Path -Parent $PSCommandPath)) |
| 6 | + $ReportPath = Join-Path $RepoRoot 'Modules/CIPPCore/Public/Get-CIPPSharedMailboxAccountEnabledReport.ps1' |
| 7 | + |
| 8 | + # Minimal stubs so Mock has commands to replace during tests |
| 9 | + function Get-CIPPDbItem { param($TenantFilter, $Type) } |
| 10 | + function Get-Tenants { param([switch]$IncludeErrors) } |
| 11 | + function Write-LogMessage { param($API, $tenant, $message, $sev) } |
| 12 | + |
| 13 | + . $ReportPath |
| 14 | + |
| 15 | + function New-DbItem { |
| 16 | + param($PartitionKey, $RowKey, $Data, $Timestamp) |
| 17 | + [pscustomobject]@{ |
| 18 | + PartitionKey = $PartitionKey |
| 19 | + RowKey = $RowKey |
| 20 | + Timestamp = $Timestamp |
| 21 | + Data = ($Data | ConvertTo-Json -Depth 5 -Compress) |
| 22 | + } |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +Describe 'Get-CIPPSharedMailboxAccountEnabledReport' { |
| 27 | + BeforeEach { |
| 28 | + $script:Tenant = 'contoso.onmicrosoft.com' |
| 29 | + |
| 30 | + $script:SharedMailbox = @{ UPN = 'shared@contoso.com'; recipientTypeDetails = 'SharedMailbox' } |
| 31 | + $script:RegularMailbox = @{ UPN = 'user@contoso.com'; recipientTypeDetails = 'UserMailbox' } |
| 32 | + |
| 33 | + $script:EnabledUser = @{ |
| 34 | + userPrincipalName = 'shared@contoso.com' |
| 35 | + displayName = 'Shared Mailbox' |
| 36 | + givenName = 'Shared' |
| 37 | + surname = 'Mailbox' |
| 38 | + accountEnabled = $true |
| 39 | + assignedLicenses = @(@{ skuId = 'sku-1' }) |
| 40 | + id = 'user-id-shared' |
| 41 | + onPremisesSyncEnabled = $false |
| 42 | + } |
| 43 | + $script:RegularUser = @{ |
| 44 | + userPrincipalName = 'user@contoso.com' |
| 45 | + displayName = 'Regular User' |
| 46 | + accountEnabled = $true |
| 47 | + id = 'user-id-regular' |
| 48 | + onPremisesSyncEnabled = $false |
| 49 | + } |
| 50 | + |
| 51 | + $script:Now = Get-Date |
| 52 | + |
| 53 | + Mock -CommandName Write-LogMessage -MockWith { } |
| 54 | + Mock -CommandName Get-Tenants -MockWith { @([pscustomobject]@{ defaultDomainName = 'contoso.onmicrosoft.com' }) } |
| 55 | + } |
| 56 | + |
| 57 | + It 'joins a shared mailbox to its user and returns the live payload shape' { |
| 58 | + Mock -CommandName Get-CIPPDbItem -ParameterFilter { $Type -eq 'Mailboxes' } -MockWith { |
| 59 | + @( |
| 60 | + New-DbItem -PartitionKey $script:Tenant -RowKey 'Mailboxes-Count' -Data @{ Count = 2 } -Timestamp $script:Now |
| 61 | + New-DbItem -PartitionKey $script:Tenant -RowKey '1' -Data $script:SharedMailbox -Timestamp $script:Now |
| 62 | + New-DbItem -PartitionKey $script:Tenant -RowKey '2' -Data $script:RegularMailbox -Timestamp $script:Now |
| 63 | + ) |
| 64 | + } |
| 65 | + Mock -CommandName Get-CIPPDbItem -ParameterFilter { $Type -eq 'Users' } -MockWith { |
| 66 | + @( |
| 67 | + New-DbItem -PartitionKey $script:Tenant -RowKey 'Users-Count' -Data @{ Count = 2 } -Timestamp $script:Now |
| 68 | + New-DbItem -PartitionKey $script:Tenant -RowKey 'u1' -Data $script:EnabledUser -Timestamp $script:Now |
| 69 | + New-DbItem -PartitionKey $script:Tenant -RowKey 'u2' -Data $script:RegularUser -Timestamp $script:Now |
| 70 | + ) |
| 71 | + } |
| 72 | + |
| 73 | + $Result = Get-CIPPSharedMailboxAccountEnabledReport -TenantFilter $script:Tenant |
| 74 | + |
| 75 | + @($Result).Count | Should -Be 1 |
| 76 | + $Result[0].UserPrincipalName | Should -Be 'shared@contoso.com' |
| 77 | + $Result[0].id | Should -Be 'user-id-shared' |
| 78 | + $Result[0].accountEnabled | Should -BeTrue |
| 79 | + $Result[0].onPremisesSyncEnabled | Should -BeFalse |
| 80 | + $Result[0].CacheTimestamp | Should -Not -BeNullOrEmpty |
| 81 | + # Must not leak the regular (non-shared) mailbox |
| 82 | + $Result.UserPrincipalName | Should -Not -Contain 'user@contoso.com' |
| 83 | + } |
| 84 | + |
| 85 | + It 'excludes shared mailboxes whose user account is disabled' { |
| 86 | + $script:EnabledUser.accountEnabled = $false |
| 87 | + Mock -CommandName Get-CIPPDbItem -ParameterFilter { $Type -eq 'Mailboxes' } -MockWith { |
| 88 | + @( |
| 89 | + New-DbItem -PartitionKey $script:Tenant -RowKey 'Mailboxes-Count' -Data @{ Count = 1 } -Timestamp $script:Now |
| 90 | + New-DbItem -PartitionKey $script:Tenant -RowKey '1' -Data $script:SharedMailbox -Timestamp $script:Now |
| 91 | + ) |
| 92 | + } |
| 93 | + Mock -CommandName Get-CIPPDbItem -ParameterFilter { $Type -eq 'Users' } -MockWith { |
| 94 | + @(New-DbItem -PartitionKey $script:Tenant -RowKey 'u1' -Data $script:EnabledUser -Timestamp $script:Now) |
| 95 | + } |
| 96 | + |
| 97 | + $Result = Get-CIPPSharedMailboxAccountEnabledReport -TenantFilter $script:Tenant |
| 98 | + |
| 99 | + @($Result).Count | Should -Be 0 |
| 100 | + } |
| 101 | + |
| 102 | + It 'returns an empty result (no throw) when the cache holds no enabled shared mailboxes' { |
| 103 | + Mock -CommandName Get-CIPPDbItem -ParameterFilter { $Type -eq 'Mailboxes' } -MockWith { |
| 104 | + @( |
| 105 | + New-DbItem -PartitionKey $script:Tenant -RowKey 'Mailboxes-Count' -Data @{ Count = 1 } -Timestamp $script:Now |
| 106 | + New-DbItem -PartitionKey $script:Tenant -RowKey '1' -Data $script:RegularMailbox -Timestamp $script:Now |
| 107 | + ) |
| 108 | + } |
| 109 | + Mock -CommandName Get-CIPPDbItem -ParameterFilter { $Type -eq 'Users' } -MockWith { |
| 110 | + @(New-DbItem -PartitionKey $script:Tenant -RowKey 'u2' -Data $script:RegularUser -Timestamp $script:Now) |
| 111 | + } |
| 112 | + |
| 113 | + { Get-CIPPSharedMailboxAccountEnabledReport -TenantFilter $script:Tenant } | Should -Not -Throw |
| 114 | + @(Get-CIPPSharedMailboxAccountEnabledReport -TenantFilter $script:Tenant).Count | Should -Be 0 |
| 115 | + } |
| 116 | + |
| 117 | + It 'throws when no mailbox data is cached' { |
| 118 | + Mock -CommandName Get-CIPPDbItem -ParameterFilter { $Type -eq 'Mailboxes' } -MockWith { @() } |
| 119 | + Mock -CommandName Get-CIPPDbItem -ParameterFilter { $Type -eq 'Users' } -MockWith { @() } |
| 120 | + |
| 121 | + { Get-CIPPSharedMailboxAccountEnabledReport -TenantFilter $script:Tenant } | Should -Throw '*Sync the report data first*' |
| 122 | + } |
| 123 | + |
| 124 | + It 'adds a Tenant column for AllTenants' { |
| 125 | + Mock -CommandName Get-CIPPDbItem -ParameterFilter { $Type -eq 'Mailboxes' } -MockWith { |
| 126 | + @( |
| 127 | + New-DbItem -PartitionKey $script:Tenant -RowKey 'Mailboxes-Count' -Data @{ Count = 1 } -Timestamp $script:Now |
| 128 | + New-DbItem -PartitionKey $script:Tenant -RowKey '1' -Data $script:SharedMailbox -Timestamp $script:Now |
| 129 | + ) |
| 130 | + } |
| 131 | + Mock -CommandName Get-CIPPDbItem -ParameterFilter { $Type -eq 'Users' } -MockWith { |
| 132 | + @(New-DbItem -PartitionKey $script:Tenant -RowKey 'u1' -Data $script:EnabledUser -Timestamp $script:Now) |
| 133 | + } |
| 134 | + |
| 135 | + $Result = Get-CIPPSharedMailboxAccountEnabledReport -TenantFilter 'AllTenants' |
| 136 | + |
| 137 | + @($Result).Count | Should -Be 1 |
| 138 | + $Result[0].Tenant | Should -Be 'contoso.onmicrosoft.com' |
| 139 | + $Result[0].UserPrincipalName | Should -Be 'shared@contoso.com' |
| 140 | + } |
| 141 | +} |
0 commit comments