Skip to content

Commit 096a0ab

Browse files
authored
Merge pull request #72 from robbieveivers/main
Ensure Test-Mgcommand prerequisites is v1.0 and beta compatible
2 parents 1c32c5b + 2550002 commit 096a0ab

File tree

3 files changed

+116
-43
lines changed

3 files changed

+116
-43
lines changed

src/Get-MsIdCrossTenantAccessActivity.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ function Get-MsIdCrossTenantAccessActivity {
177177
begin {
178178
## Initialize Critical Dependencies
179179
$CriticalError = $null
180-
if (!(Test-MgCommandPrerequisites 'Get-MgBetaAuditLogSignIn' -MinimumVersion 2.8.0 -ErrorVariable CriticalError)) { return }
180+
if (!(Test-MgCommandPrerequisites 'Get-MgBetaAuditLogSignIn' -ApiVersion beta -MinimumVersion 2.8.0 -ErrorVariable CriticalError)) { return }
181181

182182
#External Tenant ID check
183183

src/internal/Test-MgCommandPrerequisites.ps1

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ function Test-MgCommandPrerequisites {
1515
[Alias('Command')]
1616
[string[]] $Name,
1717
# The service API version.
18-
[Parameter(Mandatory = $false, Position = 2)]
19-
[ValidateSet('v1.0')]
18+
[Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true, Position = 2)]
19+
[ValidateSet('v1.0', 'beta')]
2020
[string] $ApiVersion = 'v1.0',
21+
#Default value is 'v1.0' if not specified.
2122
# Specifies a minimum version.
2223
[Parameter(Mandatory = $false)]
2324
[version] $MinimumVersion,
@@ -45,8 +46,8 @@ function Test-MgCommandPrerequisites {
4546
## Get Graph Command Details
4647
[hashtable] $MgCommandLookup = @{}
4748
foreach ($CommandName in $Name) {
48-
49-
[array] $MgCommands = Find-MgGraphCommand -Command $CommandName -ApiVersion $ApiVersion -ErrorAction Break
49+
[array] $MgCommands = @()
50+
$MgCommands = Find-MgGraphCommand -Command $CommandName -ApiVersion $ApiVersion -ErrorAction Break
5051

5152
if ($MgCommands.Count -eq 1) {
5253
$MgCommand = $MgCommands[0]
@@ -151,3 +152,5 @@ function Test-MgCommandPrerequisites {
151152
return $result
152153
}
153154
}
155+
156+
#Test-MgCommandPrerequisites -Name "Get-MgBetaUser" -ApiVersion "beta"

tests/Test-MgCommandPrerequisites.tests.ps1

Lines changed: 108 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -28,74 +28,115 @@ Describe 'Test-MgCommandPrerequisites' {
2828

2929
## Mock commands with external dependancies or unavailable commands
3030
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
31+
3132
Mock -ModuleName $PSModule.Name Find-MgGraphCommand {
32-
New-Object Microsoft.Graph.PowerShell.Authentication.Models.GraphCommand -Property @{
33-
Command = 'Get-MgUser'
34-
Module = 'Users'
35-
APIVersion = 'v1.0'
36-
Method = 'GET'
37-
URI = '/users'
38-
Permissions = @(
39-
New-Object Microsoft.Graph.PowerShell.Authentication.Models.GraphPermission -Property @{ Name = 'User.Read.All'; IsAdmin = $true; Description = "Read all users' full profiles" }
40-
New-Object Microsoft.Graph.PowerShell.Authentication.Models.GraphPermission -Property @{ Name = 'User.ReadBasic.All'; IsAdmin = $false; Description = "Read all users' basic profiles" }
41-
New-Object Microsoft.Graph.PowerShell.Authentication.Models.GraphPermission -Property @{ Name = 'User.ReadWrite.All'; IsAdmin = $true; Description = "Read and write all users' full profiles" }
42-
)
43-
}
44-
New-Object Microsoft.Graph.PowerShell.Authentication.Models.GraphCommand -Property @{
45-
Command = 'Get-MgUser'
46-
Module = 'Users'
47-
APIVersion = 'v1.0'
48-
Method = 'GET'
49-
URI = '/users/{user-id}'
50-
Permissions = @(
51-
New-Object Microsoft.Graph.PowerShell.Authentication.Models.GraphPermission -Property @{ Name = 'User.Read'; IsAdmin = $false; Description = "Sign you in and read your profile" }
52-
New-Object Microsoft.Graph.PowerShell.Authentication.Models.GraphPermission -Property @{ Name = 'User.ReadWrite'; IsAdmin = $false; Description = "Read and update your profile" }
53-
New-Object Microsoft.Graph.PowerShell.Authentication.Models.GraphPermission -Property @{ Name = 'User.Read.All'; IsAdmin = $true; Description = "Read all users' full profiles" }
54-
)
33+
param ($Command, $ApiVersion)
34+
if ($ApiVersion -eq 'v1.0') {
35+
New-Object Microsoft.Graph.PowerShell.Authentication.Models.GraphCommand -Property @{
36+
Command = 'Get-MgUser'
37+
Module = 'Users'
38+
APIVersion = 'v1.0'
39+
Method = 'GET'
40+
URI = '/users'
41+
Permissions = @(
42+
New-Object Microsoft.Graph.PowerShell.Authentication.Models.GraphPermission -Property @{ Name = 'User.Read.All'; IsAdmin = $true; Description = "Read all users' full profiles" }
43+
New-Object Microsoft.Graph.PowerShell.Authentication.Models.GraphPermission -Property @{ Name = 'User.ReadBasic.All'; IsAdmin = $false; Description = "Read all users' basic profiles" }
44+
New-Object Microsoft.Graph.PowerShell.Authentication.Models.GraphPermission -Property @{ Name = 'User.ReadWrite.All'; IsAdmin = $true; Description = "Read and write all users' full profiles" }
45+
)
46+
}
47+
New-Object Microsoft.Graph.PowerShell.Authentication.Models.GraphCommand -Property @{
48+
Command = 'Get-MgUser'
49+
Module = 'Users'
50+
APIVersion = 'v1.0'
51+
Method = 'GET'
52+
URI = '/users/{user-id}'
53+
Permissions = @(
54+
New-Object Microsoft.Graph.PowerShell.Authentication.Models.GraphPermission -Property @{ Name = 'User.Read'; IsAdmin = $false; Description = "Sign you in and read your profile" }
55+
New-Object Microsoft.Graph.PowerShell.Authentication.Models.GraphPermission -Property @{ Name = 'User.ReadWrite'; IsAdmin = $false; Description = "Read and update your profile" }
56+
New-Object Microsoft.Graph.PowerShell.Authentication.Models.GraphPermission -Property @{ Name = 'User.Read.All'; IsAdmin = $true; Description = "Read all users' full profiles" }
57+
)
58+
}
59+
60+
} elseif ($ApiVersion -eq 'beta') {
61+
New-Object Microsoft.Graph.PowerShell.Authentication.Models.GraphCommand -Property @{
62+
Command = 'Get-MgBetaUser'
63+
Module = 'Users'
64+
APIVersion = 'beta'
65+
Method = 'GET'
66+
URI = '/beta/users'
67+
Permissions = @(
68+
New-Object Microsoft.Graph.PowerShell.Authentication.Models.GraphPermission -Property @{ Name = 'User.Read.All'; IsAdmin = $true; Description = "Read all users' full profiles" }
69+
New-Object Microsoft.Graph.PowerShell.Authentication.Models.GraphPermission -Property @{ Name = 'User.ReadBasic.All'; IsAdmin = $false; Description = "Read all users' basic profiles" }
70+
New-Object Microsoft.Graph.PowerShell.Authentication.Models.GraphPermission -Property @{ Name = 'User.ReadWrite.All'; IsAdmin = $true; Description = "Read and write all users' full profiles" }
71+
)
72+
}
73+
New-Object Microsoft.Graph.PowerShell.Authentication.Models.GraphCommand -Property @{
74+
Command = 'Get-MgBetaUser'
75+
Module = 'Users'
76+
APIVersion = 'beta'
77+
Method = 'GET'
78+
URI = '/beta/users/{user-id}'
79+
Permissions = @(
80+
New-Object Microsoft.Graph.PowerShell.Authentication.Models.GraphPermission -Property @{ Name = 'User.Read'; IsAdmin = $false; Description = "Sign you in and read your profile" }
81+
New-Object Microsoft.Graph.PowerShell.Authentication.Models.GraphPermission -Property @{ Name = 'User.ReadWrite'; IsAdmin = $false; Description = "Read and update your profile" }
82+
New-Object Microsoft.Graph.PowerShell.Authentication.Models.GraphPermission -Property @{ Name = 'User.Read.All'; IsAdmin = $true; Description = "Read all users' full profiles" }
83+
)
84+
}
5585
}
56-
} -ParameterFilter { $Command -eq 'Get-MgUser' } -Verifiable
86+
} -ParameterFilter { $Command -eq 'Get-MgUser' -or $Command -eq 'Get-MgBetaUser' } -Verifiable
87+
5788
Mock -ModuleName $PSModule.Name Import-Module { } -ParameterFilter { $Name -ne 'Microsoft.Graph.Authentication' } -Verifiable
5889

5990
## Test Cases
6091
$TestCases = @(
6192
@{ Name = 'Get-MgUser'; Expected = $true }
62-
@{ Name = 'Get-MgUser'; ApiVersion = 'Beta'; Expected = $true }
63-
@{ Name = 'Get-MgUser'; ApiVersion = 'Beta'; MinimumVersion = '1.0'; Expected = $true }
93+
@{ Name = 'Get-MgUser'; ApiVersion = 'v1.0'; Expected = $true }
94+
@{ Name = 'Get-MgUser'; ApiVersion = 'v1.0'; MinimumVersion = '1.0'; Expected = $true }
95+
@{ Name = 'Get-MgBetaUser'; ApiVersion = 'Beta'; MinimumVersion = '2.8.0'; Expected = $true }
6496
)
6597
}
6698

6799
Context 'Name: <Name>' -ForEach @(
68100
@{ Name = 'Get-MgUser'; Expected = $true }
69101
@{ Name = 'Get-MgUser'; ApiVersion = 'V1.0'; Expected = $true }
70102
@{ Name = 'Get-MgUser'; ApiVersion = 'V1.0'; MinimumVersion = '1.0'; Expected = $true }
103+
@{ Name = 'Get-MgBetaUser'; ApiVersion = 'Beta'; MinimumVersion = '1.0'; Expected = $true }
71104
) {
72105
BeforeAll {
73106
InModuleScope $PSModule.Name -ArgumentList $_ {
74107
$script:params = $args[0].Clone()
75108
$script:params.Remove('Name')
76109
$script:params.Remove('Expected')
110+
111+
77112
}
78113
}
79114

80115
It 'Positional Parameter' {
81116
InModuleScope $PSModule.Name -Parameters $_ {
82-
$Output = Test-MgCommandPrerequisites $Name @params -ErrorVariable actualErrors
117+
if ($script:params['ApiVersion']) {
118+
# Call Test-MgCommandPrerequisites with positional parameters
119+
$Output = Test-MgCommandPrerequisites -Name $Name -ApiVersion $ApiVersion -ErrorVariable actualErrors
120+
} else {
121+
# Call Test-MgCommandPrerequisites without ApiVersion
122+
$Output = Test-MgCommandPrerequisites -Name $Name -ErrorVariable actualErrors
123+
}
83124
$Output | Should -BeOfType [bool]
84125
$Output | Should -BeExactly $Expected
85126
Should -Invoke Find-MgGraphCommand -ParameterFilter {
86-
$Command -eq $Name
127+
$Command -eq $Name -and $ApiVersion -eq $ApiVersion
87128
}
88129
$actualErrors | Should -HaveCount 0
89130
}
90131
}
91-
132+
92133
It 'Pipeline Input' {
93134
InModuleScope $PSModule.Name -Parameters $_ {
94135
$Output = $Name | Test-MgCommandPrerequisites @params -ErrorVariable actualErrors
95136
$Output | Should -BeOfType [bool]
96137
$Output | Should -BeExactly $Expected
97138
Should -Invoke Find-MgGraphCommand -ParameterFilter {
98-
$Command -eq $Name
139+
$Command -eq $Name -and $ApiVersion -eq $ApiVersion
99140
}
100141
$actualErrors | Should -HaveCount 0
101142
}
@@ -106,7 +147,7 @@ Describe 'Test-MgCommandPrerequisites' {
106147
BeforeAll {
107148
Mock -ModuleName $PSModule.Name Find-MgGraphCommand {
108149
New-Object Microsoft.Graph.PowerShell.Authentication.Models.GraphCommand -Property @{
109-
Command = 'Get-MgDirectoryObjectById'
150+
Command = 'Get-MgUser'
110151
Module = 'Users'
111152
APIVersion = 'v1.0'
112153
Method = 'POST'
@@ -146,19 +187,48 @@ Describe 'Test-MgCommandPrerequisites' {
146187

147188
It 'Positional Parameter' {
148189
InModuleScope $PSModule.Name -Parameters @{ TestCases = $TestCases } {
149-
$Output = Test-MgCommandPrerequisites $TestCases.Name -ErrorVariable actualErrors
150-
$Output | Should -BeOfType [bool]
151-
$Output | Should -HaveCount 1 # Only pipeline will return multiple outputs
152-
Should -Invoke Find-MgGraphCommand -Times 3 -ParameterFilter {
153-
$Command -in $TestCases.Name
190+
foreach ($testCase in $TestCases) {
191+
# Provide default value for ApiVersion if not specified
192+
try {
193+
$apiVersion = $testCase.ApiVersion
194+
}
195+
catch {
196+
$apiVersion = 'v1.0'
197+
}
198+
# Call the function with positional parameters
199+
$Output = Test-MgCommandPrerequisites $testCase.Name $apiVersion -ErrorVariable actualErrors
200+
201+
# Validate the output
202+
$Output | Should -BeOfType [bool]
203+
$Output | Should -HaveCount 1 # Only pipeline will return multiple outputs
204+
Should -Invoke Find-MgGraphCommand -Times 1 -ParameterFilter {
205+
$Command -in $testCase.Name
206+
}
207+
$actualErrors | Should -HaveCount 0
154208
}
155-
$actualErrors | Should -HaveCount 0
156209
}
157210
}
158211

159212
It 'Pipeline Input' {
160213
InModuleScope $PSModule.Name -Parameters @{ TestCases = $TestCases } {
161-
$Output = $TestCases.Name | Test-MgCommandPrerequisites -ErrorVariable actualErrors
214+
# Create custom objects with Name and ApiVersion properties
215+
$TestCasesWithApiVersion = foreach ($testCase in $TestCases) {
216+
try {
217+
$apiVersion = $testCase.ApiVersion
218+
}
219+
catch {
220+
$apiVersion = 'v1.0'
221+
}
222+
[PSCustomObject]@{
223+
Name = $testCase.Name
224+
ApiVersion = $apiVersion
225+
}
226+
}
227+
228+
# Pipe the custom objects to the function
229+
$Output = $TestCasesWithApiVersion | Test-MgCommandPrerequisites -ErrorVariable actualErrors
230+
231+
# Validate the output
162232
$Output | Should -BeOfType [bool]
163233
$Output | Should -HaveCount $TestCases.Count
164234
for ($i = 0; $i -lt $TestCases.Count; $i++) {

0 commit comments

Comments
 (0)