Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 121 additions & 0 deletions test/module/Entra/New-EntraServicePrincipal.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
# ------------------------------------------------------------------------------
# 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 New-MgServicePrincipal with parameters: $($args | ConvertTo-Json -Depth 3)"
return @(
[PSCustomObject]@{
"AppId" = "00001111-aaaa-2222-bbbb-3333cccc4444"
"AccountEnabled" = $True
"Id" = "00aa00aa-bb11-cc22-dd33-44ee44ee44ee"
"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" = "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" = @{}
}
)
}

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 "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 "00001111-aaaa-2222-bbbb-3333cccc4444"
$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 "11bb11bb-cc22-dd33-ee44-55ff55ff55ff"

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 "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
$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 "00001111-aaaa-2222-bbbb-3333cccc4444" -KeyCredentials $creds
$result | Should -Not -Be NullOrEmpty
$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 "00001111-aaaa-2222-bbbb-3333cccc4444" -KeyCredentials } | Should -Throw "Missing an argument for parameter 'KeyCredentials'.*"
}
It "Should fail when KeyCredentials is Invalid" {
{ 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 "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" {
$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"

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" {
# 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
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# ------------------------------------------------------------------------------
# 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 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" = "aaaaaaaa-0b0b-1c1c-2d2d-333333333333"
"SecretText" = "Aa1Bb2Cc3.-Dd4Ee5Ff6Gg7Hh8Ii9_~Jj0Kk1Ll2"
"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 "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"

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 "bbbbbbbb-1111-2222-3333-cccccccccccc" -StartDate } | Should -Throw "Missing an argument for parameter 'StartDate'.*"
}
It "Should fail when StartDate is invalid" {
{ 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 "bbbbbbbb-1111-2222-3333-cccccccccccc" -EndDate } | Should -Throw "Missing an argument for parameter 'EndDate'.*"
}
It "Should fail when EndDate is invalid" {
{ 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 "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"
}
It "Should contain 'User-Agent' header" {
$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"
$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
}
}
}
}