From 8c0d07363934b1f99a373ff32546544970d972ee Mon Sep 17 00:00:00 2001 From: "Snehal Kotwal (Perennial Systems Inc)" Date: Tue, 3 Sep 2024 16:45:02 +0530 Subject: [PATCH 1/2] updated mock test cases --- ...EntraServicePrincipalOwnedObject.Tests.ps1 | 61 +++++++++++++++---- 1 file changed, 48 insertions(+), 13 deletions(-) diff --git a/test/module/Entra/Get-EntraServicePrincipalOwnedObject.Tests.ps1 b/test/module/Entra/Get-EntraServicePrincipalOwnedObject.Tests.ps1 index f2d04dd3c..c7f1471d0 100644 --- a/test/module/Entra/Get-EntraServicePrincipalOwnedObject.Tests.ps1 +++ b/test/module/Entra/Get-EntraServicePrincipalOwnedObject.Tests.ps1 @@ -11,7 +11,7 @@ BeforeAll { $scriptblock = { return @( [PSCustomObject]@{ - "Id" = "111cc9b5-fce9-485e-9566-c68debafac5f" + "Id" = "00aa00aa-bb11-cc22-dd33-44ee44ee44ee" "DeletedDateTime" = $null "AdditionalProperties" = @{ accountEnabled = $true; @@ -29,9 +29,9 @@ BeforeAll { Describe "Get-EntraServicePrincipalOwnedObject" { Context "Test for Get-EntraServicePrincipalOwnedObject" { It "Should return specific Owned Object" { - $result = Get-EntraServicePrincipalOwnedObject -ObjectId "2d028fff-7e65-4340-80ca-89be16dae0b3" + $result = Get-EntraServicePrincipalOwnedObject -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" $result | Should -Not -BeNullOrEmpty - $result.Id | should -Be @('111cc9b5-fce9-485e-9566-c68debafac5f') + $result.Id | should -Be @('00aa00aa-bb11-cc22-dd33-44ee44ee44ee') Should -Invoke -CommandName Get-MgServicePrincipalOwnedObject -ModuleName Microsoft.Graph.Entra -Times 1 } It "Should fail when ObjectId is empty" { @@ -41,32 +41,67 @@ Describe "Get-EntraServicePrincipalOwnedObject" { { Get-EntraServicePrincipalOwnedObject -ObjectId } | Should -Throw "Missing an argument for parameter 'ObjectId'*" } It "Should return all Owned Objects" { - $result = Get-EntraServicePrincipalOwnedObject -ObjectId "2d028fff-7e65-4340-80ca-89be16dae0b3" -All + $result = Get-EntraServicePrincipalOwnedObject -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -All $result | Should -Not -BeNullOrEmpty Should -Invoke -CommandName Get-MgServicePrincipalOwnedObject -ModuleName Microsoft.Graph.Entra -Times 1 } It "Should fail when All has an argument" { - { Get-EntraServicePrincipalOwnedObject -ObjectId "2d028fff-7e65-4340-80ca-89be16dae0b3" -All $true } | Should -Throw "A positional parameter cannot be found that accepts argument 'True'.*" + { Get-EntraServicePrincipalOwnedObject -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -All $true } | Should -Throw "A positional parameter cannot be found that accepts argument 'True'.*" } It "Should return top Owned Object" { - $result = Get-EntraServicePrincipalOwnedObject -ObjectId "2d028fff-7e65-4340-80ca-89be16dae0b3" -Top 1 + $result = Get-EntraServicePrincipalOwnedObject -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -Top 1 $result | Should -Not -BeNullOrEmpty Should -Invoke -CommandName Get-MgServicePrincipalOwnedObject -ModuleName Microsoft.Graph.Entra -Times 1 } It "Result should Contain ObjectId" { - $result = Get-EntraServicePrincipalOwnedObject -ObjectId "2d028fff-7e65-4340-80ca-89be16dae0b3" - $result.ObjectId | should -Be "111cc9b5-fce9-485e-9566-c68debafac5f" + $result = Get-EntraServicePrincipalOwnedObject -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + $result.ObjectId | should -Be "00aa00aa-bb11-cc22-dd33-44ee44ee44ee" } It "Should contain ServicePrincipalId in parameters when passed ObjectId to it" { - $result = Get-EntraServicePrincipalOwnedObject -ObjectId "2d028fff-7e65-4340-80ca-89be16dae0b3" + $result = Get-EntraServicePrincipalOwnedObject -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" $params = Get-Parameters -data $result.Parameters - $params.ServicePrincipalId | Should -Be "2d028fff-7e65-4340-80ca-89be16dae0b3" + $params.ServicePrincipalId | Should -Be "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" } + It "Should contain 'User-Agent' header" { $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraServicePrincipalOwnedObject" - $result = Get-EntraServicePrincipalOwnedObject -ObjectId "2d028fff-7e65-4340-80ca-89be16dae0b3" - $params = Get-Parameters -data $result.Parameters - $params.Headers["User-Agent"] | Should -Be $userAgentHeaderValue + + $result = Get-EntraServicePrincipalOwnedObject -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + $result | Should -Not -BeNullOrEmpty + + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraServicePrincipalOwnedObject" + + Should -Invoke -CommandName Get-MgServicePrincipalOwnedObject -ModuleName Microsoft.Graph.Entra -Times 1 -ParameterFilter { + $Headers.'User-Agent' | Should -Be $userAgentHeaderValue + $true + } + } + It "Property parameter should work" { + $result = Get-EntraServicePrincipalOwnedObject -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -Property Id + $result | Should -Not -BeNullOrEmpty + $result.Id | Should -Be '00aa00aa-bb11-cc22-dd33-44ee44ee44ee' + + Should -Invoke -CommandName Get-MgServicePrincipalOwnedObject -ModuleName Microsoft.Graph.Entra -Times 1 + } + + It "Should fail when Property is empty" { + { Get-EntraServicePrincipalOwnedObject -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -Property } | Should -Throw "Missing an argument for parameter 'Property'.*" + } + + It "Should execute successfully without throwing an error " { + # Disable confirmation prompts + $originalDebugPreference = $DebugPreference + $DebugPreference = 'Continue' + + try { + # Act & Assert: Ensure the function doesn't throw an exception + { + Get-EntraServicePrincipalOwnedObject -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -Debug + } | Should -Not -Throw + } finally { + # Restore original confirmation preference + $DebugPreference = $originalDebugPreference + } } } } \ No newline at end of file From edd2a4359ca579dc234373162b54067d76d84fe8 Mon Sep 17 00:00:00 2001 From: "Snehal Kotwal (Perennial Systems Inc)" Date: Tue, 3 Sep 2024 17:08:46 +0530 Subject: [PATCH 2/2] updated --- ...Update-EntraSignedInUserPassword.Tests.ps1 | 35 ++++++++++++++++--- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/test/module/Entra/Update-EntraSignedInUserPassword.Tests.ps1 b/test/module/Entra/Update-EntraSignedInUserPassword.Tests.ps1 index b3a3d07ab..51095c4de 100644 --- a/test/module/Entra/Update-EntraSignedInUserPassword.Tests.ps1 +++ b/test/module/Entra/Update-EntraSignedInUserPassword.Tests.ps1 @@ -14,6 +14,7 @@ BeforeAll{ $NewPassword = ConvertTo-SecureString 'test@1234' -AsPlainText -Force } Describe "Tests for Update-EntraSignedInUserPassword"{ + Context "Test for Update-EntraSignedInUserPassword" { It "should return empty object"{ $result = Update-EntraSignedInUserPassword -CurrentPassword $CurrentPassword -NewPassword $NewPassword $result | Should -BeNullOrEmpty @@ -31,11 +32,35 @@ Describe "Tests for Update-EntraSignedInUserPassword"{ It "Should fail when NewPassword is empty" { { Update-EntraSignedInUserPassword -NewPassword "" } | Should -Throw "Cannot process argument transformation on parameter 'NewPassword'*" } + It "Should contain 'User-Agent' header" { - Mock -CommandName Invoke-GraphRequest -MockWith {$args} -ModuleName Microsoft.Graph.Entra $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Update-EntraSignedInUserPassword" - $result = Update-EntraSignedInUserPassword -CurrentPassword $CurrentPassword -NewPassword $NewPassword - $params = Get-Parameters -data $result - $params.Headers["User-Agent"] | Should -Be $userAgentHeaderValue + + Update-EntraSignedInUserPassword -CurrentPassword $CurrentPassword -NewPassword $NewPassword + + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Update-EntraSignedInUserPassword" + + Should -Invoke -CommandName Invoke-GraphRequest -ModuleName Microsoft.Graph.Entra -Times 1 -ParameterFilter { + $Headers.'User-Agent' | Should -Be $userAgentHeaderValue + $true + } + } + + It "Should execute successfully without throwing an error " { + # Disable confirmation prompts + $originalDebugPreference = $DebugPreference + $DebugPreference = 'Continue' + + try { + # Act & Assert: Ensure the function doesn't throw an exception + { + Update-EntraSignedInUserPassword -CurrentPassword $CurrentPassword -NewPassword $NewPassword -Debug + } | Should -Not -Throw + } finally { + # Restore original confirmation preference + $DebugPreference = $originalDebugPreference + } + } + } -} \ No newline at end of file +}