|
4 | 4 |
|
5 | 5 | # Add Pester test to check the API for GroupPolicyEnforcement
|
6 | 6 |
|
7 |
| -Describe 'GroupPolicyEnforcement API Tests' { |
8 |
| - BeforeAll { |
9 |
| - # Setup code if needed |
| 7 | +Describe 'GroupPolicyEnforcement API Tests' -Tags 'CI' { |
| 8 | + |
| 9 | + It 'Should return the correct policy enforcement status' -Skip:(-not $IsWindows) { |
| 10 | + $actualStatus = [Microsoft.PowerShell.PSResourceGet.Cmdlets.GroupPolicyRepositoryEnforcement]::IsGroupPolicyEnabled() |
| 11 | + $actualStatus | Should -Be $false |
10 | 12 | }
|
11 | 13 |
|
12 |
| - AfterAll { |
13 |
| - # Cleanup code if needed |
| 14 | + It 'Should return platform not supported exception on non-windows platform' -Skip:$IsWindows { |
| 15 | + try { |
| 16 | + [Microsoft.PowerShell.PSResourceGet.Cmdlets.GroupPolicyRepositoryEnforcement]::IsGroupPolicyEnabled() |
| 17 | + } |
| 18 | + catch { |
| 19 | + $_.Exception.Message | Should -Be 'Group Policy is not supported on this platform.' |
| 20 | + $_.Exception.InnerException.GetType().FullName | Should -Be 'System.InvalidOperationException' |
| 21 | + } |
14 | 22 | }
|
15 | 23 |
|
16 |
| - It 'Should return the correct policy enforcement status' { |
| 24 | + It 'Group Policy must be enabled before getting allowed repositories' -Skip:(-not $IsWindows) { |
| 25 | + try { |
| 26 | + [Microsoft.PowerShell.PSResourceGet.Cmdlets.GroupPolicyRepositoryEnforcement]::GetAllowedRepositoryURIs() |
| 27 | + } |
| 28 | + catch { |
| 29 | + $_.Exception.InnerException.Message | Should -Be 'Group policy is not enabled.' |
| 30 | + } |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +Describe 'GroupPolicyEnforcement Cmdlet Tests' -Tags 'CI' { |
| 35 | + BeforeEach { |
| 36 | + [Microsoft.PowerShell.PSResourceGet.UtilClasses.InternalHooks]::SetTestHook('EnableGPRegistryHook', $true) |
| 37 | + [Microsoft.PowerShell.PSResourceGet.UtilClasses.InternalHooks]::SetTestHook('GPEnabledStatus', $true) |
| 38 | + [Microsoft.PowerShell.PSResourceGet.UtilClasses.InternalHooks]::SetTestHook('AllowedUri', "https://www.example.com/") |
| 39 | + } |
| 40 | + |
| 41 | + AfterEach { |
| 42 | + [Microsoft.PowerShell.PSResourceGet.UtilClasses.InternalHooks]::SetTestHook('EnableGPRegistryHook', $false) |
| 43 | + [Microsoft.PowerShell.PSResourceGet.UtilClasses.InternalHooks]::SetTestHook('GPEnabledStatus', $false) |
| 44 | + [Microsoft.PowerShell.PSResourceGet.UtilClasses.InternalHooks]::SetTestHook('AllowedUri', $null) |
| 45 | + } |
| 46 | + |
| 47 | + It 'Getting allowed repositories works as expected' -Skip:(-not $IsWindows) { |
| 48 | + $allowedReps = [Microsoft.PowerShell.PSResourceGet.Cmdlets.GroupPolicyRepositoryEnforcement]::GetAllowedRepositoryURIs() |
| 49 | + $allowedReps.AbsoluteUri | Should -Be @("https://www.example.com/") |
| 50 | + } |
17 | 51 |
|
| 52 | + It 'Get-PSResourceRepository lists the allowed repository' -Skip:(-not $IsWindows) { |
18 | 53 | try {
|
19 |
| - $expectedStatus = 'Disabled' |
20 |
| - $actualStatus = [Microsoft.PowerShell.PSResourceGet.Cmdlets.GroupPolicyRepositoryEnforcement]::IsGroupPolicyEnabled() |
21 |
| - $actualStatus | Should -Be $expectedStatus |
| 54 | + Register-PSResourceRepository -Name 'Example' -Uri 'https://www.example.com/' |
| 55 | + $psrep = Get-PSResourceRepository -Name 'Example' |
| 56 | + $psrep | Should -Not -BeNullOrEmpty |
| 57 | + $psrep.IsAllowedByPolicy | Should -Be $true |
22 | 58 | }
|
23 | 59 | finally {
|
24 |
| - |
| 60 | + Unregister-PSResourceRepository -Name 'Example' |
25 | 61 | }
|
| 62 | + } |
| 63 | + |
| 64 | + It 'Find-PSResource is blocked by policy' -Skip:(-not $IsWindows) { |
| 65 | + try { |
| 66 | + Register-PSResourceRepository -Name 'Example' -Uri 'https://www.example.com/' -ApiVersion 'v3' |
| 67 | + { Find-PSResource -Repository PSGallery -Name 'Az.Accounts' -ErrorAction Stop } | Should -Throw "Repository 'PSGallery' is not allowed by Group Policy." |
26 | 68 |
|
| 69 | + # Allow PSGallery and it should not fail |
| 70 | + [Microsoft.PowerShell.PSResourceGet.UtilClasses.InternalHooks]::SetTestHook('AllowedUri', " https://www.powershellgallery.com/api/v2") |
| 71 | + { Find-PSResource -Repository PSGallery -Name 'Az.Accounts' -ErrorAction Stop } | Should -Not -Throw |
| 72 | + } |
| 73 | + finally { |
| 74 | + Unregister-PSResourceRepository -Name 'Example' |
| 75 | + } |
27 | 76 | }
|
28 | 77 |
|
29 |
| - It 'Should throw an error for invalid policy name' { |
30 |
| - # Arrange |
31 |
| - $invalidPolicyName = 'InvalidPolicy' |
| 78 | + It 'Install-PSResource is blocked by policy' -Skip:(-not $IsWindows) { |
| 79 | + try { |
| 80 | + Register-PSResourceRepository -Name 'Example' -Uri 'https://www.example.com/' -ApiVersion 'v3' |
| 81 | + { Find-PSResource -Repository PSGallery -Name 'Az.Accounts' -ErrorAction Stop } | Should -Throw "Repository 'PSGallery' is not allowed by Group Policy." |
32 | 82 |
|
33 |
| - # Act & Assert |
34 |
| - { Get-GroupPolicyEnforcementStatus -PolicyName $invalidPolicyName } | Should -Throw |
| 83 | + # Allow PSGallery and it should not fail |
| 84 | + [Microsoft.PowerShell.PSResourceGet.UtilClasses.InternalHooks]::SetTestHook('AllowedUri', " https://www.powershellgallery.com/api/v2") |
| 85 | + { Find-PSResource -Repository PSGallery -Name 'Az.Accounts' -ErrorAction Stop } | Should -Not -Throw |
| 86 | + } |
| 87 | + finally { |
| 88 | + Unregister-PSResourceRepository -Name 'Example' |
| 89 | + } |
35 | 90 | }
|
36 | 91 |
|
37 |
| - It 'Should return a list of all enforced policies' { |
38 |
| - # Act |
39 |
| - $policies = Get-AllEnforcedPolicies |
| 92 | + It 'Save-PSResource is blocked by policy' -Skip:(-not $IsWindows) { |
| 93 | + try { |
| 94 | + Register-PSResourceRepository -Name 'Example' -Uri 'https://www.example.com/' -ApiVersion 'v3' |
| 95 | + { Find-PSResource -Repository PSGallery -Name 'Az.Accounts' -ErrorAction Stop } | Should -Throw "Repository 'PSGallery' is not allowed by Group Policy." |
40 | 96 |
|
41 |
| - # Assert |
42 |
| - $policies | Should -Not -BeNullOrEmpty |
43 |
| - $policies | Should -BeOfType 'System.Collections.Generic.List[System.String]' |
| 97 | + # Allow PSGallery and it should not fail |
| 98 | + [Microsoft.PowerShell.PSResourceGet.UtilClasses.InternalHooks]::SetTestHook('AllowedUri', " https://www.powershellgallery.com/api/v2") |
| 99 | + { Find-PSResource -Repository PSGallery -Name 'Az.Accounts' -ErrorAction Stop } | Should -Not -Throw |
| 100 | + } |
| 101 | + finally { |
| 102 | + Unregister-PSResourceRepository -Name 'Example' |
| 103 | + } |
44 | 104 | }
|
45 | 105 | }
|
0 commit comments