diff --git a/test/module/Entra/Get-EntraServicePrincipalAppRoleAssignedTo.Tests.ps1 b/test/module/Entra/Get-EntraServicePrincipalAppRoleAssignedTo.Tests.ps1 new file mode 100644 index 000000000..be1b10fe7 --- /dev/null +++ b/test/module/Entra/Get-EntraServicePrincipalAppRoleAssignedTo.Tests.ps1 @@ -0,0 +1,84 @@ +# ------------------------------------------------------------------------------ +# Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information. +# ------------------------------------------------------------------------------ +BeforeAll { + if((Get-Module -Name Microsoft.Graph.Entra) -eq $null){ + Import-Module Microsoft.Graph.Entra + } + Import-Module (Join-Path $psscriptroot "..\Common-Functions.ps1") -Force + + $scriptblock = { + # Write-Host "Mocking Get-EntraServicePrincipalAppRoleAssignedTo with parameters: $($args | ConvertTo-Json -Depth 3)" + return @( + [PSCustomObject]@{ + "AppRoleId" = "bdd80a03-d9bc-451d-b7c4-ce7c63fe3c8f" + "Id" = "I8uPTcetR02TKCQg6xB170ZWgaqJluBEqPHHxTxJ9Hs" + "PrincipalDisplayName" = "Entra-App-Testing" + "PrincipalType" = "ServicePrincipal" + "ResourceDisplayName" = "Microsoft Graph" + "PrincipalId" = "4d8fcb23-adc7-4d47-9328-2420eb1075ef" + "ResourceId" = "7af1d6f7-755a-4803-a078-a4f5a431ad51" + "Parameters" = $args + } + ) + } + + Mock -CommandName Get-MgServicePrincipalAppRoleAssignment -MockWith $scriptblock -ModuleName Microsoft.Graph.Entra +} + +Describe "Get-EntraServicePrincipalAppRoleAssignedTo" { + Context "Test for Get-EntraServicePrincipalAppRoleAssignedTo" { + It "Should return app role assignments" { + $result = Get-EntraServicePrincipalAppRoleAssignedTo -ObjectId "4d8fcb23-adc7-4d47-9328-2420eb1075ef" + $result | Should -Not -BeNullOrEmpty + $result.PrincipalId | should -Be '4d8fcb23-adc7-4d47-9328-2420eb1075ef' + + Should -Invoke -CommandName Get-MgServicePrincipalAppRoleAssignment -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Should fail when ObjectId is empty" { + { Get-EntraServicePrincipalAppRoleAssignedTo -ObjectId } | Should -Throw "Missing an argument for parameter 'ObjectId'.*" + } + It "Should fail when ObjectId is invalid" { + {Get-EntraServicePrincipalAppRoleAssignedTo -ObjectId "" } | Should -Throw "Cannot bind argument to parameter 'ObjectId' because it is an empty string.*" + } + It "Should return all app role assignments" { + $result = Get-EntraServicePrincipalAppRoleAssignedTo -ObjectId "4d8fcb23-adc7-4d47-9328-2420eb1075ef" -All + $result | Should -Not -BeNullOrEmpty + + Should -Invoke -CommandName Get-MgServicePrincipalAppRoleAssignment -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Should return top app role assignments " { + $result = Get-EntraServicePrincipalAppRoleAssignedTo -ObjectId "4d8fcb23-adc7-4d47-9328-2420eb1075ef" -top 1 + $result | Should -Not -BeNullOrEmpty + + Should -Invoke -CommandName Get-MgServicePrincipalAppRoleAssignment -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Should fail when Top is empty" { + { Get-EntraServicePrincipalAppRoleAssignedTo -ObjectId "4d8fcb23-adc7-4d47-9328-2420eb1075ef" -Top } | Should -Throw "Missing an argument for parameter 'Top'*" + } + It "Should fail when Top is invalid" { + { Get-EntraServicePrincipalAppRoleAssignedTo -ObjectId "4d8fcb23-adc7-4d47-9328-2420eb1075ef" -Top xyz } | Should -Throw "Cannot process argument transformation on parameter 'Top'*" + } + It "Result should Contain ObjectId" { + $result = Get-EntraServicePrincipalAppRoleAssignedTo -ObjectId "4d8fcb23-adc7-4d47-9328-2420eb1075ef" + $result.ObjectId | should -Be "I8uPTcetR02TKCQg6xB170ZWgaqJluBEqPHHxTxJ9Hs" + } + It "Should contain ServicePrincipalId in parameters when passed ObjectId to it" { + Mock -CommandName Get-MgServicePrincipalAppRoleAssignment -MockWith {$args} -ModuleName Microsoft.Graph.Entra + + $result = Get-EntraServicePrincipalAppRoleAssignedTo -ObjectId "4d8fcb23-adc7-4d47-9328-2420eb1075ef" + $params = Get-Parameters -data $result + $params.ServicePrincipalId | Should -Be "4d8fcb23-adc7-4d47-9328-2420eb1075ef" + } + + It "Should contain 'User-Agent' header" { + Mock -CommandName Get-MgServicePrincipalAppRoleAssignment -MockWith {$args} -ModuleName Microsoft.Graph.Entra + + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraServicePrincipalAppRoleAssignedTo" + + $result = Get-EntraServicePrincipalAppRoleAssignedTo -ObjectId "4d8fcb23-adc7-4d47-9328-2420eb1075ef" + $params = Get-Parameters -data $result + $params.Headers["User-Agent"] | Should -Be $userAgentHeaderValue + } + } +} \ No newline at end of file diff --git a/test/module/Entra/Get-EntraServicePrincipalAppRoleAssignment.Tests.ps1 b/test/module/Entra/Get-EntraServicePrincipalAppRoleAssignment.Tests.ps1 new file mode 100644 index 000000000..c98ddabfb --- /dev/null +++ b/test/module/Entra/Get-EntraServicePrincipalAppRoleAssignment.Tests.ps1 @@ -0,0 +1,85 @@ +# ------------------------------------------------------------------------------ +# Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information. +# ------------------------------------------------------------------------------ +BeforeAll { + if((Get-Module -Name Microsoft.Graph.Entra) -eq $null){ + Import-Module Microsoft.Graph.Entra + } + Import-Module (Join-Path $psscriptroot "..\Common-Functions.ps1") -Force + + $scriptblock = { + # Write-Host "Mocking Get-EntraServicePrincipalAppRoleAssignment with parameters: $($args | ConvertTo-Json -Depth 3)" + return @( + [PSCustomObject]@{ + "AppRoleId" = "00000000-0000-0000-0000-000000000000" + "Id" = "qjltmaz9l02qPcgftHNirITXiOnmHR5GmW_oEXl_ZL8" + "PrincipalDisplayName" = "MOD Administrator" + "PrincipalType" = "User" + "ResourceDisplayName" = "ProvisioningPowerBi" + "PrincipalId" = "996d39aa-fdac-4d97-aa3d-c81fb47362ac" + "ResourceId" = "021510b7-e753-40aa-b668-29753295ca34" + "Parameters" = $args + } + ) + } + + Mock -CommandName Get-MgServicePrincipalAppRoleAssignedTo -MockWith $scriptblock -ModuleName Microsoft.Graph.Entra +} + +Describe "Get-EntraServiceAppRoleAssigned" { + Context "Test for Get-EntraServiceAppRoleAssigned" { + It "Should return service principal application role assignment." { + $result = Get-EntraServicePrincipalAppRoleAssignment -ObjectId "021510b7-e753-40aa-b668-29753295ca34" + $result | Should -Not -BeNullOrEmpty + $result.ResourceId | should -Be '021510b7-e753-40aa-b668-29753295ca34' + + Should -Invoke -CommandName Get-MgServicePrincipalAppRoleAssignedTo -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Should fail when ObjectId is empty" { + { Get-EntraServicePrincipalAppRoleAssignment -ObjectId } | Should -Throw "Missing an argument for parameter 'ObjectId'.*" + } + It "Should fail when ObjectId is invalid" { + { Get-EntraServicePrincipalAppRoleAssignment -ObjectId "" } | Should -Throw "Cannot bind argument to parameter 'ObjectId' because it is an empty string.*" + } + It "Should return all service principal application role assignment." { + $result = Get-EntraServicePrincipalAppRoleAssignment -ObjectId "021510b7-e753-40aa-b668-29753295ca34" -All + $result | Should -Not -BeNullOrEmpty + + Should -Invoke -CommandName Get-MgServicePrincipalAppRoleAssignedTo -ModuleName Microsoft.Graph.Entra -Times 1 + } + + It "Should return top service principal application role assignment." { + $result = Get-EntraServicePrincipalAppRoleAssignment -ObjectId "021510b7-e753-40aa-b668-29753295ca34" -top 1 + $result | Should -Not -BeNullOrEmpty + + Should -Invoke -CommandName Get-MgServicePrincipalAppRoleAssignedTo -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Should fail when Top is empty" { + { Get-EntraServicePrincipalAppRoleAssignment -ObjectId "021510b7-e753-40aa-b668-29753295ca34" -Top } | Should -Throw "Missing an argument for parameter 'Top'*" + } + It "Should fail when Top is invalid" { + { Get-EntraServicePrincipalAppRoleAssignment -ObjectId "021510b7-e753-40aa-b668-29753295ca34" -Top xyz } | Should -Throw "Cannot process argument transformation on parameter 'Top'*" + } + It "Result should Contain ObjectId" { + $result = Get-EntraServicePrincipalAppRoleAssignment -ObjectId "021510b7-e753-40aa-b668-29753295ca34" + $result.ObjectId | should -Be "qjltmaz9l02qPcgftHNirITXiOnmHR5GmW_oEXl_ZL8" + } + It "Should contain ServicePrincipalId in parameters when passed ObjectId to it" { + Mock -CommandName Get-MgServicePrincipalAppRoleAssignedTo -MockWith {$args} -ModuleName Microsoft.Graph.Entra + + $result = Get-EntraServicePrincipalAppRoleAssignment -ObjectId "021510b7-e753-40aa-b668-29753295ca34" + $params = Get-Parameters -data $result + $params.ServicePrincipalId | Should -Be "021510b7-e753-40aa-b668-29753295ca34" + } + + It "Should contain 'User-Agent' header" { + Mock -CommandName Get-MgServicePrincipalAppRoleAssignedTo -MockWith {$args} -ModuleName Microsoft.Graph.Entra + + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraServicePrincipalAppRoleAssignment" + + $result = Get-EntraServicePrincipalAppRoleAssignment -ObjectId "021510b7-e753-40aa-b668-29753295ca34" + $params = Get-Parameters -data $result + $params.Headers["User-Agent"] | Should -Be $userAgentHeaderValue + } + } +} \ No newline at end of file diff --git a/test/module/Entra/Remove-EntraServicePrincipalAppRoleAssignment.Tests.ps1 b/test/module/Entra/Remove-EntraServicePrincipalAppRoleAssignment.Tests.ps1 new file mode 100644 index 000000000..640044344 --- /dev/null +++ b/test/module/Entra/Remove-EntraServicePrincipalAppRoleAssignment.Tests.ps1 @@ -0,0 +1,49 @@ +# ------------------------------------------------------------------------------ +# Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information. +# ------------------------------------------------------------------------------ +BeforeAll { + if((Get-Module -Name Microsoft.Graph.Entra) -eq $null){ + Import-Module Microsoft.Graph.Entra + } + Import-Module (Join-Path $psscriptroot "..\Common-Functions.ps1") -Force + + Mock -CommandName Remove-MgServicePrincipalAppRoleAssignment -MockWith {} -ModuleName Microsoft.Graph.Entra +} +Describe "Remove-EntraServicePrincipalAppRoleAssignment" { + Context "Test for Remove-EntraServicePrincipalAppRoleAssignment" { + It "Should return empty object" { + $result = Remove-EntraServicePrincipalAppRoleAssignment -ObjectId "cc7fcc82-ac1b-4785-af47-2ca3b7052886" -AppRoleAssignmentId "gsx_zBushUevRyyjtwUohm_RMYjcGsJIjXwKOVMr3ww" + $result | Should -BeNullOrEmpty + Should -Invoke -CommandName Remove-MgServicePrincipalAppRoleAssignment -ModuleName Microsoft.Graph.Entra -Times 1 + } + + It "Should fail when ObjectId is empty" { + { Remove-EntraServicePrincipalAppRoleAssignment -ObjectId -AppRoleAssignmentId "gsx_zBushUevRyyjtwUohm_RMYjcGsJIjXwKOVMr3ww"}| Should -Throw "Missing an argument for parameter 'ObjectId'.*" + } + It "Should fail when ObjectId is invalid" { + { Remove-EntraServicePrincipalAppRoleAssignment -ObjectId "" -AppRoleAssignmentId "gsx_zBushUevRyyjtwUohm_RMYjcGsJIjXwKOVMr3ww" } | Should -Throw "Cannot bind argument to parameter 'ObjectId' because it is an empty string.*" + } + It "Should fail when AppRoleAssignmentId is empty" { + { Remove-EntraServicePrincipalAppRoleAssignment -ObjectId "cc7fcc82-ac1b-4785-af47-2ca3b7052886" -AppRoleAssignmentId }| Should -Throw "Missing an argument for parameter 'AppRoleAssignmentId'.*" + } + It "Should fail when AppRoleAssignmentId is invalid" { + { Remove-EntraServicePrincipalAppRoleAssignment -ObjectId "cc7fcc82-ac1b-4785-af47-2ca3b7052886" -AppRoleAssignmentId "" } | Should -Throw "Cannot bind argument to parameter 'AppRoleAssignmentId' because it is an empty string.*" + } + It "Should contain ServicePrincipalId in parameters when passed ObjectId to it" { + Mock -CommandName Remove-MgServicePrincipalAppRoleAssignment -MockWith {$args} -ModuleName Microsoft.Graph.Entra + + $result = Remove-EntraServicePrincipalAppRoleAssignment -ObjectId "cc7fcc82-ac1b-4785-af47-2ca3b7052886" -AppRoleAssignmentId "gsx_zBushUevRyyjtwUohm_RMYjcGsJIjXwKOVMr3ww" + $params = Get-Parameters -data $result + $params.ServicePrincipalId | Should -Be "cc7fcc82-ac1b-4785-af47-2ca3b7052886" + + } + It "Should contain 'User-Agent' header" { + Mock -CommandName Remove-MgServicePrincipalAppRoleAssignment -MockWith {$args} -ModuleName Microsoft.Graph.Entra + + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Remove-EntraServicePrincipalAppRoleAssignment" + $result = Remove-EntraServicePrincipalAppRoleAssignment -ObjectId "cc7fcc82-ac1b-4785-af47-2ca3b7052886" -AppRoleAssignmentId "gsx_zBushUevRyyjtwUohm_RMYjcGsJIjXwKOVMr3ww" + $params = Get-Parameters -data $result + $params.Headers["User-Agent"] | Should -Be $userAgentHeaderValue + } + } +} \ No newline at end of file