Testing/Unit/PowerShell/Orchestrator/Invoke-RunRego.Tests.ps1

$OrchestratorPath = '../../../../Modules/Orchestrator.psm1'
Import-Module (Join-Path -Path $PSScriptRoot -ChildPath $OrchestratorPath) -Function 'Invoke-RunRego' -Force

InModuleScope Orchestrator {
    Describe -Tag 'Orchestrator' -Name 'Invoke-RunRego' {
        BeforeAll {
            function Invoke-Rego {}
            Mock -ModuleName Orchestrator Invoke-Rego
            function Get-FileEncoding {}
            Mock -ModuleName Orchestrator Get-FileEncoding { 'utf8' }

            Mock -CommandName Write-Progress {}
            Mock -CommandName Join-Path { "." }
            Mock -CommandName Set-Content {}
            Mock -CommandName ConvertTo-Json {}
            Mock -CommandName ConvertTo-Csv {}
        }
        Context 'When running the rego on a provider json' {
            BeforeAll {
                [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', 'RunRegoParameters')]
                $CyberConfig = [PSCustomObject]@{
                    ProductNames = @('aad')
                    OPAPath = "./"
                    OutProviderFileName = "ProviderSettingsExport"
                    OutRegoFileName = "TestResults"
                    OutReportName = "BaselineReports"
                    LogIn = $false
                }
                [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', 'ParentPath')]
                $ParentPath = "./"
                [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', 'OutFolderPath')]
                $OutFolderPath = "./"
            }
            It 'With -ProductNames "aad", should not throw' {
                $CyberConfig.ProductNames = @("aad")
                { Invoke-RunRego -CyberConfig $CyberConfig -ParentPath $ParentPath -OutFolderPath $OutFolderPath } | Should -Not -Throw
            }
            It 'With -ProductNames "defender", should not throw' {
                $CyberConfig.ProductNames = @("defender")
                { Invoke-RunRego -CyberConfig $CyberConfig -ParentPath $ParentPath -OutFolderPath $OutFolderPath } | Should -Not -Throw
            }
            It 'With -ProductNames "exo", should not throw' {
                $CyberConfig.ProductNames = @("exo")
                { Invoke-RunRego -CyberConfig $CyberConfig -ParentPath $ParentPath -OutFolderPath $OutFolderPath } | Should -Not -Throw
            }
            It 'With -ProductNames "powerplatform", should not throw' {
                $CyberConfig.ProductNames = @("powerplatform")
                { Invoke-RunRego -CyberConfig $CyberConfig -ParentPath $ParentPath -OutFolderPath $OutFolderPath } | Should -Not -Throw
            }
            It 'With -ProductNames "sharepoint", should not throw' {
                $CyberConfig.ProductNames = @("sharepoint")
                { Invoke-RunRego -CyberConfig $CyberConfig -ParentPath $ParentPath -OutFolderPath $OutFolderPath } | Should -Not -Throw
            }
            It 'With -ProductNames "teams", should not throw' {
                $CyberConfig.ProductNames = @("teams")
                { Invoke-RunRego -CyberConfig $CyberConfig -ParentPath $ParentPath -OutFolderPath $OutFolderPath } | Should -Not -Throw
            }
            It 'With all products, should not throw' {
                $CyberConfig.ProductNames = @("aad", "defender", "exo", "powerplatform", "sharepoint", "teams")
                { Invoke-RunRego -CyberConfig $CyberConfig -ParentPath $ParentPath -OutFolderPath $OutFolderPath } | Should -Not -Throw
            }
        }
    }
}

AfterAll {
    Remove-Module Orchestrator -ErrorAction SilentlyContinue
}