From a47fb76532397c69086537381945d4322001bd21 Mon Sep 17 00:00:00 2001 From: "Snehal Kotwal (Perennial Systems Inc)" Date: Thu, 9 May 2024 17:02:47 +0530 Subject: [PATCH 1/4] added unit test for New-EntraServicePrincipal and New-EntraServicePrincipalPaswordCrediential --- .../Entra/New-EntraServicePrincipal.Tests.ps1 | 99 +++++++++++++++++++ ...rvicePrincipalPasswordCredential.Tests.ps1 | 68 +++++++++++++ 2 files changed, 167 insertions(+) create mode 100644 test/module/Entra/New-EntraServicePrincipal.Tests.ps1 create mode 100644 test/module/Entra/New-EntraServicePrincipalPasswordCredential.Tests.ps1 diff --git a/test/module/Entra/New-EntraServicePrincipal.Tests.ps1 b/test/module/Entra/New-EntraServicePrincipal.Tests.ps1 new file mode 100644 index 000000000..1e6803d31 --- /dev/null +++ b/test/module/Entra/New-EntraServicePrincipal.Tests.ps1 @@ -0,0 +1,99 @@ +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 New-MgServicePrincipal with parameters: $($args | ConvertTo-Json -Depth 3)" + return @( + [PSCustomObject]@{ + "AppId" = "3ee2fcac-fa2b-4080-a8fe-442c6536ca94" + "AccountEnabled" = $True + "Id" = "8eb49881-a102-49c0-87ef-4fa85359dc0f" + "AppDisplayName" = "ToGraph_443DEM" + "ServicePrincipalType" = "Application" + "SignInAudience" = "AzureADMyOrg" + "AppRoleAssignmentRequired" = $true + "AlternativeNames" = "unitalternative" + "Homepage" = "http://localhost/home" + "DisplayName" = "ToGraph_443DEM" + "LogoutUrl" = "htpp://localhost/logout" + "ReplyUrls" = "http://localhost/redirect" + "Tags" = "{WindowsAzureActiveDirectoryIntegratedApp}" + "ServicePrincipalNames" = "3ee2fcac-fa2b-4080-a8fe-442c6536ca94" + "AppOwnerOrganizationId" = "d5aec55f-2d12-4442-8d2f-ccca95d4390e" + "KeyCredentials" = @{CustomKeyIdentifier = @(84, 101, 115, 116);DisplayName =""; Key="";KeyId="bf620d66-bd18-4348-94e4-7431d7ad20a6";Type="Symmetric";Usage="Sign"} + "PasswordCredentials" = @{} + } + ) + } + + Mock -CommandName New-MgServicePrincipal -MockWith $scriptblock -ModuleName Microsoft.Graph.Entra +} + +Describe "New-EntraServicePrincipal"{ + Context "Test for New-EntraServicePrincipal" { + It "Should return created service principal"{ + $result = New-EntraServicePrincipal -AppId "3ee2fcac-fa2b-4080-a8fe-442c6536ca94" -Homepage 'http://localhost/home' -LogoutUrl 'htpp://localhost/logout' -ReplyUrls 'http://localhost/redirect' -AccountEnabled $true -DisplayName "ToGraph_443DEM" -AlternativeNames "unitalternative" -Tags {WindowsAzureActiveDirectoryIntegratedApp} -AppRoleAssignmentRequired $true -ServicePrincipalType "Application" -ServicePrincipalNames "3ee2fcac-fa2b-4080-a8fe-442c6536ca94" + $result | Should -Not -Be NullOrEmpty + $result.DisplayName | should -Be "ToGraph_443DEM" + $result.AccountEnabled | should -Be "True" + $result.AppId | should -Be "3ee2fcac-fa2b-4080-a8fe-442c6536ca94" + $result.Homepage | should -Be "http://localhost/home" + $result.LogoutUrl | should -Be "htpp://localhost/logout" + $result.AlternativeNames | should -Be "unitalternative" + $result.Tags | should -Be "{WindowsAzureActiveDirectoryIntegratedApp}" + $result.AppRoleAssignmentRequired | should -Be "True" + $result.ReplyUrls | should -Be "http://localhost/redirect" + $result.ServicePrincipalType | should -Be "Application" + $result.ServicePrincipalNames | should -Be "3ee2fcac-fa2b-4080-a8fe-442c6536ca94" + + Should -Invoke -CommandName New-MgServicePrincipal -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Should fail when AppID is empty" { + { New-EntraServicePrincipal -AppId } | Should -Throw "Missing an argument for parameter 'AppId'.*" + } + It "Should fail when AppID is Invalid" { + { New-EntraServicePrincipal -AppId "" } | Should -Throw "Cannot bind argument to parameter 'AppId' because it is an empty string.*" + } + It "Should fail when non-mandatory is empty" { + { New-EntraServicePrincipal -AppId "3ee2fcac-fa2b-4080-a8fe-442c6536ca94" -Tags -ReplyUrls -AccountEnabled -AlternativeNames } | Should -Throw "Missing an argument for parameter*" + } + It "Should create service principal with KeyCredentials parameter"{ + $creds = New-Object Microsoft.Open.AzureAD.Model.KeyCredential + $creds.CustomKeyIdentifier = [System.Text.Encoding]::UTF8.GetBytes("Test") + $startdate = Get-Date -Year 2023 -Month 10 -Day 23 + $creds.StartDate = $startdate + $creds.Type = "Symmetric" + $creds.Usage = 'Sign' + $creds.Value = [System.Text.Encoding]::UTF8.GetBytes("123") + $creds.EndDate = Get-Date -Year 2024 -Month 10 -Day 23 + $result= New-EntraServicePrincipal -AppId "3ee2fcac-fa2b-4080-a8fe-442c6536ca94" -KeyCredentials $creds + $result | Should -Not -Be NullOrEmpty + $result.AppId | should -Be "3ee2fcac-fa2b-4080-a8fe-442c6536ca94" + $keycredentials = @{CustomKeyIdentifier = @(84, 101, 115, 116);DisplayName =""; Key="";KeyId="bf620d66-bd18-4348-94e4-7431d7ad20a6";Type="Symmetric";Usage="Sign"} | ConvertTo-json + ($result.KeyCredentials | ConvertTo-json ) | should -Be $keycredentials + } + It "Should fail when KeyCredentials is empty" { + { New-EntraServicePrincipal -AppId "3ee2fcac-fa2b-4080-a8fe-442c6536ca94" -KeyCredentials } | Should -Throw "Missing an argument for parameter 'KeyCredentials'.*" + } + It "Should fail when KeyCredentials is Invalid" { + { New-EntraServicePrincipal -AppId "3ee2fcac-fa2b-4080-a8fe-442c6536ca94" -KeyCredentials "xyz" } | Should -Throw "Cannot process argument transformation on parameter 'KeyCredentials'.*" + } + It "Result should Contain ObjectId and AppOwnerTenantId" { + $result = New-EntraServicePrincipal -AppId "3ee2fcac-fa2b-4080-a8fe-442c6536ca94" + $result.ObjectId | should -Be "8eb49881-a102-49c0-87ef-4fa85359dc0f" + $result.AppOwnerTenantId | should -Be "d5aec55f-2d12-4442-8d2f-ccca95d4390e" + } + It "Should contain 'User-Agent' header" { + Mock -CommandName New-MgServicePrincipal -MockWith {$args} -ModuleName Microsoft.Graph.Entra + + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion New-EntraServicePrincipal" + + $result = New-EntraServicePrincipal -AppId "3ee2fcac-fa2b-4080-a8fe-442c6536ca94" -Homepage 'http://localhost/home' -LogoutUrl 'htpp://localhost/logout' -AccountEnabled $true -DisplayName "ToGraph_443DEM" -AlternativeNames "unitalternative" -Tags {WindowsAzureActiveDirectoryIntegratedApp} -AppRoleAssignmentRequired $true -ReplyUrls 'http://localhost/redirect' -ServicePrincipalType "Application" -ServicePrincipalNames "3ee2fcac-fa2b-4080-a8fe-442c6536ca94" + $params = Get-Parameters -data $result + $params.Headers["User-Agent"] | Should -Be $userAgentHeaderValue + } + } +} \ No newline at end of file diff --git a/test/module/Entra/New-EntraServicePrincipalPasswordCredential.Tests.ps1 b/test/module/Entra/New-EntraServicePrincipalPasswordCredential.Tests.ps1 new file mode 100644 index 000000000..593b2769c --- /dev/null +++ b/test/module/Entra/New-EntraServicePrincipalPasswordCredential.Tests.ps1 @@ -0,0 +1,68 @@ +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 Add-MgServicePrincipalPassword with parameters: $($args | ConvertTo-Json -Depth 3)" + return @( + [PSCustomObject]@{ + "CustomKeyIdentifier" = $null + "DisplayName" = $null + "EndDateTime" = "16/12/2024 13:14:14" + "Hint" = "YWE" + "KeyId" = "7aa34377-276f-4ea7-a7cb-31c3711f794b" + "SecretText" = "YWE8Q~~yRXoB42WwGVEP.5csr2gwD10DOPfJWc~o" + "StartDateTime" = "16/09/2024 14:14:14" + + } + ) + } + + Mock -CommandName Add-MgServicePrincipalPassword -MockWith $scriptblock -ModuleName Microsoft.Graph.Entra +} + +Describe "New-EntraServicePrincipalPasswordCredential"{ + Context "Test for New-EntraServicePrincipalPasswordCredential" { + It "Should return created password credential for a service principal."{ + $result = New-EntraServicePrincipalPasswordCredential -ObjectID "50b6ee9c-563f-402f-9922-c0d7adc86bf0" -StartDate "2024-09-16T14:14:14Z" -EndDate "2024-12-16T13:14:14Z" + $result | Should -Not -Be NullOrEmpty + $result.StartDate | should -Be "16/09/2024 14:14:14" + $result.EndDate | should -Be "16/12/2024 13:14:14" + + Should -Invoke -CommandName Add-MgServicePrincipalPassword -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Should fail when ObjectID is empty" { + {New-EntraServicePrincipalPasswordCredential -ObjectID } | Should -Throw "Missing an argument for parameter 'ObjectID'.*" + } + It "Should fail when ObjectID is Invalid" { + { New-EntraServicePrincipalPasswordCredential -ObjectID "" } | Should -Throw "Cannot bind argument to parameter 'ObjectID' because it is an empty string.*" + } + It "Should fail when StartDate is empty" { + { New-EntraServicePrincipalPasswordCredential -ObjectID "50b6ee9c-563f-402f-9922-c0d7adc86bf0" -StartDate } | Should -Throw "Missing an argument for parameter 'StartDate'.*" + } + It "Should fail when StartDate is invalid" { + { New-EntraServicePrincipalPasswordCredential -ObjectID "50b6ee9c-563f-402f-9922-c0d7adc86bf0" -StartDate "xyz" } | Should -Throw "Cannot process argument transformation on parameter 'StartDate'. Cannot convert value*" + } + It "Should fail when EndDate is empty" { + { New-EntraServicePrincipalPasswordCredential -ObjectID "50b6ee9c-563f-402f-9922-c0d7adc86bf0" -EndDate } | Should -Throw "Missing an argument for parameter 'EndDate'.*" + } + It "Should fail when EndDate is invalid" { + { New-EntraServicePrincipalPasswordCredential -ObjectID "50b6ee9c-563f-402f-9922-c0d7adc86bf0" -EndDate "xyz" } | Should -Throw "Cannot process argument transformation on parameter 'EndDate'. Cannot convert value*" + } + It "Result should Contain StartDate and EndDate" { + $result = New-EntraServicePrincipalPasswordCredential -ObjectID "50b6ee9c-563f-402f-9922-c0d7adc86bf0" -StartDate "2024-09-16T14:14:14Z" -EndDate "2024-12-16T13:14:14Z" + $result.StartDate | should -Be "16/09/2024 14:14:14" + $result.EndDate | should -Be "16/12/2024 13:14:14" + } + It "Should contain 'User-Agent' header" { + Mock -CommandName Add-MgServicePrincipalPassword -MockWith {$args} -ModuleName Microsoft.Graph.Entra + + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion New-EntraServicePrincipalPasswordCredential" + $result = New-EntraServicePrincipalPasswordCredential -ObjectID "50b6ee9c-563f-402f-9922-c0d7adc86bf0" -StartDate "2024-09-16T14:14:14Z" -EndDate "2024-12-16T13:14:14Z" + $params = Get-Parameters -data $result + $params.Headers["User-Agent"] | Should -Be $userAgentHeaderValue + } + } +} \ No newline at end of file From cb28f2bdf85f962808f53da59a9aa09a35ad4e17 Mon Sep 17 00:00:00 2001 From: v-uansari Date: Thu, 8 Aug 2024 12:27:08 +0530 Subject: [PATCH 2/4] added licance --- test/module/Entra/New-EntraServicePrincipal.Tests.ps1 | 3 +++ .../New-EntraServicePrincipalPasswordCredential.Tests.ps1 | 3 +++ 2 files changed, 6 insertions(+) diff --git a/test/module/Entra/New-EntraServicePrincipal.Tests.ps1 b/test/module/Entra/New-EntraServicePrincipal.Tests.ps1 index 1e6803d31..9d5fd25d8 100644 --- a/test/module/Entra/New-EntraServicePrincipal.Tests.ps1 +++ b/test/module/Entra/New-EntraServicePrincipal.Tests.ps1 @@ -1,3 +1,6 @@ +# ------------------------------------------------------------------------------ +# 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 diff --git a/test/module/Entra/New-EntraServicePrincipalPasswordCredential.Tests.ps1 b/test/module/Entra/New-EntraServicePrincipalPasswordCredential.Tests.ps1 index 593b2769c..462fb05ff 100644 --- a/test/module/Entra/New-EntraServicePrincipalPasswordCredential.Tests.ps1 +++ b/test/module/Entra/New-EntraServicePrincipalPasswordCredential.Tests.ps1 @@ -1,3 +1,6 @@ +# ------------------------------------------------------------------------------ +# 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 From 2a47dc7e7efdd00c1771148c6f7fa6e28442dbd1 Mon Sep 17 00:00:00 2001 From: "Snehal Kotwal (Perennial Systems Inc)" Date: Fri, 30 Aug 2024 15:08:24 +0530 Subject: [PATCH 3/4] updated mock --- .../Entra/New-EntraServicePrincipal.Tests.ps1 | 46 ++++++++++++------- ...rvicePrincipalPasswordCredential.Tests.ps1 | 20 ++++---- 2 files changed, 40 insertions(+), 26 deletions(-) diff --git a/test/module/Entra/New-EntraServicePrincipal.Tests.ps1 b/test/module/Entra/New-EntraServicePrincipal.Tests.ps1 index 9d5fd25d8..5d2a9bf01 100644 --- a/test/module/Entra/New-EntraServicePrincipal.Tests.ps1 +++ b/test/module/Entra/New-EntraServicePrincipal.Tests.ps1 @@ -11,9 +11,9 @@ BeforeAll { # Write-Host "Mocking New-MgServicePrincipal with parameters: $($args | ConvertTo-Json -Depth 3)" return @( [PSCustomObject]@{ - "AppId" = "3ee2fcac-fa2b-4080-a8fe-442c6536ca94" + "AppId" = "00001111-aaaa-2222-bbbb-3333cccc4444" "AccountEnabled" = $True - "Id" = "8eb49881-a102-49c0-87ef-4fa85359dc0f" + "Id" = "00aa00aa-bb11-cc22-dd33-44ee44ee44ee" "AppDisplayName" = "ToGraph_443DEM" "ServicePrincipalType" = "Application" "SignInAudience" = "AzureADMyOrg" @@ -24,8 +24,8 @@ BeforeAll { "LogoutUrl" = "htpp://localhost/logout" "ReplyUrls" = "http://localhost/redirect" "Tags" = "{WindowsAzureActiveDirectoryIntegratedApp}" - "ServicePrincipalNames" = "3ee2fcac-fa2b-4080-a8fe-442c6536ca94" - "AppOwnerOrganizationId" = "d5aec55f-2d12-4442-8d2f-ccca95d4390e" + "ServicePrincipalNames" = "11bb11bb-cc22-dd33-ee44-55ff55ff55ff" + "AppOwnerOrganizationId" = "44445555-eeee-6666-ffff-7777aaaa8888" "KeyCredentials" = @{CustomKeyIdentifier = @(84, 101, 115, 116);DisplayName =""; Key="";KeyId="bf620d66-bd18-4348-94e4-7431d7ad20a6";Type="Symmetric";Usage="Sign"} "PasswordCredentials" = @{} } @@ -38,11 +38,11 @@ BeforeAll { Describe "New-EntraServicePrincipal"{ Context "Test for New-EntraServicePrincipal" { It "Should return created service principal"{ - $result = New-EntraServicePrincipal -AppId "3ee2fcac-fa2b-4080-a8fe-442c6536ca94" -Homepage 'http://localhost/home' -LogoutUrl 'htpp://localhost/logout' -ReplyUrls 'http://localhost/redirect' -AccountEnabled $true -DisplayName "ToGraph_443DEM" -AlternativeNames "unitalternative" -Tags {WindowsAzureActiveDirectoryIntegratedApp} -AppRoleAssignmentRequired $true -ServicePrincipalType "Application" -ServicePrincipalNames "3ee2fcac-fa2b-4080-a8fe-442c6536ca94" + $result = New-EntraServicePrincipal -AppId "00001111-aaaa-2222-bbbb-3333cccc4444" -Homepage 'http://localhost/home' -LogoutUrl 'htpp://localhost/logout' -ReplyUrls 'http://localhost/redirect' -AccountEnabled $true -DisplayName "ToGraph_443DEM" -AlternativeNames "unitalternative" -Tags {WindowsAzureActiveDirectoryIntegratedApp} -AppRoleAssignmentRequired $true -ServicePrincipalType "Application" -ServicePrincipalNames "11bb11bb-cc22-dd33-ee44-55ff55ff55ff" $result | Should -Not -Be NullOrEmpty $result.DisplayName | should -Be "ToGraph_443DEM" $result.AccountEnabled | should -Be "True" - $result.AppId | should -Be "3ee2fcac-fa2b-4080-a8fe-442c6536ca94" + $result.AppId | should -Be "00001111-aaaa-2222-bbbb-3333cccc4444" $result.Homepage | should -Be "http://localhost/home" $result.LogoutUrl | should -Be "htpp://localhost/logout" $result.AlternativeNames | should -Be "unitalternative" @@ -50,7 +50,7 @@ Describe "New-EntraServicePrincipal"{ $result.AppRoleAssignmentRequired | should -Be "True" $result.ReplyUrls | should -Be "http://localhost/redirect" $result.ServicePrincipalType | should -Be "Application" - $result.ServicePrincipalNames | should -Be "3ee2fcac-fa2b-4080-a8fe-442c6536ca94" + $result.ServicePrincipalNames | should -Be "11bb11bb-cc22-dd33-ee44-55ff55ff55ff" Should -Invoke -CommandName New-MgServicePrincipal -ModuleName Microsoft.Graph.Entra -Times 1 } @@ -61,7 +61,7 @@ Describe "New-EntraServicePrincipal"{ { New-EntraServicePrincipal -AppId "" } | Should -Throw "Cannot bind argument to parameter 'AppId' because it is an empty string.*" } It "Should fail when non-mandatory is empty" { - { New-EntraServicePrincipal -AppId "3ee2fcac-fa2b-4080-a8fe-442c6536ca94" -Tags -ReplyUrls -AccountEnabled -AlternativeNames } | Should -Throw "Missing an argument for parameter*" + { New-EntraServicePrincipal -AppId "00001111-aaaa-2222-bbbb-3333cccc4444" -Tags -ReplyUrls -AccountEnabled -AlternativeNames } | Should -Throw "Missing an argument for parameter*" } It "Should create service principal with KeyCredentials parameter"{ $creds = New-Object Microsoft.Open.AzureAD.Model.KeyCredential @@ -72,31 +72,45 @@ Describe "New-EntraServicePrincipal"{ $creds.Usage = 'Sign' $creds.Value = [System.Text.Encoding]::UTF8.GetBytes("123") $creds.EndDate = Get-Date -Year 2024 -Month 10 -Day 23 - $result= New-EntraServicePrincipal -AppId "3ee2fcac-fa2b-4080-a8fe-442c6536ca94" -KeyCredentials $creds + $result= New-EntraServicePrincipal -AppId "00001111-aaaa-2222-bbbb-3333cccc4444" -KeyCredentials $creds $result | Should -Not -Be NullOrEmpty - $result.AppId | should -Be "3ee2fcac-fa2b-4080-a8fe-442c6536ca94" + $result.AppId | should -Be "00001111-aaaa-2222-bbbb-3333cccc4444" $keycredentials = @{CustomKeyIdentifier = @(84, 101, 115, 116);DisplayName =""; Key="";KeyId="bf620d66-bd18-4348-94e4-7431d7ad20a6";Type="Symmetric";Usage="Sign"} | ConvertTo-json ($result.KeyCredentials | ConvertTo-json ) | should -Be $keycredentials } It "Should fail when KeyCredentials is empty" { - { New-EntraServicePrincipal -AppId "3ee2fcac-fa2b-4080-a8fe-442c6536ca94" -KeyCredentials } | Should -Throw "Missing an argument for parameter 'KeyCredentials'.*" + { New-EntraServicePrincipal -AppId "00001111-aaaa-2222-bbbb-3333cccc4444" -KeyCredentials } | Should -Throw "Missing an argument for parameter 'KeyCredentials'.*" } It "Should fail when KeyCredentials is Invalid" { - { New-EntraServicePrincipal -AppId "3ee2fcac-fa2b-4080-a8fe-442c6536ca94" -KeyCredentials "xyz" } | Should -Throw "Cannot process argument transformation on parameter 'KeyCredentials'.*" + { New-EntraServicePrincipal -AppId "00001111-aaaa-2222-bbbb-3333cccc4444" -KeyCredentials "xyz" } | Should -Throw "Cannot process argument transformation on parameter 'KeyCredentials'.*" } It "Result should Contain ObjectId and AppOwnerTenantId" { - $result = New-EntraServicePrincipal -AppId "3ee2fcac-fa2b-4080-a8fe-442c6536ca94" - $result.ObjectId | should -Be "8eb49881-a102-49c0-87ef-4fa85359dc0f" - $result.AppOwnerTenantId | should -Be "d5aec55f-2d12-4442-8d2f-ccca95d4390e" + $result = New-EntraServicePrincipal -AppId "00001111-aaaa-2222-bbbb-3333cccc4444" + $result.ObjectId | should -Be "00aa00aa-bb11-cc22-dd33-44ee44ee44ee" + $result.AppOwnerTenantId | should -Be "44445555-eeee-6666-ffff-7777aaaa8888" } It "Should contain 'User-Agent' header" { Mock -CommandName New-MgServicePrincipal -MockWith {$args} -ModuleName Microsoft.Graph.Entra $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion New-EntraServicePrincipal" - $result = New-EntraServicePrincipal -AppId "3ee2fcac-fa2b-4080-a8fe-442c6536ca94" -Homepage 'http://localhost/home' -LogoutUrl 'htpp://localhost/logout' -AccountEnabled $true -DisplayName "ToGraph_443DEM" -AlternativeNames "unitalternative" -Tags {WindowsAzureActiveDirectoryIntegratedApp} -AppRoleAssignmentRequired $true -ReplyUrls 'http://localhost/redirect' -ServicePrincipalType "Application" -ServicePrincipalNames "3ee2fcac-fa2b-4080-a8fe-442c6536ca94" + $result = New-EntraServicePrincipal -AppId "00001111-aaaa-2222-bbbb-3333cccc4444" -Homepage 'http://localhost/home' -LogoutUrl 'htpp://localhost/logout' -AccountEnabled $true -DisplayName "ToGraph_443DEM" -AlternativeNames "unitalternative" -Tags {WindowsAzureActiveDirectoryIntegratedApp} -AppRoleAssignmentRequired $true -ReplyUrls 'http://localhost/redirect' -ServicePrincipalType "Application" -ServicePrincipalNames "11bb11bb-cc22-dd33-ee44-55ff55ff55ff" $params = Get-Parameters -data $result $params.Headers["User-Agent"] | Should -Be $userAgentHeaderValue } + + 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 + { New-EntraServicePrincipal -AppId "00001111-aaaa-2222-bbbb-3333cccc4444" -Homepage 'http://localhost/home' -LogoutUrl 'htpp://localhost/logout' -AccountEnabled $true -DisplayName "ToGraph_443DEM" -AlternativeNames "unitalternative" -Tags {WindowsAzureActiveDirectoryIntegratedApp} -AppRoleAssignmentRequired $true -ReplyUrls 'http://localhost/redirect' -ServicePrincipalType "Application" -ServicePrincipalNames "11bb11bb-cc22-dd33-ee44-55ff55ff55ff" -Debug } | Should -Not -Throw + } finally { + # Restore original confirmation preference + $DebugPreference = $originalDebugPreference + } + } } } \ No newline at end of file diff --git a/test/module/Entra/New-EntraServicePrincipalPasswordCredential.Tests.ps1 b/test/module/Entra/New-EntraServicePrincipalPasswordCredential.Tests.ps1 index 462fb05ff..edd1bafb8 100644 --- a/test/module/Entra/New-EntraServicePrincipalPasswordCredential.Tests.ps1 +++ b/test/module/Entra/New-EntraServicePrincipalPasswordCredential.Tests.ps1 @@ -15,8 +15,8 @@ BeforeAll { "DisplayName" = $null "EndDateTime" = "16/12/2024 13:14:14" "Hint" = "YWE" - "KeyId" = "7aa34377-276f-4ea7-a7cb-31c3711f794b" - "SecretText" = "YWE8Q~~yRXoB42WwGVEP.5csr2gwD10DOPfJWc~o" + "KeyId" = "aaaaaaaa-0b0b-1c1c-2d2d-333333333333" + "SecretText" = "Aa1Bb2Cc3.-Dd4Ee5Ff6Gg7Hh8Ii9_~Jj0Kk1Ll2" "StartDateTime" = "16/09/2024 14:14:14" } @@ -29,7 +29,7 @@ BeforeAll { Describe "New-EntraServicePrincipalPasswordCredential"{ Context "Test for New-EntraServicePrincipalPasswordCredential" { It "Should return created password credential for a service principal."{ - $result = New-EntraServicePrincipalPasswordCredential -ObjectID "50b6ee9c-563f-402f-9922-c0d7adc86bf0" -StartDate "2024-09-16T14:14:14Z" -EndDate "2024-12-16T13:14:14Z" + $result = New-EntraServicePrincipalPasswordCredential -ObjectID "bbbbbbbb-1111-2222-3333-cccccccccccc" -StartDate "2024-09-16T14:14:14Z" -EndDate "2024-12-16T13:14:14Z" $result | Should -Not -Be NullOrEmpty $result.StartDate | should -Be "16/09/2024 14:14:14" $result.EndDate | should -Be "16/12/2024 13:14:14" @@ -43,19 +43,19 @@ Describe "New-EntraServicePrincipalPasswordCredential"{ { New-EntraServicePrincipalPasswordCredential -ObjectID "" } | Should -Throw "Cannot bind argument to parameter 'ObjectID' because it is an empty string.*" } It "Should fail when StartDate is empty" { - { New-EntraServicePrincipalPasswordCredential -ObjectID "50b6ee9c-563f-402f-9922-c0d7adc86bf0" -StartDate } | Should -Throw "Missing an argument for parameter 'StartDate'.*" + { New-EntraServicePrincipalPasswordCredential -ObjectID "bbbbbbbb-1111-2222-3333-cccccccccccc" -StartDate } | Should -Throw "Missing an argument for parameter 'StartDate'.*" } It "Should fail when StartDate is invalid" { - { New-EntraServicePrincipalPasswordCredential -ObjectID "50b6ee9c-563f-402f-9922-c0d7adc86bf0" -StartDate "xyz" } | Should -Throw "Cannot process argument transformation on parameter 'StartDate'. Cannot convert value*" + { New-EntraServicePrincipalPasswordCredential -ObjectID "bbbbbbbb-1111-2222-3333-cccccccccccc" -StartDate "xyz" } | Should -Throw "Cannot process argument transformation on parameter 'StartDate'. Cannot convert value*" } It "Should fail when EndDate is empty" { - { New-EntraServicePrincipalPasswordCredential -ObjectID "50b6ee9c-563f-402f-9922-c0d7adc86bf0" -EndDate } | Should -Throw "Missing an argument for parameter 'EndDate'.*" + { New-EntraServicePrincipalPasswordCredential -ObjectID "bbbbbbbb-1111-2222-3333-cccccccccccc" -EndDate } | Should -Throw "Missing an argument for parameter 'EndDate'.*" } It "Should fail when EndDate is invalid" { - { New-EntraServicePrincipalPasswordCredential -ObjectID "50b6ee9c-563f-402f-9922-c0d7adc86bf0" -EndDate "xyz" } | Should -Throw "Cannot process argument transformation on parameter 'EndDate'. Cannot convert value*" + { New-EntraServicePrincipalPasswordCredential -ObjectID "bbbbbbbb-1111-2222-3333-cccccccccccc" -EndDate "xyz" } | Should -Throw "Cannot process argument transformation on parameter 'EndDate'. Cannot convert value*" } It "Result should Contain StartDate and EndDate" { - $result = New-EntraServicePrincipalPasswordCredential -ObjectID "50b6ee9c-563f-402f-9922-c0d7adc86bf0" -StartDate "2024-09-16T14:14:14Z" -EndDate "2024-12-16T13:14:14Z" + $result = New-EntraServicePrincipalPasswordCredential -ObjectID "bbbbbbbb-1111-2222-3333-cccccccccccc" -StartDate "2024-09-16T14:14:14Z" -EndDate "2024-12-16T13:14:14Z" $result.StartDate | should -Be "16/09/2024 14:14:14" $result.EndDate | should -Be "16/12/2024 13:14:14" } @@ -63,9 +63,9 @@ Describe "New-EntraServicePrincipalPasswordCredential"{ Mock -CommandName Add-MgServicePrincipalPassword -MockWith {$args} -ModuleName Microsoft.Graph.Entra $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion New-EntraServicePrincipalPasswordCredential" - $result = New-EntraServicePrincipalPasswordCredential -ObjectID "50b6ee9c-563f-402f-9922-c0d7adc86bf0" -StartDate "2024-09-16T14:14:14Z" -EndDate "2024-12-16T13:14:14Z" + $result = New-EntraServicePrincipalPasswordCredential -ObjectID "bbbbbbbb-1111-2222-3333-cccccccccccc" -StartDate "2024-09-16T14:14:14Z" -EndDate "2024-12-16T13:14:14Z" $params = Get-Parameters -data $result $params.Headers["User-Agent"] | Should -Be $userAgentHeaderValue - } + } } } \ No newline at end of file From cbb5ee0517b03d879168bea2a4bcdab49f76cd43 Mon Sep 17 00:00:00 2001 From: "Snehal Kotwal (Perennial Systems Inc)" Date: Wed, 4 Sep 2024 14:33:13 +0530 Subject: [PATCH 4/4] added mock test cases --- .../Entra/New-EntraServicePrincipal.Tests.ps1 | 13 +++++++++---- ...ntraServicePrincipalPasswordCredential.Tests.ps1 | 13 +++++++++---- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/test/module/Entra/New-EntraServicePrincipal.Tests.ps1 b/test/module/Entra/New-EntraServicePrincipal.Tests.ps1 index 5d2a9bf01..28352a329 100644 --- a/test/module/Entra/New-EntraServicePrincipal.Tests.ps1 +++ b/test/module/Entra/New-EntraServicePrincipal.Tests.ps1 @@ -89,14 +89,19 @@ Describe "New-EntraServicePrincipal"{ $result.ObjectId | should -Be "00aa00aa-bb11-cc22-dd33-44ee44ee44ee" $result.AppOwnerTenantId | should -Be "44445555-eeee-6666-ffff-7777aaaa8888" } + It "Should contain 'User-Agent' header" { - Mock -CommandName New-MgServicePrincipal -MockWith {$args} -ModuleName Microsoft.Graph.Entra + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion New-EntraServicePrincipal" + + $result = New-EntraServicePrincipal -AppId "00001111-aaaa-2222-bbbb-3333cccc4444" + $result | Should -Not -BeNullOrEmpty $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion New-EntraServicePrincipal" - $result = New-EntraServicePrincipal -AppId "00001111-aaaa-2222-bbbb-3333cccc4444" -Homepage 'http://localhost/home' -LogoutUrl 'htpp://localhost/logout' -AccountEnabled $true -DisplayName "ToGraph_443DEM" -AlternativeNames "unitalternative" -Tags {WindowsAzureActiveDirectoryIntegratedApp} -AppRoleAssignmentRequired $true -ReplyUrls 'http://localhost/redirect' -ServicePrincipalType "Application" -ServicePrincipalNames "11bb11bb-cc22-dd33-ee44-55ff55ff55ff" - $params = Get-Parameters -data $result - $params.Headers["User-Agent"] | Should -Be $userAgentHeaderValue + Should -Invoke -CommandName New-MgServicePrincipal -ModuleName Microsoft.Graph.Entra -Times 1 -ParameterFilter { + $Headers.'User-Agent' | Should -Be $userAgentHeaderValue + $true + } } It "Should execute successfully without throwing an error" { diff --git a/test/module/Entra/New-EntraServicePrincipalPasswordCredential.Tests.ps1 b/test/module/Entra/New-EntraServicePrincipalPasswordCredential.Tests.ps1 index edd1bafb8..6808aa657 100644 --- a/test/module/Entra/New-EntraServicePrincipalPasswordCredential.Tests.ps1 +++ b/test/module/Entra/New-EntraServicePrincipalPasswordCredential.Tests.ps1 @@ -60,12 +60,17 @@ Describe "New-EntraServicePrincipalPasswordCredential"{ $result.EndDate | should -Be "16/12/2024 13:14:14" } It "Should contain 'User-Agent' header" { - Mock -CommandName Add-MgServicePrincipalPassword -MockWith {$args} -ModuleName Microsoft.Graph.Entra - $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion New-EntraServicePrincipalPasswordCredential" + $result = New-EntraServicePrincipalPasswordCredential -ObjectID "bbbbbbbb-1111-2222-3333-cccccccccccc" -StartDate "2024-09-16T14:14:14Z" -EndDate "2024-12-16T13:14:14Z" - $params = Get-Parameters -data $result - $params.Headers["User-Agent"] | Should -Be $userAgentHeaderValue + $result | Should -Not -BeNullOrEmpty + + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion New-EntraServicePrincipalPasswordCredential" + + Should -Invoke -CommandName Add-MgServicePrincipalPassword -ModuleName Microsoft.Graph.Entra -Times 1 -ParameterFilter { + $Headers.'User-Agent' | Should -Be $userAgentHeaderValue + $true + } } } } \ No newline at end of file