Skip to content

Commit bfc3fee

Browse files
authored
Split rules tests file per type (#90)
* Split rules tests file per type
1 parent dd63885 commit bfc3fee

12 files changed

Lines changed: 2141 additions & 1707 deletions

tests/Rules.Common.Tests.ps1

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
BeforeAll {
2+
# Setup error handling
3+
$ErrorActionPreference = 'Stop';
4+
Set-StrictMode -Version latest;
5+
6+
if ($Env:SYSTEM_DEBUG -eq 'true') {
7+
$VerbosePreference = 'Continue';
8+
}
9+
10+
# Setup tests paths
11+
# $rootPath = $PWD;
12+
$rootPath = $env:GITHUB_WORKSPACE
13+
$ourModule = (Join-Path -Path $rootPath -ChildPath '/src/PSRule.Rules.AzureDevOps')
14+
15+
Import-Module -Name $ourModule -Force;
16+
$here = (Resolve-Path $PSScriptRoot).Path;
17+
18+
# Create tempory test output folder and store path
19+
$outPath = New-Item -Path (Join-Path -Path $here -ChildPath 'out') -ItemType Directory -Force;
20+
$outPath = $outPath.FullName;
21+
22+
# Export all Azure DevOps rule data for project 'psrule-fail-project' to output folder
23+
Connect-AzDevOps -Organization $env:ADO_ORGANIZATION -PAT $env:ADO_PAT
24+
Export-AzDevOpsRuleData -Project $env:ADO_PROJECT -OutputPath $outPath
25+
26+
# Create a temporary test output folder for tests with the ReadOnly TokenType
27+
$outPathReadOnly = New-Item -Path (Join-Path -Path $here -ChildPath 'outReadOnly') -ItemType Directory -Force;
28+
$outPathReadOnly = $outPathReadOnly.FullName;
29+
30+
# Export all Azure DevOps rule data for project 'psrule-fail-project' to ReadOnly output folder
31+
Connect-AzDevOps -Organization $env:ADO_ORGANIZATION -PAT $env:ADO_PAT_READONLY -TokenType ReadOnly
32+
Export-AzDevOpsRuleData -Project $env:ADO_PROJECT -OutputPath $outPathReadOnly
33+
34+
# Create a temporary test output folder for tests with the FineGrained TokenType
35+
$outPathFineGrained = New-Item -Path (Join-Path -Path $here -ChildPath 'outFineGrained') -ItemType Directory -Force;
36+
$outPathFineGrained = $outPathFineGrained.FullName;
37+
38+
# Export all Azure DevOps rule data for project 'psrule-fail-project' to FineGrained output folder
39+
Connect-AzDevOps -Organization $env:ADO_ORGANIZATION -PAT $env:ADO_PAT_FINEGRAINED -TokenType FineGrained
40+
Export-AzDevOpsRuleData -Project $env:ADO_PROJECT -OutputPath $outPathFineGrained
41+
}
42+
43+
Describe "PSRule.Rules.AzureDevOps Rules" {
44+
Context ' Base rules' {
45+
It ' should contain 59 rules' {
46+
$rules = Get-PSRule -Module PSRule.Rules.AzureDevOps
47+
$rules.Count | Should -Be 59
48+
}
49+
50+
It ' should contain a markdown help file for each rule' {
51+
$rules = Get-PSRule -Module PSRule.Rules.AzureDevOps
52+
$rules | ForEach-Object {
53+
$helpFile = Join-Path -Path "$ourModule/en" -ChildPath "$($_.Name).md"
54+
Test-Path -Path $helpFile | Should -Be $true
55+
}
56+
}
57+
}
58+
}
59+
60+
AfterAll {
61+
# Remove Module
62+
Disconnect-AzDevOps
63+
Remove-Module -Name PSRule.Rules.AzureDevOps -Force;
64+
}

tests/Rules.Groups.Tests.ps1

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
BeforeAll {
2+
# Setup error handling
3+
$ErrorActionPreference = 'Stop';
4+
Set-StrictMode -Version latest;
5+
6+
if ($Env:SYSTEM_DEBUG -eq 'true') {
7+
$VerbosePreference = 'Continue';
8+
}
9+
10+
# Setup tests paths
11+
# $rootPath = $PWD;
12+
$rootPath = $env:GITHUB_WORKSPACE
13+
$ourModule = (Join-Path -Path $rootPath -ChildPath '/src/PSRule.Rules.AzureDevOps')
14+
15+
Import-Module -Name $ourModule -Force
16+
$here = (Resolve-Path $PSScriptRoot).Path
17+
18+
# Get tempory test output folder and store path
19+
$outPath = Get-Item -Path (Join-Path -Path $here -ChildPath 'out')
20+
$outPath = $outPath.FullName
21+
22+
# Run rules with default token type
23+
$ruleResult = Invoke-PSRule -InputPath "$($outPath)/" -Module PSRule.Rules.AzureDevOps -Format Detect -Culture en
24+
25+
# Get temporary test output folder for tests with the ReadOnly TokenType
26+
$outPathReadOnly = Get-Item -Path (Join-Path -Path $here -ChildPath 'outReadOnly')
27+
$outPathReadOnly = $outPathReadOnly.FullName
28+
29+
# Run rules with ReadOnly token type
30+
$ruleResultReadOnly = Invoke-PSRule -InputPath "$($outPathReadOnly)/" -Module PSRule.Rules.AzureDevOps -Format Detect -Culture en
31+
32+
# Get temporary test output folder for tests with the FineGrained TokenType
33+
$outPathFineGrained = Get-Item -Path (Join-Path -Path $here -ChildPath 'outFineGrained')
34+
$outPathFineGrained = $outPathFineGrained.FullName
35+
36+
# Run rules with FineGrained token type
37+
$ruleResultFineGrained = Invoke-PSRule -InputPath "$($outPathFineGrained)/" -Module PSRule.Rules.AzureDevOps -Format Detect -Culture en
38+
}
39+
40+
Describe 'Azure.DevOps.Groups rules' {
41+
Context 'Azure.DevOps.Groups.ProjectAdmins.MinMembers' {
42+
It ' should pass once' {
43+
$ruleHits = @($ruleResult | Where-Object { $_.RuleName -eq 'Azure.DevOps.Groups.ProjectAdmins.MinMembers' })
44+
$ruleHits[0].Outcome | Should -Be 'Pass';
45+
$ruleHits.Count | Should -Be 1;
46+
}
47+
48+
It ' should pass once for ReadOnly token type' {
49+
$ruleHits = @($ruleResultReadOnly | Where-Object { $_.RuleName -eq 'Azure.DevOps.Groups.ProjectAdmins.MinMembers' })
50+
$ruleHits[0].Outcome | Should -Be 'Pass';
51+
$ruleHits.Count | Should -Be 1;
52+
}
53+
54+
It ' should pass once for FineGrained token type' {
55+
$ruleHits = @($ruleResultFineGrained | Where-Object { $_.RuleName -eq 'Azure.DevOps.Groups.ProjectAdmins.MinMembers' })
56+
$ruleHits[0].Outcome | Should -Be 'Pass';
57+
$ruleHits.Count | Should -Be 1;
58+
}
59+
}
60+
61+
Context 'Azure.DevOps.Groups.ProjectAdmins.MaxMembers' {
62+
It ' should pass once' {
63+
$ruleHits = @($ruleResult | Where-Object { $_.RuleName -eq 'Azure.DevOps.Groups.ProjectAdmins.MaxMembers' })
64+
$ruleHits[0].Outcome | Should -Be 'Pass';
65+
$ruleHits.Count | Should -Be 1;
66+
}
67+
68+
It ' should pass once for ReadOnly token type' {
69+
$ruleHits = @($ruleResultReadOnly | Where-Object { $_.RuleName -eq 'Azure.DevOps.Groups.ProjectAdmins.MaxMembers' })
70+
$ruleHits[0].Outcome | Should -Be 'Pass';
71+
$ruleHits.Count | Should -Be 1;
72+
}
73+
74+
It ' should pass once for FineGrained token type' {
75+
$ruleHits = @($ruleResultFineGrained | Where-Object { $_.RuleName -eq 'Azure.DevOps.Groups.ProjectAdmins.MaxMembers' })
76+
$ruleHits[0].Outcome | Should -Be 'Pass';
77+
$ruleHits.Count | Should -Be 1;
78+
}
79+
}
80+
81+
Context 'Azure.DevOps.Groups.ProjectValidUsers.DoNotAssignMemberOfOtherGroups' {
82+
It ' should pass once' {
83+
$ruleHits = @($ruleResult | Where-Object { $_.RuleName -eq 'Azure.DevOps.Groups.ProjectValidUsers.DoNotAssignMemberOfOtherGroups' })
84+
$ruleHits[0].Outcome | Should -Be 'Pass';
85+
$ruleHits.Count | Should -Be 1;
86+
}
87+
88+
It ' should pass once for ReadOnly token type' {
89+
$ruleHits = @($ruleResultReadOnly | Where-Object { $_.RuleName -eq 'Azure.DevOps.Groups.ProjectValidUsers.DoNotAssignMemberOfOtherGroups' })
90+
$ruleHits[0].Outcome | Should -Be 'Pass';
91+
$ruleHits.Count | Should -Be 1;
92+
}
93+
94+
It ' should pass once for FineGrained token type' {
95+
$ruleHits = @($ruleResultFineGrained | Where-Object { $_.RuleName -eq 'Azure.DevOps.Groups.ProjectValidUsers.DoNotAssignMemberOfOtherGroups' })
96+
$ruleHits[0].Outcome | Should -Be 'Pass';
97+
$ruleHits.Count | Should -Be 1;
98+
}
99+
}
100+
}
101+
102+
AfterAll {
103+
# Remove Module
104+
Remove-Module -Name PSRule.Rules.AzureDevOps -Force;
105+
}
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
BeforeAll {
2+
# Setup error handling
3+
$ErrorActionPreference = 'Stop';
4+
Set-StrictMode -Version latest;
5+
6+
if ($Env:SYSTEM_DEBUG -eq 'true') {
7+
$VerbosePreference = 'Continue';
8+
}
9+
10+
# Setup tests paths
11+
# $rootPath = $PWD;
12+
$rootPath = $env:GITHUB_WORKSPACE
13+
$ourModule = (Join-Path -Path $rootPath -ChildPath '/src/PSRule.Rules.AzureDevOps')
14+
15+
Import-Module -Name $ourModule -Force
16+
$here = (Resolve-Path $PSScriptRoot).Path
17+
18+
# Get tempory test output folder and store path
19+
$outPath = Get-Item -Path (Join-Path -Path $here -ChildPath 'out')
20+
$outPath = $outPath.FullName
21+
22+
# Run rules with default token type
23+
$ruleResult = Invoke-PSRule -InputPath "$($outPath)/" -Module PSRule.Rules.AzureDevOps -Format Detect -Culture en
24+
25+
# Get temporary test output folder for tests with the ReadOnly TokenType
26+
$outPathReadOnly = Get-Item -Path (Join-Path -Path $here -ChildPath 'outReadOnly')
27+
$outPathReadOnly = $outPathReadOnly.FullName
28+
29+
# Run rules with ReadOnly token type
30+
$ruleResultReadOnly = Invoke-PSRule -InputPath "$($outPathReadOnly)/" -Module PSRule.Rules.AzureDevOps -Format Detect -Culture en
31+
32+
# Get temporary test output folder for tests with the FineGrained TokenType
33+
$outPathFineGrained = Get-Item -Path (Join-Path -Path $here -ChildPath 'outFineGrained')
34+
$outPathFineGrained = $outPathFineGrained.FullName
35+
36+
# Run rules with FineGrained token type
37+
$ruleResultFineGrained = Invoke-PSRule -InputPath "$($outPathFineGrained)/" -Module PSRule.Rules.AzureDevOps -Format Detect -Culture en
38+
}
39+
40+
Describe "Azure.DevOps.Pipelines.Core rules" {
41+
Context ' Azure.DevOps.Pipelines.Core.UseYamlDefinition' {
42+
It ' should fail for targets named fail' {
43+
$ruleHits = @($ruleResult | Where-Object { $_.RuleName -eq 'Azure.DevOps.Pipelines.Core.UseYamlDefinition' -and $_.TargetName -match 'fail-project-CI-gui' })
44+
$ruleHits[0].Outcome | Should -Be 'Fail';
45+
$ruleHits.Count | Should -Be 1;
46+
}
47+
48+
It ' should pass for targets named success' {
49+
$ruleHits = @($ruleResult | Where-Object { $_.RuleName -eq 'Azure.DevOps.Pipelines.Core.UseYamlDefinition' -and $_.TargetName -match 'success' })
50+
$ruleHits[0].Outcome | Should -Be 'Pass';
51+
$ruleHits.Count | Should -Be 1;
52+
}
53+
54+
It ' should be the same for ReadOnly TokenType' {
55+
$ruleHits = @($ruleResultReadOnly | Where-Object { $_.RuleName -eq 'Azure.DevOps.Pipelines.Core.UseYamlDefinition' -and $_.TargetName -match 'fail-project-CI-gui' })
56+
$ruleHits[0].Outcome | Should -Be 'Fail';
57+
$ruleHits.Count | Should -Be 1;
58+
59+
$ruleHits = @($ruleResultReadOnly | Where-Object { $_.RuleName -eq 'Azure.DevOps.Pipelines.Core.UseYamlDefinition' -and $_.TargetName -match 'success' })
60+
$ruleHits[0].Outcome | Should -Be 'Pass';
61+
$ruleHits.Count | Should -Be 1;
62+
}
63+
64+
It ' should be the same for the FineGrained TokenType' {
65+
$ruleHits = @($ruleResultFineGrained | Where-Object { $_.RuleName -eq 'Azure.DevOps.Pipelines.Core.UseYamlDefinition' -and $_.TargetName -match 'fail-project-CI-gui' })
66+
$ruleHits[0].Outcome | Should -Be 'Fail';
67+
$ruleHits.Count | Should -Be 1;
68+
69+
$ruleHits = @($ruleResultFineGrained | Where-Object { $_.RuleName -eq 'Azure.DevOps.Pipelines.Core.UseYamlDefinition' -and $_.TargetName -match 'success' })
70+
$ruleHits[0].Outcome | Should -Be 'Pass';
71+
$ruleHits.Count | Should -Be 1;
72+
}
73+
74+
It ' should have an English markdown help file' {
75+
$fileExists = Test-Path -Path (Join-Path -Path $ourModule -ChildPath 'en/Azure.DevOps.Pipelines.Core.UseYamlDefinition.md');
76+
$fileExists | Should -Be $true;
77+
}
78+
}
79+
80+
Context ' Azure.DevOps.Pipelines.Core.InheritedPermissions' {
81+
It ' should fail for targets named fail' {
82+
$ruleHits = @($ruleResult | Where-Object { $_.RuleName -eq 'Azure.DevOps.Pipelines.Core.InheritedPermissions' -and $_.TargetName -match "psrule-fail-project$" })
83+
$ruleHits[0].Outcome | Should -Be 'Fail';
84+
$ruleHits.Count | Should -Be 1;
85+
}
86+
87+
It ' should pass for targets named success' {
88+
$ruleHits = @($ruleResult | Where-Object { $_.RuleName -eq 'Azure.DevOps.Pipelines.Core.InheritedPermissions' -and $_.TargetName -match 'success' })
89+
$ruleHits[0].Outcome | Should -Be 'Pass';
90+
$ruleHits.Count | Should -Be 1;
91+
}
92+
93+
It ' should not be present for ReadOnly TokenType' {
94+
$ruleHits = @($ruleResultReadOnly | Where-Object { $_.RuleName -eq 'Azure.DevOps.Pipelines.Core.InheritedPermissions' })
95+
$ruleHits.Count | Should -Be 0;
96+
}
97+
98+
It ' should be the same for the FineGrained TokenType' {
99+
$ruleHits = @($ruleResultFineGrained | Where-Object { $_.RuleName -eq 'Azure.DevOps.Pipelines.Core.InheritedPermissions' -and $_.TargetName -match "psrule-fail-project$" })
100+
$ruleHits[0].Outcome | Should -Be 'Fail';
101+
$ruleHits.Count | Should -Be 1;
102+
103+
$ruleHits = @($ruleResultFineGrained | Where-Object { $_.RuleName -eq 'Azure.DevOps.Pipelines.Core.InheritedPermissions' -and $_.TargetName -match 'success' })
104+
$ruleHits[0].Outcome | Should -Be 'Pass';
105+
$ruleHits.Count | Should -Be 1;
106+
}
107+
108+
It ' should have an English markdown help file' {
109+
$fileExists = Test-Path -Path (Join-Path -Path $ourModule -ChildPath 'en/Azure.DevOps.Pipelines.Core.InheritedPermissions.md');
110+
$fileExists | Should -Be $true;
111+
}
112+
}
113+
114+
Context ' Azure.DevOps.Pipelines.Core.NoPlainTextSecrets' {
115+
It ' should fail for targets named fail' {
116+
$ruleHits = @($ruleResult | Where-Object { $_.RuleName -eq 'Azure.DevOps.Pipelines.Core.NoPlainTextSecrets' -and $_.TargetName -match "psrule-fail-project-CI-gui$" })
117+
$ruleHits[0].Outcome | Should -Be 'Fail';
118+
$ruleHits.Count | Should -Be 1;
119+
}
120+
121+
It ' should be the same for ReadOnly TokenType' {
122+
$ruleHits = @($ruleResultReadOnly | Where-Object { $_.RuleName -eq 'Azure.DevOps.Pipelines.Core.NoPlainTextSecrets' -and $_.TargetName -match "psrule-fail-project-CI-gui$" })
123+
$ruleHits[0].Outcome | Should -Be 'Fail';
124+
$ruleHits.Count | Should -Be 1;
125+
}
126+
127+
It ' should be the same for the FineGrained TokenType' {
128+
$ruleHits = @($ruleResultFineGrained | Where-Object { $_.RuleName -eq 'Azure.DevOps.Pipelines.Core.NoPlainTextSecrets' -and $_.TargetName -match "psrule-fail-project-CI-gui$" })
129+
$ruleHits[0].Outcome | Should -Be 'Fail';
130+
$ruleHits.Count | Should -Be 1;
131+
}
132+
133+
It ' should have an English markdown help file' {
134+
$fileExists = Test-Path -Path (Join-Path -Path $ourModule -ChildPath 'en/Azure.DevOps.Pipelines.Core.NoPlainTextSecrets.md');
135+
$fileExists | Should -Be $true;
136+
}
137+
}
138+
}
139+
140+
AfterAll {
141+
# Remove Module
142+
Remove-Module -Name PSRule.Rules.AzureDevOps -Force;
143+
}

0 commit comments

Comments
 (0)