Skip to content

Commit 3876f53

Browse files
Add tests for Group Policy Enforcement
1 parent 190db19 commit 3876f53

File tree

3 files changed

+99
-21
lines changed

3 files changed

+99
-21
lines changed

src/code/GroupPolicyRepositoryEnforcement.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System;
77
using System.Collections.Generic;
88
using System.Linq;
9+
using Microsoft.PowerShell.PSResourceGet.UtilClasses;
910
using Microsoft.Win32;
1011

1112
namespace Microsoft.PowerShell.PSResourceGet.Cmdlets
@@ -35,6 +36,11 @@ public static bool IsGroupPolicyEnabled()
3536
throw new PlatformNotSupportedException("Group policy is only supported on Windows.");
3637
}
3738

39+
if (InternalHooks.EnableGPRegistryHook)
40+
{
41+
return InternalHooks.GPEnabledStatus;
42+
}
43+
3844
var values = ReadGPFromRegistry();
3945

4046
if (values is not null && values.Count > 0)
@@ -57,6 +63,12 @@ public static bool IsGroupPolicyEnabled()
5763
throw new PlatformNotSupportedException("Group policy is only supported on Windows.");
5864
}
5965

66+
if (InternalHooks.EnableGPRegistryHook)
67+
{
68+
var uri = new Uri(InternalHooks.AllowedUri);
69+
return new Uri[] { uri };
70+
}
71+
6072
if (!IsGroupPolicyEnabled())
6173
{
6274
throw new InvalidOperationException("Group policy is not enabled.");

src/code/InternalHooks.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ public class InternalHooks
99
{
1010
internal static bool InvokedFromCompat;
1111

12+
internal static bool EnableGPRegistryHook;
13+
14+
internal static bool GPEnabledStatus;
15+
16+
internal static string AllowedUri;
17+
1218
public static void SetTestHook(string property, object value)
1319
{
1420
var fieldInfo = typeof(InternalHooks).GetField(property, BindingFlags.Static | BindingFlags.NonPublic);

test/GroupPolicyEnforcement.Tests.ps1

Lines changed: 81 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,102 @@
44

55
# Add Pester test to check the API for GroupPolicyEnforcement
66

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
1012
}
1113

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+
}
1422
}
1523

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+
}
1751

52+
It 'Get-PSResourceRepository lists the allowed repository' -Skip:(-not $IsWindows) {
1853
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
2258
}
2359
finally {
24-
60+
Unregister-PSResourceRepository -Name 'Example'
2561
}
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."
2668

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+
}
2776
}
2877

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."
3282

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+
}
3590
}
3691

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."
4096

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+
}
44104
}
45105
}

0 commit comments

Comments
 (0)