|
| 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