Testing/Unit/PowerShell/Providers/PowerPlatformProvider/Get-PowerPlatformTenantDetail.Tests.ps1

$ProviderPath = "../../../../../Modules/Providers"
Import-Module (Join-Path -Path $PSScriptRoot -ChildPath "$($ProviderPath)/ExportPowerPlatformProvider.psm1") -Function 'Get-PowerPlatformTenantDetail' -Force

InModuleScope ExportPowerPlatformProvider {
    Describe -Tag 'PowerPlatformProvider' -Name "Get-PowerPlatformTenantDetail" {
        BeforeAll {
            Mock Import-Module {}
            # empty stub required for mocked cmdlets called directly in the provider
            function Get-TenantDetailsFromGraph {}
            Mock -ModuleName ExportPowerPlatformProvider Get-TenantDetailsFromGraph {
                return [pscustomobject]@{
                    Domains = @(
                        @{
                            Name = "example.onmicrosoft.com";
                            initial = $true;
                        },
                        @{
                            Name = "contoso.onmicrosoft.com";
                            initial = $false;
                        }
                    );
                    DisplayName = "DisplayName";
                    TenantId = "TenantId";
                }
            }
            function Test-CyberAssessmentValidJson {
                param (
                    [string]
                    $Json
                )
                $ValidJson = $true
                try {
                    ConvertFrom-Json $Json -ErrorAction Stop | Out-Null
                }
                catch {
                    $ValidJson = $false;
                }
                $ValidJson
            }
        }
        It "When called with -M365Environment 'commercial', returns valid JSON" {
            $Json = Get-PowerPlatformTenantDetail -M365Environment "commercial"
            $ValidJson = Test-CyberAssessmentValidJson -Json $Json | Select-Object -Last 1
            $ValidJson | Should -Be $true
        }
        It "When called with -M365Environment 'gcc', returns valid JSON" {
            $Json = Get-PowerPlatformTenantDetail -M365Environment "gcc"
            $ValidJson = Test-CyberAssessmentValidJson -Json $Json | Select-Object -Last 1
            $ValidJson | Should -Be $true
        }
        It "When called with -M365Environment 'gcchigh', returns valid JSON" {
            $Json = Get-PowerPlatformTenantDetail -M365Environment "gcchigh"
            $ValidJson = Test-CyberAssessmentValidJson -Json $Json | Select-Object -Last 1
            $ValidJson | Should -Be $true
        }
        It "When called with -M365Environment 'dod', returns valid JSON" {
            $Json = Get-PowerPlatformTenantDetail -M365Environment "dod"
            $ValidJson = Test-CyberAssessmentValidJson -Json $Json | Select-Object -Last 1
            $ValidJson | Should -Be $true
        }
    }
}

AfterAll {
    Remove-Module ExportPowerPlatformProvider -Force -ErrorAction SilentlyContinue
    Remove-Module Microsoft.PowerApps.Administration.PowerShell -Force -ErrorAction SilentlyContinue
}