From 71b5681d0a9d2a62c40029d4478fe41593648e8f Mon Sep 17 00:00:00 2001 From: Robbie Veivers Date: Tue, 8 Oct 2024 11:37:10 +1000 Subject: [PATCH 1/2] Beta Tests Added and need to have beta version flag when calling test-mgcommandpre --- src/Get-MsIdCrossTenantAccessActivity.ps1 | 4 +- src/internal/Test-MgCommandPrerequisites.ps1 | 11 +- tests/Test-MgCommandPrerequisites.tests.ps1 | 146 ++++++++++++++----- 3 files changed, 118 insertions(+), 43 deletions(-) diff --git a/src/Get-MsIdCrossTenantAccessActivity.ps1 b/src/Get-MsIdCrossTenantAccessActivity.ps1 index 978e1bc..1153788 100644 --- a/src/Get-MsIdCrossTenantAccessActivity.ps1 +++ b/src/Get-MsIdCrossTenantAccessActivity.ps1 @@ -177,7 +177,7 @@ function Get-MsIdCrossTenantAccessActivity { begin { ## Initialize Critical Dependencies $CriticalError = $null - if (!(Test-MgCommandPrerequisites 'Get-MgBetaAuditLogSignIn' -MinimumVersion 2.8.0 -ErrorVariable CriticalError)) { return } + if (!(Test-MgCommandPrerequisites 'Get-MgBetaAuditLogSignIn' -ApiVersion beta -MinimumVersion 2.8.0 -ErrorVariable CriticalError)) { return } #External Tenant ID check @@ -533,3 +533,5 @@ function Get-MsIdCrossTenantAccessActivity { if ($CriticalError) { return } } } + +#Get-MsIdCrossTenantAccessActivity -Verbose diff --git a/src/internal/Test-MgCommandPrerequisites.ps1 b/src/internal/Test-MgCommandPrerequisites.ps1 index 7826193..9ade640 100644 --- a/src/internal/Test-MgCommandPrerequisites.ps1 +++ b/src/internal/Test-MgCommandPrerequisites.ps1 @@ -15,9 +15,10 @@ function Test-MgCommandPrerequisites { [Alias('Command')] [string[]] $Name, # The service API version. - [Parameter(Mandatory = $false, Position = 2)] - [ValidateSet('v1.0')] + [Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true, Position = 2)] + [ValidateSet('v1.0', 'beta')] [string] $ApiVersion = 'v1.0', + #Default value is 'v1.0' if not specified. # Specifies a minimum version. [Parameter(Mandatory = $false)] [version] $MinimumVersion, @@ -45,8 +46,8 @@ function Test-MgCommandPrerequisites { ## Get Graph Command Details [hashtable] $MgCommandLookup = @{} foreach ($CommandName in $Name) { - - [array] $MgCommands = Find-MgGraphCommand -Command $CommandName -ApiVersion $ApiVersion -ErrorAction Break + [array] $MgCommands = @() + $MgCommands = Find-MgGraphCommand -Command $CommandName -ApiVersion $ApiVersion -ErrorAction Break if ($MgCommands.Count -eq 1) { $MgCommand = $MgCommands[0] @@ -151,3 +152,5 @@ function Test-MgCommandPrerequisites { return $result } } + +#Test-MgCommandPrerequisites -Name "Get-MgBetaUser" -ApiVersion "beta" diff --git a/tests/Test-MgCommandPrerequisites.tests.ps1 b/tests/Test-MgCommandPrerequisites.tests.ps1 index c41d86d..c3f6d66 100644 --- a/tests/Test-MgCommandPrerequisites.tests.ps1 +++ b/tests/Test-MgCommandPrerequisites.tests.ps1 @@ -28,39 +28,71 @@ Describe 'Test-MgCommandPrerequisites' { ## Mock commands with external dependancies or unavailable commands Mock -ModuleName $PSModule.Name Get-MgContext { New-Object Microsoft.Graph.PowerShell.Authentication.AuthContext -Property @{ Scopes = @('email', 'openid', 'profile', 'User.Read', 'User.Read.All'); AppName = 'Microsoft Graph PowerShell'; PSHostVersion = $PSVersionTable['PSVersion'] } } -Verifiable + Mock -ModuleName $PSModule.Name Find-MgGraphCommand { - New-Object Microsoft.Graph.PowerShell.Authentication.Models.GraphCommand -Property @{ - Command = 'Get-MgUser' - Module = 'Users' - APIVersion = 'v1.0' - Method = 'GET' - URI = '/users' - Permissions = @( - New-Object Microsoft.Graph.PowerShell.Authentication.Models.GraphPermission -Property @{ Name = 'User.Read.All'; IsAdmin = $true; Description = "Read all users' full profiles" } - New-Object Microsoft.Graph.PowerShell.Authentication.Models.GraphPermission -Property @{ Name = 'User.ReadBasic.All'; IsAdmin = $false; Description = "Read all users' basic profiles" } - New-Object Microsoft.Graph.PowerShell.Authentication.Models.GraphPermission -Property @{ Name = 'User.ReadWrite.All'; IsAdmin = $true; Description = "Read and write all users' full profiles" } - ) - } - New-Object Microsoft.Graph.PowerShell.Authentication.Models.GraphCommand -Property @{ - Command = 'Get-MgUser' - Module = 'Users' - APIVersion = 'v1.0' - Method = 'GET' - URI = '/users/{user-id}' - Permissions = @( - New-Object Microsoft.Graph.PowerShell.Authentication.Models.GraphPermission -Property @{ Name = 'User.Read'; IsAdmin = $false; Description = "Sign you in and read your profile" } - New-Object Microsoft.Graph.PowerShell.Authentication.Models.GraphPermission -Property @{ Name = 'User.ReadWrite'; IsAdmin = $false; Description = "Read and update your profile" } - New-Object Microsoft.Graph.PowerShell.Authentication.Models.GraphPermission -Property @{ Name = 'User.Read.All'; IsAdmin = $true; Description = "Read all users' full profiles" } - ) + param ($Command, $ApiVersion) + if ($ApiVersion -eq 'v1.0') { + New-Object Microsoft.Graph.PowerShell.Authentication.Models.GraphCommand -Property @{ + Command = 'Get-MgUser' + Module = 'Users' + APIVersion = 'v1.0' + Method = 'GET' + URI = '/users' + Permissions = @( + New-Object Microsoft.Graph.PowerShell.Authentication.Models.GraphPermission -Property @{ Name = 'User.Read.All'; IsAdmin = $true; Description = "Read all users' full profiles" } + New-Object Microsoft.Graph.PowerShell.Authentication.Models.GraphPermission -Property @{ Name = 'User.ReadBasic.All'; IsAdmin = $false; Description = "Read all users' basic profiles" } + New-Object Microsoft.Graph.PowerShell.Authentication.Models.GraphPermission -Property @{ Name = 'User.ReadWrite.All'; IsAdmin = $true; Description = "Read and write all users' full profiles" } + ) + } + New-Object Microsoft.Graph.PowerShell.Authentication.Models.GraphCommand -Property @{ + Command = 'Get-MgUser' + Module = 'Users' + APIVersion = 'v1.0' + Method = 'GET' + URI = '/users/{user-id}' + Permissions = @( + New-Object Microsoft.Graph.PowerShell.Authentication.Models.GraphPermission -Property @{ Name = 'User.Read'; IsAdmin = $false; Description = "Sign you in and read your profile" } + New-Object Microsoft.Graph.PowerShell.Authentication.Models.GraphPermission -Property @{ Name = 'User.ReadWrite'; IsAdmin = $false; Description = "Read and update your profile" } + New-Object Microsoft.Graph.PowerShell.Authentication.Models.GraphPermission -Property @{ Name = 'User.Read.All'; IsAdmin = $true; Description = "Read all users' full profiles" } + ) + } + + } elseif ($ApiVersion -eq 'beta') { + New-Object Microsoft.Graph.PowerShell.Authentication.Models.GraphCommand -Property @{ + Command = 'Get-MgBetaUser' + Module = 'Users' + APIVersion = 'beta' + Method = 'GET' + URI = '/beta/users' + Permissions = @( + New-Object Microsoft.Graph.PowerShell.Authentication.Models.GraphPermission -Property @{ Name = 'User.Read.All'; IsAdmin = $true; Description = "Read all users' full profiles" } + New-Object Microsoft.Graph.PowerShell.Authentication.Models.GraphPermission -Property @{ Name = 'User.ReadBasic.All'; IsAdmin = $false; Description = "Read all users' basic profiles" } + New-Object Microsoft.Graph.PowerShell.Authentication.Models.GraphPermission -Property @{ Name = 'User.ReadWrite.All'; IsAdmin = $true; Description = "Read and write all users' full profiles" } + ) + } + New-Object Microsoft.Graph.PowerShell.Authentication.Models.GraphCommand -Property @{ + Command = 'Get-MgBetaUser' + Module = 'Users' + APIVersion = 'beta' + Method = 'GET' + URI = '/beta/users/{user-id}' + Permissions = @( + New-Object Microsoft.Graph.PowerShell.Authentication.Models.GraphPermission -Property @{ Name = 'User.Read'; IsAdmin = $false; Description = "Sign you in and read your profile" } + New-Object Microsoft.Graph.PowerShell.Authentication.Models.GraphPermission -Property @{ Name = 'User.ReadWrite'; IsAdmin = $false; Description = "Read and update your profile" } + New-Object Microsoft.Graph.PowerShell.Authentication.Models.GraphPermission -Property @{ Name = 'User.Read.All'; IsAdmin = $true; Description = "Read all users' full profiles" } + ) + } } - } -ParameterFilter { $Command -eq 'Get-MgUser' } -Verifiable + } -ParameterFilter { $Command -eq 'Get-MgUser' -or $Command -eq 'Get-MgBetaUser' } -Verifiable + Mock -ModuleName $PSModule.Name Import-Module { } -ParameterFilter { $Name -ne 'Microsoft.Graph.Authentication' } -Verifiable ## Test Cases $TestCases = @( @{ Name = 'Get-MgUser'; Expected = $true } - @{ Name = 'Get-MgUser'; ApiVersion = 'Beta'; Expected = $true } - @{ Name = 'Get-MgUser'; ApiVersion = 'Beta'; MinimumVersion = '1.0'; Expected = $true } + @{ Name = 'Get-MgUser'; ApiVersion = 'v1.0'; Expected = $true } + @{ Name = 'Get-MgUser'; ApiVersion = 'v1.0'; MinimumVersion = '1.0'; Expected = $true } + @{ Name = 'Get-MgBetaUser'; ApiVersion = 'Beta'; MinimumVersion = '2.8.0'; Expected = $true } ) } @@ -68,34 +100,43 @@ Describe 'Test-MgCommandPrerequisites' { @{ Name = 'Get-MgUser'; Expected = $true } @{ Name = 'Get-MgUser'; ApiVersion = 'V1.0'; Expected = $true } @{ Name = 'Get-MgUser'; ApiVersion = 'V1.0'; MinimumVersion = '1.0'; Expected = $true } + @{ Name = 'Get-MgBetaUser'; ApiVersion = 'Beta'; MinimumVersion = '1.0'; Expected = $true } ) { BeforeAll { InModuleScope $PSModule.Name -ArgumentList $_ { $script:params = $args[0].Clone() $script:params.Remove('Name') $script:params.Remove('Expected') + + } } It 'Positional Parameter' { InModuleScope $PSModule.Name -Parameters $_ { - $Output = Test-MgCommandPrerequisites $Name @params -ErrorVariable actualErrors + if ($script:params['ApiVersion']) { + # Call Test-MgCommandPrerequisites with positional parameters + $Output = Test-MgCommandPrerequisites -Name $Name -ApiVersion $ApiVersion -ErrorVariable actualErrors + } else { + # Call Test-MgCommandPrerequisites without ApiVersion + $Output = Test-MgCommandPrerequisites -Name $Name -ErrorVariable actualErrors + } $Output | Should -BeOfType [bool] $Output | Should -BeExactly $Expected Should -Invoke Find-MgGraphCommand -ParameterFilter { - $Command -eq $Name + $Command -eq $Name -and $ApiVersion -eq $ApiVersion } $actualErrors | Should -HaveCount 0 } } - + It 'Pipeline Input' { InModuleScope $PSModule.Name -Parameters $_ { $Output = $Name | Test-MgCommandPrerequisites @params -ErrorVariable actualErrors $Output | Should -BeOfType [bool] $Output | Should -BeExactly $Expected Should -Invoke Find-MgGraphCommand -ParameterFilter { - $Command -eq $Name + $Command -eq $Name -and $ApiVersion -eq $ApiVersion } $actualErrors | Should -HaveCount 0 } @@ -106,7 +147,7 @@ Describe 'Test-MgCommandPrerequisites' { BeforeAll { Mock -ModuleName $PSModule.Name Find-MgGraphCommand { New-Object Microsoft.Graph.PowerShell.Authentication.Models.GraphCommand -Property @{ - Command = 'Get-MgDirectoryObjectById' + Command = 'Get-MgUser' Module = 'Users' APIVersion = 'v1.0' Method = 'POST' @@ -146,19 +187,48 @@ Describe 'Test-MgCommandPrerequisites' { It 'Positional Parameter' { InModuleScope $PSModule.Name -Parameters @{ TestCases = $TestCases } { - $Output = Test-MgCommandPrerequisites $TestCases.Name -ErrorVariable actualErrors - $Output | Should -BeOfType [bool] - $Output | Should -HaveCount 1 # Only pipeline will return multiple outputs - Should -Invoke Find-MgGraphCommand -Times 3 -ParameterFilter { - $Command -in $TestCases.Name + foreach ($testCase in $TestCases) { + # Provide default value for ApiVersion if not specified + try { + $apiVersion = $testCase.ApiVersion + } + catch { + $apiVersion = 'v1.0' + } + # Call the function with positional parameters + $Output = Test-MgCommandPrerequisites $testCase.Name $apiVersion -ErrorVariable actualErrors + + # Validate the output + $Output | Should -BeOfType [bool] + $Output | Should -HaveCount 1 # Only pipeline will return multiple outputs + Should -Invoke Find-MgGraphCommand -Times 1 -ParameterFilter { + $Command -in $testCase.Name + } + $actualErrors | Should -HaveCount 0 } - $actualErrors | Should -HaveCount 0 } } It 'Pipeline Input' { InModuleScope $PSModule.Name -Parameters @{ TestCases = $TestCases } { - $Output = $TestCases.Name | Test-MgCommandPrerequisites -ErrorVariable actualErrors + # Create custom objects with Name and ApiVersion properties + $TestCasesWithApiVersion = foreach ($testCase in $TestCases) { + try { + $apiVersion = $testCase.ApiVersion + } + catch { + $apiVersion = 'v1.0' + } + [PSCustomObject]@{ + Name = $testCase.Name + ApiVersion = $apiVersion + } + } + + # Pipe the custom objects to the function + $Output = $TestCasesWithApiVersion | Test-MgCommandPrerequisites -ErrorVariable actualErrors + + # Validate the output $Output | Should -BeOfType [bool] $Output | Should -HaveCount $TestCases.Count for ($i = 0; $i -lt $TestCases.Count; $i++) { From a1cf3912949138ee97960fcfdf27b493b6f36dfe Mon Sep 17 00:00:00 2001 From: Robbie Veivers Date: Tue, 8 Oct 2024 13:13:36 +1000 Subject: [PATCH 2/2] Clean up Testing command Comment --- src/Get-MsIdCrossTenantAccessActivity.ps1 | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Get-MsIdCrossTenantAccessActivity.ps1 b/src/Get-MsIdCrossTenantAccessActivity.ps1 index 1153788..7b15a08 100644 --- a/src/Get-MsIdCrossTenantAccessActivity.ps1 +++ b/src/Get-MsIdCrossTenantAccessActivity.ps1 @@ -533,5 +533,3 @@ function Get-MsIdCrossTenantAccessActivity { if ($CriticalError) { return } } } - -#Get-MsIdCrossTenantAccessActivity -Verbose