From 0b04b1850df5682e8171fbe07ac549645fb1e89f Mon Sep 17 00:00:00 2001 From: "Snehal Kotwal (Perennial Systems Inc)" Date: Mon, 23 Sep 2024 12:44:35 +0530 Subject: [PATCH 01/15] updated mock test cases --- ...et-EntraApplicationKeyCredential.Tests.ps1 | 64 ++++++++ .../Entra/Get-EntraApplicationOwner.Tests.ps1 | 54 +++++++ ...traApplicationPasswordCredential.Tests.ps1 | 67 +++++++++ .../Get-EntraDeletedApplication.Tests.ps1 | 140 ++++++++++++++++++ .../Get-EntraDeletedDirectoryObject.Tests.ps1 | 66 +++++++++ ...EntraPermissionGrantConditionSet.Tests.ps1 | 77 ++++++++++ ...-EntraServicePrincipalMembership.Tests.ps1 | 86 +++++++++++ ...cePrincipalOAuth2PermissionGrant.tests.ps1 | 90 +++++++++++ .../Get-EntraServicePrincipalOwner.Tests.ps1 | 110 ++++++++++++++ ...ntraApplicationExtensionProperty.Tests.ps1 | 3 - .../New-EntraApplicationPassword.Tests.ps1 | 90 +++++++++++ ...traApplicationPasswordCredential.Tests.ps1 | 96 ++++++++++++ ...EntraPermissionGrantConditionSet.Tests.ps1 | 75 ++++++++++ ...ervicePrincipalAppRoleAssignment.Tests.ps1 | 99 +++++++++++++ .../Remove-EntraApplicationOwner.Tests.ps1 | 60 ++++++++ .../Remove-EntraApplicationPassword.Tests.ps1 | 60 ++++++++ ...traApplicationPasswordCredential.Tests.ps1 | 63 ++++++++ ...EntraPermissionGrantConditionSet.Tests.ps1 | 67 +++++++++ 18 files changed, 1364 insertions(+), 3 deletions(-) create mode 100644 test/module/Entra/Get-EntraApplicationKeyCredential.Tests.ps1 create mode 100644 test/module/Entra/Get-EntraApplicationOwner.Tests.ps1 create mode 100644 test/module/Entra/Get-EntraApplicationPasswordCredential.Tests.ps1 create mode 100644 test/module/Entra/Get-EntraDeletedApplication.Tests.ps1 create mode 100644 test/module/Entra/Get-EntraDeletedDirectoryObject.Tests.ps1 create mode 100644 test/module/Entra/Get-EntraPermissionGrantConditionSet.Tests.ps1 create mode 100644 test/module/Entra/Get-EntraServicePrincipalMembership.Tests.ps1 create mode 100644 test/module/Entra/Get-EntraServicePrincipalOAuth2PermissionGrant.tests.ps1 create mode 100644 test/module/Entra/Get-EntraServicePrincipalOwner.Tests.ps1 create mode 100644 test/module/Entra/New-EntraApplicationPassword.Tests.ps1 create mode 100644 test/module/Entra/New-EntraApplicationPasswordCredential.Tests.ps1 create mode 100644 test/module/Entra/New-EntraPermissionGrantConditionSet.Tests.ps1 create mode 100644 test/module/Entra/New-EntraServicePrincipalAppRoleAssignment.Tests.ps1 create mode 100644 test/module/Entra/Remove-EntraApplicationOwner.Tests.ps1 create mode 100644 test/module/Entra/Remove-EntraApplicationPassword.Tests.ps1 create mode 100644 test/module/Entra/Remove-EntraApplicationPasswordCredential.Tests.ps1 create mode 100644 test/module/Entra/Set-EntraPermissionGrantConditionSet.Tests.ps1 diff --git a/test/module/Entra/Get-EntraApplicationKeyCredential.Tests.ps1 b/test/module/Entra/Get-EntraApplicationKeyCredential.Tests.ps1 new file mode 100644 index 000000000..b52c3afd9 --- /dev/null +++ b/test/module/Entra/Get-EntraApplicationKeyCredential.Tests.ps1 @@ -0,0 +1,64 @@ +# ------------------------------------------------------------------------------ +# 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 = { + @{ + "KeyCredentials" = @( + @{ + "CustomKeyIdentifier" = "" + "EndDate" = "10/23/2024 11:36:56 AM" + "KeyId" = "aaaaaaaa-0b0b-1c1c-2d2d-333333333333" + "StartDate" = "11/22/2023 11:35:16 AM" + "Type" = "Symmetric" + "Usage" = "Sign" + "Value" = "" + "Parameters" = $args + } + ) + } + } + + Mock -CommandName Get-MgApplication -MockWith $scriptblock -ModuleName Microsoft.Graph.Entra + } + + Describe "Get-EntraApplicationKeyCredential" { + Context "Test for Get-EntraApplicationKeyCredential" { + It "Should not return empty" { + $result = Get-EntraApplicationKeyCredential -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + $result | Should -Not -BeNullOrEmpty + Should -Invoke -CommandName Get-MgApplication -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Should fail when ObjectId is empty" { + { Get-EntraApplicationKeyCredential -ObjectId "" } | Should -Throw "Cannot bind argument to parameter 'ObjectId' because it is an empty string." + } + It "Should contain 'User-Agent' header" { + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraApplicationKeyCredential" + $result = Get-EntraApplicationKeyCredential -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + $result | Should -Not -BeNullOrEmpty + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraApplicationKeyCredential" + Should -Invoke -CommandName Get-MgApplication -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 + { Get-EntraApplicationKeyCredential -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -Debug } | Should -Not -Throw + } finally { + # Restore original confirmation preference + $DebugPreference = $originalDebugPreference + } + } + } + } \ No newline at end of file diff --git a/test/module/Entra/Get-EntraApplicationOwner.Tests.ps1 b/test/module/Entra/Get-EntraApplicationOwner.Tests.ps1 new file mode 100644 index 000000000..e1e0cbbcc --- /dev/null +++ b/test/module/Entra/Get-EntraApplicationOwner.Tests.ps1 @@ -0,0 +1,54 @@ +# ------------------------------------------------------------------------------ +# 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 + + $mockResponse = { + return @{ + value = @( + @{ + Id = "e3108c4d-86ff-4ceb-9429-24e85b4b8cea" + ageGroup = $null + onPremisesLastSyncDateTime = $null + creationType = $null + imAddresses = {"adelev@m365x99297270.onmicrosoft.com"} + preferredLanguage = $null + mail = "AdeleV@M365x99297270.OnMicrosoft.com" + securityIdentifier = "S-1-12-1-1093396945-1080104032-2731339150-364051459" + identities = {} + consentProvidedForMinor = $null + onPremisesUserPrincipalName = $null + Parameters = $args + } + ) + } + } + + Mock -CommandName Invoke-GraphRequest -MockWith $mockResponse -ModuleName Microsoft.Graph.Entra +} + +Describe "Get-EntraApplicationOwner"{ + Context "Test for Get-EntraApplicationOwner"{ + It "Should return application owner" { + $result = Get-EntraApplicationOwner -ObjectId "e3108c4d-86ff-4ceb-9429-24e85b4b8cea" + $result | Should -Not -BeNullOrEmpty + $result.Id | should -Be @('e3108c4d-86ff-4ceb-9429-24e85b4b8cea') + + Should -Invoke -CommandName Invoke-GraphRequest -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Should fail when ObjectId is empty" { + { Get-EntraApplicationOwner -ObjectId "" } | Should -Throw "Cannot bind argument to parameter 'ObjectId' because it is an empty string." + } + It "Should contain 'User-Agent' header" { + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraApplicationOwner" + $result = Get-EntraApplicationOwner -ObjectId "e3108c4d-86ff-4ceb-9429-24e85b4b8cea" + $params = Get-Parameters -data $result.Parameters + $a= $params | ConvertTo-json | ConvertFrom-Json + $a.headers.'User-Agent' | Should -Be $userAgentHeaderValue + } + } +} \ No newline at end of file diff --git a/test/module/Entra/Get-EntraApplicationPasswordCredential.Tests.ps1 b/test/module/Entra/Get-EntraApplicationPasswordCredential.Tests.ps1 new file mode 100644 index 000000000..0ca03b400 --- /dev/null +++ b/test/module/Entra/Get-EntraApplicationPasswordCredential.Tests.ps1 @@ -0,0 +1,67 @@ +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 = { + @{ + "PasswordCredentials" = @( + @{ + "CustomKeyIdentifier" = {116, 101, 115, 116} + "DisplayName" = "" + "EndDateTime" = "10/23/2024 11:36:56 AM" + "KeyId" = "aaaaaaaa-0b0b-1c1c-2d2d-333333333333" + "StartDateTime" = "11/22/2023 11:35:16 AM" + "Hint" = "123" + "SecretText" = "" + "Parameters" = $args + } + ) + } + } + + Mock -CommandName Get-MgApplication -MockWith $scriptblock -ModuleName Microsoft.Graph.Entra + } + + Describe "Get-EntraApplicationPasswordCredential" { + Context "Test for Get-EntraApplicationPasswordCredential" { + It "Should not return empty" { + $result = Get-EntraApplicationPasswordCredential -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + $result | Should -Not -BeNullOrEmpty + Should -Invoke -CommandName Get-MgApplication -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Should fail when ObjectId is empty" { + { Get-EntraApplicationPasswordCredential -ObjectId "" } | Should -Throw "Cannot bind argument to parameter 'ObjectId'*" + } + It "Should fail when ObjectId is null" { + { Get-EntraApplicationPasswordCredential -ObjectId } | Should -Throw "Missing an argument for parameter 'ObjectId'*" + } + It "Should fail when invalid parameter is passed" { + { Get-EntraApplicationPasswordCredential -DisplayName "abc" } | Should -Throw "A parameter cannot be found that matches parameter name 'DisplayName'*" + } + It "Should contain 'User-Agent' header" { + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraApplicationPasswordCredential" + $result = Get-EntraApplicationPasswordCredential -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + $result | Should -Not -BeNullOrEmpty + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraApplicationPasswordCredential" + Should -Invoke -CommandName Get-MgApplication -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 + { Get-EntraApplicationPasswordCredential -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb"-Debug } | Should -Not -Throw + } finally { + # Restore original confirmation preference + $DebugPreference = $originalDebugPreference + } + } + } + } \ No newline at end of file diff --git a/test/module/Entra/Get-EntraDeletedApplication.Tests.ps1 b/test/module/Entra/Get-EntraDeletedApplication.Tests.ps1 new file mode 100644 index 000000000..282f27169 --- /dev/null +++ b/test/module/Entra/Get-EntraDeletedApplication.Tests.ps1 @@ -0,0 +1,140 @@ +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 = { + return @( + [PSCustomObject]@{ + "AddIns" = {} + "AppRoles" = {} + "GroupMembershipClaims" = {} + "IdentifierUris" = {} + "Info" = @{ + LogoUrl=""; + MarketingUrl=""; + PrivacyStatementUrl=""; + SupportUrl=""; + TermsOfServiceUrl=""; + } + "IsDeviceOnlyAuthSupported" = $null + "KeyCredentials" = {} + "OptionalClaims" = {} + "ParentalControlSettings" = @{ + CountriesBlockedForMinors=@{}; + LegalAgeGroupRule="Allow"; + } + "PasswordCredentials" = {} + "Api" = @{ + AcceptMappedClaims=@{}; + KnownClientApplications=@{}; + Oauth2PermissionScopes=@{}; + PreAuthorizedApplications=@{}; + RequestedAccessTokenVersion="2"; + } + "PublicClient" = @{ + RedirectUris=@{}; + } + "PublisherDomain" = "M365x99297270.onmicrosoft.com" + "Web" = @{ + HomePageUrl=""; + ImplicitGrantSettings=""; + LogoutUrl=""; + RedirectUriSettings=@{}; + RedirectUris=@{}; + } + "RequiredResourceAccess" = $null + "AppId" = "9c17362d-20b6-4572-bb6f-600e57c840e5" + "AppManagementPolicies" = $null + "ApplicationTemplateId" = $null + "Certification" = {} + "CreatedDateTime" = "12/12/2023 11:28:45 AM" + "CreatedOnBehalfOf" = {} + "DefaultRedirectUri" = $null + "DeletedDateTime" = "12/12/2023 12:23:39 PM" + "Description" = $null + "DisabledByMicrosoftStatus" = $null + "DisplayName" = "Mock-test-App" + "ExtensionProperties" = $null + "FederatedIdentityCredentials" = $null + "HomeRealmDiscoveryPolicies" = $null + "Id" = "01157307-373c-47b0-889a-3bc57033d73e" + "IsFallbackPublicClient" = $null + "Logo" = $null + "Notes" = $null + "Oauth2RequirePostResponse" = $null + "Owners" = $null + "RequestSignatureVerification" = {} + "SamlMetadataUrl" = $null + "ServiceManagementReference" = $null + "ServicePrincipalLockConfiguration" = {} + "SignInAudience" = "AzureADandPersonalMicrosoftAccount" + "Spa" = {} + "Synchronization" = {} + "Tags" = {} + "TokenEncryptionKeyId" = $null + "TokenIssuancePolicies" = $null + "TokenLifetimePolicies" = $null + "UniqueName" = $null + "VerifiedPublisher" = {} + "AdditionalProperties" = {} + "Parameters" = $args + } + ) + } + + Mock -CommandName Get-MgDirectoryDeletedItemAsApplication -MockWith $scriptblock -ModuleName Microsoft.Graph.Entra +} + +Describe "Get-EntraDeletedApplication" { + Context "Test for Get-EntraDeletedApplication" { + It "Should return all applications" { + $result = Get-EntraDeletedApplication | ConvertTo-Json | ConvertFrom-Json + $result | Should -Not -BeNullOrEmpty + Should -Invoke -CommandName Get-MgDirectoryDeletedItemAsApplication -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Should fail when All is empty" { + { Get-EntraDeletedApplication -All } | Should -Throw "Missing an argument for parameter 'All'*" + } + It "Should fail when invalid parameter is passed" { + { Get-EntraDeletedApplication -DisplayName "abc" } | Should -Throw "A parameter cannot be found that matches parameter name 'DisplayName'*" + } + It "Should return specific application by searchstring" { + $result = Get-EntraDeletedApplication -SearchString 'Mock-test-App' | ConvertTo-Json | ConvertFrom-Json + $result | Should -Not -BeNullOrEmpty + $result.DisplayName | should -Be 'Mock-test-App' + + Should -Invoke -CommandName Get-MgDirectoryDeletedItemAsApplication -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Should return specific application by filter" { + $result = Get-EntraDeletedApplication -Filter "DisplayName -eq 'Mock-test-App'" | ConvertTo-Json | ConvertFrom-Json + $result | Should -Not -BeNullOrEmpty + $result.DisplayName | should -Be 'Mock-test-App' + + Should -Invoke -CommandName Get-MgDirectoryDeletedItemAsApplication -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Should return top application" { + $result = Get-EntraDeletedApplication -Top 1 | ConvertTo-Json | ConvertFrom-Json + $result | Should -Not -BeNullOrEmpty + + Should -Invoke -CommandName Get-MgDirectoryDeletedItemAsApplication -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Result should Contain ObjectId" { + $result = Get-EntraDeletedApplication -Filter "DisplayName -eq 'Mock-test-App'" | ConvertTo-Json | ConvertFrom-Json + $result.ObjectId | should -Be "01157307-373c-47b0-889a-3bc57033d73e" + } + It "Should contain Filter in parameters when passed SearchString to it" { + $result = Get-EntraDeletedApplication -SearchString 'Mock-test-App' | ConvertTo-Json | ConvertFrom-Json + $params = Get-Parameters -data $result.Parameters + $params.Filter | Should -Match "Mock-test-App" + } + It "Should contain 'User-Agent' header" { + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraDeletedApplication" + $result = Get-EntraDeletedApplication -Top 1 | ConvertTo-Json | ConvertFrom-Json + $params = Get-Parameters -data $result.Parameters + $a= $params | ConvertTo-json | ConvertFrom-Json + $a.headers.'User-Agent' | Should -Be $userAgentHeaderValue + } + } +} \ No newline at end of file diff --git a/test/module/Entra/Get-EntraDeletedDirectoryObject.Tests.ps1 b/test/module/Entra/Get-EntraDeletedDirectoryObject.Tests.ps1 new file mode 100644 index 000000000..767d92c46 --- /dev/null +++ b/test/module/Entra/Get-EntraDeletedDirectoryObject.Tests.ps1 @@ -0,0 +1,66 @@ +# ------------------------------------------------------------------------------ +# 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 = { + return @( + [PSCustomObject]@{ + "Id" = "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + "AdditionalProperties" = @{DisplayName="Test-App";} + "DeletedDateTime" = "2/2/2024 5:33:56 AM" + "Parameters" = $args + } + ) + } + + Mock -CommandName Get-MgDirectoryDeletedItem -MockWith $scriptblock -ModuleName Microsoft.Graph.Entra +} + +Describe "Get-EntraDeletedDirectoryObject"{ + It "Should fail when Id is empty" { + { Get-EntraDeletedDirectoryObject -Id "" } | Should -Throw "Cannot bind argument to parameter 'Id'*" + } + It "Should fail when Id is null" { + { Get-EntraDeletedDirectoryObject -Id } | Should -Throw "Missing an argument for parameter 'Id'*" + } + It "Should fail when invalid parameter is passed" { + { Get-EntraDeletedDirectoryObject -DisplayName "abc" } | Should -Throw "A parameter cannot be found that matches parameter name 'DisplayName'*" + } + It "Result should Contain ObjectId" { + $result = Get-EntraDeletedDirectoryObject -Id "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + $result.ObjectId | should -Be "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + } + It "Should contain DirectoryObjectId in parameters when passed Id to it" { + $result = Get-EntraDeletedDirectoryObject -Id "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + $params = Get-Parameters -data $result.Parameters + $params.DirectoryObjectId | Should -Be "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + } + It "Should contain 'User-Agent' header" { + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraDeletedDirectoryObject" + $result = Get-EntraDeletedDirectoryObject -Id "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + $result | Should -Not -BeNullOrEmpty + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraDeletedDirectoryObject" + Should -Invoke -CommandName Get-MgDirectoryDeletedItem -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 + { Get-EntraDeletedDirectoryObject -Id "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -Debug } | Should -Not -Throw + } finally { + # Restore original confirmation preference + $DebugPreference = $originalDebugPreference + } + } +} \ No newline at end of file diff --git a/test/module/Entra/Get-EntraPermissionGrantConditionSet.Tests.ps1 b/test/module/Entra/Get-EntraPermissionGrantConditionSet.Tests.ps1 new file mode 100644 index 000000000..fb1262047 --- /dev/null +++ b/test/module/Entra/Get-EntraPermissionGrantConditionSet.Tests.ps1 @@ -0,0 +1,77 @@ +# ------------------------------------------------------------------------------ +# 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 = { + return @( + [PSCustomObject]@{ + "Id" = "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + "ClientApplicationIds" = {"All"} + "ClientApplicationPublisherIds" = {"All"} + "ClientApplicationTenantIds" = {"All"} + "ClientApplicationsFromVerifiedPublisherOnly" = $true + "Parameters" = $args + } + ) + } + + Mock -CommandName Get-MgPolicyPermissionGrantPolicyInclude -MockWith $scriptblock -ModuleName Microsoft.Graph.Entra + + Mock -CommandName Get-MgPolicyPermissionGrantPolicyExclude -MockWith $scriptblock -ModuleName Microsoft.Graph.Entra +} +Describe "Get-EntraMSPermissionGrantConditionSet"{ + It "Should not return empty object for condition set 'includes'"{ + $result = Get-EntraPermissionGrantConditionSet -PolicyId "policy1" -ConditionSetType "includes" -Id "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + $result | Should -Not -BeNullOrEmpty + Should -Invoke -CommandName Get-MgPolicyPermissionGrantPolicyInclude -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Should not return empty object for condition set 'excludes'"{ + $result = Get-EntraPermissionGrantConditionSet -PolicyId "policy1" -ConditionSetType "excludes" -Id "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + $result | Should -Not -BeNullOrEmpty + Should -Invoke -CommandName Get-MgPolicyPermissionGrantPolicyExclude -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Should fail when parameters are empty" { + { Get-EntraPermissionGrantConditionSet -PolicyId "" -ConditionSetType "" -Id ""} | Should -Throw "Cannot bind argument to parameter*" + } + It "Should fail when parameters are null" { + { Get-EntraPermissionGrantConditionSet -PolicyId -ConditionSetType -Id} | Should -Throw "Missing an argument for parameter*" + } + It "Should contain PermissionGrantConditionSetId in parameters when passed Id to it" { + $result = Get-EntraPermissionGrantConditionSet -PolicyId "policy1" -ConditionSetType "includes" -Id "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + $params = Get-Parameters -data $result.Parameters + $params.PermissionGrantConditionSetId | Should -Be "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + } + It "Should contain PermissionGrantPolicyId in parameters when passed PolicyId to it" { + $result = Get-EntraPermissionGrantConditionSet -PolicyId "policy1" -ConditionSetType "includes" -Id "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + $params = Get-Parameters -data $result.Parameters + $params.PermissionGrantPolicyId | Should -Be "policy1" + } + It "Should contain 'User-Agent' header" { + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraPermissionGrantConditionSet" + $result = Get-EntraPermissionGrantConditionSet -PolicyId "policy1" -ConditionSetType "includes" -Id "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + $result | Should -Not -BeNullOrEmpty + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraPermissionGrantConditionSet" + Should -Invoke -CommandName Get-MgPolicyPermissionGrantPolicyInclude -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 + { Get-EntraPermissionGrantConditionSet -PolicyId "policy1" -ConditionSetType "includes" -Id "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -Debug } | Should -Not -Throw + } finally { + # Restore original confirmation preference + $DebugPreference = $originalDebugPreference + } + } +} \ No newline at end of file diff --git a/test/module/Entra/Get-EntraServicePrincipalMembership.Tests.ps1 b/test/module/Entra/Get-EntraServicePrincipalMembership.Tests.ps1 new file mode 100644 index 000000000..880f4ceb3 --- /dev/null +++ b/test/module/Entra/Get-EntraServicePrincipalMembership.Tests.ps1 @@ -0,0 +1,86 @@ +# ------------------------------------------------------------------------------ +# 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 = { + return @( + [PSCustomObject]@{ + "Id" = "00aa00aa-bb11-cc22-dd33-44ee44ee44ee" + "AdditionalProperties" = @{DeletedDateTime = $null} + "Parameters" = $args + } + ) + } + + Mock -CommandName Get-MgServicePrincipalTransitiveMemberOf -MockWith $scriptblock -ModuleName Microsoft.Graph.Entra +} +Describe "Get-EntraServicePrincipalMembership"{ + It "Result should not be empty" { + $result = Get-EntraServicePrincipalMembership -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + $result | Should -Not -BeNullOrEmpty + Should -Invoke -CommandName Get-MgServicePrincipalTransitiveMemberOf -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Should fail when ObjectId is empty" { + { Get-EntraServicePrincipalMembership -ObjectId "" } | Should -Throw "Cannot bind argument to parameter 'ObjectId' because it is an empty string." + } + It "Should return all applications" { + $result = Get-EntraServicePrincipalMembership -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -All + $result | Should -Not -BeNullOrEmpty + Should -Invoke -CommandName Get-MgServicePrincipalTransitiveMemberOf -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Should fail when All has an argument" { + { Get-EntraServicePrincipalMembership -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -All $true} | Should -Throw "A positional parameter cannot be found that accepts argument 'True'." + } + It "Should return top application" { + $result = Get-EntraServicePrincipalMembership -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -Top 1 + $result | Should -Not -BeNullOrEmpty + Should -Invoke -CommandName Get-MgServicePrincipalTransitiveMemberOf -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Result should Contain ObjectId" { + $result = Get-EntraServicePrincipalMembership -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-EntraServicePrincipalMembership -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + $params = Get-Parameters -data $result.Parameters + $params.ServicePrincipalId | Should -Be "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + } + It "Property parameter should work" { + $result = Get-EntraServicePrincipalMembership -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-MgServicePrincipalTransitiveMemberOf -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Should fail when Property is empty" { + { Get-EntraServicePrincipalMembership -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -Property } | Should -Throw "Missing an argument for parameter 'Property'*" + } + It "Should contain 'User-Agent' header" { + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraServicePrincipalMembership" + $result = Get-EntraServicePrincipalMembership -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + $result | Should -Not -BeNullOrEmpty + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraServicePrincipalMembership" + Should -Invoke -CommandName Get-MgServicePrincipalTransitiveMemberOf -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 + { Get-EntraServicePrincipalMembership -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -Debug } | Should -Not -Throw + } finally { + # Restore original confirmation preference + $DebugPreference = $originalDebugPreference + } + } +} \ No newline at end of file diff --git a/test/module/Entra/Get-EntraServicePrincipalOAuth2PermissionGrant.tests.ps1 b/test/module/Entra/Get-EntraServicePrincipalOAuth2PermissionGrant.tests.ps1 new file mode 100644 index 000000000..12b687bb3 --- /dev/null +++ b/test/module/Entra/Get-EntraServicePrincipalOAuth2PermissionGrant.tests.ps1 @@ -0,0 +1,90 @@ +# ------------------------------------------------------------------------------ +# 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 = { + return @( + [PSCustomObject]@{ + "Id" = "aaaaaaaa-0b0b-1c1c-2d2d-333333333333" + "ClientId" = "4773e0f6-b400-40b3-8508-340de8ee0893" + "ConsentType" = "AllPrincipals" + "PrincipalId" = "aaaaaaaa-bbbb-cccc-1111-222222222222" + "ResourceId" = "a0a0a0a0-bbbb-cccc-dddd-e1e1e1e1e1e1" + "Scope" = "openid" + "Parameters" = $args + } + ) + } + + Mock -CommandName Get-MgServicePrincipalOauth2PermissionGrant -MockWith $scriptblock -ModuleName Microsoft.Graph.Entra +} +Describe "Get-EntraServicePrincipalOAuth2PermissionGrant"{ + It "Result should not be empty" { + $result = Get-EntraServicePrincipalOAuth2PermissionGrant -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + $result | Should -Not -BeNullOrEmpty + Should -Invoke -CommandName Get-MgServicePrincipalOauth2PermissionGrant -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Should fail when ObjectId is empty" { + { Get-EntraServicePrincipalOAuth2PermissionGrant -ObjectId "" } | Should -Throw "Cannot bind argument to parameter 'ObjectId' because it is an empty string." + } + It "Should return all applications" { + $result = Get-EntraServicePrincipalOAuth2PermissionGrant -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -All + $result | Should -Not -BeNullOrEmpty + Should -Invoke -CommandName Get-MgServicePrincipalOauth2PermissionGrant -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Should fail when All has an argument" { + { Get-EntraServicePrincipalOAuth2PermissionGrant -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -All $true} | Should -Throw "A positional parameter cannot be found that accepts argument 'True'." + } + It "Should return top application" { + $result = Get-EntraServicePrincipalOAuth2PermissionGrant -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -Top 1 + $result | Should -Not -BeNullOrEmpty + Should -Invoke -CommandName Get-MgServicePrincipalOauth2PermissionGrant -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Result should Contain ObjectId" { + $result = Get-EntraServicePrincipalOAuth2PermissionGrant -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + $result.ObjectId | should -Be "aaaaaaaa-0b0b-1c1c-2d2d-333333333333" + } + It "Should contain ServicePrincipalId in parameters when passed ObjectId to it" { + $result = Get-EntraServicePrincipalOAuth2PermissionGrant -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + $params = Get-Parameters -data $result.Parameters + $params.ServicePrincipalId | Should -Be "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + } + It "Property parameter should work" { + $result = Get-EntraServicePrincipalOAuth2PermissionGrant -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -Property Id + $result | Should -Not -BeNullOrEmpty + $result.Id | Should -Be 'aaaaaaaa-0b0b-1c1c-2d2d-333333333333' + + Should -Invoke -CommandName Get-MgServicePrincipalOauth2PermissionGrant -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Should fail when Property is empty" { + { Get-EntraServicePrincipalOAuth2PermissionGrant -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -Property } | Should -Throw "Missing an argument for parameter 'Property'*" + } + It "Should contain 'User-Agent' header" { + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraServicePrincipalOAuth2PermissionGrant" + $result = Get-EntraServicePrincipalOAuth2PermissionGrant -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + $result | Should -Not -BeNullOrEmpty + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraServicePrincipalOAuth2PermissionGrant" + Should -Invoke -CommandName Get-MgServicePrincipalOauth2PermissionGrant -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 + { Get-EntraServicePrincipalOAuth2PermissionGrant -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -Debug } | Should -Not -Throw + } finally { + # Restore original confirmation preference + $DebugPreference = $originalDebugPreference + } + } +} \ No newline at end of file diff --git a/test/module/Entra/Get-EntraServicePrincipalOwner.Tests.ps1 b/test/module/Entra/Get-EntraServicePrincipalOwner.Tests.ps1 new file mode 100644 index 000000000..d80d1a2f3 --- /dev/null +++ b/test/module/Entra/Get-EntraServicePrincipalOwner.Tests.ps1 @@ -0,0 +1,110 @@ +# ------------------------------------------------------------------------------ +# 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 = { + return @( + [PSCustomObject]@{ + "Id" = "00aa00aa-bb11-cc22-dd33-44ee44ee44ee" + "DisplayName" = "Adams Smith" + "UserPrincipalName" = "Adams@contoso.com" + "UserType" = "Member" + "appRoles" = @{ + allowedMemberTypes=$null; + description="msiam_access"; + displayName="msiam_access"; + id="d0d7e4e4-96be-41c9-805a-08e0526868ad"; + isEnabled=$True; + origin="Application" + } + "oauth2PermissionScopes" = @{ + adminConsentDescription="Allow the application to access from tmplate test 3 on behalf of the signed-in user."; + adminConsentDisplayName="Access from tmplate test 3"; + id="64c2cef3-e118-4795-a580-a32bdbd7ba88"; + isEnabled=$True; + type="User"; + userConsentDescription="Allow the application to access from tmplate test 3 on your behalf."; + userConsentDisplayName="Access from tmplate test 3"; + value="user_impersonation" + } + "AdditionalProperties" = @{ + "@odata.type" = "#microsoft.graph.servicePrincipal"; + accountEnabled = $true + } + "Parameters" = $args + } + ) + } + + Mock -CommandName Get-MgServicePrincipalOwner -MockWith $scriptblock -ModuleName Microsoft.Graph.Entra +} +Describe "Get-EntraServicePrincipalOwner"{ + It "Result should not be empty" { + $result = Get-EntraServicePrincipalOwner -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + $result | Should -Not -BeNullOrEmpty + Should -Invoke -CommandName Get-MgServicePrincipalOwner -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Should fail when ObjectId is empty" { + { Get-EntraServicePrincipalOwner -ObjectId "" } | Should -Throw "Cannot bind argument to parameter 'ObjectId' because it is an empty string." + } + It "Should return all applications" { + $result = Get-EntraServicePrincipalOwner -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -All + $result | Should -Not -BeNullOrEmpty + Should -Invoke -CommandName Get-MgServicePrincipalOwner -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Should fail when All has an argument" { + { Get-EntraServicePrincipalOwner -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -All $true} | Should -Throw "A positional parameter cannot be found that accepts argument 'True'." + } + It "Should return top application" { + $result = Get-EntraServicePrincipalOwner -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -Top 1 + $result | Should -Not -BeNullOrEmpty + Should -Invoke -CommandName Get-MgServicePrincipalOwner -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Result should Contain ObjectId" { + $result = Get-EntraServicePrincipalOwner -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-EntraServicePrincipalOwner -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + $params = Get-Parameters -data $result.Parameters + $params.ServicePrincipalId | Should -Be "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + } + It "Property parameter should work" { + $result = Get-EntraServicePrincipalOwner -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -Property DisplayName + $result | Should -Not -BeNullOrEmpty + $result.DisplayName | Should -Be 'Adams Smith' + + Should -Invoke -CommandName Get-MgServicePrincipalOwner -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Should fail when Property is empty" { + { Get-EntraServicePrincipalOwner -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -Property } | Should -Throw "Missing an argument for parameter 'Property'*" + } + It "Should contain 'User-Agent' header" { + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraServicePrincipalOwner" + $result = Get-EntraServicePrincipalOwner -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + $result | Should -Not -BeNullOrEmpty + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraServicePrincipalOwner" + Should -Invoke -CommandName Get-MgServicePrincipalOwner -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 + {Get-EntraServicePrincipalOwner -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -Debug } | Should -Not -Throw + } finally { + # Restore original confirmation preference + $DebugPreference = $originalDebugPreference + } + } +} \ No newline at end of file diff --git a/test/module/Entra/New-EntraApplicationExtensionProperty.Tests.ps1 b/test/module/Entra/New-EntraApplicationExtensionProperty.Tests.ps1 index ce8ccc529..6d0cdbf7e 100644 --- a/test/module/Entra/New-EntraApplicationExtensionProperty.Tests.ps1 +++ b/test/module/Entra/New-EntraApplicationExtensionProperty.Tests.ps1 @@ -66,12 +66,9 @@ Context "Test for New-EntraApplicationExtensionProperty" { } It "Should contain 'User-Agent' header" { $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion New-EntraApplicationExtensionProperty" - $result = New-EntraApplicationExtensionProperty -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -DataType "MockType" -Name "Mock-App" -TargetObjects "Application" $result | Should -Not -BeNullOrEmpty - $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion New-EntraApplicationExtensionProperty" - Should -Invoke -CommandName New-MgApplicationExtensionProperty -ModuleName Microsoft.Graph.Entra -Times 1 -ParameterFilter { $Headers.'User-Agent' | Should -Be $userAgentHeaderValue $true diff --git a/test/module/Entra/New-EntraApplicationPassword.Tests.ps1 b/test/module/Entra/New-EntraApplicationPassword.Tests.ps1 new file mode 100644 index 000000000..5495c2e9e --- /dev/null +++ b/test/module/Entra/New-EntraApplicationPassword.Tests.ps1 @@ -0,0 +1,90 @@ +# ------------------------------------------------------------------------------ +# 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 = { + return @( + [PSCustomObject]@{ + "CustomKeyIdentifier" = $null + "DisplayName" = "mypassword" + "EndDateTime" = "10/23/2024 11:36:56 AM" + "KeyId" = "aaaaaaaa-0b0b-1c1c-2d2d-333333333333" + "StartDateTime" = "11/22/2023 11:35:16 AM" + "Hint" = "123" + "SecretText" = "wbBNW8kCuiPjNRg9NX98W_EaU6cqG" + "Parameters" = $args + } + ) + } + + Mock -CommandName Add-MgApplicationPassword -MockWith $scriptblock -ModuleName Microsoft.Graph.Entra +} +Describe "New-EntraApplicationPassword"{ + It "Should return created password credential"{ + $result = New-EntraApplicationPassword -ObjectID "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -PasswordCredential @{ displayname = "mypassword" } | ConvertTo-Json | ConvertFrom-Json + $result | Should -Not -BeNullOrEmpty + $result.KeyId | should -Be "aaaaaaaa-0b0b-1c1c-2d2d-333333333333" + $result.SecretText | Should -Be "wbBNW8kCuiPjNRg9NX98W_EaU6cqG" + Should -Invoke -CommandName Add-MgApplicationPassword -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Should fail when ObjectId is empty" { + { New-EntraApplicationPassword -ObjectId "" } | Should -Throw "Cannot bind argument to parameter 'ObjectId'*" + } + It "Should fail when ObjectId is null" { + { New-EntraApplicationPassword -ObjectId } | Should -Throw "Missing an argument for parameter 'ObjectId'*" + } + It "Should fail when PasswordCredential is null" { + { New-EntraApplicationPassword -PasswordCredential } | Should -Throw "Missing an argument for parameter 'PasswordCredential'*" + } + It "Should fail when StartDate is empty" { + { New-EntraApplicationPassword -ObjectID "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -PasswordCredential @{ StartDateTime = "" } } | Should -Throw "Cannot process argument transformation on parameter 'PasswordCredential'*" + } + It "Should fail when EndDate is empty" { + { New-EntraApplicationPassword -ObjectID "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -PasswordCredential @{ EndDateTime = "" } } | Should -Throw "Cannot process argument transformation on parameter 'PasswordCredential'*" + } + It "Should fail when invalid parameter is passed" { + { New-EntraApplicationPassword -DisplayName "abc" } | Should -Throw "A parameter cannot be found that matches parameter name 'DisplayName'." + } + It "Should contain ApplicationId in parameters when passed ObjectId to it" { + $result = New-EntraApplicationPassword -ObjectID "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -PasswordCredential @{ displayname = "mypassword" } + $params = Get-Parameters -data $result.Parameters + $params.ApplicationId | Should -Be "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + } + It "should contain password credential parameters in body parameter when passed PasswordCredential to it"{ + $result = New-EntraApplicationPassword -ObjectID "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -PasswordCredential @{ DisplayName = "mypassword"; Hint = "123"; StartDateTime=(get-date).AddYears(0); EndDateTime=(get-date).AddYears(2) } + $params = Get-Parameters -data $result.Parameters + $a = $params.PasswordCredential | ConvertTo-json | ConvertFrom-Json + $a.DisplayName | Should -Be "mypassword" + $a.Hint | Should -Be "123" + $a.StartDateTime | Should -Not -BeNullOrEmpty + $a.EndDateTime | Should -Not -BeNullOrEmpty + } + It "Should contain 'User-Agent' header" { + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion New-EntraApplicationPassword" + $result = New-EntraApplicationPassword -ObjectID "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -PasswordCredential @{ displayname = "mypassword" } | ConvertTo-Json | ConvertFrom-Json + $result | Should -Not -BeNullOrEmpty + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion New-EntraApplicationPassword" + Should -Invoke -CommandName Add-MgApplicationPassword -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-EntraApplicationPassword -ObjectID "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -PasswordCredential @{ displayname = "mypassword" } | ConvertTo-Json | ConvertFrom-Json -Debug } | Should -Not -Throw + } finally { + # Restore original confirmation preference + $DebugPreference = $originalDebugPreference + } + } +} \ No newline at end of file diff --git a/test/module/Entra/New-EntraApplicationPasswordCredential.Tests.ps1 b/test/module/Entra/New-EntraApplicationPasswordCredential.Tests.ps1 new file mode 100644 index 000000000..a83231fd8 --- /dev/null +++ b/test/module/Entra/New-EntraApplicationPasswordCredential.Tests.ps1 @@ -0,0 +1,96 @@ +# ------------------------------------------------------------------------------ +# 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 = { + return @( + [PSCustomObject]@{ + "CustomKeyIdentifier" = $null + "DisplayName" = "" + "EndDateTime" = "10/23/2024 11:36:56 AM" + "KeyId" = "aaaaaaaa-0b0b-1c1c-2d2d-333333333333" + "StartDateTime" = "11/22/2023 11:35:16 AM" + "Hint" = "123" + "SecretText" = "wbBNW8kCuiPjNRg9NX98W_EaU6cqG" + "Parameters" = $args + } + ) + } + + Mock -CommandName Add-MgApplicationPassword -MockWith $scriptblock -ModuleName Microsoft.Graph.Entra +} +Describe "New-EntraApplicationPasswordCredential"{ + It "Should return created password credential"{ + $result = New-EntraApplicationPasswordCredential -ObjectID "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + $result | Should -Not -BeNullOrEmpty + $result.KeyId | should -Be "aaaaaaaa-0b0b-1c1c-2d2d-333333333333" + $result.SecretText | Should -Be "wbBNW8kCuiPjNRg9NX98W_EaU6cqG" + Should -Invoke -CommandName Add-MgApplicationPassword -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Should fail when ObjectId is empty" { + { New-EntraApplicationPasswordCredential -ObjectId "" } | Should -Throw "Cannot bind argument to parameter 'ObjectId'*" + } + It "Should fail when ObjectId is null" { + { New-EntraApplicationPasswordCredential -ObjectId } | Should -Throw "Missing an argument for parameter 'ObjectId'*" + } + It "Should fail when StartDate is empty" { + { New-EntraApplicationPasswordCredential -ObjectID "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -StartDate "" } | Should -Throw "Cannot process argument transformation on parameter 'StartDate'*" + } + It "Should fail when StartDate is null" { + { New-EntraApplicationPasswordCredential -ObjectID "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -StartDate } | Should -Throw "Missing an argument for parameter 'StartDate'*" + } + It "Should fail when EndDate is empty" { + { New-EntraApplicationPasswordCredential -ObjectID "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -EndDate "" } | Should -Throw "Cannot process argument transformation on parameter 'EndDate'*" + } + It "Should fail when EndDate is null" { + { New-EntraApplicationPasswordCredential -ObjectID "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -EndDate } | Should -Throw "Missing an argument for parameter 'EndDate'*" + } + It "Should fail when invalid parameter is passed" { + { New-EntraApplicationPasswordCredential -DisplayName "abc" } | Should -Throw "A parameter cannot be found that matches parameter name 'DisplayName'." + } + It "Should contain ApplicationId in parameters when passed ObjectId to it" { + $result = New-EntraApplicationPasswordCredential -ObjectID "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + $params = Get-Parameters -data $result.Parameters + $params.ApplicationId | Should -Be "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + } + It "should contain startDateTime in body parameter when passed StartDate to it"{ + $result = New-EntraApplicationPasswordCredential -ObjectID "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -StartDate (get-date).AddYears(0) + $params = Get-Parameters -data $result.Parameters + $a = $params.PasswordCredential | ConvertTo-json | ConvertFrom-Json + $a.startDateTime | Should -Not -BeNullOrEmpty + } + It "should contain endDateTime in body parameter when passed EndDate to it"{ + $result = New-EntraApplicationPasswordCredential -ObjectID "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -EndDate (get-date).AddYears(0) + $params = Get-Parameters -data $result.Parameters + $a = $params.PasswordCredential | ConvertTo-json | ConvertFrom-Json + $a.endDateTime | Should -Not -BeNullOrEmpty + } + It "Should contain 'User-Agent' header" { + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion New-EntraApplicationPasswordCredential" + $result = New-EntraApplicationPasswordCredential -ObjectID "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + $result | Should -Not -BeNullOrEmpty + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion New-EntraApplicationPasswordCredential" + Should -Invoke -CommandName Add-MgApplicationPassword -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-EntraApplicationPasswordCredential -ObjectID "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -Debug } | Should -Not -Throw + } finally { + # Restore original confirmation preference + $DebugPreference = $originalDebugPreference + } + } +} \ No newline at end of file diff --git a/test/module/Entra/New-EntraPermissionGrantConditionSet.Tests.ps1 b/test/module/Entra/New-EntraPermissionGrantConditionSet.Tests.ps1 new file mode 100644 index 000000000..f992b4e5c --- /dev/null +++ b/test/module/Entra/New-EntraPermissionGrantConditionSet.Tests.ps1 @@ -0,0 +1,75 @@ +# ------------------------------------------------------------------------------ +# 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 = { + return @( + [PSCustomObject]@{ + "Id" = "00aa00aa-bb11-cc22-dd33-44ee44ee44ee" + "ClientApplicationIds" = {"All"} + "ClientApplicationPublisherIds" = {"All"} + "ClientApplicationTenantIds" = {"All"} + "ClientApplicationsFromVerifiedPublisherOnly" = $true + "PermissionClassification" = "all" + "PermissionType" = "delegated" + "Parameters" = $args + } + ) + } + + Mock -CommandName New-MgPolicyPermissionGrantPolicyInclude -MockWith $scriptblock -ModuleName Microsoft.Graph.Entra + + Mock -CommandName New-MgPolicyPermissionGrantPolicyExclude -MockWith $scriptblock -ModuleName Microsoft.Graph.Entra +} +Describe "New-EntraPermissionGrantConditionSet"{ + It "Should not return empty object for condition set 'includes'"{ + $result = New-EntraPermissionGrantConditionSet -PolicyId "test1" -ConditionSetType "includes" -PermissionType "delegated" + $result | Should -Not -BeNullOrEmpty + Should -Invoke -CommandName New-MgPolicyPermissionGrantPolicyInclude -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Should not return empty object for condition set 'excludes'"{ + $result = New-EntraPermissionGrantConditionSet -PolicyId "test1" -ConditionSetType "excludes" -PermissionType "delegated" + $result | Should -Not -BeNullOrEmpty + Should -Invoke -CommandName New-MgPolicyPermissionGrantPolicyExclude -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Should fail when parameters are empty" { + { New-EntraPermissionGrantConditionSet -PolicyId "" -ConditionSetType ""} | Should -Throw "Cannot bind argument to parameter*" + } + It "Should fail when parameters are null" { + { New-EntraPermissionGrantConditionSet -PolicyId -ConditionSetType } | Should -Throw "Missing an argument for parameter*" + } + It "Should contain PermissionGrantPolicyId in parameters when passed PolicyId to it" { + $result = New-EntraPermissionGrantConditionSet -PolicyId "test1" -ConditionSetType "includes" -PermissionType "delegated" + $params = Get-Parameters -data $result.Parameters + $params.PermissionGrantPolicyId | Should -Be "test1" + } + It "Should contain 'User-Agent' header" { + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion New-EntraPermissionGrantConditionSet" + $result = New-EntraPermissionGrantConditionSet -PolicyId "test1" -ConditionSetType "includes" -PermissionType "delegated" + $result | Should -Not -BeNullOrEmpty + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion New-EntraPermissionGrantConditionSet" + Should -Invoke -CommandName New-MgPolicyPermissionGrantPolicyInclude -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-EntraPermissionGrantConditionSet -PolicyId "test1" -ConditionSetType "includes" -PermissionType "delegated" -Debug } | Should -Not -Throw + } finally { + # Restore original confirmation preference + $DebugPreference = $originalDebugPreference + } + } + +} \ No newline at end of file diff --git a/test/module/Entra/New-EntraServicePrincipalAppRoleAssignment.Tests.ps1 b/test/module/Entra/New-EntraServicePrincipalAppRoleAssignment.Tests.ps1 new file mode 100644 index 000000000..1ea4e9273 --- /dev/null +++ b/test/module/Entra/New-EntraServicePrincipalAppRoleAssignment.Tests.ps1 @@ -0,0 +1,99 @@ +# ------------------------------------------------------------------------------ +# 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-MgServicePrincipalAppRoleAssignment with parameters: $($args | ConvertTo-Json -Depth 3)" + return @( + [PSCustomObject]@{ + "DeletedDateTime" = $null + "Id" = "gsx_zBushUevRyyjtwUohm_RMYjcGsJIjXwKOVMr3ww" + "PrincipalDisplayName" = "Mock-App" + "AppRoleId" = "e18f0405-fdec-4ae8-a8a0-d8edb98b061f" + "CreatedDateTime" = "3/12/2024 11:05:29 AM" + "PrincipalId" = "aaaaaaaa-bbbb-cccc-1111-222222222222" + "Parameters" = $args + } + ) + } + + Mock -CommandName New-MgServicePrincipalAppRoleAssignment -MockWith $scriptblock -ModuleName Microsoft.Graph.Entra +} + +Describe "New-EntraServicePrincipalAppRoleAssignment"{ + Context "Test for New-EntraServicePrincipalAppRoleAssignment" { + It "Should return New-EntraServicePrincipalAppRoleAssignment"{ + $result = New-EntraServicePrincipalAppRoleAssignment -ObjectId "02ed943d-6eca-4f99-83d6-e6fbf9dc63ae" -ResourceId "02ed943d-6eca-4f99-83d6-e6fbf9dc63ae" -Id "e18f0405-fdec-4ae8-a8a0-d8edb98b061f" -PrincipalId "d2d0a585-0c52-4bab-8c64-a096b98b061f" + $result | Should -Not -BeNullOrEmpty + $result.PrincipalDisplayName | should -Be "Mock-App" + $result.PrincipalId | should -Be "aaaaaaaa-bbbb-cccc-1111-222222222222" + + Should -Invoke -CommandName New-MgServicePrincipalAppRoleAssignment -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Should fail when ObjectId is empty" { + { New-EntraServicePrincipalAppRoleAssignment -ObjectId "" } | Should -Throw "Cannot bind argument to parameter 'ObjectId'*" + } + It "Should fail when ObjectId is null" { + { New-EntraServicePrincipalAppRoleAssignment -ObjectId } | Should -Throw "Missing an argument for parameter 'ObjectId'*" + } + It "Should fail when ResourceId is empty" { + { New-EntraServicePrincipalAppRoleAssignment -ResourceId "" } | Should -Throw "Cannot bind argument to parameter 'ResourceId'*" + } + It "Should fail when ResourceId is null" { + { New-EntraServicePrincipalAppRoleAssignment -ResourceId } | Should -Throw "Missing an argument for parameter 'ResourceId'*" + } + It "Should fail when Id is empty" { + { New-EntraServicePrincipalAppRoleAssignment -Id "" } | Should -Throw "Cannot bind argument to parameter 'Id'*" + } + It "Should fail when Id is null" { + { New-EntraServicePrincipalAppRoleAssignment -Id } | Should -Throw "Missing an argument for parameter 'Id'*" + } + It "Should fail when PrincipalId is empty" { + { New-EntraServicePrincipalAppRoleAssignment -PrincipalId "" } | Should -Throw "Cannot bind argument to parameter 'PrincipalId'*" + } + It "Should fail when PrincipalId is null" { + { New-EntraServicePrincipalAppRoleAssignment -PrincipalId } | Should -Throw "Missing an argument for parameter 'PrincipalId'*" + } + It "Should fail when invalid parameter is passed" { + { New-EntraServicePrincipalAppRoleAssignment -DisplayName "abc" } | Should -Throw "A parameter cannot be found that matches parameter name 'DisplayName'." + } + It "Should contain AppRoleId in parameters when passed Id to it" { + $result = New-EntraServicePrincipalAppRoleAssignment -ObjectId "02ed943d-6eca-4f99-83d6-e6fbf9dc63ae" -ResourceId "02ed943d-6eca-4f99-83d6-e6fbf9dc63ae" -Id "e18f0405-fdec-4ae8-a8a0-d8edb98b061f" -PrincipalId "d2d0a585-0c52-4bab-8c64-a096b98b061f" + $params = Get-Parameters -data $result.Parameters + $params.AppRoleId | Should -Be "e18f0405-fdec-4ae8-a8a0-d8edb98b061f" + } + It "Should contain ServicePrincipalId in parameters when passed ObjectId to it" { + $result = New-EntraServicePrincipalAppRoleAssignment -ObjectId "02ed943d-6eca-4f99-83d6-e6fbf9dc63ae" -ResourceId "02ed943d-6eca-4f99-83d6-e6fbf9dc63ae" -Id "e18f0405-fdec-4ae8-a8a0-d8edb98b061f" -PrincipalId "d2d0a585-0c52-4bab-8c64-a096b98b061f" + $params = Get-Parameters -data $result.Parameters + $params.ServicePrincipalId | Should -Be "02ed943d-6eca-4f99-83d6-e6fbf9dc63ae" + } + It "Should contain 'User-Agent' header" { + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion New-EntraServicePrincipalAppRoleAssignment" + $result = New-EntraServicePrincipalAppRoleAssignment -ObjectId "02ed943d-6eca-4f99-83d6-e6fbf9dc63ae" -ResourceId "02ed943d-6eca-4f99-83d6-e6fbf9dc63ae" -Id "e18f0405-fdec-4ae8-a8a0-d8edb98b061f" -PrincipalId "d2d0a585-0c52-4bab-8c64-a096b98b061f" + $result | Should -Not -BeNullOrEmpty + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion New-EntraServicePrincipalAppRoleAssignment" + Should -Invoke -CommandName New-MgServicePrincipalAppRoleAssignment -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-EntraServicePrincipalAppRoleAssignment -ObjectId "02ed943d-6eca-4f99-83d6-e6fbf9dc63ae" -ResourceId "02ed943d-6eca-4f99-83d6-e6fbf9dc63ae" -Id "e18f0405-fdec-4ae8-a8a0-d8edb98b061f" -PrincipalId "d2d0a585-0c52-4bab-8c64-a096b98b061f" -Debug } | Should -Not -Throw + } finally { + # Restore original confirmation preference + $DebugPreference = $originalDebugPreference + } + } + } +} \ No newline at end of file diff --git a/test/module/Entra/Remove-EntraApplicationOwner.Tests.ps1 b/test/module/Entra/Remove-EntraApplicationOwner.Tests.ps1 new file mode 100644 index 000000000..e09af7a59 --- /dev/null +++ b/test/module/Entra/Remove-EntraApplicationOwner.Tests.ps1 @@ -0,0 +1,60 @@ +# ------------------------------------------------------------------------------ +# 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-MgApplicationOwnerByRef -MockWith {} -ModuleName Microsoft.Graph.Entra +} + +Describe "Remove-EntraApplicationOwner"{ + It "Should return empty object" { + $result = Remove-EntraApplicationOwner -ObjectId "aaaaaaaa-bbbb-cccc-1111-222222222222" -OwnerId "bbbbbbbb-cccc-dddd-2222-333333333333" + $result | Should -BeNullOrEmpty + Should -Invoke -CommandName Remove-MgApplicationOwnerByRef -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Should fail when ObjectId is empty" { + { Remove-EntraApplicationOwner -ObjectId "" } + } + It "Should fail when -OwnerId is empty" { + { Remove-EntraApplicationOwner -OwnerId "" } + } + It "Should contain DeviceId in parameters" { + Mock -CommandName Remove-MgApplicationOwnerByRef -MockWith {$args} -ModuleName Microsoft.Graph.Entra + $result = Remove-EntraApplicationOwner -ObjectId "aaaaaaaa-bbbb-cccc-1111-222222222222" -OwnerId "bbbbbbbb-cccc-dddd-2222-333333333333" + $params = Get-Parameters -data $result + $params.ApplicationId | Should -Be "aaaaaaaa-bbbb-cccc-1111-222222222222" + } + It "Should contain DirectoryObjectId in parameters" { + Mock -CommandName Remove-MgApplicationOwnerByRef -MockWith {$args} -ModuleName Microsoft.Graph.Entra + $result = Remove-EntraApplicationOwner -ObjectId "aaaaaaaa-bbbb-cccc-1111-222222222222" -OwnerId "bbbbbbbb-cccc-dddd-2222-333333333333" + $params = Get-Parameters -data $result + $params.DirectoryObjectId | Should -Be "bbbbbbbb-cccc-dddd-2222-333333333333" + } + It "Should contain 'User-Agent' header" { + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Remove-EntraApplicationOwner" + $result = Remove-EntraApplicationOwner -ObjectId "aaaaaaaa-bbbb-cccc-1111-222222222222" -OwnerId "bbbbbbbb-cccc-dddd-2222-333333333333" + $result | Should -BeNullOrEmpty + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Remove-EntraApplicationOwner" + Should -Invoke -CommandName Remove-MgApplicationOwnerByRef -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 + { Remove-EntraApplicationOwner -ObjectId "aaaaaaaa-bbbb-cccc-1111-222222222222" -OwnerId "bbbbbbbb-cccc-dddd-2222-333333333333" -Debug } | Should -Not -Throw + } finally { + # Restore original confirmation preference + $DebugPreference = $originalDebugPreference + } + } +} \ No newline at end of file diff --git a/test/module/Entra/Remove-EntraApplicationPassword.Tests.ps1 b/test/module/Entra/Remove-EntraApplicationPassword.Tests.ps1 new file mode 100644 index 000000000..370204e05 --- /dev/null +++ b/test/module/Entra/Remove-EntraApplicationPassword.Tests.ps1 @@ -0,0 +1,60 @@ +# ------------------------------------------------------------------------------ +# 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-MgApplicationPassword -MockWith {} -ModuleName Microsoft.Graph.Entra +} + +Describe "Remove-EntraApplicationPassword"{ + It "Should return empty object" { + $result = Remove-EntraApplicationPassword -ObjectId "aaaaaaaa-bbbb-cccc-1111-222222222222" -KeyId "aaaaaaaa-0b0b-1c1c-2d2d-333333333333" + $result | Should -BeNullOrEmpty + Should -Invoke -CommandName Remove-MgApplicationPassword -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Should fail when ObjectId is empty" { + { Remove-EntraApplicationPassword -ObjectId "" } | Should -Throw "Cannot bind argument to parameter 'ObjectId'*" + } + It "Should fail when ObjectId is null" { + { Remove-EntraApplicationPassword -ObjectId } | Should -Throw "Missing an argument for parameter 'ObjectId'*" + } + It "Should fail when KeyId is null" { + { Remove-EntraApplicationPassword -ObjectId "aaaaaaaa-bbbb-cccc-1111-222222222222" -KeyId } | Should -Throw "Missing an argument for parameter 'KeyId'*" + } + It "Should fail when invalid parameter is passed" { + { Remove-EntraApplicationPassword -DisplayName "abc" } | Should -Throw "A parameter cannot be found that matches parameter name 'DisplayName'." + } + It "Should contain ApplicationId in parameters when passed ObjectId to it" { + Mock -CommandName Remove-MgApplicationPassword -MockWith {$args} -ModuleName Microsoft.Graph.Entra + $result = Remove-EntraApplicationPassword -ObjectId "aaaaaaaa-bbbb-cccc-1111-222222222222" -KeyId "aaaaaaaa-0b0b-1c1c-2d2d-333333333333" + $params = Get-Parameters -data $result + $params.ApplicationId | Should -Be "aaaaaaaa-bbbb-cccc-1111-222222222222" + } + It "Should contain 'User-Agent' header" { + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Remove-EntraApplicationPassword" + $result = Remove-EntraApplicationPassword -ObjectId "aaaaaaaa-bbbb-cccc-1111-222222222222" -KeyId "aaaaaaaa-0b0b-1c1c-2d2d-333333333333" + $result | Should -BeNullOrEmpty + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Remove-EntraApplicationPassword" + Should -Invoke -CommandName Remove-MgApplicationPassword -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 + { Remove-EntraApplicationPassword -ObjectId "aaaaaaaa-bbbb-cccc-1111-222222222222" -KeyId "aaaaaaaa-0b0b-1c1c-2d2d-333333333333" -Debug } | Should -Not -Throw + } finally { + # Restore original confirmation preference + $DebugPreference = $originalDebugPreference + } + } +} \ No newline at end of file diff --git a/test/module/Entra/Remove-EntraApplicationPasswordCredential.Tests.ps1 b/test/module/Entra/Remove-EntraApplicationPasswordCredential.Tests.ps1 new file mode 100644 index 000000000..6b48b4803 --- /dev/null +++ b/test/module/Entra/Remove-EntraApplicationPasswordCredential.Tests.ps1 @@ -0,0 +1,63 @@ +# ------------------------------------------------------------------------------ +# 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-MgApplicationPassword -MockWith {} -ModuleName Microsoft.Graph.Entra +} + +Describe "Remove-EntraApplicationPasswordCredential"{ + It "Should return empty object" { + $result = Remove-EntraApplicationPasswordCredential -ObjectId "aaaaaaaa-bbbb-cccc-1111-222222222222" -KeyId "bbbbbbbb-cccc-dddd-2222-333333333333" + $result | Should -BeNullOrEmpty + Should -Invoke -CommandName Remove-MgApplicationPassword -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Should fail when ObjectId is empty" { + { Remove-EntraApplicationPasswordCredential -ObjectId "" } | Should -Throw "Cannot bind argument to parameter 'ObjectId'*" + } + It "Should fail when ObjectId is null" { + { Remove-EntraApplicationPasswordCredential -ObjectId } | Should -Throw "Missing an argument for parameter 'ObjectId'*" + } + It "Should fail when KeyId is empty" { + { Remove-EntraApplicationPasswordCredential -KeyId "" } | Should -Throw "Cannot bind argument to parameter 'KeyId'*" + } + It "Should fail when KeyId is null" { + { Remove-EntraApplicationPasswordCredential -KeyId } | Should -Throw "Missing an argument for parameter 'KeyId'*" + } + It "Should fail when invalid parameter is passed" { + { Remove-EntraApplicationPasswordCredential -DisplayName "abc" } | Should -Throw "A parameter cannot be found that matches parameter name 'DisplayName'." + } + It "Should contain ApplicationId in parameters when passed ObjectId to it" { + Mock -CommandName Remove-MgApplicationPassword -MockWith {$args} -ModuleName Microsoft.Graph.Entra + $result = Remove-EntraApplicationPasswordCredential -ObjectId "aaaaaaaa-bbbb-cccc-1111-222222222222" -KeyId "bbbbbbbb-cccc-dddd-2222-333333333333" + $params = Get-Parameters -data $result + $params.ApplicationId | Should -Be "aaaaaaaa-bbbb-cccc-1111-222222222222" + } + It "Should contain 'User-Agent' header" { + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Remove-EntraApplicationPasswordCredential" + $result = Remove-EntraApplicationPasswordCredential -ObjectId "aaaaaaaa-bbbb-cccc-1111-222222222222" -KeyId "bbbbbbbb-cccc-dddd-2222-333333333333" + $result | Should -BeNullOrEmpty + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Remove-EntraApplicationPasswordCredential" + Should -Invoke -CommandName Remove-MgApplicationPassword -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 + { Remove-EntraApplicationPasswordCredential -ObjectId "aaaaaaaa-bbbb-cccc-1111-222222222222" -KeyId "bbbbbbbb-cccc-dddd-2222-333333333333" -Debug } | Should -Not -Throw + } finally { + # Restore original confirmation preference + $DebugPreference = $originalDebugPreference + } + } +} \ No newline at end of file diff --git a/test/module/Entra/Set-EntraPermissionGrantConditionSet.Tests.ps1 b/test/module/Entra/Set-EntraPermissionGrantConditionSet.Tests.ps1 new file mode 100644 index 000000000..aafb639b5 --- /dev/null +++ b/test/module/Entra/Set-EntraPermissionGrantConditionSet.Tests.ps1 @@ -0,0 +1,67 @@ +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 Update-MgPolicyPermissionGrantPolicyInclude -MockWith {} -ModuleName Microsoft.Graph.Entra + + Mock -CommandName Update-MgPolicyPermissionGrantPolicyExclude -MockWith {} -ModuleName Microsoft.Graph.Entra +} +Describe "Set-EntraPermissionGrantConditionSet"{ + It "Should return empty object for condition set 'includes'"{ + $result = Set-EntraPermissionGrantConditionSet -PolicyId "policy1" -ConditionSetType "includes" -Id "00aa00aa-bb11-cc22-dd33-44ee44ee44ee" -PermissionClassification "Low" + $result | Should -BeNullOrEmpty + Should -Invoke -CommandName Update-MgPolicyPermissionGrantPolicyInclude -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Should return empty object for condition set 'excludes'"{ + $result = Set-EntraPermissionGrantConditionSet -PolicyId "policy1" -ConditionSetType "excludes" -Id "00aa00aa-bb11-cc22-dd33-44ee44ee44ee" -PermissionClassification "Low" + $result | Should -BeNullOrEmpty + Should -Invoke -CommandName Update-MgPolicyPermissionGrantPolicyExclude -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Should fail when parameters are empty" { + { Set-EntraPermissionGrantConditionSet -PolicyId "" -ConditionSetType "" -Id ""} | Should -Throw "Cannot bind argument to parameter*" + } + It "Should fail when parameters are null" { + { Set-EntraPermissionGrantConditionSet -PolicyId -ConditionSetType -Id } | Should -Throw "Missing an argument for parameter*" + } + It "Should contain parameters for condition set 'includes'" { + Mock -CommandName Update-MgPolicyPermissionGrantPolicyInclude -MockWith {$args} -ModuleName Microsoft.Graph.Entra + $result = Set-EntraPermissionGrantConditionSet -PolicyId "policy1" -ConditionSetType "includes" -Id "00aa00aa-bb11-cc22-dd33-44ee44ee44ee" -PermissionClassification "Low" + $params = Get-Parameters -data $result + $params.PermissionGrantPolicyId | Should -Be "policy1" + $params.PermissionGrantConditionSetId | Should -Be "00aa00aa-bb11-cc22-dd33-44ee44ee44ee" + Should -Invoke -CommandName Update-MgPolicyPermissionGrantPolicyInclude -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Should contain parameters for condition set 'excludes'" { + Mock -CommandName Update-MgPolicyPermissionGrantPolicyExclude -MockWith {$args} -ModuleName Microsoft.Graph.Entra + $result = Set-EntraPermissionGrantConditionSet -PolicyId "policy1" -ConditionSetType "excludes" -Id "00aa00aa-bb11-cc22-dd33-44ee44ee44ee" -PermissionClassification "Low" + $params = Get-Parameters -data $result + $params.PermissionGrantPolicyId | Should -Be "policy1" + $params.PermissionGrantConditionSetId | Should -Be "00aa00aa-bb11-cc22-dd33-44ee44ee44ee" + Should -Invoke -CommandName Update-MgPolicyPermissionGrantPolicyExclude -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Should contain 'User-Agent' header" { + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Set-EntraPermissionGrantConditionSet" + $result = Set-EntraPermissionGrantConditionSet -PolicyId "policy1" -ConditionSetType "excludes" -Id "00aa00aa-bb11-cc22-dd33-44ee44ee44ee" -PermissionClassification "Low" + $result | Should -BeNullOrEmpty + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Set-EntraPermissionGrantConditionSet" + Should -Invoke -CommandName Update-MgPolicyPermissionGrantPolicyExclude -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 + { Set-EntraPermissionGrantConditionSet -PolicyId "policy1" -ConditionSetType "excludes" -Id "00aa00aa-bb11-cc22-dd33-44ee44ee44ee" -PermissionClassification "Low" -Debug } | Should -Not -Throw + } finally { + # Restore original confirmation preference + $DebugPreference = $originalDebugPreference + } + } +} \ No newline at end of file From 984fd8a1b87dd41a94dd11263ef8d30203ffdd48 Mon Sep 17 00:00:00 2001 From: "Snehal Kotwal (Perennial Systems Inc)" Date: Mon, 23 Sep 2024 13:36:33 +0530 Subject: [PATCH 02/15] update --- .../Get-EntraBetaApplication.Tests.ps1 | 128 ++++++++++++++++++ .../New-EntraBetaApplication.Tests.ps1 | 61 +++++++++ .../Remove-EntraBetaApplication.Tests.ps1 | 40 ++++++ .../Set-EntraBetaApplication.Tests.ps1 | 56 ++++++++ 4 files changed, 285 insertions(+) create mode 100644 test/module/EntraBeta/Get-EntraBetaApplication.Tests.ps1 create mode 100644 test/module/EntraBeta/New-EntraBetaApplication.Tests.ps1 create mode 100644 test/module/EntraBeta/Remove-EntraBetaApplication.Tests.ps1 create mode 100644 test/module/EntraBeta/Set-EntraBetaApplication.Tests.ps1 diff --git a/test/module/EntraBeta/Get-EntraBetaApplication.Tests.ps1 b/test/module/EntraBeta/Get-EntraBetaApplication.Tests.ps1 new file mode 100644 index 000000000..0665e836b --- /dev/null +++ b/test/module/EntraBeta/Get-EntraBetaApplication.Tests.ps1 @@ -0,0 +1,128 @@ +# ------------------------------------------------------------------------------ +# 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.Beta) -eq $null){ + Import-Module Microsoft.Graph.Entra.Beta + } + Import-Module (Join-Path $psscriptroot "..\Common-Functions.ps1") -Force + + $scriptblock = { + # Write-Host "Mocking Get-MgBetaApplication with parameters: $($args | ConvertTo-Json -Depth 3)" + return @( + [PSCustomObject]@{ + "AppId" = "5f783237-3457-45d8-93e7-a0edb1cfbfd1" + "AppRoles" = $null + "DeletedDateTime" = $null + "Id" = "111cc9b5-fce9-485e-9566-c68debafac5f" + "DisplayName" = "Mock-App" + "Info" = @{LogoUrl=""; MarketingUrl=""; PrivacyStatementUrl=""; SupportUrl=""; TermsOfServiceUrl=""} + "IsDeviceOnlyAuthSupported" = $True + "IsFallbackPublicClient" = $true + "KeyCredentials" = @{CustomKeyIdentifier = @(211, 174, 247);DisplayName =""; Key="";KeyId="d903c7a3-75ea-4772-8935-5c0cf82068a7";Type="Symmetric";Usage="Sign"} + "OptionalClaims" = @{AccessToken=""; IdToken=""; Saml2Token=""} + "ParentalControlSettings" = @{CountriesBlockedForMinors=$null; LegalAgeGroupRule="Allow"} + "PasswordCredentials" = @{} + "PublicClient" = @{RedirectUris=$null} + "PublisherDomain" = "M365x99297270.onmicrosoft.com" + "SignInAudience" = "AzureADandPersonalMicrosoftAccount" + "Web" = @{HomePageUrl="https://localhost/demoapp"; ImplicitGrantSettings=""; LogoutUrl="";} + "Parameters" = $args + } + ) + } + + Mock -CommandName Get-MgBetaApplication -MockWith $scriptblock -ModuleName Microsoft.Graph.Entra.Beta +} + +Describe "Get-EntraBetaApplication" { + Context "Test for Get-EntraBetaApplication" { + It "Should return specific application" { + $result = Get-EntraBetaApplication -ObjectId "111cc9b5-fce9-485e-9566-c68debafac5f" + $result | Should -Not -BeNullOrEmpty + $result.Id | should -Be @('111cc9b5-fce9-485e-9566-c68debafac5f') + Should -Invoke -CommandName Get-MgBetaApplication -ModuleName Microsoft.Graph.Entra.Beta -Times 1 + } + It "Should fail when ObjectId is empty" { + { Get-EntraBetaApplication -ObjectId "" } | Should -Throw "Cannot bind argument to parameter 'ObjectId' because it is an empty string." + } + It "Should return all applications" { + $result = Get-EntraBetaApplication -All $true + $result | Should -Not -BeNullOrEmpty + Should -Invoke -CommandName Get-MgBetaApplication -ModuleName Microsoft.Graph.Entra.Beta -Times 1 + } + It "Should fail when All is empty" { + { Get-EntraBetaApplication -All } | Should -Throw "Missing an argument for parameter 'All'*" + } + It "Should fail when invalid parameter is passed" { + { Get-EntraBetaApplication -Power "abc" } | Should -Throw "A parameter cannot be found that matches parameter name 'Power'*" + } + It "Should return specific application by searchstring" { + $result = Get-EntraBetaApplication -SearchString 'Mock-App' + $result | Should -Not -BeNullOrEmpty + $result.DisplayName | should -Be 'Mock-App' + Should -Invoke -CommandName Get-MgBetaApplication -ModuleName Microsoft.Graph.Entra.Beta -Times 1 + } + It "Should return specific application by filter" { + $result = Get-EntraBetaApplication -Filter "DisplayName -eq 'Mock-App'" + $result | Should -Not -BeNullOrEmpty + $result.DisplayName | should -Be 'Mock-App' + Should -Invoke -CommandName Get-MgBetaApplication -ModuleName Microsoft.Graph.Entra.Beta -Times 1 + } + It "Should return top application" { + $result = Get-EntraBetaApplication -Top 1 + $result | Should -Not -BeNullOrEmpty + Should -Invoke -CommandName Get-MgBetaApplication -ModuleName Microsoft.Graph.Entra.Beta -Times 1 + } + It "Result should Contain ObjectId" { + $result = Get-EntraBetaApplication -ObjectId "111cc9b5-fce9-485e-9566-c68debafac5f" + $result.ObjectId | should -Be "111cc9b5-fce9-485e-9566-c68debafac5f" + } + It "Should contain ApplicationId in parameters when passed ObjectId to it" { + $result = Get-EntraBetaApplication -ObjectId "111cc9b5-fce9-485e-9566-c68debafac5f" + $params = Get-Parameters -data $result.Parameters + $params.ApplicationId | Should -Be "111cc9b5-fce9-485e-9566-c68debafac5f" + } + It "Should contain Filter in parameters when passed SearchString to it" { + $result = Get-EntraBetaApplication -SearchString 'Mock-App' + $params = Get-Parameters -data $result.Parameters + $params.Filter | Should -Match "Mock-App" + } + It "Should contain 'User-Agent' header" { + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraBetaApplication" + $result = Get-EntraBetaApplication -SearchString 'Mock-App' + $params = Get-Parameters -data $result.Parameters + $params.Headers["User-Agent"] | Should -Be $userAgentHeaderValue + } + It "Should support minimum set of parameter sets" { + $GetAzureADApplication = Get-Command Get-EntraBetaApplication + $GetAzureADApplication.ParameterSets.Name | Should -BeIn @("GetQuery", "GetVague", "GetById") + $GetAzureADApplication.Visibility | Should -Be "Public" + $GetAzureADApplication.CommandType | Should -Be "Function" + } + + It "Should return a list of applications by default" { + $GetAzureADApplication = Get-Command Get-EntraBetaApplication + $GetAzureADApplication.ModuleName | Should -Be "Microsoft.Graph.Entra.Beta" + $GetAzureADApplication.DefaultParameterSet | Should -Be "GetQuery" + } + It 'Should have List parameterSet' { + $GetAzureADApplication = Get-Command Get-EntraBetaApplication + $ListParameterSet = $GetAzureADApplication.ParameterSets | Where-Object Name -eq "GetQuery" + $ListParameterSet.Parameters.Name | Should -Contain All + $ListParameterSet.Parameters.Name | Should -Contain Filter + $ListParameterSet.Parameters.Name | Should -Contain Top + } + It 'Should have Get parameterSet' { + $GetAzureADApplication = Get-Command Get-EntraBetaApplication + $GetParameterSet = $GetAzureADApplication.ParameterSets | Where-Object Name -eq "GetById" + $GetParameterSet.Parameters.Name | Should -Contain ObjectId + } + It 'Should have GetViaIdentity parameterSet' { + $GetAzureADApplication = Get-Command Get-EntraBetaApplication + $GetViaIdentityParameterSet = $GetAzureADApplication.ParameterSets | Where-Object Name -eq "GetVague" + $GetViaIdentityParameterSet.Parameters.Name | Should -Contain SearchString + $GetViaIdentityParameterSet.Parameters.Name | Should -Contain All + } + } +} \ No newline at end of file diff --git a/test/module/EntraBeta/New-EntraBetaApplication.Tests.ps1 b/test/module/EntraBeta/New-EntraBetaApplication.Tests.ps1 new file mode 100644 index 000000000..d14cd6b81 --- /dev/null +++ b/test/module/EntraBeta/New-EntraBetaApplication.Tests.ps1 @@ -0,0 +1,61 @@ +# ------------------------------------------------------------------------------ +# 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.Beta) -eq $null){ + Import-Module Microsoft.Graph.Entra.Beta + } + Import-Module (Join-Path $psscriptroot "..\Common-Functions.ps1") -Force + + $scriptblock = { + # Write-Host "Mocking New-MgBetaApplication with parameters: $($args | ConvertTo-Json -Depth 3)" + return @( + [PSCustomObject]@{ + "AppId" = "5f783237-3457-45d8-93e7-a0edb1cfbfd1" + "DeletedDateTime" = $null + "Id" = "111cc9b5-fce9-485e-9566-c68debafac5f" + "DisplayName" = "Mock-App" + "Info" = @{LogoUrl=""; MarketingUrl=""; PrivacyStatementUrl=""; SupportUrl=""; TermsOfServiceUrl=""} + "IsDeviceOnlyAuthSupported" = $True + "IsFallbackPublicClient" = $true + "KeyCredentials" = @{CustomKeyIdentifier = @(211, 174, 247);DisplayName =""; Key="";KeyId="d903c7a3-75ea-4772-8935-5c0cf82068a7";Type="Symmetric";Usage="Sign"} + "OptionalClaims" = @{AccessToken=""; IdToken=""; Saml2Token=""} + "ParentalControlSettings" = @{CountriesBlockedForMinors=$null; LegalAgeGroupRule="Allow"} + "PasswordCredentials" = @{} + "PublicClient" = @{RedirectUris=$null} + "PublisherDomain" = "M365x99297270.onmicrosoft.com" + "SignInAudience" = "AzureADandPersonalMicrosoftAccount" + "Web" = @{HomePageUrl="https://localhost/demoapp"; ImplicitGrantSettings=""; LogoutUrl="";} + "Parameters" = $args + } + ) + } + + Mock -CommandName New-MgBetaApplication -MockWith $scriptblock -ModuleName Microsoft.Graph.Entra.Beta +} + +Describe "New-EntraBetaApplication"{ + Context "Test for New-EntraBetaApplication" { + It "Should return created Application"{ + $result = New-EntraBetaApplication -DisplayName "Mock-App" + $result | Should -Not -BeNullOrEmpty + $result.DisplayName | should -Be "Mock-App" + $result.IsDeviceOnlyAuthSupported | should -Be "True" + $result.IsFallbackPublicClient | should -Be "True" + $result.SignInAudience | should -Be "AzureADandPersonalMicrosoftAccount" + Should -Invoke -CommandName New-MgBetaApplication -ModuleName Microsoft.Graph.Entra.Beta -Times 1 + } + It "Should fail when DisplayName is empty" { + { New-EntraBetaApplication -DisplayName "" } | Should -Throw "Cannot bind argument to parameter*" + } + It "Should fail when invalid parameter is passed" { + { New-EntraBetaApplication -Power "abc" } | Should -Throw "A parameter cannot be found that matches parameter name 'Power'*" + } + It "Should contain 'User-Agent' header" { + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion New-EntraBetaApplication" + $result = New-EntraBetaApplication -DisplayName "Mock-App" + $params = Get-Parameters -data $result.Parameters + $params.Headers["User-Agent"] | Should -Be $userAgentHeaderValue + } + } +} \ No newline at end of file diff --git a/test/module/EntraBeta/Remove-EntraBetaApplication.Tests.ps1 b/test/module/EntraBeta/Remove-EntraBetaApplication.Tests.ps1 new file mode 100644 index 000000000..62e5e7c29 --- /dev/null +++ b/test/module/EntraBeta/Remove-EntraBetaApplication.Tests.ps1 @@ -0,0 +1,40 @@ +# ------------------------------------------------------------------------------ +# 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.Beta) -eq $null){ + Import-Module Microsoft.Graph.Entra.Beta + } + Import-Module (Join-Path $psscriptroot "..\Common-Functions.ps1") -Force + + Mock -CommandName Remove-MgBetaApplication -MockWith {} -ModuleName Microsoft.Graph.Entra.Beta +} + +Describe "Remove-EntraBetaApplication" { + Context "Test for Remove-EntraBetaApplication" { + It "Should return empty object" { + $result = Remove-EntraBetaApplication -ObjectId 056b2531-005e-4f3e-be78-01a71ea30a04 + $result | Should -BeNullOrEmpty + Should -Invoke -CommandName Remove-MgBetaApplication -ModuleName Microsoft.Graph.Entra.Beta -Times 1 + } + It "Should fail when ObjectId is empty" { + { Remove-EntraBetaApplication -ObjectId "" } | Should -Throw "Cannot bind argument to parameter*" + } + It "Should fail when invalid parameter is passed" { + { Remove-EntraBetaApplication -Power "abc" } | Should -Throw "A parameter cannot be found that matches parameter name 'Power'*" + } + It "Should contain ApplicationId in parameters when passed ObjectId to it" { + Mock -CommandName Remove-MgBetaApplication -MockWith {$args} -ModuleName Microsoft.Graph.Entra.Beta + $result = Remove-EntraBetaApplication -ObjectId 056b2531-005e-4f3e-be78-01a71ea30a04 + $params = Get-Parameters -data $result + $params.ApplicationId | Should -Be "056b2531-005e-4f3e-be78-01a71ea30a04" + } + It "Should contain 'User-Agent' header" { + Mock -CommandName Remove-MgBetaApplication -MockWith {$args} -ModuleName Microsoft.Graph.Entra.Beta + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Remove-EntraBetaApplication" + $result = Remove-EntraBetaApplication -ObjectId 056b2531-005e-4f3e-be78-01a71ea30a04 + $params = Get-Parameters -data $result + $params.Headers["User-Agent"] | Should -Be $userAgentHeaderValue + } + } +} \ No newline at end of file diff --git a/test/module/EntraBeta/Set-EntraBetaApplication.Tests.ps1 b/test/module/EntraBeta/Set-EntraBetaApplication.Tests.ps1 new file mode 100644 index 000000000..c63a7f373 --- /dev/null +++ b/test/module/EntraBeta/Set-EntraBetaApplication.Tests.ps1 @@ -0,0 +1,56 @@ +# ------------------------------------------------------------------------------ +# 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.Beta) -eq $null){ + Import-Module Microsoft.Graph.Entra.Beta + } + Import-Module (Join-Path $psscriptroot "..\Common-Functions.ps1") -Force + + Mock -CommandName Update-MgBetaApplication -MockWith {} -ModuleName Microsoft.Graph.Entra.Beta +} + +Describe "Set-EntraBetaApplication"{ + Context "Test for Set-EntraBetaApplication" { + It "Should return empty object"{ + $result = Set-EntraBetaApplication -ObjectId "aaaaaaaa-1111-1111-1111-000000000000" -DisplayName "Mock-App" + $result | Should -BeNullOrEmpty + Should -Invoke -CommandName Update-MgBetaApplication -ModuleName Microsoft.Graph.Entra.Beta -Times 1 + } + It "Should fail when ObjectId is empty" { + { Set-EntraBetaApplication -ObjectId "" } | Should -Throw "Cannot bind argument to parameter 'ObjectId' because it is an empty string." + } + It "Should fail when invalid parameter is passed" { + { Set-EntraBetaApplication -Power "abc" } | Should -Throw "A parameter cannot be found that matches parameter name 'Power'*" + } + It "Should contain ApplicationId in parameters when passed ObjectId to it" { + Mock -CommandName Update-MgBetaApplication -MockWith {$args} -ModuleName Microsoft.Graph.Entra.Beta + $result = Set-EntraBetaApplication -ObjectId "aaaaaaaa-1111-1111-1111-000000000000" + $params = Get-Parameters -data $result + $params.ApplicationId | Should -Be "aaaaaaaa-1111-1111-1111-000000000000" + } + It "Should contain 'User-Agent' header" { + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Set-EntraBetaApplication" + $result = Set-EntraBetaApplication -ObjectId "aaaaaaaa-1111-1111-1111-000000000000" + $result | Should -BeNullOrEmpty + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Set-EntraBetaApplication" + Should -Invoke -CommandName Update-MgBetaApplication -ModuleName Microsoft.Graph.Entra.Beta -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 + {Set-EntraBetaApplication -ObjectId "aaaaaaaa-1111-1111-1111-000000000000" -Debug } | Should -Not -Throw + } finally { + # Restore original confirmation preference + $DebugPreference = $originalDebugPreference + } + } + } +} \ No newline at end of file From da7c00d8161e4003589e4e18577b337c79b0d542 Mon Sep 17 00:00:00 2001 From: "Snehal Kotwal (Perennial Systems Inc)" Date: Mon, 23 Sep 2024 13:55:14 +0530 Subject: [PATCH 03/15] updated test cases --- .../Get-EntraBetaApplication.Tests.ps1 | 54 ++++++++++++++----- .../New-EntraBetaApplication.Tests.ps1 | 23 ++++++-- .../Remove-EntraBetaApplication.Tests.ps1 | 32 ++++++++--- 3 files changed, 85 insertions(+), 24 deletions(-) diff --git a/test/module/EntraBeta/Get-EntraBetaApplication.Tests.ps1 b/test/module/EntraBeta/Get-EntraBetaApplication.Tests.ps1 index 0665e836b..3f8356ec9 100644 --- a/test/module/EntraBeta/Get-EntraBetaApplication.Tests.ps1 +++ b/test/module/EntraBeta/Get-EntraBetaApplication.Tests.ps1 @@ -14,7 +14,7 @@ BeforeAll { "AppId" = "5f783237-3457-45d8-93e7-a0edb1cfbfd1" "AppRoles" = $null "DeletedDateTime" = $null - "Id" = "111cc9b5-fce9-485e-9566-c68debafac5f" + "Id" = "aaaaaaaa-1111-1111-1111-000000000000" "DisplayName" = "Mock-App" "Info" = @{LogoUrl=""; MarketingUrl=""; PrivacyStatementUrl=""; SupportUrl=""; TermsOfServiceUrl=""} "IsDeviceOnlyAuthSupported" = $True @@ -38,21 +38,21 @@ BeforeAll { Describe "Get-EntraBetaApplication" { Context "Test for Get-EntraBetaApplication" { It "Should return specific application" { - $result = Get-EntraBetaApplication -ObjectId "111cc9b5-fce9-485e-9566-c68debafac5f" + $result = Get-EntraBetaApplication -ObjectId "aaaaaaaa-1111-1111-1111-000000000000" $result | Should -Not -BeNullOrEmpty - $result.Id | should -Be @('111cc9b5-fce9-485e-9566-c68debafac5f') + $result.Id | should -Be @('aaaaaaaa-1111-1111-1111-000000000000') Should -Invoke -CommandName Get-MgBetaApplication -ModuleName Microsoft.Graph.Entra.Beta -Times 1 } It "Should fail when ObjectId is empty" { { Get-EntraBetaApplication -ObjectId "" } | Should -Throw "Cannot bind argument to parameter 'ObjectId' because it is an empty string." } It "Should return all applications" { - $result = Get-EntraBetaApplication -All $true + $result = Get-EntraBetaApplication -All $result | Should -Not -BeNullOrEmpty Should -Invoke -CommandName Get-MgBetaApplication -ModuleName Microsoft.Graph.Entra.Beta -Times 1 } - It "Should fail when All is empty" { - { Get-EntraBetaApplication -All } | Should -Throw "Missing an argument for parameter 'All'*" + It "Should fail when All has argument" { + { Get-EntraBetaAuditDirectoryLog -All $true } | Should -Throw "A positional parameter cannot be found that accepts argument 'True'." } It "Should fail when invalid parameter is passed" { { Get-EntraBetaApplication -Power "abc" } | Should -Throw "A parameter cannot be found that matches parameter name 'Power'*" @@ -75,13 +75,13 @@ Describe "Get-EntraBetaApplication" { Should -Invoke -CommandName Get-MgBetaApplication -ModuleName Microsoft.Graph.Entra.Beta -Times 1 } It "Result should Contain ObjectId" { - $result = Get-EntraBetaApplication -ObjectId "111cc9b5-fce9-485e-9566-c68debafac5f" - $result.ObjectId | should -Be "111cc9b5-fce9-485e-9566-c68debafac5f" + $result = Get-EntraBetaApplication -ObjectId "aaaaaaaa-1111-1111-1111-000000000000" + $result.ObjectId | should -Be "aaaaaaaa-1111-1111-1111-000000000000" } It "Should contain ApplicationId in parameters when passed ObjectId to it" { - $result = Get-EntraBetaApplication -ObjectId "111cc9b5-fce9-485e-9566-c68debafac5f" + $result = Get-EntraBetaApplication -ObjectId "aaaaaaaa-1111-1111-1111-000000000000" $params = Get-Parameters -data $result.Parameters - $params.ApplicationId | Should -Be "111cc9b5-fce9-485e-9566-c68debafac5f" + $params.ApplicationId | Should -Be "aaaaaaaa-1111-1111-1111-000000000000" } It "Should contain Filter in parameters when passed SearchString to it" { $result = Get-EntraBetaApplication -SearchString 'Mock-App' @@ -90,9 +90,37 @@ Describe "Get-EntraBetaApplication" { } It "Should contain 'User-Agent' header" { $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraBetaApplication" - $result = Get-EntraBetaApplication -SearchString 'Mock-App' - $params = Get-Parameters -data $result.Parameters - $params.Headers["User-Agent"] | Should -Be $userAgentHeaderValue + $result = Get-EntraBetaApplication -ObjectId "aaaaaaaa-1111-1111-1111-000000000000" + $result | Should -Not -BeNullOrEmpty + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraBetaApplication" + Should -Invoke -CommandName Get-MgBetaApplication -ModuleName Microsoft.Graph.Entra.Beta -Times 1 -ParameterFilter { + $Headers.'User-Agent' | Should -Be $userAgentHeaderValue + $true + } + } + It "Property parameter should work" { + $result = Get-EntraBetaApplication -ObjectId "aaaaaaaa-1111-1111-1111-000000000000" -Property DisplayName + $result | Should -Not -BeNullOrEmpty + $result.DisplayName | Should -Be 'Mock-App' + + Should -Invoke -CommandName Get-MgBetaApplication -ModuleName Microsoft.Graph.Entra.Beta -Times 1 + } + + It "Should fail when Property is empty" { + { Get-EntraBetaApplication -ObjectId "aaaaaaaa-1111-1111-1111-000000000000" -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-EntraBetaApplication -ObjectId "aaaaaaaa-1111-1111-1111-000000000000" -Debug } | Should -Not -Throw + } finally { + # Restore original confirmation preference + $DebugPreference = $originalDebugPreference + } } It "Should support minimum set of parameter sets" { $GetAzureADApplication = Get-Command Get-EntraBetaApplication diff --git a/test/module/EntraBeta/New-EntraBetaApplication.Tests.ps1 b/test/module/EntraBeta/New-EntraBetaApplication.Tests.ps1 index d14cd6b81..6c3f27336 100644 --- a/test/module/EntraBeta/New-EntraBetaApplication.Tests.ps1 +++ b/test/module/EntraBeta/New-EntraBetaApplication.Tests.ps1 @@ -53,9 +53,26 @@ Describe "New-EntraBetaApplication"{ } It "Should contain 'User-Agent' header" { $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion New-EntraBetaApplication" - $result = New-EntraBetaApplication -DisplayName "Mock-App" - $params = Get-Parameters -data $result.Parameters - $params.Headers["User-Agent"] | Should -Be $userAgentHeaderValue + $result = New-EntraBetaApplication -DisplayName "Mock-App" + $result | Should -Not -BeNullOrEmpty + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion New-EntraBetaApplication" + Should -Invoke -CommandName New-MgBetaApplication -ModuleName Microsoft.Graph.Entra.Beta -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-EntraBetaApplication -DisplayName "Mock-App" -Debug } | Should -Not -Throw + } finally { + # Restore original confirmation preference + $DebugPreference = $originalDebugPreference + } } } } \ No newline at end of file diff --git a/test/module/EntraBeta/Remove-EntraBetaApplication.Tests.ps1 b/test/module/EntraBeta/Remove-EntraBetaApplication.Tests.ps1 index 62e5e7c29..8a33f6c32 100644 --- a/test/module/EntraBeta/Remove-EntraBetaApplication.Tests.ps1 +++ b/test/module/EntraBeta/Remove-EntraBetaApplication.Tests.ps1 @@ -13,7 +13,7 @@ BeforeAll { Describe "Remove-EntraBetaApplication" { Context "Test for Remove-EntraBetaApplication" { It "Should return empty object" { - $result = Remove-EntraBetaApplication -ObjectId 056b2531-005e-4f3e-be78-01a71ea30a04 + $result = Remove-EntraBetaApplication -ObjectId "aaaaaaaa-1111-1111-1111-000000000000" $result | Should -BeNullOrEmpty Should -Invoke -CommandName Remove-MgBetaApplication -ModuleName Microsoft.Graph.Entra.Beta -Times 1 } @@ -25,16 +25,32 @@ Describe "Remove-EntraBetaApplication" { } It "Should contain ApplicationId in parameters when passed ObjectId to it" { Mock -CommandName Remove-MgBetaApplication -MockWith {$args} -ModuleName Microsoft.Graph.Entra.Beta - $result = Remove-EntraBetaApplication -ObjectId 056b2531-005e-4f3e-be78-01a71ea30a04 + $result = Remove-EntraBetaApplication -ObjectId "aaaaaaaa-1111-1111-1111-000000000000" $params = Get-Parameters -data $result - $params.ApplicationId | Should -Be "056b2531-005e-4f3e-be78-01a71ea30a04" + $params.ApplicationId | Should -Be "aaaaaaaa-1111-1111-1111-000000000000" } It "Should contain 'User-Agent' header" { - Mock -CommandName Remove-MgBetaApplication -MockWith {$args} -ModuleName Microsoft.Graph.Entra.Beta $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Remove-EntraBetaApplication" - $result = Remove-EntraBetaApplication -ObjectId 056b2531-005e-4f3e-be78-01a71ea30a04 - $params = Get-Parameters -data $result - $params.Headers["User-Agent"] | Should -Be $userAgentHeaderValue - } + $result = Remove-EntraBetaApplication -ObjectId "aaaaaaaa-1111-1111-1111-000000000000" + $result | Should -BeNullOrEmpty + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Remove-EntraBetaApplication" + Should -Invoke -CommandName Remove-MgBetaApplication -ModuleName Microsoft.Graph.Entra.Beta -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 + { Remove-EntraBetaApplication -ObjectId "aaaaaaaa-1111-1111-1111-000000000000" -Debug } | Should -Not -Throw + } finally { + # Restore original confirmation preference + $DebugPreference = $originalDebugPreference + } + } } } \ No newline at end of file From fa3d2bcc641658a08494211162a6b6b2faebee99 Mon Sep 17 00:00:00 2001 From: Ashwini Karke Date: Tue, 24 Sep 2024 13:45:09 +0530 Subject: [PATCH 04/15] fixed UT --- .../Get-EntraDeletedApplication.ps1 | 2 +- .../Entra/Get-EntraApplicationOwner.Tests.ps1 | 43 ++++++++++-- .../Get-EntraDeletedApplication.Tests.ps1 | 68 ++++++------------- 3 files changed, 58 insertions(+), 55 deletions(-) diff --git a/module/Entra/customizations/Get-EntraDeletedApplication.ps1 b/module/Entra/customizations/Get-EntraDeletedApplication.ps1 index f52015c4a..00f7acf7a 100644 --- a/module/Entra/customizations/Get-EntraDeletedApplication.ps1 +++ b/module/Entra/customizations/Get-EntraDeletedApplication.ps1 @@ -109,7 +109,7 @@ 'PublisherDomain','Web','RequiredResourceAccess') foreach ($prop in $propsToConvert) { - $value = $_.$prop | ConvertTo-Json -Depth 10 | ConvertFrom-Json + $value = $_.$prop | ConvertTo-Json -Depth 5 | ConvertFrom-Json $_ | Add-Member -MemberType NoteProperty -Name $prop -Value ($value) -Force } diff --git a/test/module/Entra/Get-EntraApplicationOwner.Tests.ps1 b/test/module/Entra/Get-EntraApplicationOwner.Tests.ps1 index e1e0cbbcc..8d65d362d 100644 --- a/test/module/Entra/Get-EntraApplicationOwner.Tests.ps1 +++ b/test/module/Entra/Get-EntraApplicationOwner.Tests.ps1 @@ -11,15 +11,13 @@ BeforeAll { return @{ value = @( @{ - Id = "e3108c4d-86ff-4ceb-9429-24e85b4b8cea" + Id = "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" ageGroup = $null onPremisesLastSyncDateTime = $null creationType = $null - imAddresses = {"adelev@m365x99297270.onmicrosoft.com"} preferredLanguage = $null - mail = "AdeleV@M365x99297270.OnMicrosoft.com" + mail = "admin@contonso.com" securityIdentifier = "S-1-12-1-1093396945-1080104032-2731339150-364051459" - identities = {} consentProvidedForMinor = $null onPremisesUserPrincipalName = $null Parameters = $args @@ -34,21 +32,52 @@ BeforeAll { Describe "Get-EntraApplicationOwner"{ Context "Test for Get-EntraApplicationOwner"{ It "Should return application owner" { - $result = Get-EntraApplicationOwner -ObjectId "e3108c4d-86ff-4ceb-9429-24e85b4b8cea" + $result = Get-EntraApplicationOwner -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + Write-Host $result $result | Should -Not -BeNullOrEmpty - $result.Id | should -Be @('e3108c4d-86ff-4ceb-9429-24e85b4b8cea') + $result.Id | should -Be @('aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb') Should -Invoke -CommandName Invoke-GraphRequest -ModuleName Microsoft.Graph.Entra -Times 1 } It "Should fail when ObjectId is empty" { { Get-EntraApplicationOwner -ObjectId "" } | Should -Throw "Cannot bind argument to parameter 'ObjectId' because it is an empty string." } + It "Should fail when ObjectId is null" { + { Get-EntraApplicationOwner -ObjectId } | Should -Throw "Missing an argument for parameter 'ObjectId'*" + } + It "Should fail when All has an argument" { + { Get-EntraApplicationOwner -All $true } | Should -Throw "A positional parameter cannot be found that accepts argument 'True'.*" + } + It "Should fail when Top is empty" { + { Get-EntraApplicationOwner -Top } | Should -Throw "Missing an argument for parameter 'Top'*" + } + It "Should fail when Top is invalid" { + { Get-EntraApplicationOwner -Top XY } | Should -Throw "Cannot process argument transformation on parameter 'Top'*" + } + It "Property parameter should work" { + $result = Get-EntraApplicationOwner -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -Property Id + $result | Should -Not -BeNullOrEmpty + $result.Id | Should -Be "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + } It "Should contain 'User-Agent' header" { $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraApplicationOwner" - $result = Get-EntraApplicationOwner -ObjectId "e3108c4d-86ff-4ceb-9429-24e85b4b8cea" + $result = Get-EntraApplicationOwner -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" $params = Get-Parameters -data $result.Parameters $a= $params | ConvertTo-json | ConvertFrom-Json $a.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 + { Get-EntraApplicationOwner -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -Debug } | Should -Not -Throw + } finally { + # Restore original confirmation preference + $DebugPreference = $originalDebugPreference + } + } } } \ No newline at end of file diff --git a/test/module/Entra/Get-EntraDeletedApplication.Tests.ps1 b/test/module/Entra/Get-EntraDeletedApplication.Tests.ps1 index 282f27169..48fc51893 100644 --- a/test/module/Entra/Get-EntraDeletedApplication.Tests.ps1 +++ b/test/module/Entra/Get-EntraDeletedApplication.Tests.ps1 @@ -12,11 +12,7 @@ BeforeAll { "GroupMembershipClaims" = {} "IdentifierUris" = {} "Info" = @{ - LogoUrl=""; - MarketingUrl=""; - PrivacyStatementUrl=""; - SupportUrl=""; - TermsOfServiceUrl=""; + LogoUrl=""; } "IsDeviceOnlyAuthSupported" = $null "KeyCredentials" = {} @@ -27,58 +23,23 @@ BeforeAll { } "PasswordCredentials" = {} "Api" = @{ - AcceptMappedClaims=@{}; KnownClientApplications=@{}; - Oauth2PermissionScopes=@{}; PreAuthorizedApplications=@{}; - RequestedAccessTokenVersion="2"; } "PublicClient" = @{ RedirectUris=@{}; } "PublisherDomain" = "M365x99297270.onmicrosoft.com" "Web" = @{ - HomePageUrl=""; - ImplicitGrantSettings=""; - LogoutUrl=""; - RedirectUriSettings=@{}; + HomePageUrl=""; + LogoutUrl=""; RedirectUris=@{}; + Oauth2AllowImplicitFlow="" } "RequiredResourceAccess" = $null - "AppId" = "9c17362d-20b6-4572-bb6f-600e57c840e5" - "AppManagementPolicies" = $null - "ApplicationTemplateId" = $null - "Certification" = {} - "CreatedDateTime" = "12/12/2023 11:28:45 AM" - "CreatedOnBehalfOf" = {} - "DefaultRedirectUri" = $null - "DeletedDateTime" = "12/12/2023 12:23:39 PM" - "Description" = $null - "DisabledByMicrosoftStatus" = $null "DisplayName" = "Mock-test-App" - "ExtensionProperties" = $null - "FederatedIdentityCredentials" = $null - "HomeRealmDiscoveryPolicies" = $null - "Id" = "01157307-373c-47b0-889a-3bc57033d73e" - "IsFallbackPublicClient" = $null + "Id" = "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" "Logo" = $null - "Notes" = $null - "Oauth2RequirePostResponse" = $null - "Owners" = $null - "RequestSignatureVerification" = {} - "SamlMetadataUrl" = $null - "ServiceManagementReference" = $null - "ServicePrincipalLockConfiguration" = {} - "SignInAudience" = "AzureADandPersonalMicrosoftAccount" - "Spa" = {} - "Synchronization" = {} - "Tags" = {} - "TokenEncryptionKeyId" = $null - "TokenIssuancePolicies" = $null - "TokenLifetimePolicies" = $null - "UniqueName" = $null - "VerifiedPublisher" = {} - "AdditionalProperties" = {} "Parameters" = $args } ) @@ -90,12 +51,12 @@ BeforeAll { Describe "Get-EntraDeletedApplication" { Context "Test for Get-EntraDeletedApplication" { It "Should return all applications" { - $result = Get-EntraDeletedApplication | ConvertTo-Json | ConvertFrom-Json + $result = Get-EntraDeletedApplication | ConvertTo-Json -Depth 10 | ConvertFrom-Json $result | Should -Not -BeNullOrEmpty Should -Invoke -CommandName Get-MgDirectoryDeletedItemAsApplication -ModuleName Microsoft.Graph.Entra -Times 1 } It "Should fail when All is empty" { - { Get-EntraDeletedApplication -All } | Should -Throw "Missing an argument for parameter 'All'*" + { Get-EntraDeletedApplication -All $true} | Should -Throw "A positional parameter cannot be found that accepts argument 'True'.*" } It "Should fail when invalid parameter is passed" { { Get-EntraDeletedApplication -DisplayName "abc" } | Should -Throw "A parameter cannot be found that matches parameter name 'DisplayName'*" @@ -122,7 +83,7 @@ Describe "Get-EntraDeletedApplication" { } It "Result should Contain ObjectId" { $result = Get-EntraDeletedApplication -Filter "DisplayName -eq 'Mock-test-App'" | ConvertTo-Json | ConvertFrom-Json - $result.ObjectId | should -Be "01157307-373c-47b0-889a-3bc57033d73e" + $result.ObjectId | should -Be "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" } It "Should contain Filter in parameters when passed SearchString to it" { $result = Get-EntraDeletedApplication -SearchString 'Mock-test-App' | ConvertTo-Json | ConvertFrom-Json @@ -136,5 +97,18 @@ Describe "Get-EntraDeletedApplication" { $a= $params | ConvertTo-json | ConvertFrom-Json $a.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 + { Get-EntraDeletedApplication -Debug } | Should -Not -Throw + } finally { + # Restore original confirmation preference + $DebugPreference = $originalDebugPreference + } + } } } \ No newline at end of file From 0bb6b3e01e870076cb40b7569c9f3704fc5aaedb Mon Sep 17 00:00:00 2001 From: "Snehal Kotwal (Perennial Systems Inc)" Date: Tue, 24 Sep 2024 16:13:44 +0530 Subject: [PATCH 05/15] updated --- .../Entra/Get-EntraApplicationOwner.Tests.ps1 | 11 +++++++---- .../Entra/Get-EntraDeletedApplication.Tests.ps1 | 16 +++++++++++----- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/test/module/Entra/Get-EntraApplicationOwner.Tests.ps1 b/test/module/Entra/Get-EntraApplicationOwner.Tests.ps1 index 8d65d362d..ebde421cd 100644 --- a/test/module/Entra/Get-EntraApplicationOwner.Tests.ps1 +++ b/test/module/Entra/Get-EntraApplicationOwner.Tests.ps1 @@ -61,10 +61,13 @@ Describe "Get-EntraApplicationOwner"{ } It "Should contain 'User-Agent' header" { $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraApplicationOwner" - $result = Get-EntraApplicationOwner -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" - $params = Get-Parameters -data $result.Parameters - $a= $params | ConvertTo-json | ConvertFrom-Json - $a.headers.'User-Agent' | Should -Be $userAgentHeaderValue + $result = Get-EntraApplicationOwner -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + $result | Should -Not -BeNullOrEmpty + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraApplicationOwner" + 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 diff --git a/test/module/Entra/Get-EntraDeletedApplication.Tests.ps1 b/test/module/Entra/Get-EntraDeletedApplication.Tests.ps1 index 48fc51893..e50cf85c5 100644 --- a/test/module/Entra/Get-EntraDeletedApplication.Tests.ps1 +++ b/test/module/Entra/Get-EntraDeletedApplication.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 @@ -29,7 +32,7 @@ BeforeAll { "PublicClient" = @{ RedirectUris=@{}; } - "PublisherDomain" = "M365x99297270.onmicrosoft.com" + "PublisherDomain" = "contoso.com" "Web" = @{ HomePageUrl=""; LogoutUrl=""; @@ -92,10 +95,13 @@ Describe "Get-EntraDeletedApplication" { } It "Should contain 'User-Agent' header" { $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraDeletedApplication" - $result = Get-EntraDeletedApplication -Top 1 | ConvertTo-Json | ConvertFrom-Json - $params = Get-Parameters -data $result.Parameters - $a= $params | ConvertTo-json | ConvertFrom-Json - $a.headers.'User-Agent' | Should -Be $userAgentHeaderValue + $result = Get-EntraDeletedApplication -Filter "DisplayName -eq 'Mock-test-App'" | ConvertTo-Json | ConvertFrom-Json + $result | Should -Not -BeNullOrEmpty + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraDeletedApplication" + Should -Invoke -CommandName Get-MgDirectoryDeletedItemAsApplication -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 From ecfdde4d28fd280d5d86e6a09db764758ecae204 Mon Sep 17 00:00:00 2001 From: "Snehal Kotwal (Perennial Systems Inc)" Date: Tue, 24 Sep 2024 19:30:13 +0530 Subject: [PATCH 06/15] update --- ...ntraApplicationPasswordCredential.Tests.ps1 | 16 ++++++++++++++-- .../Get-EntraDeletedApplication.Tests.ps1 | 11 +++++++++++ .../Get-EntraDeletedDirectoryObject.Tests.ps1 | 11 +++++++++++ ...-EntraPermissionGrantConditionSet.Tests.ps1 | 13 ++++++++++++- ...ServicePrincipalAppRoleAssignment.Tests.ps1 | 18 +++++++++--------- 5 files changed, 57 insertions(+), 12 deletions(-) diff --git a/test/module/Entra/Get-EntraApplicationPasswordCredential.Tests.ps1 b/test/module/Entra/Get-EntraApplicationPasswordCredential.Tests.ps1 index 0ca03b400..115c04eeb 100644 --- a/test/module/Entra/Get-EntraApplicationPasswordCredential.Tests.ps1 +++ b/test/module/Entra/Get-EntraApplicationPasswordCredential.Tests.ps1 @@ -9,7 +9,7 @@ BeforeAll { "PasswordCredentials" = @( @{ "CustomKeyIdentifier" = {116, 101, 115, 116} - "DisplayName" = "" + "DisplayName" = "Test" "EndDateTime" = "10/23/2024 11:36:56 AM" "KeyId" = "aaaaaaaa-0b0b-1c1c-2d2d-333333333333" "StartDateTime" = "11/22/2023 11:35:16 AM" @@ -40,6 +40,18 @@ BeforeAll { It "Should fail when invalid parameter is passed" { { Get-EntraApplicationPasswordCredential -DisplayName "abc" } | Should -Throw "A parameter cannot be found that matches parameter name 'DisplayName'*" } + It "Property parameter should work" { + $result = Get-EntraApplicationPasswordCredential -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -Property DisplayName + $result | Should -Not -BeNullOrEmpty + $result.DisplayName | Should -Be "Test" + + Should -Invoke -CommandName Get-MgApplication -ModuleName Microsoft.Graph.Entra -Times 1 + } + + It "Should fail when Property is empty" { + { Get-EntraApplicationPasswordCredential -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -Property } | Should -Throw "Missing an argument for parameter 'Property'*" + } + It "Should contain 'User-Agent' header" { $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraApplicationPasswordCredential" $result = Get-EntraApplicationPasswordCredential -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" @@ -57,7 +69,7 @@ BeforeAll { try { # Act & Assert: Ensure the function doesn't throw an exception - { Get-EntraApplicationPasswordCredential -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb"-Debug } | Should -Not -Throw + { Get-EntraApplicationPasswordCredential -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -Debug } | Should -Not -Throw } finally { # Restore original confirmation preference $DebugPreference = $originalDebugPreference diff --git a/test/module/Entra/Get-EntraDeletedApplication.Tests.ps1 b/test/module/Entra/Get-EntraDeletedApplication.Tests.ps1 index e50cf85c5..f2c478d4a 100644 --- a/test/module/Entra/Get-EntraDeletedApplication.Tests.ps1 +++ b/test/module/Entra/Get-EntraDeletedApplication.Tests.ps1 @@ -93,6 +93,17 @@ Describe "Get-EntraDeletedApplication" { $params = Get-Parameters -data $result.Parameters $params.Filter | Should -Match "Mock-test-App" } + It "Property parameter should work" { + $result = Get-EntraDeletedApplication -Property DisplayName + $result | Should -Not -BeNullOrEmpty + $result.DisplayName | Should -Be "Mock-test-App" + + Should -Invoke -CommandName Get-MgDirectoryDeletedItemAsApplication -ModuleName Microsoft.Graph.Entra -Times 1 + } + + It "Should fail when Property is empty" { + { Get-EntraDeletedApplication -Property } | Should -Throw "Missing an argument for parameter 'Property'*" + } It "Should contain 'User-Agent' header" { $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraDeletedApplication" $result = Get-EntraDeletedApplication -Filter "DisplayName -eq 'Mock-test-App'" | ConvertTo-Json | ConvertFrom-Json diff --git a/test/module/Entra/Get-EntraDeletedDirectoryObject.Tests.ps1 b/test/module/Entra/Get-EntraDeletedDirectoryObject.Tests.ps1 index 767d92c46..bc05403bc 100644 --- a/test/module/Entra/Get-EntraDeletedDirectoryObject.Tests.ps1 +++ b/test/module/Entra/Get-EntraDeletedDirectoryObject.Tests.ps1 @@ -40,6 +40,17 @@ Describe "Get-EntraDeletedDirectoryObject"{ $params = Get-Parameters -data $result.Parameters $params.DirectoryObjectId | Should -Be "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" } + It "Property parameter should work" { + $result = Get-EntraDeletedDirectoryObject -Id "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -Property Id + $result | Should -Not -BeNullOrEmpty + $result.Id | Should -Be "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + + Should -Invoke -CommandName Get-MgDirectoryDeletedItem -ModuleName Microsoft.Graph.Entra -Times 1 + } + + It "Should fail when Property is empty" { + {Get-EntraDeletedDirectoryObject -Id "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -Property } | Should -Throw "Missing an argument for parameter 'Property'*" + } It "Should contain 'User-Agent' header" { $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraDeletedDirectoryObject" $result = Get-EntraDeletedDirectoryObject -Id "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" diff --git a/test/module/Entra/Get-EntraPermissionGrantConditionSet.Tests.ps1 b/test/module/Entra/Get-EntraPermissionGrantConditionSet.Tests.ps1 index fb1262047..3d73bd2a3 100644 --- a/test/module/Entra/Get-EntraPermissionGrantConditionSet.Tests.ps1 +++ b/test/module/Entra/Get-EntraPermissionGrantConditionSet.Tests.ps1 @@ -24,7 +24,7 @@ BeforeAll { Mock -CommandName Get-MgPolicyPermissionGrantPolicyExclude -MockWith $scriptblock -ModuleName Microsoft.Graph.Entra } -Describe "Get-EntraMSPermissionGrantConditionSet"{ +Describe "Get-EntraPermissionGrantConditionSet"{ It "Should not return empty object for condition set 'includes'"{ $result = Get-EntraPermissionGrantConditionSet -PolicyId "policy1" -ConditionSetType "includes" -Id "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" $result | Should -Not -BeNullOrEmpty @@ -51,6 +51,17 @@ Describe "Get-EntraMSPermissionGrantConditionSet"{ $params = Get-Parameters -data $result.Parameters $params.PermissionGrantPolicyId | Should -Be "policy1" } + It "Property parameter should work" { + $result = Get-EntraPermissionGrantConditionSet -PolicyId "policy1" -ConditionSetType "includes" -Id "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -Property Id + $result | Should -Not -BeNullOrEmpty + $result.Id | Should -Be "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + + Should -Invoke -CommandName Get-MgPolicyPermissionGrantPolicyInclude -ModuleName Microsoft.Graph.Entra -Times 1 + } + + It "Should fail when Property is empty" { + {Get-EntraPermissionGrantConditionSet -PolicyId "policy1" -ConditionSetType "includes" -Id "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -Property } | Should -Throw "Missing an argument for parameter 'Property'*" + } It "Should contain 'User-Agent' header" { $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraPermissionGrantConditionSet" $result = Get-EntraPermissionGrantConditionSet -PolicyId "policy1" -ConditionSetType "includes" -Id "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" diff --git a/test/module/Entra/New-EntraServicePrincipalAppRoleAssignment.Tests.ps1 b/test/module/Entra/New-EntraServicePrincipalAppRoleAssignment.Tests.ps1 index 1ea4e9273..0a6302d43 100644 --- a/test/module/Entra/New-EntraServicePrincipalAppRoleAssignment.Tests.ps1 +++ b/test/module/Entra/New-EntraServicePrincipalAppRoleAssignment.Tests.ps1 @@ -12,9 +12,9 @@ BeforeAll { return @( [PSCustomObject]@{ "DeletedDateTime" = $null - "Id" = "gsx_zBushUevRyyjtwUohm_RMYjcGsJIjXwKOVMr3ww" + "Id" = "00aa00aa-bb11-cc22-dd33-44ee44ee44ee" "PrincipalDisplayName" = "Mock-App" - "AppRoleId" = "e18f0405-fdec-4ae8-a8a0-d8edb98b061f" + "AppRoleId" = "bbbb1b1b-cc2c-dd3d-ee4e-ffffff5f5f5f" "CreatedDateTime" = "3/12/2024 11:05:29 AM" "PrincipalId" = "aaaaaaaa-bbbb-cccc-1111-222222222222" "Parameters" = $args @@ -28,7 +28,7 @@ BeforeAll { Describe "New-EntraServicePrincipalAppRoleAssignment"{ Context "Test for New-EntraServicePrincipalAppRoleAssignment" { It "Should return New-EntraServicePrincipalAppRoleAssignment"{ - $result = New-EntraServicePrincipalAppRoleAssignment -ObjectId "02ed943d-6eca-4f99-83d6-e6fbf9dc63ae" -ResourceId "02ed943d-6eca-4f99-83d6-e6fbf9dc63ae" -Id "e18f0405-fdec-4ae8-a8a0-d8edb98b061f" -PrincipalId "d2d0a585-0c52-4bab-8c64-a096b98b061f" + $result = New-EntraServicePrincipalAppRoleAssignment -ObjectId "00001111-aaaa-2222-bbbb-3333cccc4444" -ResourceId "a0a0a0a0-bbbb-cccc-dddd-e1e1e1e1e1e1" -Id "bbbb1b1b-cc2c-dd3d-ee4e-ffffff5f5f5f" -PrincipalId "aaaaaaaa-bbbb-cccc-1111-222222222222" $result | Should -Not -BeNullOrEmpty $result.PrincipalDisplayName | should -Be "Mock-App" $result.PrincipalId | should -Be "aaaaaaaa-bbbb-cccc-1111-222222222222" @@ -63,18 +63,18 @@ Describe "New-EntraServicePrincipalAppRoleAssignment"{ { New-EntraServicePrincipalAppRoleAssignment -DisplayName "abc" } | Should -Throw "A parameter cannot be found that matches parameter name 'DisplayName'." } It "Should contain AppRoleId in parameters when passed Id to it" { - $result = New-EntraServicePrincipalAppRoleAssignment -ObjectId "02ed943d-6eca-4f99-83d6-e6fbf9dc63ae" -ResourceId "02ed943d-6eca-4f99-83d6-e6fbf9dc63ae" -Id "e18f0405-fdec-4ae8-a8a0-d8edb98b061f" -PrincipalId "d2d0a585-0c52-4bab-8c64-a096b98b061f" + $result = New-EntraServicePrincipalAppRoleAssignment -ObjectId "00001111-aaaa-2222-bbbb-3333cccc4444" -ResourceId "a0a0a0a0-bbbb-cccc-dddd-e1e1e1e1e1e1" -Id "bbbb1b1b-cc2c-dd3d-ee4e-ffffff5f5f5f" -PrincipalId "aaaaaaaa-bbbb-cccc-1111-222222222222" $params = Get-Parameters -data $result.Parameters - $params.AppRoleId | Should -Be "e18f0405-fdec-4ae8-a8a0-d8edb98b061f" + $params.AppRoleId | Should -Be "bbbb1b1b-cc2c-dd3d-ee4e-ffffff5f5f5f" } It "Should contain ServicePrincipalId in parameters when passed ObjectId to it" { - $result = New-EntraServicePrincipalAppRoleAssignment -ObjectId "02ed943d-6eca-4f99-83d6-e6fbf9dc63ae" -ResourceId "02ed943d-6eca-4f99-83d6-e6fbf9dc63ae" -Id "e18f0405-fdec-4ae8-a8a0-d8edb98b061f" -PrincipalId "d2d0a585-0c52-4bab-8c64-a096b98b061f" + $result = New-EntraServicePrincipalAppRoleAssignment -ObjectId "00001111-aaaa-2222-bbbb-3333cccc4444" -ResourceId "a0a0a0a0-bbbb-cccc-dddd-e1e1e1e1e1e1" -Id "bbbb1b1b-cc2c-dd3d-ee4e-ffffff5f5f5f" -PrincipalId "aaaaaaaa-bbbb-cccc-1111-222222222222" $params = Get-Parameters -data $result.Parameters - $params.ServicePrincipalId | Should -Be "02ed943d-6eca-4f99-83d6-e6fbf9dc63ae" + $params.ServicePrincipalId | Should -Be "00001111-aaaa-2222-bbbb-3333cccc4444" } It "Should contain 'User-Agent' header" { $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion New-EntraServicePrincipalAppRoleAssignment" - $result = New-EntraServicePrincipalAppRoleAssignment -ObjectId "02ed943d-6eca-4f99-83d6-e6fbf9dc63ae" -ResourceId "02ed943d-6eca-4f99-83d6-e6fbf9dc63ae" -Id "e18f0405-fdec-4ae8-a8a0-d8edb98b061f" -PrincipalId "d2d0a585-0c52-4bab-8c64-a096b98b061f" + $result = New-EntraServicePrincipalAppRoleAssignment -ObjectId "00001111-aaaa-2222-bbbb-3333cccc4444" -ResourceId "a0a0a0a0-bbbb-cccc-dddd-e1e1e1e1e1e1" -Id "bbbb1b1b-cc2c-dd3d-ee4e-ffffff5f5f5f" -PrincipalId "aaaaaaaa-bbbb-cccc-1111-222222222222" $result | Should -Not -BeNullOrEmpty $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion New-EntraServicePrincipalAppRoleAssignment" Should -Invoke -CommandName New-MgServicePrincipalAppRoleAssignment -ModuleName Microsoft.Graph.Entra -Times 1 -ParameterFilter { @@ -89,7 +89,7 @@ Describe "New-EntraServicePrincipalAppRoleAssignment"{ try { # Act & Assert: Ensure the function doesn't throw an exception - { New-EntraServicePrincipalAppRoleAssignment -ObjectId "02ed943d-6eca-4f99-83d6-e6fbf9dc63ae" -ResourceId "02ed943d-6eca-4f99-83d6-e6fbf9dc63ae" -Id "e18f0405-fdec-4ae8-a8a0-d8edb98b061f" -PrincipalId "d2d0a585-0c52-4bab-8c64-a096b98b061f" -Debug } | Should -Not -Throw + { New-EntraServicePrincipalAppRoleAssignment -ObjectId "00001111-aaaa-2222-bbbb-3333cccc4444" -ResourceId "a0a0a0a0-bbbb-cccc-dddd-e1e1e1e1e1e1" -Id "bbbb1b1b-cc2c-dd3d-ee4e-ffffff5f5f5f" -PrincipalId "aaaaaaaa-bbbb-cccc-1111-222222222222" -Debug } | Should -Not -Throw } finally { # Restore original confirmation preference $DebugPreference = $originalDebugPreference From e0b4682089cd34345cc914bf1edb0e6d84809da1 Mon Sep 17 00:00:00 2001 From: v-uansari Date: Wed, 25 Sep 2024 11:32:16 +0530 Subject: [PATCH 07/15] added json convert and depth --- .../Entra/Get-EntraDeletedApplication.Tests.ps1 | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/test/module/Entra/Get-EntraDeletedApplication.Tests.ps1 b/test/module/Entra/Get-EntraDeletedApplication.Tests.ps1 index f2c478d4a..36f50b989 100644 --- a/test/module/Entra/Get-EntraDeletedApplication.Tests.ps1 +++ b/test/module/Entra/Get-EntraDeletedApplication.Tests.ps1 @@ -65,36 +65,36 @@ Describe "Get-EntraDeletedApplication" { { Get-EntraDeletedApplication -DisplayName "abc" } | Should -Throw "A parameter cannot be found that matches parameter name 'DisplayName'*" } It "Should return specific application by searchstring" { - $result = Get-EntraDeletedApplication -SearchString 'Mock-test-App' | ConvertTo-Json | ConvertFrom-Json + $result = Get-EntraDeletedApplication -SearchString 'Mock-test-App' | ConvertTo-Json -Depth 10 | ConvertFrom-Json $result | Should -Not -BeNullOrEmpty $result.DisplayName | should -Be 'Mock-test-App' Should -Invoke -CommandName Get-MgDirectoryDeletedItemAsApplication -ModuleName Microsoft.Graph.Entra -Times 1 } It "Should return specific application by filter" { - $result = Get-EntraDeletedApplication -Filter "DisplayName -eq 'Mock-test-App'" | ConvertTo-Json | ConvertFrom-Json + $result = Get-EntraDeletedApplication -Filter "DisplayName -eq 'Mock-test-App'" | ConvertTo-Json -Depth 10 | ConvertFrom-Json $result | Should -Not -BeNullOrEmpty $result.DisplayName | should -Be 'Mock-test-App' Should -Invoke -CommandName Get-MgDirectoryDeletedItemAsApplication -ModuleName Microsoft.Graph.Entra -Times 1 } It "Should return top application" { - $result = Get-EntraDeletedApplication -Top 1 | ConvertTo-Json | ConvertFrom-Json + $result = Get-EntraDeletedApplication -Top 1 | ConvertTo-Json -Depth 10 | ConvertFrom-Json $result | Should -Not -BeNullOrEmpty Should -Invoke -CommandName Get-MgDirectoryDeletedItemAsApplication -ModuleName Microsoft.Graph.Entra -Times 1 } It "Result should Contain ObjectId" { - $result = Get-EntraDeletedApplication -Filter "DisplayName -eq 'Mock-test-App'" | ConvertTo-Json | ConvertFrom-Json + $result = Get-EntraDeletedApplication -Filter "DisplayName -eq 'Mock-test-App'" | ConvertTo-Json -Depth 10 | ConvertFrom-Json $result.ObjectId | should -Be "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" } It "Should contain Filter in parameters when passed SearchString to it" { - $result = Get-EntraDeletedApplication -SearchString 'Mock-test-App' | ConvertTo-Json | ConvertFrom-Json + $result = Get-EntraDeletedApplication -SearchString 'Mock-test-App' | ConvertTo-Json -Depth 10| ConvertFrom-Json $params = Get-Parameters -data $result.Parameters $params.Filter | Should -Match "Mock-test-App" } It "Property parameter should work" { - $result = Get-EntraDeletedApplication -Property DisplayName + $result = Get-EntraDeletedApplication -Property "DisplayName" | ConvertTo-Json | ConvertFrom-Json $result | Should -Not -BeNullOrEmpty $result.DisplayName | Should -Be "Mock-test-App" @@ -106,7 +106,7 @@ Describe "Get-EntraDeletedApplication" { } It "Should contain 'User-Agent' header" { $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraDeletedApplication" - $result = Get-EntraDeletedApplication -Filter "DisplayName -eq 'Mock-test-App'" | ConvertTo-Json | ConvertFrom-Json + $result = Get-EntraDeletedApplication -Filter "DisplayName -eq 'Mock-test-App'" | ConvertTo-Json -Depth 10 | ConvertFrom-Json $result | Should -Not -BeNullOrEmpty $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraDeletedApplication" Should -Invoke -CommandName Get-MgDirectoryDeletedItemAsApplication -ModuleName Microsoft.Graph.Entra -Times 1 -ParameterFilter { From 982aac33d056b059ac49c569c136010a14e601ad Mon Sep 17 00:00:00 2001 From: "Snehal Kotwal (Perennial Systems Inc)" Date: Mon, 23 Sep 2024 12:44:35 +0530 Subject: [PATCH 08/15] updated mock test cases --- ...et-EntraApplicationKeyCredential.Tests.ps1 | 64 ++++++++ .../Entra/Get-EntraApplicationOwner.Tests.ps1 | 54 +++++++ ...traApplicationPasswordCredential.Tests.ps1 | 67 +++++++++ .../Get-EntraDeletedApplication.Tests.ps1 | 140 ++++++++++++++++++ .../Get-EntraDeletedDirectoryObject.Tests.ps1 | 66 +++++++++ ...EntraPermissionGrantConditionSet.Tests.ps1 | 77 ++++++++++ ...-EntraServicePrincipalMembership.Tests.ps1 | 86 +++++++++++ ...cePrincipalOAuth2PermissionGrant.tests.ps1 | 90 +++++++++++ .../Get-EntraServicePrincipalOwner.Tests.ps1 | 110 ++++++++++++++ ...ntraApplicationExtensionProperty.Tests.ps1 | 3 - .../New-EntraApplicationPassword.Tests.ps1 | 90 +++++++++++ ...traApplicationPasswordCredential.Tests.ps1 | 96 ++++++++++++ ...EntraPermissionGrantConditionSet.Tests.ps1 | 75 ++++++++++ ...ervicePrincipalAppRoleAssignment.Tests.ps1 | 99 +++++++++++++ .../Remove-EntraApplicationOwner.Tests.ps1 | 60 ++++++++ .../Remove-EntraApplicationPassword.Tests.ps1 | 60 ++++++++ ...traApplicationPasswordCredential.Tests.ps1 | 63 ++++++++ ...EntraPermissionGrantConditionSet.Tests.ps1 | 67 +++++++++ 18 files changed, 1364 insertions(+), 3 deletions(-) create mode 100644 test/module/Entra/Get-EntraApplicationKeyCredential.Tests.ps1 create mode 100644 test/module/Entra/Get-EntraApplicationOwner.Tests.ps1 create mode 100644 test/module/Entra/Get-EntraApplicationPasswordCredential.Tests.ps1 create mode 100644 test/module/Entra/Get-EntraDeletedApplication.Tests.ps1 create mode 100644 test/module/Entra/Get-EntraDeletedDirectoryObject.Tests.ps1 create mode 100644 test/module/Entra/Get-EntraPermissionGrantConditionSet.Tests.ps1 create mode 100644 test/module/Entra/Get-EntraServicePrincipalMembership.Tests.ps1 create mode 100644 test/module/Entra/Get-EntraServicePrincipalOAuth2PermissionGrant.tests.ps1 create mode 100644 test/module/Entra/Get-EntraServicePrincipalOwner.Tests.ps1 create mode 100644 test/module/Entra/New-EntraApplicationPassword.Tests.ps1 create mode 100644 test/module/Entra/New-EntraApplicationPasswordCredential.Tests.ps1 create mode 100644 test/module/Entra/New-EntraPermissionGrantConditionSet.Tests.ps1 create mode 100644 test/module/Entra/New-EntraServicePrincipalAppRoleAssignment.Tests.ps1 create mode 100644 test/module/Entra/Remove-EntraApplicationOwner.Tests.ps1 create mode 100644 test/module/Entra/Remove-EntraApplicationPassword.Tests.ps1 create mode 100644 test/module/Entra/Remove-EntraApplicationPasswordCredential.Tests.ps1 create mode 100644 test/module/Entra/Set-EntraPermissionGrantConditionSet.Tests.ps1 diff --git a/test/module/Entra/Get-EntraApplicationKeyCredential.Tests.ps1 b/test/module/Entra/Get-EntraApplicationKeyCredential.Tests.ps1 new file mode 100644 index 000000000..b52c3afd9 --- /dev/null +++ b/test/module/Entra/Get-EntraApplicationKeyCredential.Tests.ps1 @@ -0,0 +1,64 @@ +# ------------------------------------------------------------------------------ +# 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 = { + @{ + "KeyCredentials" = @( + @{ + "CustomKeyIdentifier" = "" + "EndDate" = "10/23/2024 11:36:56 AM" + "KeyId" = "aaaaaaaa-0b0b-1c1c-2d2d-333333333333" + "StartDate" = "11/22/2023 11:35:16 AM" + "Type" = "Symmetric" + "Usage" = "Sign" + "Value" = "" + "Parameters" = $args + } + ) + } + } + + Mock -CommandName Get-MgApplication -MockWith $scriptblock -ModuleName Microsoft.Graph.Entra + } + + Describe "Get-EntraApplicationKeyCredential" { + Context "Test for Get-EntraApplicationKeyCredential" { + It "Should not return empty" { + $result = Get-EntraApplicationKeyCredential -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + $result | Should -Not -BeNullOrEmpty + Should -Invoke -CommandName Get-MgApplication -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Should fail when ObjectId is empty" { + { Get-EntraApplicationKeyCredential -ObjectId "" } | Should -Throw "Cannot bind argument to parameter 'ObjectId' because it is an empty string." + } + It "Should contain 'User-Agent' header" { + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraApplicationKeyCredential" + $result = Get-EntraApplicationKeyCredential -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + $result | Should -Not -BeNullOrEmpty + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraApplicationKeyCredential" + Should -Invoke -CommandName Get-MgApplication -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 + { Get-EntraApplicationKeyCredential -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -Debug } | Should -Not -Throw + } finally { + # Restore original confirmation preference + $DebugPreference = $originalDebugPreference + } + } + } + } \ No newline at end of file diff --git a/test/module/Entra/Get-EntraApplicationOwner.Tests.ps1 b/test/module/Entra/Get-EntraApplicationOwner.Tests.ps1 new file mode 100644 index 000000000..e1e0cbbcc --- /dev/null +++ b/test/module/Entra/Get-EntraApplicationOwner.Tests.ps1 @@ -0,0 +1,54 @@ +# ------------------------------------------------------------------------------ +# 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 + + $mockResponse = { + return @{ + value = @( + @{ + Id = "e3108c4d-86ff-4ceb-9429-24e85b4b8cea" + ageGroup = $null + onPremisesLastSyncDateTime = $null + creationType = $null + imAddresses = {"adelev@m365x99297270.onmicrosoft.com"} + preferredLanguage = $null + mail = "AdeleV@M365x99297270.OnMicrosoft.com" + securityIdentifier = "S-1-12-1-1093396945-1080104032-2731339150-364051459" + identities = {} + consentProvidedForMinor = $null + onPremisesUserPrincipalName = $null + Parameters = $args + } + ) + } + } + + Mock -CommandName Invoke-GraphRequest -MockWith $mockResponse -ModuleName Microsoft.Graph.Entra +} + +Describe "Get-EntraApplicationOwner"{ + Context "Test for Get-EntraApplicationOwner"{ + It "Should return application owner" { + $result = Get-EntraApplicationOwner -ObjectId "e3108c4d-86ff-4ceb-9429-24e85b4b8cea" + $result | Should -Not -BeNullOrEmpty + $result.Id | should -Be @('e3108c4d-86ff-4ceb-9429-24e85b4b8cea') + + Should -Invoke -CommandName Invoke-GraphRequest -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Should fail when ObjectId is empty" { + { Get-EntraApplicationOwner -ObjectId "" } | Should -Throw "Cannot bind argument to parameter 'ObjectId' because it is an empty string." + } + It "Should contain 'User-Agent' header" { + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraApplicationOwner" + $result = Get-EntraApplicationOwner -ObjectId "e3108c4d-86ff-4ceb-9429-24e85b4b8cea" + $params = Get-Parameters -data $result.Parameters + $a= $params | ConvertTo-json | ConvertFrom-Json + $a.headers.'User-Agent' | Should -Be $userAgentHeaderValue + } + } +} \ No newline at end of file diff --git a/test/module/Entra/Get-EntraApplicationPasswordCredential.Tests.ps1 b/test/module/Entra/Get-EntraApplicationPasswordCredential.Tests.ps1 new file mode 100644 index 000000000..0ca03b400 --- /dev/null +++ b/test/module/Entra/Get-EntraApplicationPasswordCredential.Tests.ps1 @@ -0,0 +1,67 @@ +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 = { + @{ + "PasswordCredentials" = @( + @{ + "CustomKeyIdentifier" = {116, 101, 115, 116} + "DisplayName" = "" + "EndDateTime" = "10/23/2024 11:36:56 AM" + "KeyId" = "aaaaaaaa-0b0b-1c1c-2d2d-333333333333" + "StartDateTime" = "11/22/2023 11:35:16 AM" + "Hint" = "123" + "SecretText" = "" + "Parameters" = $args + } + ) + } + } + + Mock -CommandName Get-MgApplication -MockWith $scriptblock -ModuleName Microsoft.Graph.Entra + } + + Describe "Get-EntraApplicationPasswordCredential" { + Context "Test for Get-EntraApplicationPasswordCredential" { + It "Should not return empty" { + $result = Get-EntraApplicationPasswordCredential -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + $result | Should -Not -BeNullOrEmpty + Should -Invoke -CommandName Get-MgApplication -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Should fail when ObjectId is empty" { + { Get-EntraApplicationPasswordCredential -ObjectId "" } | Should -Throw "Cannot bind argument to parameter 'ObjectId'*" + } + It "Should fail when ObjectId is null" { + { Get-EntraApplicationPasswordCredential -ObjectId } | Should -Throw "Missing an argument for parameter 'ObjectId'*" + } + It "Should fail when invalid parameter is passed" { + { Get-EntraApplicationPasswordCredential -DisplayName "abc" } | Should -Throw "A parameter cannot be found that matches parameter name 'DisplayName'*" + } + It "Should contain 'User-Agent' header" { + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraApplicationPasswordCredential" + $result = Get-EntraApplicationPasswordCredential -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + $result | Should -Not -BeNullOrEmpty + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraApplicationPasswordCredential" + Should -Invoke -CommandName Get-MgApplication -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 + { Get-EntraApplicationPasswordCredential -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb"-Debug } | Should -Not -Throw + } finally { + # Restore original confirmation preference + $DebugPreference = $originalDebugPreference + } + } + } + } \ No newline at end of file diff --git a/test/module/Entra/Get-EntraDeletedApplication.Tests.ps1 b/test/module/Entra/Get-EntraDeletedApplication.Tests.ps1 new file mode 100644 index 000000000..282f27169 --- /dev/null +++ b/test/module/Entra/Get-EntraDeletedApplication.Tests.ps1 @@ -0,0 +1,140 @@ +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 = { + return @( + [PSCustomObject]@{ + "AddIns" = {} + "AppRoles" = {} + "GroupMembershipClaims" = {} + "IdentifierUris" = {} + "Info" = @{ + LogoUrl=""; + MarketingUrl=""; + PrivacyStatementUrl=""; + SupportUrl=""; + TermsOfServiceUrl=""; + } + "IsDeviceOnlyAuthSupported" = $null + "KeyCredentials" = {} + "OptionalClaims" = {} + "ParentalControlSettings" = @{ + CountriesBlockedForMinors=@{}; + LegalAgeGroupRule="Allow"; + } + "PasswordCredentials" = {} + "Api" = @{ + AcceptMappedClaims=@{}; + KnownClientApplications=@{}; + Oauth2PermissionScopes=@{}; + PreAuthorizedApplications=@{}; + RequestedAccessTokenVersion="2"; + } + "PublicClient" = @{ + RedirectUris=@{}; + } + "PublisherDomain" = "M365x99297270.onmicrosoft.com" + "Web" = @{ + HomePageUrl=""; + ImplicitGrantSettings=""; + LogoutUrl=""; + RedirectUriSettings=@{}; + RedirectUris=@{}; + } + "RequiredResourceAccess" = $null + "AppId" = "9c17362d-20b6-4572-bb6f-600e57c840e5" + "AppManagementPolicies" = $null + "ApplicationTemplateId" = $null + "Certification" = {} + "CreatedDateTime" = "12/12/2023 11:28:45 AM" + "CreatedOnBehalfOf" = {} + "DefaultRedirectUri" = $null + "DeletedDateTime" = "12/12/2023 12:23:39 PM" + "Description" = $null + "DisabledByMicrosoftStatus" = $null + "DisplayName" = "Mock-test-App" + "ExtensionProperties" = $null + "FederatedIdentityCredentials" = $null + "HomeRealmDiscoveryPolicies" = $null + "Id" = "01157307-373c-47b0-889a-3bc57033d73e" + "IsFallbackPublicClient" = $null + "Logo" = $null + "Notes" = $null + "Oauth2RequirePostResponse" = $null + "Owners" = $null + "RequestSignatureVerification" = {} + "SamlMetadataUrl" = $null + "ServiceManagementReference" = $null + "ServicePrincipalLockConfiguration" = {} + "SignInAudience" = "AzureADandPersonalMicrosoftAccount" + "Spa" = {} + "Synchronization" = {} + "Tags" = {} + "TokenEncryptionKeyId" = $null + "TokenIssuancePolicies" = $null + "TokenLifetimePolicies" = $null + "UniqueName" = $null + "VerifiedPublisher" = {} + "AdditionalProperties" = {} + "Parameters" = $args + } + ) + } + + Mock -CommandName Get-MgDirectoryDeletedItemAsApplication -MockWith $scriptblock -ModuleName Microsoft.Graph.Entra +} + +Describe "Get-EntraDeletedApplication" { + Context "Test for Get-EntraDeletedApplication" { + It "Should return all applications" { + $result = Get-EntraDeletedApplication | ConvertTo-Json | ConvertFrom-Json + $result | Should -Not -BeNullOrEmpty + Should -Invoke -CommandName Get-MgDirectoryDeletedItemAsApplication -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Should fail when All is empty" { + { Get-EntraDeletedApplication -All } | Should -Throw "Missing an argument for parameter 'All'*" + } + It "Should fail when invalid parameter is passed" { + { Get-EntraDeletedApplication -DisplayName "abc" } | Should -Throw "A parameter cannot be found that matches parameter name 'DisplayName'*" + } + It "Should return specific application by searchstring" { + $result = Get-EntraDeletedApplication -SearchString 'Mock-test-App' | ConvertTo-Json | ConvertFrom-Json + $result | Should -Not -BeNullOrEmpty + $result.DisplayName | should -Be 'Mock-test-App' + + Should -Invoke -CommandName Get-MgDirectoryDeletedItemAsApplication -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Should return specific application by filter" { + $result = Get-EntraDeletedApplication -Filter "DisplayName -eq 'Mock-test-App'" | ConvertTo-Json | ConvertFrom-Json + $result | Should -Not -BeNullOrEmpty + $result.DisplayName | should -Be 'Mock-test-App' + + Should -Invoke -CommandName Get-MgDirectoryDeletedItemAsApplication -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Should return top application" { + $result = Get-EntraDeletedApplication -Top 1 | ConvertTo-Json | ConvertFrom-Json + $result | Should -Not -BeNullOrEmpty + + Should -Invoke -CommandName Get-MgDirectoryDeletedItemAsApplication -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Result should Contain ObjectId" { + $result = Get-EntraDeletedApplication -Filter "DisplayName -eq 'Mock-test-App'" | ConvertTo-Json | ConvertFrom-Json + $result.ObjectId | should -Be "01157307-373c-47b0-889a-3bc57033d73e" + } + It "Should contain Filter in parameters when passed SearchString to it" { + $result = Get-EntraDeletedApplication -SearchString 'Mock-test-App' | ConvertTo-Json | ConvertFrom-Json + $params = Get-Parameters -data $result.Parameters + $params.Filter | Should -Match "Mock-test-App" + } + It "Should contain 'User-Agent' header" { + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraDeletedApplication" + $result = Get-EntraDeletedApplication -Top 1 | ConvertTo-Json | ConvertFrom-Json + $params = Get-Parameters -data $result.Parameters + $a= $params | ConvertTo-json | ConvertFrom-Json + $a.headers.'User-Agent' | Should -Be $userAgentHeaderValue + } + } +} \ No newline at end of file diff --git a/test/module/Entra/Get-EntraDeletedDirectoryObject.Tests.ps1 b/test/module/Entra/Get-EntraDeletedDirectoryObject.Tests.ps1 new file mode 100644 index 000000000..767d92c46 --- /dev/null +++ b/test/module/Entra/Get-EntraDeletedDirectoryObject.Tests.ps1 @@ -0,0 +1,66 @@ +# ------------------------------------------------------------------------------ +# 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 = { + return @( + [PSCustomObject]@{ + "Id" = "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + "AdditionalProperties" = @{DisplayName="Test-App";} + "DeletedDateTime" = "2/2/2024 5:33:56 AM" + "Parameters" = $args + } + ) + } + + Mock -CommandName Get-MgDirectoryDeletedItem -MockWith $scriptblock -ModuleName Microsoft.Graph.Entra +} + +Describe "Get-EntraDeletedDirectoryObject"{ + It "Should fail when Id is empty" { + { Get-EntraDeletedDirectoryObject -Id "" } | Should -Throw "Cannot bind argument to parameter 'Id'*" + } + It "Should fail when Id is null" { + { Get-EntraDeletedDirectoryObject -Id } | Should -Throw "Missing an argument for parameter 'Id'*" + } + It "Should fail when invalid parameter is passed" { + { Get-EntraDeletedDirectoryObject -DisplayName "abc" } | Should -Throw "A parameter cannot be found that matches parameter name 'DisplayName'*" + } + It "Result should Contain ObjectId" { + $result = Get-EntraDeletedDirectoryObject -Id "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + $result.ObjectId | should -Be "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + } + It "Should contain DirectoryObjectId in parameters when passed Id to it" { + $result = Get-EntraDeletedDirectoryObject -Id "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + $params = Get-Parameters -data $result.Parameters + $params.DirectoryObjectId | Should -Be "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + } + It "Should contain 'User-Agent' header" { + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraDeletedDirectoryObject" + $result = Get-EntraDeletedDirectoryObject -Id "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + $result | Should -Not -BeNullOrEmpty + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraDeletedDirectoryObject" + Should -Invoke -CommandName Get-MgDirectoryDeletedItem -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 + { Get-EntraDeletedDirectoryObject -Id "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -Debug } | Should -Not -Throw + } finally { + # Restore original confirmation preference + $DebugPreference = $originalDebugPreference + } + } +} \ No newline at end of file diff --git a/test/module/Entra/Get-EntraPermissionGrantConditionSet.Tests.ps1 b/test/module/Entra/Get-EntraPermissionGrantConditionSet.Tests.ps1 new file mode 100644 index 000000000..fb1262047 --- /dev/null +++ b/test/module/Entra/Get-EntraPermissionGrantConditionSet.Tests.ps1 @@ -0,0 +1,77 @@ +# ------------------------------------------------------------------------------ +# 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 = { + return @( + [PSCustomObject]@{ + "Id" = "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + "ClientApplicationIds" = {"All"} + "ClientApplicationPublisherIds" = {"All"} + "ClientApplicationTenantIds" = {"All"} + "ClientApplicationsFromVerifiedPublisherOnly" = $true + "Parameters" = $args + } + ) + } + + Mock -CommandName Get-MgPolicyPermissionGrantPolicyInclude -MockWith $scriptblock -ModuleName Microsoft.Graph.Entra + + Mock -CommandName Get-MgPolicyPermissionGrantPolicyExclude -MockWith $scriptblock -ModuleName Microsoft.Graph.Entra +} +Describe "Get-EntraMSPermissionGrantConditionSet"{ + It "Should not return empty object for condition set 'includes'"{ + $result = Get-EntraPermissionGrantConditionSet -PolicyId "policy1" -ConditionSetType "includes" -Id "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + $result | Should -Not -BeNullOrEmpty + Should -Invoke -CommandName Get-MgPolicyPermissionGrantPolicyInclude -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Should not return empty object for condition set 'excludes'"{ + $result = Get-EntraPermissionGrantConditionSet -PolicyId "policy1" -ConditionSetType "excludes" -Id "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + $result | Should -Not -BeNullOrEmpty + Should -Invoke -CommandName Get-MgPolicyPermissionGrantPolicyExclude -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Should fail when parameters are empty" { + { Get-EntraPermissionGrantConditionSet -PolicyId "" -ConditionSetType "" -Id ""} | Should -Throw "Cannot bind argument to parameter*" + } + It "Should fail when parameters are null" { + { Get-EntraPermissionGrantConditionSet -PolicyId -ConditionSetType -Id} | Should -Throw "Missing an argument for parameter*" + } + It "Should contain PermissionGrantConditionSetId in parameters when passed Id to it" { + $result = Get-EntraPermissionGrantConditionSet -PolicyId "policy1" -ConditionSetType "includes" -Id "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + $params = Get-Parameters -data $result.Parameters + $params.PermissionGrantConditionSetId | Should -Be "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + } + It "Should contain PermissionGrantPolicyId in parameters when passed PolicyId to it" { + $result = Get-EntraPermissionGrantConditionSet -PolicyId "policy1" -ConditionSetType "includes" -Id "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + $params = Get-Parameters -data $result.Parameters + $params.PermissionGrantPolicyId | Should -Be "policy1" + } + It "Should contain 'User-Agent' header" { + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraPermissionGrantConditionSet" + $result = Get-EntraPermissionGrantConditionSet -PolicyId "policy1" -ConditionSetType "includes" -Id "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + $result | Should -Not -BeNullOrEmpty + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraPermissionGrantConditionSet" + Should -Invoke -CommandName Get-MgPolicyPermissionGrantPolicyInclude -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 + { Get-EntraPermissionGrantConditionSet -PolicyId "policy1" -ConditionSetType "includes" -Id "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -Debug } | Should -Not -Throw + } finally { + # Restore original confirmation preference + $DebugPreference = $originalDebugPreference + } + } +} \ No newline at end of file diff --git a/test/module/Entra/Get-EntraServicePrincipalMembership.Tests.ps1 b/test/module/Entra/Get-EntraServicePrincipalMembership.Tests.ps1 new file mode 100644 index 000000000..880f4ceb3 --- /dev/null +++ b/test/module/Entra/Get-EntraServicePrincipalMembership.Tests.ps1 @@ -0,0 +1,86 @@ +# ------------------------------------------------------------------------------ +# 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 = { + return @( + [PSCustomObject]@{ + "Id" = "00aa00aa-bb11-cc22-dd33-44ee44ee44ee" + "AdditionalProperties" = @{DeletedDateTime = $null} + "Parameters" = $args + } + ) + } + + Mock -CommandName Get-MgServicePrincipalTransitiveMemberOf -MockWith $scriptblock -ModuleName Microsoft.Graph.Entra +} +Describe "Get-EntraServicePrincipalMembership"{ + It "Result should not be empty" { + $result = Get-EntraServicePrincipalMembership -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + $result | Should -Not -BeNullOrEmpty + Should -Invoke -CommandName Get-MgServicePrincipalTransitiveMemberOf -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Should fail when ObjectId is empty" { + { Get-EntraServicePrincipalMembership -ObjectId "" } | Should -Throw "Cannot bind argument to parameter 'ObjectId' because it is an empty string." + } + It "Should return all applications" { + $result = Get-EntraServicePrincipalMembership -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -All + $result | Should -Not -BeNullOrEmpty + Should -Invoke -CommandName Get-MgServicePrincipalTransitiveMemberOf -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Should fail when All has an argument" { + { Get-EntraServicePrincipalMembership -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -All $true} | Should -Throw "A positional parameter cannot be found that accepts argument 'True'." + } + It "Should return top application" { + $result = Get-EntraServicePrincipalMembership -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -Top 1 + $result | Should -Not -BeNullOrEmpty + Should -Invoke -CommandName Get-MgServicePrincipalTransitiveMemberOf -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Result should Contain ObjectId" { + $result = Get-EntraServicePrincipalMembership -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-EntraServicePrincipalMembership -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + $params = Get-Parameters -data $result.Parameters + $params.ServicePrincipalId | Should -Be "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + } + It "Property parameter should work" { + $result = Get-EntraServicePrincipalMembership -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-MgServicePrincipalTransitiveMemberOf -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Should fail when Property is empty" { + { Get-EntraServicePrincipalMembership -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -Property } | Should -Throw "Missing an argument for parameter 'Property'*" + } + It "Should contain 'User-Agent' header" { + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraServicePrincipalMembership" + $result = Get-EntraServicePrincipalMembership -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + $result | Should -Not -BeNullOrEmpty + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraServicePrincipalMembership" + Should -Invoke -CommandName Get-MgServicePrincipalTransitiveMemberOf -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 + { Get-EntraServicePrincipalMembership -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -Debug } | Should -Not -Throw + } finally { + # Restore original confirmation preference + $DebugPreference = $originalDebugPreference + } + } +} \ No newline at end of file diff --git a/test/module/Entra/Get-EntraServicePrincipalOAuth2PermissionGrant.tests.ps1 b/test/module/Entra/Get-EntraServicePrincipalOAuth2PermissionGrant.tests.ps1 new file mode 100644 index 000000000..12b687bb3 --- /dev/null +++ b/test/module/Entra/Get-EntraServicePrincipalOAuth2PermissionGrant.tests.ps1 @@ -0,0 +1,90 @@ +# ------------------------------------------------------------------------------ +# 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 = { + return @( + [PSCustomObject]@{ + "Id" = "aaaaaaaa-0b0b-1c1c-2d2d-333333333333" + "ClientId" = "4773e0f6-b400-40b3-8508-340de8ee0893" + "ConsentType" = "AllPrincipals" + "PrincipalId" = "aaaaaaaa-bbbb-cccc-1111-222222222222" + "ResourceId" = "a0a0a0a0-bbbb-cccc-dddd-e1e1e1e1e1e1" + "Scope" = "openid" + "Parameters" = $args + } + ) + } + + Mock -CommandName Get-MgServicePrincipalOauth2PermissionGrant -MockWith $scriptblock -ModuleName Microsoft.Graph.Entra +} +Describe "Get-EntraServicePrincipalOAuth2PermissionGrant"{ + It "Result should not be empty" { + $result = Get-EntraServicePrincipalOAuth2PermissionGrant -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + $result | Should -Not -BeNullOrEmpty + Should -Invoke -CommandName Get-MgServicePrincipalOauth2PermissionGrant -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Should fail when ObjectId is empty" { + { Get-EntraServicePrincipalOAuth2PermissionGrant -ObjectId "" } | Should -Throw "Cannot bind argument to parameter 'ObjectId' because it is an empty string." + } + It "Should return all applications" { + $result = Get-EntraServicePrincipalOAuth2PermissionGrant -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -All + $result | Should -Not -BeNullOrEmpty + Should -Invoke -CommandName Get-MgServicePrincipalOauth2PermissionGrant -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Should fail when All has an argument" { + { Get-EntraServicePrincipalOAuth2PermissionGrant -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -All $true} | Should -Throw "A positional parameter cannot be found that accepts argument 'True'." + } + It "Should return top application" { + $result = Get-EntraServicePrincipalOAuth2PermissionGrant -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -Top 1 + $result | Should -Not -BeNullOrEmpty + Should -Invoke -CommandName Get-MgServicePrincipalOauth2PermissionGrant -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Result should Contain ObjectId" { + $result = Get-EntraServicePrincipalOAuth2PermissionGrant -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + $result.ObjectId | should -Be "aaaaaaaa-0b0b-1c1c-2d2d-333333333333" + } + It "Should contain ServicePrincipalId in parameters when passed ObjectId to it" { + $result = Get-EntraServicePrincipalOAuth2PermissionGrant -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + $params = Get-Parameters -data $result.Parameters + $params.ServicePrincipalId | Should -Be "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + } + It "Property parameter should work" { + $result = Get-EntraServicePrincipalOAuth2PermissionGrant -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -Property Id + $result | Should -Not -BeNullOrEmpty + $result.Id | Should -Be 'aaaaaaaa-0b0b-1c1c-2d2d-333333333333' + + Should -Invoke -CommandName Get-MgServicePrincipalOauth2PermissionGrant -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Should fail when Property is empty" { + { Get-EntraServicePrincipalOAuth2PermissionGrant -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -Property } | Should -Throw "Missing an argument for parameter 'Property'*" + } + It "Should contain 'User-Agent' header" { + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraServicePrincipalOAuth2PermissionGrant" + $result = Get-EntraServicePrincipalOAuth2PermissionGrant -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + $result | Should -Not -BeNullOrEmpty + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraServicePrincipalOAuth2PermissionGrant" + Should -Invoke -CommandName Get-MgServicePrincipalOauth2PermissionGrant -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 + { Get-EntraServicePrincipalOAuth2PermissionGrant -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -Debug } | Should -Not -Throw + } finally { + # Restore original confirmation preference + $DebugPreference = $originalDebugPreference + } + } +} \ No newline at end of file diff --git a/test/module/Entra/Get-EntraServicePrincipalOwner.Tests.ps1 b/test/module/Entra/Get-EntraServicePrincipalOwner.Tests.ps1 new file mode 100644 index 000000000..d80d1a2f3 --- /dev/null +++ b/test/module/Entra/Get-EntraServicePrincipalOwner.Tests.ps1 @@ -0,0 +1,110 @@ +# ------------------------------------------------------------------------------ +# 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 = { + return @( + [PSCustomObject]@{ + "Id" = "00aa00aa-bb11-cc22-dd33-44ee44ee44ee" + "DisplayName" = "Adams Smith" + "UserPrincipalName" = "Adams@contoso.com" + "UserType" = "Member" + "appRoles" = @{ + allowedMemberTypes=$null; + description="msiam_access"; + displayName="msiam_access"; + id="d0d7e4e4-96be-41c9-805a-08e0526868ad"; + isEnabled=$True; + origin="Application" + } + "oauth2PermissionScopes" = @{ + adminConsentDescription="Allow the application to access from tmplate test 3 on behalf of the signed-in user."; + adminConsentDisplayName="Access from tmplate test 3"; + id="64c2cef3-e118-4795-a580-a32bdbd7ba88"; + isEnabled=$True; + type="User"; + userConsentDescription="Allow the application to access from tmplate test 3 on your behalf."; + userConsentDisplayName="Access from tmplate test 3"; + value="user_impersonation" + } + "AdditionalProperties" = @{ + "@odata.type" = "#microsoft.graph.servicePrincipal"; + accountEnabled = $true + } + "Parameters" = $args + } + ) + } + + Mock -CommandName Get-MgServicePrincipalOwner -MockWith $scriptblock -ModuleName Microsoft.Graph.Entra +} +Describe "Get-EntraServicePrincipalOwner"{ + It "Result should not be empty" { + $result = Get-EntraServicePrincipalOwner -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + $result | Should -Not -BeNullOrEmpty + Should -Invoke -CommandName Get-MgServicePrincipalOwner -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Should fail when ObjectId is empty" { + { Get-EntraServicePrincipalOwner -ObjectId "" } | Should -Throw "Cannot bind argument to parameter 'ObjectId' because it is an empty string." + } + It "Should return all applications" { + $result = Get-EntraServicePrincipalOwner -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -All + $result | Should -Not -BeNullOrEmpty + Should -Invoke -CommandName Get-MgServicePrincipalOwner -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Should fail when All has an argument" { + { Get-EntraServicePrincipalOwner -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -All $true} | Should -Throw "A positional parameter cannot be found that accepts argument 'True'." + } + It "Should return top application" { + $result = Get-EntraServicePrincipalOwner -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -Top 1 + $result | Should -Not -BeNullOrEmpty + Should -Invoke -CommandName Get-MgServicePrincipalOwner -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Result should Contain ObjectId" { + $result = Get-EntraServicePrincipalOwner -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-EntraServicePrincipalOwner -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + $params = Get-Parameters -data $result.Parameters + $params.ServicePrincipalId | Should -Be "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + } + It "Property parameter should work" { + $result = Get-EntraServicePrincipalOwner -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -Property DisplayName + $result | Should -Not -BeNullOrEmpty + $result.DisplayName | Should -Be 'Adams Smith' + + Should -Invoke -CommandName Get-MgServicePrincipalOwner -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Should fail when Property is empty" { + { Get-EntraServicePrincipalOwner -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -Property } | Should -Throw "Missing an argument for parameter 'Property'*" + } + It "Should contain 'User-Agent' header" { + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraServicePrincipalOwner" + $result = Get-EntraServicePrincipalOwner -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + $result | Should -Not -BeNullOrEmpty + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraServicePrincipalOwner" + Should -Invoke -CommandName Get-MgServicePrincipalOwner -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 + {Get-EntraServicePrincipalOwner -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -Debug } | Should -Not -Throw + } finally { + # Restore original confirmation preference + $DebugPreference = $originalDebugPreference + } + } +} \ No newline at end of file diff --git a/test/module/Entra/New-EntraApplicationExtensionProperty.Tests.ps1 b/test/module/Entra/New-EntraApplicationExtensionProperty.Tests.ps1 index ce8ccc529..6d0cdbf7e 100644 --- a/test/module/Entra/New-EntraApplicationExtensionProperty.Tests.ps1 +++ b/test/module/Entra/New-EntraApplicationExtensionProperty.Tests.ps1 @@ -66,12 +66,9 @@ Context "Test for New-EntraApplicationExtensionProperty" { } It "Should contain 'User-Agent' header" { $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion New-EntraApplicationExtensionProperty" - $result = New-EntraApplicationExtensionProperty -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -DataType "MockType" -Name "Mock-App" -TargetObjects "Application" $result | Should -Not -BeNullOrEmpty - $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion New-EntraApplicationExtensionProperty" - Should -Invoke -CommandName New-MgApplicationExtensionProperty -ModuleName Microsoft.Graph.Entra -Times 1 -ParameterFilter { $Headers.'User-Agent' | Should -Be $userAgentHeaderValue $true diff --git a/test/module/Entra/New-EntraApplicationPassword.Tests.ps1 b/test/module/Entra/New-EntraApplicationPassword.Tests.ps1 new file mode 100644 index 000000000..5495c2e9e --- /dev/null +++ b/test/module/Entra/New-EntraApplicationPassword.Tests.ps1 @@ -0,0 +1,90 @@ +# ------------------------------------------------------------------------------ +# 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 = { + return @( + [PSCustomObject]@{ + "CustomKeyIdentifier" = $null + "DisplayName" = "mypassword" + "EndDateTime" = "10/23/2024 11:36:56 AM" + "KeyId" = "aaaaaaaa-0b0b-1c1c-2d2d-333333333333" + "StartDateTime" = "11/22/2023 11:35:16 AM" + "Hint" = "123" + "SecretText" = "wbBNW8kCuiPjNRg9NX98W_EaU6cqG" + "Parameters" = $args + } + ) + } + + Mock -CommandName Add-MgApplicationPassword -MockWith $scriptblock -ModuleName Microsoft.Graph.Entra +} +Describe "New-EntraApplicationPassword"{ + It "Should return created password credential"{ + $result = New-EntraApplicationPassword -ObjectID "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -PasswordCredential @{ displayname = "mypassword" } | ConvertTo-Json | ConvertFrom-Json + $result | Should -Not -BeNullOrEmpty + $result.KeyId | should -Be "aaaaaaaa-0b0b-1c1c-2d2d-333333333333" + $result.SecretText | Should -Be "wbBNW8kCuiPjNRg9NX98W_EaU6cqG" + Should -Invoke -CommandName Add-MgApplicationPassword -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Should fail when ObjectId is empty" { + { New-EntraApplicationPassword -ObjectId "" } | Should -Throw "Cannot bind argument to parameter 'ObjectId'*" + } + It "Should fail when ObjectId is null" { + { New-EntraApplicationPassword -ObjectId } | Should -Throw "Missing an argument for parameter 'ObjectId'*" + } + It "Should fail when PasswordCredential is null" { + { New-EntraApplicationPassword -PasswordCredential } | Should -Throw "Missing an argument for parameter 'PasswordCredential'*" + } + It "Should fail when StartDate is empty" { + { New-EntraApplicationPassword -ObjectID "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -PasswordCredential @{ StartDateTime = "" } } | Should -Throw "Cannot process argument transformation on parameter 'PasswordCredential'*" + } + It "Should fail when EndDate is empty" { + { New-EntraApplicationPassword -ObjectID "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -PasswordCredential @{ EndDateTime = "" } } | Should -Throw "Cannot process argument transformation on parameter 'PasswordCredential'*" + } + It "Should fail when invalid parameter is passed" { + { New-EntraApplicationPassword -DisplayName "abc" } | Should -Throw "A parameter cannot be found that matches parameter name 'DisplayName'." + } + It "Should contain ApplicationId in parameters when passed ObjectId to it" { + $result = New-EntraApplicationPassword -ObjectID "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -PasswordCredential @{ displayname = "mypassword" } + $params = Get-Parameters -data $result.Parameters + $params.ApplicationId | Should -Be "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + } + It "should contain password credential parameters in body parameter when passed PasswordCredential to it"{ + $result = New-EntraApplicationPassword -ObjectID "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -PasswordCredential @{ DisplayName = "mypassword"; Hint = "123"; StartDateTime=(get-date).AddYears(0); EndDateTime=(get-date).AddYears(2) } + $params = Get-Parameters -data $result.Parameters + $a = $params.PasswordCredential | ConvertTo-json | ConvertFrom-Json + $a.DisplayName | Should -Be "mypassword" + $a.Hint | Should -Be "123" + $a.StartDateTime | Should -Not -BeNullOrEmpty + $a.EndDateTime | Should -Not -BeNullOrEmpty + } + It "Should contain 'User-Agent' header" { + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion New-EntraApplicationPassword" + $result = New-EntraApplicationPassword -ObjectID "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -PasswordCredential @{ displayname = "mypassword" } | ConvertTo-Json | ConvertFrom-Json + $result | Should -Not -BeNullOrEmpty + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion New-EntraApplicationPassword" + Should -Invoke -CommandName Add-MgApplicationPassword -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-EntraApplicationPassword -ObjectID "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -PasswordCredential @{ displayname = "mypassword" } | ConvertTo-Json | ConvertFrom-Json -Debug } | Should -Not -Throw + } finally { + # Restore original confirmation preference + $DebugPreference = $originalDebugPreference + } + } +} \ No newline at end of file diff --git a/test/module/Entra/New-EntraApplicationPasswordCredential.Tests.ps1 b/test/module/Entra/New-EntraApplicationPasswordCredential.Tests.ps1 new file mode 100644 index 000000000..a83231fd8 --- /dev/null +++ b/test/module/Entra/New-EntraApplicationPasswordCredential.Tests.ps1 @@ -0,0 +1,96 @@ +# ------------------------------------------------------------------------------ +# 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 = { + return @( + [PSCustomObject]@{ + "CustomKeyIdentifier" = $null + "DisplayName" = "" + "EndDateTime" = "10/23/2024 11:36:56 AM" + "KeyId" = "aaaaaaaa-0b0b-1c1c-2d2d-333333333333" + "StartDateTime" = "11/22/2023 11:35:16 AM" + "Hint" = "123" + "SecretText" = "wbBNW8kCuiPjNRg9NX98W_EaU6cqG" + "Parameters" = $args + } + ) + } + + Mock -CommandName Add-MgApplicationPassword -MockWith $scriptblock -ModuleName Microsoft.Graph.Entra +} +Describe "New-EntraApplicationPasswordCredential"{ + It "Should return created password credential"{ + $result = New-EntraApplicationPasswordCredential -ObjectID "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + $result | Should -Not -BeNullOrEmpty + $result.KeyId | should -Be "aaaaaaaa-0b0b-1c1c-2d2d-333333333333" + $result.SecretText | Should -Be "wbBNW8kCuiPjNRg9NX98W_EaU6cqG" + Should -Invoke -CommandName Add-MgApplicationPassword -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Should fail when ObjectId is empty" { + { New-EntraApplicationPasswordCredential -ObjectId "" } | Should -Throw "Cannot bind argument to parameter 'ObjectId'*" + } + It "Should fail when ObjectId is null" { + { New-EntraApplicationPasswordCredential -ObjectId } | Should -Throw "Missing an argument for parameter 'ObjectId'*" + } + It "Should fail when StartDate is empty" { + { New-EntraApplicationPasswordCredential -ObjectID "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -StartDate "" } | Should -Throw "Cannot process argument transformation on parameter 'StartDate'*" + } + It "Should fail when StartDate is null" { + { New-EntraApplicationPasswordCredential -ObjectID "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -StartDate } | Should -Throw "Missing an argument for parameter 'StartDate'*" + } + It "Should fail when EndDate is empty" { + { New-EntraApplicationPasswordCredential -ObjectID "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -EndDate "" } | Should -Throw "Cannot process argument transformation on parameter 'EndDate'*" + } + It "Should fail when EndDate is null" { + { New-EntraApplicationPasswordCredential -ObjectID "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -EndDate } | Should -Throw "Missing an argument for parameter 'EndDate'*" + } + It "Should fail when invalid parameter is passed" { + { New-EntraApplicationPasswordCredential -DisplayName "abc" } | Should -Throw "A parameter cannot be found that matches parameter name 'DisplayName'." + } + It "Should contain ApplicationId in parameters when passed ObjectId to it" { + $result = New-EntraApplicationPasswordCredential -ObjectID "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + $params = Get-Parameters -data $result.Parameters + $params.ApplicationId | Should -Be "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + } + It "should contain startDateTime in body parameter when passed StartDate to it"{ + $result = New-EntraApplicationPasswordCredential -ObjectID "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -StartDate (get-date).AddYears(0) + $params = Get-Parameters -data $result.Parameters + $a = $params.PasswordCredential | ConvertTo-json | ConvertFrom-Json + $a.startDateTime | Should -Not -BeNullOrEmpty + } + It "should contain endDateTime in body parameter when passed EndDate to it"{ + $result = New-EntraApplicationPasswordCredential -ObjectID "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -EndDate (get-date).AddYears(0) + $params = Get-Parameters -data $result.Parameters + $a = $params.PasswordCredential | ConvertTo-json | ConvertFrom-Json + $a.endDateTime | Should -Not -BeNullOrEmpty + } + It "Should contain 'User-Agent' header" { + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion New-EntraApplicationPasswordCredential" + $result = New-EntraApplicationPasswordCredential -ObjectID "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + $result | Should -Not -BeNullOrEmpty + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion New-EntraApplicationPasswordCredential" + Should -Invoke -CommandName Add-MgApplicationPassword -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-EntraApplicationPasswordCredential -ObjectID "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -Debug } | Should -Not -Throw + } finally { + # Restore original confirmation preference + $DebugPreference = $originalDebugPreference + } + } +} \ No newline at end of file diff --git a/test/module/Entra/New-EntraPermissionGrantConditionSet.Tests.ps1 b/test/module/Entra/New-EntraPermissionGrantConditionSet.Tests.ps1 new file mode 100644 index 000000000..f992b4e5c --- /dev/null +++ b/test/module/Entra/New-EntraPermissionGrantConditionSet.Tests.ps1 @@ -0,0 +1,75 @@ +# ------------------------------------------------------------------------------ +# 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 = { + return @( + [PSCustomObject]@{ + "Id" = "00aa00aa-bb11-cc22-dd33-44ee44ee44ee" + "ClientApplicationIds" = {"All"} + "ClientApplicationPublisherIds" = {"All"} + "ClientApplicationTenantIds" = {"All"} + "ClientApplicationsFromVerifiedPublisherOnly" = $true + "PermissionClassification" = "all" + "PermissionType" = "delegated" + "Parameters" = $args + } + ) + } + + Mock -CommandName New-MgPolicyPermissionGrantPolicyInclude -MockWith $scriptblock -ModuleName Microsoft.Graph.Entra + + Mock -CommandName New-MgPolicyPermissionGrantPolicyExclude -MockWith $scriptblock -ModuleName Microsoft.Graph.Entra +} +Describe "New-EntraPermissionGrantConditionSet"{ + It "Should not return empty object for condition set 'includes'"{ + $result = New-EntraPermissionGrantConditionSet -PolicyId "test1" -ConditionSetType "includes" -PermissionType "delegated" + $result | Should -Not -BeNullOrEmpty + Should -Invoke -CommandName New-MgPolicyPermissionGrantPolicyInclude -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Should not return empty object for condition set 'excludes'"{ + $result = New-EntraPermissionGrantConditionSet -PolicyId "test1" -ConditionSetType "excludes" -PermissionType "delegated" + $result | Should -Not -BeNullOrEmpty + Should -Invoke -CommandName New-MgPolicyPermissionGrantPolicyExclude -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Should fail when parameters are empty" { + { New-EntraPermissionGrantConditionSet -PolicyId "" -ConditionSetType ""} | Should -Throw "Cannot bind argument to parameter*" + } + It "Should fail when parameters are null" { + { New-EntraPermissionGrantConditionSet -PolicyId -ConditionSetType } | Should -Throw "Missing an argument for parameter*" + } + It "Should contain PermissionGrantPolicyId in parameters when passed PolicyId to it" { + $result = New-EntraPermissionGrantConditionSet -PolicyId "test1" -ConditionSetType "includes" -PermissionType "delegated" + $params = Get-Parameters -data $result.Parameters + $params.PermissionGrantPolicyId | Should -Be "test1" + } + It "Should contain 'User-Agent' header" { + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion New-EntraPermissionGrantConditionSet" + $result = New-EntraPermissionGrantConditionSet -PolicyId "test1" -ConditionSetType "includes" -PermissionType "delegated" + $result | Should -Not -BeNullOrEmpty + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion New-EntraPermissionGrantConditionSet" + Should -Invoke -CommandName New-MgPolicyPermissionGrantPolicyInclude -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-EntraPermissionGrantConditionSet -PolicyId "test1" -ConditionSetType "includes" -PermissionType "delegated" -Debug } | Should -Not -Throw + } finally { + # Restore original confirmation preference + $DebugPreference = $originalDebugPreference + } + } + +} \ No newline at end of file diff --git a/test/module/Entra/New-EntraServicePrincipalAppRoleAssignment.Tests.ps1 b/test/module/Entra/New-EntraServicePrincipalAppRoleAssignment.Tests.ps1 new file mode 100644 index 000000000..1ea4e9273 --- /dev/null +++ b/test/module/Entra/New-EntraServicePrincipalAppRoleAssignment.Tests.ps1 @@ -0,0 +1,99 @@ +# ------------------------------------------------------------------------------ +# 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-MgServicePrincipalAppRoleAssignment with parameters: $($args | ConvertTo-Json -Depth 3)" + return @( + [PSCustomObject]@{ + "DeletedDateTime" = $null + "Id" = "gsx_zBushUevRyyjtwUohm_RMYjcGsJIjXwKOVMr3ww" + "PrincipalDisplayName" = "Mock-App" + "AppRoleId" = "e18f0405-fdec-4ae8-a8a0-d8edb98b061f" + "CreatedDateTime" = "3/12/2024 11:05:29 AM" + "PrincipalId" = "aaaaaaaa-bbbb-cccc-1111-222222222222" + "Parameters" = $args + } + ) + } + + Mock -CommandName New-MgServicePrincipalAppRoleAssignment -MockWith $scriptblock -ModuleName Microsoft.Graph.Entra +} + +Describe "New-EntraServicePrincipalAppRoleAssignment"{ + Context "Test for New-EntraServicePrincipalAppRoleAssignment" { + It "Should return New-EntraServicePrincipalAppRoleAssignment"{ + $result = New-EntraServicePrincipalAppRoleAssignment -ObjectId "02ed943d-6eca-4f99-83d6-e6fbf9dc63ae" -ResourceId "02ed943d-6eca-4f99-83d6-e6fbf9dc63ae" -Id "e18f0405-fdec-4ae8-a8a0-d8edb98b061f" -PrincipalId "d2d0a585-0c52-4bab-8c64-a096b98b061f" + $result | Should -Not -BeNullOrEmpty + $result.PrincipalDisplayName | should -Be "Mock-App" + $result.PrincipalId | should -Be "aaaaaaaa-bbbb-cccc-1111-222222222222" + + Should -Invoke -CommandName New-MgServicePrincipalAppRoleAssignment -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Should fail when ObjectId is empty" { + { New-EntraServicePrincipalAppRoleAssignment -ObjectId "" } | Should -Throw "Cannot bind argument to parameter 'ObjectId'*" + } + It "Should fail when ObjectId is null" { + { New-EntraServicePrincipalAppRoleAssignment -ObjectId } | Should -Throw "Missing an argument for parameter 'ObjectId'*" + } + It "Should fail when ResourceId is empty" { + { New-EntraServicePrincipalAppRoleAssignment -ResourceId "" } | Should -Throw "Cannot bind argument to parameter 'ResourceId'*" + } + It "Should fail when ResourceId is null" { + { New-EntraServicePrincipalAppRoleAssignment -ResourceId } | Should -Throw "Missing an argument for parameter 'ResourceId'*" + } + It "Should fail when Id is empty" { + { New-EntraServicePrincipalAppRoleAssignment -Id "" } | Should -Throw "Cannot bind argument to parameter 'Id'*" + } + It "Should fail when Id is null" { + { New-EntraServicePrincipalAppRoleAssignment -Id } | Should -Throw "Missing an argument for parameter 'Id'*" + } + It "Should fail when PrincipalId is empty" { + { New-EntraServicePrincipalAppRoleAssignment -PrincipalId "" } | Should -Throw "Cannot bind argument to parameter 'PrincipalId'*" + } + It "Should fail when PrincipalId is null" { + { New-EntraServicePrincipalAppRoleAssignment -PrincipalId } | Should -Throw "Missing an argument for parameter 'PrincipalId'*" + } + It "Should fail when invalid parameter is passed" { + { New-EntraServicePrincipalAppRoleAssignment -DisplayName "abc" } | Should -Throw "A parameter cannot be found that matches parameter name 'DisplayName'." + } + It "Should contain AppRoleId in parameters when passed Id to it" { + $result = New-EntraServicePrincipalAppRoleAssignment -ObjectId "02ed943d-6eca-4f99-83d6-e6fbf9dc63ae" -ResourceId "02ed943d-6eca-4f99-83d6-e6fbf9dc63ae" -Id "e18f0405-fdec-4ae8-a8a0-d8edb98b061f" -PrincipalId "d2d0a585-0c52-4bab-8c64-a096b98b061f" + $params = Get-Parameters -data $result.Parameters + $params.AppRoleId | Should -Be "e18f0405-fdec-4ae8-a8a0-d8edb98b061f" + } + It "Should contain ServicePrincipalId in parameters when passed ObjectId to it" { + $result = New-EntraServicePrincipalAppRoleAssignment -ObjectId "02ed943d-6eca-4f99-83d6-e6fbf9dc63ae" -ResourceId "02ed943d-6eca-4f99-83d6-e6fbf9dc63ae" -Id "e18f0405-fdec-4ae8-a8a0-d8edb98b061f" -PrincipalId "d2d0a585-0c52-4bab-8c64-a096b98b061f" + $params = Get-Parameters -data $result.Parameters + $params.ServicePrincipalId | Should -Be "02ed943d-6eca-4f99-83d6-e6fbf9dc63ae" + } + It "Should contain 'User-Agent' header" { + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion New-EntraServicePrincipalAppRoleAssignment" + $result = New-EntraServicePrincipalAppRoleAssignment -ObjectId "02ed943d-6eca-4f99-83d6-e6fbf9dc63ae" -ResourceId "02ed943d-6eca-4f99-83d6-e6fbf9dc63ae" -Id "e18f0405-fdec-4ae8-a8a0-d8edb98b061f" -PrincipalId "d2d0a585-0c52-4bab-8c64-a096b98b061f" + $result | Should -Not -BeNullOrEmpty + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion New-EntraServicePrincipalAppRoleAssignment" + Should -Invoke -CommandName New-MgServicePrincipalAppRoleAssignment -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-EntraServicePrincipalAppRoleAssignment -ObjectId "02ed943d-6eca-4f99-83d6-e6fbf9dc63ae" -ResourceId "02ed943d-6eca-4f99-83d6-e6fbf9dc63ae" -Id "e18f0405-fdec-4ae8-a8a0-d8edb98b061f" -PrincipalId "d2d0a585-0c52-4bab-8c64-a096b98b061f" -Debug } | Should -Not -Throw + } finally { + # Restore original confirmation preference + $DebugPreference = $originalDebugPreference + } + } + } +} \ No newline at end of file diff --git a/test/module/Entra/Remove-EntraApplicationOwner.Tests.ps1 b/test/module/Entra/Remove-EntraApplicationOwner.Tests.ps1 new file mode 100644 index 000000000..e09af7a59 --- /dev/null +++ b/test/module/Entra/Remove-EntraApplicationOwner.Tests.ps1 @@ -0,0 +1,60 @@ +# ------------------------------------------------------------------------------ +# 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-MgApplicationOwnerByRef -MockWith {} -ModuleName Microsoft.Graph.Entra +} + +Describe "Remove-EntraApplicationOwner"{ + It "Should return empty object" { + $result = Remove-EntraApplicationOwner -ObjectId "aaaaaaaa-bbbb-cccc-1111-222222222222" -OwnerId "bbbbbbbb-cccc-dddd-2222-333333333333" + $result | Should -BeNullOrEmpty + Should -Invoke -CommandName Remove-MgApplicationOwnerByRef -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Should fail when ObjectId is empty" { + { Remove-EntraApplicationOwner -ObjectId "" } + } + It "Should fail when -OwnerId is empty" { + { Remove-EntraApplicationOwner -OwnerId "" } + } + It "Should contain DeviceId in parameters" { + Mock -CommandName Remove-MgApplicationOwnerByRef -MockWith {$args} -ModuleName Microsoft.Graph.Entra + $result = Remove-EntraApplicationOwner -ObjectId "aaaaaaaa-bbbb-cccc-1111-222222222222" -OwnerId "bbbbbbbb-cccc-dddd-2222-333333333333" + $params = Get-Parameters -data $result + $params.ApplicationId | Should -Be "aaaaaaaa-bbbb-cccc-1111-222222222222" + } + It "Should contain DirectoryObjectId in parameters" { + Mock -CommandName Remove-MgApplicationOwnerByRef -MockWith {$args} -ModuleName Microsoft.Graph.Entra + $result = Remove-EntraApplicationOwner -ObjectId "aaaaaaaa-bbbb-cccc-1111-222222222222" -OwnerId "bbbbbbbb-cccc-dddd-2222-333333333333" + $params = Get-Parameters -data $result + $params.DirectoryObjectId | Should -Be "bbbbbbbb-cccc-dddd-2222-333333333333" + } + It "Should contain 'User-Agent' header" { + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Remove-EntraApplicationOwner" + $result = Remove-EntraApplicationOwner -ObjectId "aaaaaaaa-bbbb-cccc-1111-222222222222" -OwnerId "bbbbbbbb-cccc-dddd-2222-333333333333" + $result | Should -BeNullOrEmpty + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Remove-EntraApplicationOwner" + Should -Invoke -CommandName Remove-MgApplicationOwnerByRef -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 + { Remove-EntraApplicationOwner -ObjectId "aaaaaaaa-bbbb-cccc-1111-222222222222" -OwnerId "bbbbbbbb-cccc-dddd-2222-333333333333" -Debug } | Should -Not -Throw + } finally { + # Restore original confirmation preference + $DebugPreference = $originalDebugPreference + } + } +} \ No newline at end of file diff --git a/test/module/Entra/Remove-EntraApplicationPassword.Tests.ps1 b/test/module/Entra/Remove-EntraApplicationPassword.Tests.ps1 new file mode 100644 index 000000000..370204e05 --- /dev/null +++ b/test/module/Entra/Remove-EntraApplicationPassword.Tests.ps1 @@ -0,0 +1,60 @@ +# ------------------------------------------------------------------------------ +# 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-MgApplicationPassword -MockWith {} -ModuleName Microsoft.Graph.Entra +} + +Describe "Remove-EntraApplicationPassword"{ + It "Should return empty object" { + $result = Remove-EntraApplicationPassword -ObjectId "aaaaaaaa-bbbb-cccc-1111-222222222222" -KeyId "aaaaaaaa-0b0b-1c1c-2d2d-333333333333" + $result | Should -BeNullOrEmpty + Should -Invoke -CommandName Remove-MgApplicationPassword -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Should fail when ObjectId is empty" { + { Remove-EntraApplicationPassword -ObjectId "" } | Should -Throw "Cannot bind argument to parameter 'ObjectId'*" + } + It "Should fail when ObjectId is null" { + { Remove-EntraApplicationPassword -ObjectId } | Should -Throw "Missing an argument for parameter 'ObjectId'*" + } + It "Should fail when KeyId is null" { + { Remove-EntraApplicationPassword -ObjectId "aaaaaaaa-bbbb-cccc-1111-222222222222" -KeyId } | Should -Throw "Missing an argument for parameter 'KeyId'*" + } + It "Should fail when invalid parameter is passed" { + { Remove-EntraApplicationPassword -DisplayName "abc" } | Should -Throw "A parameter cannot be found that matches parameter name 'DisplayName'." + } + It "Should contain ApplicationId in parameters when passed ObjectId to it" { + Mock -CommandName Remove-MgApplicationPassword -MockWith {$args} -ModuleName Microsoft.Graph.Entra + $result = Remove-EntraApplicationPassword -ObjectId "aaaaaaaa-bbbb-cccc-1111-222222222222" -KeyId "aaaaaaaa-0b0b-1c1c-2d2d-333333333333" + $params = Get-Parameters -data $result + $params.ApplicationId | Should -Be "aaaaaaaa-bbbb-cccc-1111-222222222222" + } + It "Should contain 'User-Agent' header" { + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Remove-EntraApplicationPassword" + $result = Remove-EntraApplicationPassword -ObjectId "aaaaaaaa-bbbb-cccc-1111-222222222222" -KeyId "aaaaaaaa-0b0b-1c1c-2d2d-333333333333" + $result | Should -BeNullOrEmpty + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Remove-EntraApplicationPassword" + Should -Invoke -CommandName Remove-MgApplicationPassword -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 + { Remove-EntraApplicationPassword -ObjectId "aaaaaaaa-bbbb-cccc-1111-222222222222" -KeyId "aaaaaaaa-0b0b-1c1c-2d2d-333333333333" -Debug } | Should -Not -Throw + } finally { + # Restore original confirmation preference + $DebugPreference = $originalDebugPreference + } + } +} \ No newline at end of file diff --git a/test/module/Entra/Remove-EntraApplicationPasswordCredential.Tests.ps1 b/test/module/Entra/Remove-EntraApplicationPasswordCredential.Tests.ps1 new file mode 100644 index 000000000..6b48b4803 --- /dev/null +++ b/test/module/Entra/Remove-EntraApplicationPasswordCredential.Tests.ps1 @@ -0,0 +1,63 @@ +# ------------------------------------------------------------------------------ +# 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-MgApplicationPassword -MockWith {} -ModuleName Microsoft.Graph.Entra +} + +Describe "Remove-EntraApplicationPasswordCredential"{ + It "Should return empty object" { + $result = Remove-EntraApplicationPasswordCredential -ObjectId "aaaaaaaa-bbbb-cccc-1111-222222222222" -KeyId "bbbbbbbb-cccc-dddd-2222-333333333333" + $result | Should -BeNullOrEmpty + Should -Invoke -CommandName Remove-MgApplicationPassword -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Should fail when ObjectId is empty" { + { Remove-EntraApplicationPasswordCredential -ObjectId "" } | Should -Throw "Cannot bind argument to parameter 'ObjectId'*" + } + It "Should fail when ObjectId is null" { + { Remove-EntraApplicationPasswordCredential -ObjectId } | Should -Throw "Missing an argument for parameter 'ObjectId'*" + } + It "Should fail when KeyId is empty" { + { Remove-EntraApplicationPasswordCredential -KeyId "" } | Should -Throw "Cannot bind argument to parameter 'KeyId'*" + } + It "Should fail when KeyId is null" { + { Remove-EntraApplicationPasswordCredential -KeyId } | Should -Throw "Missing an argument for parameter 'KeyId'*" + } + It "Should fail when invalid parameter is passed" { + { Remove-EntraApplicationPasswordCredential -DisplayName "abc" } | Should -Throw "A parameter cannot be found that matches parameter name 'DisplayName'." + } + It "Should contain ApplicationId in parameters when passed ObjectId to it" { + Mock -CommandName Remove-MgApplicationPassword -MockWith {$args} -ModuleName Microsoft.Graph.Entra + $result = Remove-EntraApplicationPasswordCredential -ObjectId "aaaaaaaa-bbbb-cccc-1111-222222222222" -KeyId "bbbbbbbb-cccc-dddd-2222-333333333333" + $params = Get-Parameters -data $result + $params.ApplicationId | Should -Be "aaaaaaaa-bbbb-cccc-1111-222222222222" + } + It "Should contain 'User-Agent' header" { + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Remove-EntraApplicationPasswordCredential" + $result = Remove-EntraApplicationPasswordCredential -ObjectId "aaaaaaaa-bbbb-cccc-1111-222222222222" -KeyId "bbbbbbbb-cccc-dddd-2222-333333333333" + $result | Should -BeNullOrEmpty + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Remove-EntraApplicationPasswordCredential" + Should -Invoke -CommandName Remove-MgApplicationPassword -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 + { Remove-EntraApplicationPasswordCredential -ObjectId "aaaaaaaa-bbbb-cccc-1111-222222222222" -KeyId "bbbbbbbb-cccc-dddd-2222-333333333333" -Debug } | Should -Not -Throw + } finally { + # Restore original confirmation preference + $DebugPreference = $originalDebugPreference + } + } +} \ No newline at end of file diff --git a/test/module/Entra/Set-EntraPermissionGrantConditionSet.Tests.ps1 b/test/module/Entra/Set-EntraPermissionGrantConditionSet.Tests.ps1 new file mode 100644 index 000000000..aafb639b5 --- /dev/null +++ b/test/module/Entra/Set-EntraPermissionGrantConditionSet.Tests.ps1 @@ -0,0 +1,67 @@ +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 Update-MgPolicyPermissionGrantPolicyInclude -MockWith {} -ModuleName Microsoft.Graph.Entra + + Mock -CommandName Update-MgPolicyPermissionGrantPolicyExclude -MockWith {} -ModuleName Microsoft.Graph.Entra +} +Describe "Set-EntraPermissionGrantConditionSet"{ + It "Should return empty object for condition set 'includes'"{ + $result = Set-EntraPermissionGrantConditionSet -PolicyId "policy1" -ConditionSetType "includes" -Id "00aa00aa-bb11-cc22-dd33-44ee44ee44ee" -PermissionClassification "Low" + $result | Should -BeNullOrEmpty + Should -Invoke -CommandName Update-MgPolicyPermissionGrantPolicyInclude -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Should return empty object for condition set 'excludes'"{ + $result = Set-EntraPermissionGrantConditionSet -PolicyId "policy1" -ConditionSetType "excludes" -Id "00aa00aa-bb11-cc22-dd33-44ee44ee44ee" -PermissionClassification "Low" + $result | Should -BeNullOrEmpty + Should -Invoke -CommandName Update-MgPolicyPermissionGrantPolicyExclude -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Should fail when parameters are empty" { + { Set-EntraPermissionGrantConditionSet -PolicyId "" -ConditionSetType "" -Id ""} | Should -Throw "Cannot bind argument to parameter*" + } + It "Should fail when parameters are null" { + { Set-EntraPermissionGrantConditionSet -PolicyId -ConditionSetType -Id } | Should -Throw "Missing an argument for parameter*" + } + It "Should contain parameters for condition set 'includes'" { + Mock -CommandName Update-MgPolicyPermissionGrantPolicyInclude -MockWith {$args} -ModuleName Microsoft.Graph.Entra + $result = Set-EntraPermissionGrantConditionSet -PolicyId "policy1" -ConditionSetType "includes" -Id "00aa00aa-bb11-cc22-dd33-44ee44ee44ee" -PermissionClassification "Low" + $params = Get-Parameters -data $result + $params.PermissionGrantPolicyId | Should -Be "policy1" + $params.PermissionGrantConditionSetId | Should -Be "00aa00aa-bb11-cc22-dd33-44ee44ee44ee" + Should -Invoke -CommandName Update-MgPolicyPermissionGrantPolicyInclude -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Should contain parameters for condition set 'excludes'" { + Mock -CommandName Update-MgPolicyPermissionGrantPolicyExclude -MockWith {$args} -ModuleName Microsoft.Graph.Entra + $result = Set-EntraPermissionGrantConditionSet -PolicyId "policy1" -ConditionSetType "excludes" -Id "00aa00aa-bb11-cc22-dd33-44ee44ee44ee" -PermissionClassification "Low" + $params = Get-Parameters -data $result + $params.PermissionGrantPolicyId | Should -Be "policy1" + $params.PermissionGrantConditionSetId | Should -Be "00aa00aa-bb11-cc22-dd33-44ee44ee44ee" + Should -Invoke -CommandName Update-MgPolicyPermissionGrantPolicyExclude -ModuleName Microsoft.Graph.Entra -Times 1 + } + It "Should contain 'User-Agent' header" { + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Set-EntraPermissionGrantConditionSet" + $result = Set-EntraPermissionGrantConditionSet -PolicyId "policy1" -ConditionSetType "excludes" -Id "00aa00aa-bb11-cc22-dd33-44ee44ee44ee" -PermissionClassification "Low" + $result | Should -BeNullOrEmpty + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Set-EntraPermissionGrantConditionSet" + Should -Invoke -CommandName Update-MgPolicyPermissionGrantPolicyExclude -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 + { Set-EntraPermissionGrantConditionSet -PolicyId "policy1" -ConditionSetType "excludes" -Id "00aa00aa-bb11-cc22-dd33-44ee44ee44ee" -PermissionClassification "Low" -Debug } | Should -Not -Throw + } finally { + # Restore original confirmation preference + $DebugPreference = $originalDebugPreference + } + } +} \ No newline at end of file From 5a7aef920f2f42351ddfee76436e46013ceadf2e Mon Sep 17 00:00:00 2001 From: "Snehal Kotwal (Perennial Systems Inc)" Date: Mon, 23 Sep 2024 13:36:33 +0530 Subject: [PATCH 09/15] update --- .../Get-EntraBetaApplication.Tests.ps1 | 128 ++++++++++++++++++ .../New-EntraBetaApplication.Tests.ps1 | 61 +++++++++ .../Remove-EntraBetaApplication.Tests.ps1 | 40 ++++++ .../Set-EntraBetaApplication.Tests.ps1 | 56 ++++++++ 4 files changed, 285 insertions(+) create mode 100644 test/module/EntraBeta/Get-EntraBetaApplication.Tests.ps1 create mode 100644 test/module/EntraBeta/New-EntraBetaApplication.Tests.ps1 create mode 100644 test/module/EntraBeta/Remove-EntraBetaApplication.Tests.ps1 create mode 100644 test/module/EntraBeta/Set-EntraBetaApplication.Tests.ps1 diff --git a/test/module/EntraBeta/Get-EntraBetaApplication.Tests.ps1 b/test/module/EntraBeta/Get-EntraBetaApplication.Tests.ps1 new file mode 100644 index 000000000..0665e836b --- /dev/null +++ b/test/module/EntraBeta/Get-EntraBetaApplication.Tests.ps1 @@ -0,0 +1,128 @@ +# ------------------------------------------------------------------------------ +# 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.Beta) -eq $null){ + Import-Module Microsoft.Graph.Entra.Beta + } + Import-Module (Join-Path $psscriptroot "..\Common-Functions.ps1") -Force + + $scriptblock = { + # Write-Host "Mocking Get-MgBetaApplication with parameters: $($args | ConvertTo-Json -Depth 3)" + return @( + [PSCustomObject]@{ + "AppId" = "5f783237-3457-45d8-93e7-a0edb1cfbfd1" + "AppRoles" = $null + "DeletedDateTime" = $null + "Id" = "111cc9b5-fce9-485e-9566-c68debafac5f" + "DisplayName" = "Mock-App" + "Info" = @{LogoUrl=""; MarketingUrl=""; PrivacyStatementUrl=""; SupportUrl=""; TermsOfServiceUrl=""} + "IsDeviceOnlyAuthSupported" = $True + "IsFallbackPublicClient" = $true + "KeyCredentials" = @{CustomKeyIdentifier = @(211, 174, 247);DisplayName =""; Key="";KeyId="d903c7a3-75ea-4772-8935-5c0cf82068a7";Type="Symmetric";Usage="Sign"} + "OptionalClaims" = @{AccessToken=""; IdToken=""; Saml2Token=""} + "ParentalControlSettings" = @{CountriesBlockedForMinors=$null; LegalAgeGroupRule="Allow"} + "PasswordCredentials" = @{} + "PublicClient" = @{RedirectUris=$null} + "PublisherDomain" = "M365x99297270.onmicrosoft.com" + "SignInAudience" = "AzureADandPersonalMicrosoftAccount" + "Web" = @{HomePageUrl="https://localhost/demoapp"; ImplicitGrantSettings=""; LogoutUrl="";} + "Parameters" = $args + } + ) + } + + Mock -CommandName Get-MgBetaApplication -MockWith $scriptblock -ModuleName Microsoft.Graph.Entra.Beta +} + +Describe "Get-EntraBetaApplication" { + Context "Test for Get-EntraBetaApplication" { + It "Should return specific application" { + $result = Get-EntraBetaApplication -ObjectId "111cc9b5-fce9-485e-9566-c68debafac5f" + $result | Should -Not -BeNullOrEmpty + $result.Id | should -Be @('111cc9b5-fce9-485e-9566-c68debafac5f') + Should -Invoke -CommandName Get-MgBetaApplication -ModuleName Microsoft.Graph.Entra.Beta -Times 1 + } + It "Should fail when ObjectId is empty" { + { Get-EntraBetaApplication -ObjectId "" } | Should -Throw "Cannot bind argument to parameter 'ObjectId' because it is an empty string." + } + It "Should return all applications" { + $result = Get-EntraBetaApplication -All $true + $result | Should -Not -BeNullOrEmpty + Should -Invoke -CommandName Get-MgBetaApplication -ModuleName Microsoft.Graph.Entra.Beta -Times 1 + } + It "Should fail when All is empty" { + { Get-EntraBetaApplication -All } | Should -Throw "Missing an argument for parameter 'All'*" + } + It "Should fail when invalid parameter is passed" { + { Get-EntraBetaApplication -Power "abc" } | Should -Throw "A parameter cannot be found that matches parameter name 'Power'*" + } + It "Should return specific application by searchstring" { + $result = Get-EntraBetaApplication -SearchString 'Mock-App' + $result | Should -Not -BeNullOrEmpty + $result.DisplayName | should -Be 'Mock-App' + Should -Invoke -CommandName Get-MgBetaApplication -ModuleName Microsoft.Graph.Entra.Beta -Times 1 + } + It "Should return specific application by filter" { + $result = Get-EntraBetaApplication -Filter "DisplayName -eq 'Mock-App'" + $result | Should -Not -BeNullOrEmpty + $result.DisplayName | should -Be 'Mock-App' + Should -Invoke -CommandName Get-MgBetaApplication -ModuleName Microsoft.Graph.Entra.Beta -Times 1 + } + It "Should return top application" { + $result = Get-EntraBetaApplication -Top 1 + $result | Should -Not -BeNullOrEmpty + Should -Invoke -CommandName Get-MgBetaApplication -ModuleName Microsoft.Graph.Entra.Beta -Times 1 + } + It "Result should Contain ObjectId" { + $result = Get-EntraBetaApplication -ObjectId "111cc9b5-fce9-485e-9566-c68debafac5f" + $result.ObjectId | should -Be "111cc9b5-fce9-485e-9566-c68debafac5f" + } + It "Should contain ApplicationId in parameters when passed ObjectId to it" { + $result = Get-EntraBetaApplication -ObjectId "111cc9b5-fce9-485e-9566-c68debafac5f" + $params = Get-Parameters -data $result.Parameters + $params.ApplicationId | Should -Be "111cc9b5-fce9-485e-9566-c68debafac5f" + } + It "Should contain Filter in parameters when passed SearchString to it" { + $result = Get-EntraBetaApplication -SearchString 'Mock-App' + $params = Get-Parameters -data $result.Parameters + $params.Filter | Should -Match "Mock-App" + } + It "Should contain 'User-Agent' header" { + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraBetaApplication" + $result = Get-EntraBetaApplication -SearchString 'Mock-App' + $params = Get-Parameters -data $result.Parameters + $params.Headers["User-Agent"] | Should -Be $userAgentHeaderValue + } + It "Should support minimum set of parameter sets" { + $GetAzureADApplication = Get-Command Get-EntraBetaApplication + $GetAzureADApplication.ParameterSets.Name | Should -BeIn @("GetQuery", "GetVague", "GetById") + $GetAzureADApplication.Visibility | Should -Be "Public" + $GetAzureADApplication.CommandType | Should -Be "Function" + } + + It "Should return a list of applications by default" { + $GetAzureADApplication = Get-Command Get-EntraBetaApplication + $GetAzureADApplication.ModuleName | Should -Be "Microsoft.Graph.Entra.Beta" + $GetAzureADApplication.DefaultParameterSet | Should -Be "GetQuery" + } + It 'Should have List parameterSet' { + $GetAzureADApplication = Get-Command Get-EntraBetaApplication + $ListParameterSet = $GetAzureADApplication.ParameterSets | Where-Object Name -eq "GetQuery" + $ListParameterSet.Parameters.Name | Should -Contain All + $ListParameterSet.Parameters.Name | Should -Contain Filter + $ListParameterSet.Parameters.Name | Should -Contain Top + } + It 'Should have Get parameterSet' { + $GetAzureADApplication = Get-Command Get-EntraBetaApplication + $GetParameterSet = $GetAzureADApplication.ParameterSets | Where-Object Name -eq "GetById" + $GetParameterSet.Parameters.Name | Should -Contain ObjectId + } + It 'Should have GetViaIdentity parameterSet' { + $GetAzureADApplication = Get-Command Get-EntraBetaApplication + $GetViaIdentityParameterSet = $GetAzureADApplication.ParameterSets | Where-Object Name -eq "GetVague" + $GetViaIdentityParameterSet.Parameters.Name | Should -Contain SearchString + $GetViaIdentityParameterSet.Parameters.Name | Should -Contain All + } + } +} \ No newline at end of file diff --git a/test/module/EntraBeta/New-EntraBetaApplication.Tests.ps1 b/test/module/EntraBeta/New-EntraBetaApplication.Tests.ps1 new file mode 100644 index 000000000..d14cd6b81 --- /dev/null +++ b/test/module/EntraBeta/New-EntraBetaApplication.Tests.ps1 @@ -0,0 +1,61 @@ +# ------------------------------------------------------------------------------ +# 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.Beta) -eq $null){ + Import-Module Microsoft.Graph.Entra.Beta + } + Import-Module (Join-Path $psscriptroot "..\Common-Functions.ps1") -Force + + $scriptblock = { + # Write-Host "Mocking New-MgBetaApplication with parameters: $($args | ConvertTo-Json -Depth 3)" + return @( + [PSCustomObject]@{ + "AppId" = "5f783237-3457-45d8-93e7-a0edb1cfbfd1" + "DeletedDateTime" = $null + "Id" = "111cc9b5-fce9-485e-9566-c68debafac5f" + "DisplayName" = "Mock-App" + "Info" = @{LogoUrl=""; MarketingUrl=""; PrivacyStatementUrl=""; SupportUrl=""; TermsOfServiceUrl=""} + "IsDeviceOnlyAuthSupported" = $True + "IsFallbackPublicClient" = $true + "KeyCredentials" = @{CustomKeyIdentifier = @(211, 174, 247);DisplayName =""; Key="";KeyId="d903c7a3-75ea-4772-8935-5c0cf82068a7";Type="Symmetric";Usage="Sign"} + "OptionalClaims" = @{AccessToken=""; IdToken=""; Saml2Token=""} + "ParentalControlSettings" = @{CountriesBlockedForMinors=$null; LegalAgeGroupRule="Allow"} + "PasswordCredentials" = @{} + "PublicClient" = @{RedirectUris=$null} + "PublisherDomain" = "M365x99297270.onmicrosoft.com" + "SignInAudience" = "AzureADandPersonalMicrosoftAccount" + "Web" = @{HomePageUrl="https://localhost/demoapp"; ImplicitGrantSettings=""; LogoutUrl="";} + "Parameters" = $args + } + ) + } + + Mock -CommandName New-MgBetaApplication -MockWith $scriptblock -ModuleName Microsoft.Graph.Entra.Beta +} + +Describe "New-EntraBetaApplication"{ + Context "Test for New-EntraBetaApplication" { + It "Should return created Application"{ + $result = New-EntraBetaApplication -DisplayName "Mock-App" + $result | Should -Not -BeNullOrEmpty + $result.DisplayName | should -Be "Mock-App" + $result.IsDeviceOnlyAuthSupported | should -Be "True" + $result.IsFallbackPublicClient | should -Be "True" + $result.SignInAudience | should -Be "AzureADandPersonalMicrosoftAccount" + Should -Invoke -CommandName New-MgBetaApplication -ModuleName Microsoft.Graph.Entra.Beta -Times 1 + } + It "Should fail when DisplayName is empty" { + { New-EntraBetaApplication -DisplayName "" } | Should -Throw "Cannot bind argument to parameter*" + } + It "Should fail when invalid parameter is passed" { + { New-EntraBetaApplication -Power "abc" } | Should -Throw "A parameter cannot be found that matches parameter name 'Power'*" + } + It "Should contain 'User-Agent' header" { + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion New-EntraBetaApplication" + $result = New-EntraBetaApplication -DisplayName "Mock-App" + $params = Get-Parameters -data $result.Parameters + $params.Headers["User-Agent"] | Should -Be $userAgentHeaderValue + } + } +} \ No newline at end of file diff --git a/test/module/EntraBeta/Remove-EntraBetaApplication.Tests.ps1 b/test/module/EntraBeta/Remove-EntraBetaApplication.Tests.ps1 new file mode 100644 index 000000000..62e5e7c29 --- /dev/null +++ b/test/module/EntraBeta/Remove-EntraBetaApplication.Tests.ps1 @@ -0,0 +1,40 @@ +# ------------------------------------------------------------------------------ +# 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.Beta) -eq $null){ + Import-Module Microsoft.Graph.Entra.Beta + } + Import-Module (Join-Path $psscriptroot "..\Common-Functions.ps1") -Force + + Mock -CommandName Remove-MgBetaApplication -MockWith {} -ModuleName Microsoft.Graph.Entra.Beta +} + +Describe "Remove-EntraBetaApplication" { + Context "Test for Remove-EntraBetaApplication" { + It "Should return empty object" { + $result = Remove-EntraBetaApplication -ObjectId 056b2531-005e-4f3e-be78-01a71ea30a04 + $result | Should -BeNullOrEmpty + Should -Invoke -CommandName Remove-MgBetaApplication -ModuleName Microsoft.Graph.Entra.Beta -Times 1 + } + It "Should fail when ObjectId is empty" { + { Remove-EntraBetaApplication -ObjectId "" } | Should -Throw "Cannot bind argument to parameter*" + } + It "Should fail when invalid parameter is passed" { + { Remove-EntraBetaApplication -Power "abc" } | Should -Throw "A parameter cannot be found that matches parameter name 'Power'*" + } + It "Should contain ApplicationId in parameters when passed ObjectId to it" { + Mock -CommandName Remove-MgBetaApplication -MockWith {$args} -ModuleName Microsoft.Graph.Entra.Beta + $result = Remove-EntraBetaApplication -ObjectId 056b2531-005e-4f3e-be78-01a71ea30a04 + $params = Get-Parameters -data $result + $params.ApplicationId | Should -Be "056b2531-005e-4f3e-be78-01a71ea30a04" + } + It "Should contain 'User-Agent' header" { + Mock -CommandName Remove-MgBetaApplication -MockWith {$args} -ModuleName Microsoft.Graph.Entra.Beta + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Remove-EntraBetaApplication" + $result = Remove-EntraBetaApplication -ObjectId 056b2531-005e-4f3e-be78-01a71ea30a04 + $params = Get-Parameters -data $result + $params.Headers["User-Agent"] | Should -Be $userAgentHeaderValue + } + } +} \ No newline at end of file diff --git a/test/module/EntraBeta/Set-EntraBetaApplication.Tests.ps1 b/test/module/EntraBeta/Set-EntraBetaApplication.Tests.ps1 new file mode 100644 index 000000000..c63a7f373 --- /dev/null +++ b/test/module/EntraBeta/Set-EntraBetaApplication.Tests.ps1 @@ -0,0 +1,56 @@ +# ------------------------------------------------------------------------------ +# 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.Beta) -eq $null){ + Import-Module Microsoft.Graph.Entra.Beta + } + Import-Module (Join-Path $psscriptroot "..\Common-Functions.ps1") -Force + + Mock -CommandName Update-MgBetaApplication -MockWith {} -ModuleName Microsoft.Graph.Entra.Beta +} + +Describe "Set-EntraBetaApplication"{ + Context "Test for Set-EntraBetaApplication" { + It "Should return empty object"{ + $result = Set-EntraBetaApplication -ObjectId "aaaaaaaa-1111-1111-1111-000000000000" -DisplayName "Mock-App" + $result | Should -BeNullOrEmpty + Should -Invoke -CommandName Update-MgBetaApplication -ModuleName Microsoft.Graph.Entra.Beta -Times 1 + } + It "Should fail when ObjectId is empty" { + { Set-EntraBetaApplication -ObjectId "" } | Should -Throw "Cannot bind argument to parameter 'ObjectId' because it is an empty string." + } + It "Should fail when invalid parameter is passed" { + { Set-EntraBetaApplication -Power "abc" } | Should -Throw "A parameter cannot be found that matches parameter name 'Power'*" + } + It "Should contain ApplicationId in parameters when passed ObjectId to it" { + Mock -CommandName Update-MgBetaApplication -MockWith {$args} -ModuleName Microsoft.Graph.Entra.Beta + $result = Set-EntraBetaApplication -ObjectId "aaaaaaaa-1111-1111-1111-000000000000" + $params = Get-Parameters -data $result + $params.ApplicationId | Should -Be "aaaaaaaa-1111-1111-1111-000000000000" + } + It "Should contain 'User-Agent' header" { + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Set-EntraBetaApplication" + $result = Set-EntraBetaApplication -ObjectId "aaaaaaaa-1111-1111-1111-000000000000" + $result | Should -BeNullOrEmpty + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Set-EntraBetaApplication" + Should -Invoke -CommandName Update-MgBetaApplication -ModuleName Microsoft.Graph.Entra.Beta -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 + {Set-EntraBetaApplication -ObjectId "aaaaaaaa-1111-1111-1111-000000000000" -Debug } | Should -Not -Throw + } finally { + # Restore original confirmation preference + $DebugPreference = $originalDebugPreference + } + } + } +} \ No newline at end of file From 49c1d5b1923554c27b47c1c877e7ee3c9a59d63d Mon Sep 17 00:00:00 2001 From: "Snehal Kotwal (Perennial Systems Inc)" Date: Mon, 23 Sep 2024 13:55:14 +0530 Subject: [PATCH 10/15] updated test cases --- .../Get-EntraBetaApplication.Tests.ps1 | 54 ++++++++++++++----- .../New-EntraBetaApplication.Tests.ps1 | 23 ++++++-- .../Remove-EntraBetaApplication.Tests.ps1 | 32 ++++++++--- 3 files changed, 85 insertions(+), 24 deletions(-) diff --git a/test/module/EntraBeta/Get-EntraBetaApplication.Tests.ps1 b/test/module/EntraBeta/Get-EntraBetaApplication.Tests.ps1 index 0665e836b..3f8356ec9 100644 --- a/test/module/EntraBeta/Get-EntraBetaApplication.Tests.ps1 +++ b/test/module/EntraBeta/Get-EntraBetaApplication.Tests.ps1 @@ -14,7 +14,7 @@ BeforeAll { "AppId" = "5f783237-3457-45d8-93e7-a0edb1cfbfd1" "AppRoles" = $null "DeletedDateTime" = $null - "Id" = "111cc9b5-fce9-485e-9566-c68debafac5f" + "Id" = "aaaaaaaa-1111-1111-1111-000000000000" "DisplayName" = "Mock-App" "Info" = @{LogoUrl=""; MarketingUrl=""; PrivacyStatementUrl=""; SupportUrl=""; TermsOfServiceUrl=""} "IsDeviceOnlyAuthSupported" = $True @@ -38,21 +38,21 @@ BeforeAll { Describe "Get-EntraBetaApplication" { Context "Test for Get-EntraBetaApplication" { It "Should return specific application" { - $result = Get-EntraBetaApplication -ObjectId "111cc9b5-fce9-485e-9566-c68debafac5f" + $result = Get-EntraBetaApplication -ObjectId "aaaaaaaa-1111-1111-1111-000000000000" $result | Should -Not -BeNullOrEmpty - $result.Id | should -Be @('111cc9b5-fce9-485e-9566-c68debafac5f') + $result.Id | should -Be @('aaaaaaaa-1111-1111-1111-000000000000') Should -Invoke -CommandName Get-MgBetaApplication -ModuleName Microsoft.Graph.Entra.Beta -Times 1 } It "Should fail when ObjectId is empty" { { Get-EntraBetaApplication -ObjectId "" } | Should -Throw "Cannot bind argument to parameter 'ObjectId' because it is an empty string." } It "Should return all applications" { - $result = Get-EntraBetaApplication -All $true + $result = Get-EntraBetaApplication -All $result | Should -Not -BeNullOrEmpty Should -Invoke -CommandName Get-MgBetaApplication -ModuleName Microsoft.Graph.Entra.Beta -Times 1 } - It "Should fail when All is empty" { - { Get-EntraBetaApplication -All } | Should -Throw "Missing an argument for parameter 'All'*" + It "Should fail when All has argument" { + { Get-EntraBetaAuditDirectoryLog -All $true } | Should -Throw "A positional parameter cannot be found that accepts argument 'True'." } It "Should fail when invalid parameter is passed" { { Get-EntraBetaApplication -Power "abc" } | Should -Throw "A parameter cannot be found that matches parameter name 'Power'*" @@ -75,13 +75,13 @@ Describe "Get-EntraBetaApplication" { Should -Invoke -CommandName Get-MgBetaApplication -ModuleName Microsoft.Graph.Entra.Beta -Times 1 } It "Result should Contain ObjectId" { - $result = Get-EntraBetaApplication -ObjectId "111cc9b5-fce9-485e-9566-c68debafac5f" - $result.ObjectId | should -Be "111cc9b5-fce9-485e-9566-c68debafac5f" + $result = Get-EntraBetaApplication -ObjectId "aaaaaaaa-1111-1111-1111-000000000000" + $result.ObjectId | should -Be "aaaaaaaa-1111-1111-1111-000000000000" } It "Should contain ApplicationId in parameters when passed ObjectId to it" { - $result = Get-EntraBetaApplication -ObjectId "111cc9b5-fce9-485e-9566-c68debafac5f" + $result = Get-EntraBetaApplication -ObjectId "aaaaaaaa-1111-1111-1111-000000000000" $params = Get-Parameters -data $result.Parameters - $params.ApplicationId | Should -Be "111cc9b5-fce9-485e-9566-c68debafac5f" + $params.ApplicationId | Should -Be "aaaaaaaa-1111-1111-1111-000000000000" } It "Should contain Filter in parameters when passed SearchString to it" { $result = Get-EntraBetaApplication -SearchString 'Mock-App' @@ -90,9 +90,37 @@ Describe "Get-EntraBetaApplication" { } It "Should contain 'User-Agent' header" { $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraBetaApplication" - $result = Get-EntraBetaApplication -SearchString 'Mock-App' - $params = Get-Parameters -data $result.Parameters - $params.Headers["User-Agent"] | Should -Be $userAgentHeaderValue + $result = Get-EntraBetaApplication -ObjectId "aaaaaaaa-1111-1111-1111-000000000000" + $result | Should -Not -BeNullOrEmpty + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraBetaApplication" + Should -Invoke -CommandName Get-MgBetaApplication -ModuleName Microsoft.Graph.Entra.Beta -Times 1 -ParameterFilter { + $Headers.'User-Agent' | Should -Be $userAgentHeaderValue + $true + } + } + It "Property parameter should work" { + $result = Get-EntraBetaApplication -ObjectId "aaaaaaaa-1111-1111-1111-000000000000" -Property DisplayName + $result | Should -Not -BeNullOrEmpty + $result.DisplayName | Should -Be 'Mock-App' + + Should -Invoke -CommandName Get-MgBetaApplication -ModuleName Microsoft.Graph.Entra.Beta -Times 1 + } + + It "Should fail when Property is empty" { + { Get-EntraBetaApplication -ObjectId "aaaaaaaa-1111-1111-1111-000000000000" -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-EntraBetaApplication -ObjectId "aaaaaaaa-1111-1111-1111-000000000000" -Debug } | Should -Not -Throw + } finally { + # Restore original confirmation preference + $DebugPreference = $originalDebugPreference + } } It "Should support minimum set of parameter sets" { $GetAzureADApplication = Get-Command Get-EntraBetaApplication diff --git a/test/module/EntraBeta/New-EntraBetaApplication.Tests.ps1 b/test/module/EntraBeta/New-EntraBetaApplication.Tests.ps1 index d14cd6b81..6c3f27336 100644 --- a/test/module/EntraBeta/New-EntraBetaApplication.Tests.ps1 +++ b/test/module/EntraBeta/New-EntraBetaApplication.Tests.ps1 @@ -53,9 +53,26 @@ Describe "New-EntraBetaApplication"{ } It "Should contain 'User-Agent' header" { $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion New-EntraBetaApplication" - $result = New-EntraBetaApplication -DisplayName "Mock-App" - $params = Get-Parameters -data $result.Parameters - $params.Headers["User-Agent"] | Should -Be $userAgentHeaderValue + $result = New-EntraBetaApplication -DisplayName "Mock-App" + $result | Should -Not -BeNullOrEmpty + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion New-EntraBetaApplication" + Should -Invoke -CommandName New-MgBetaApplication -ModuleName Microsoft.Graph.Entra.Beta -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-EntraBetaApplication -DisplayName "Mock-App" -Debug } | Should -Not -Throw + } finally { + # Restore original confirmation preference + $DebugPreference = $originalDebugPreference + } } } } \ No newline at end of file diff --git a/test/module/EntraBeta/Remove-EntraBetaApplication.Tests.ps1 b/test/module/EntraBeta/Remove-EntraBetaApplication.Tests.ps1 index 62e5e7c29..8a33f6c32 100644 --- a/test/module/EntraBeta/Remove-EntraBetaApplication.Tests.ps1 +++ b/test/module/EntraBeta/Remove-EntraBetaApplication.Tests.ps1 @@ -13,7 +13,7 @@ BeforeAll { Describe "Remove-EntraBetaApplication" { Context "Test for Remove-EntraBetaApplication" { It "Should return empty object" { - $result = Remove-EntraBetaApplication -ObjectId 056b2531-005e-4f3e-be78-01a71ea30a04 + $result = Remove-EntraBetaApplication -ObjectId "aaaaaaaa-1111-1111-1111-000000000000" $result | Should -BeNullOrEmpty Should -Invoke -CommandName Remove-MgBetaApplication -ModuleName Microsoft.Graph.Entra.Beta -Times 1 } @@ -25,16 +25,32 @@ Describe "Remove-EntraBetaApplication" { } It "Should contain ApplicationId in parameters when passed ObjectId to it" { Mock -CommandName Remove-MgBetaApplication -MockWith {$args} -ModuleName Microsoft.Graph.Entra.Beta - $result = Remove-EntraBetaApplication -ObjectId 056b2531-005e-4f3e-be78-01a71ea30a04 + $result = Remove-EntraBetaApplication -ObjectId "aaaaaaaa-1111-1111-1111-000000000000" $params = Get-Parameters -data $result - $params.ApplicationId | Should -Be "056b2531-005e-4f3e-be78-01a71ea30a04" + $params.ApplicationId | Should -Be "aaaaaaaa-1111-1111-1111-000000000000" } It "Should contain 'User-Agent' header" { - Mock -CommandName Remove-MgBetaApplication -MockWith {$args} -ModuleName Microsoft.Graph.Entra.Beta $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Remove-EntraBetaApplication" - $result = Remove-EntraBetaApplication -ObjectId 056b2531-005e-4f3e-be78-01a71ea30a04 - $params = Get-Parameters -data $result - $params.Headers["User-Agent"] | Should -Be $userAgentHeaderValue - } + $result = Remove-EntraBetaApplication -ObjectId "aaaaaaaa-1111-1111-1111-000000000000" + $result | Should -BeNullOrEmpty + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Remove-EntraBetaApplication" + Should -Invoke -CommandName Remove-MgBetaApplication -ModuleName Microsoft.Graph.Entra.Beta -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 + { Remove-EntraBetaApplication -ObjectId "aaaaaaaa-1111-1111-1111-000000000000" -Debug } | Should -Not -Throw + } finally { + # Restore original confirmation preference + $DebugPreference = $originalDebugPreference + } + } } } \ No newline at end of file From dad24305c5e3571f3224e792a589733374fb4d10 Mon Sep 17 00:00:00 2001 From: Ashwini Karke Date: Tue, 24 Sep 2024 13:45:09 +0530 Subject: [PATCH 11/15] fixed UT --- .../Get-EntraDeletedApplication.ps1 | 2 +- .../Entra/Get-EntraApplicationOwner.Tests.ps1 | 43 ++++++++++-- .../Get-EntraDeletedApplication.Tests.ps1 | 68 ++++++------------- 3 files changed, 58 insertions(+), 55 deletions(-) diff --git a/module/Entra/customizations/Get-EntraDeletedApplication.ps1 b/module/Entra/customizations/Get-EntraDeletedApplication.ps1 index 8172f09ef..2b354a2da 100644 --- a/module/Entra/customizations/Get-EntraDeletedApplication.ps1 +++ b/module/Entra/customizations/Get-EntraDeletedApplication.ps1 @@ -109,7 +109,7 @@ 'PublisherDomain','Web','RequiredResourceAccess') foreach ($prop in $propsToConvert) { - $value = $_.$prop | ConvertTo-Json -Depth 10 | ConvertFrom-Json + $value = $_.$prop | ConvertTo-Json -Depth 5 | ConvertFrom-Json $_ | Add-Member -MemberType NoteProperty -Name $prop -Value ($value) -Force } diff --git a/test/module/Entra/Get-EntraApplicationOwner.Tests.ps1 b/test/module/Entra/Get-EntraApplicationOwner.Tests.ps1 index e1e0cbbcc..8d65d362d 100644 --- a/test/module/Entra/Get-EntraApplicationOwner.Tests.ps1 +++ b/test/module/Entra/Get-EntraApplicationOwner.Tests.ps1 @@ -11,15 +11,13 @@ BeforeAll { return @{ value = @( @{ - Id = "e3108c4d-86ff-4ceb-9429-24e85b4b8cea" + Id = "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" ageGroup = $null onPremisesLastSyncDateTime = $null creationType = $null - imAddresses = {"adelev@m365x99297270.onmicrosoft.com"} preferredLanguage = $null - mail = "AdeleV@M365x99297270.OnMicrosoft.com" + mail = "admin@contonso.com" securityIdentifier = "S-1-12-1-1093396945-1080104032-2731339150-364051459" - identities = {} consentProvidedForMinor = $null onPremisesUserPrincipalName = $null Parameters = $args @@ -34,21 +32,52 @@ BeforeAll { Describe "Get-EntraApplicationOwner"{ Context "Test for Get-EntraApplicationOwner"{ It "Should return application owner" { - $result = Get-EntraApplicationOwner -ObjectId "e3108c4d-86ff-4ceb-9429-24e85b4b8cea" + $result = Get-EntraApplicationOwner -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + Write-Host $result $result | Should -Not -BeNullOrEmpty - $result.Id | should -Be @('e3108c4d-86ff-4ceb-9429-24e85b4b8cea') + $result.Id | should -Be @('aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb') Should -Invoke -CommandName Invoke-GraphRequest -ModuleName Microsoft.Graph.Entra -Times 1 } It "Should fail when ObjectId is empty" { { Get-EntraApplicationOwner -ObjectId "" } | Should -Throw "Cannot bind argument to parameter 'ObjectId' because it is an empty string." } + It "Should fail when ObjectId is null" { + { Get-EntraApplicationOwner -ObjectId } | Should -Throw "Missing an argument for parameter 'ObjectId'*" + } + It "Should fail when All has an argument" { + { Get-EntraApplicationOwner -All $true } | Should -Throw "A positional parameter cannot be found that accepts argument 'True'.*" + } + It "Should fail when Top is empty" { + { Get-EntraApplicationOwner -Top } | Should -Throw "Missing an argument for parameter 'Top'*" + } + It "Should fail when Top is invalid" { + { Get-EntraApplicationOwner -Top XY } | Should -Throw "Cannot process argument transformation on parameter 'Top'*" + } + It "Property parameter should work" { + $result = Get-EntraApplicationOwner -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -Property Id + $result | Should -Not -BeNullOrEmpty + $result.Id | Should -Be "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + } It "Should contain 'User-Agent' header" { $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraApplicationOwner" - $result = Get-EntraApplicationOwner -ObjectId "e3108c4d-86ff-4ceb-9429-24e85b4b8cea" + $result = Get-EntraApplicationOwner -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" $params = Get-Parameters -data $result.Parameters $a= $params | ConvertTo-json | ConvertFrom-Json $a.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 + { Get-EntraApplicationOwner -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -Debug } | Should -Not -Throw + } finally { + # Restore original confirmation preference + $DebugPreference = $originalDebugPreference + } + } } } \ No newline at end of file diff --git a/test/module/Entra/Get-EntraDeletedApplication.Tests.ps1 b/test/module/Entra/Get-EntraDeletedApplication.Tests.ps1 index 282f27169..48fc51893 100644 --- a/test/module/Entra/Get-EntraDeletedApplication.Tests.ps1 +++ b/test/module/Entra/Get-EntraDeletedApplication.Tests.ps1 @@ -12,11 +12,7 @@ BeforeAll { "GroupMembershipClaims" = {} "IdentifierUris" = {} "Info" = @{ - LogoUrl=""; - MarketingUrl=""; - PrivacyStatementUrl=""; - SupportUrl=""; - TermsOfServiceUrl=""; + LogoUrl=""; } "IsDeviceOnlyAuthSupported" = $null "KeyCredentials" = {} @@ -27,58 +23,23 @@ BeforeAll { } "PasswordCredentials" = {} "Api" = @{ - AcceptMappedClaims=@{}; KnownClientApplications=@{}; - Oauth2PermissionScopes=@{}; PreAuthorizedApplications=@{}; - RequestedAccessTokenVersion="2"; } "PublicClient" = @{ RedirectUris=@{}; } "PublisherDomain" = "M365x99297270.onmicrosoft.com" "Web" = @{ - HomePageUrl=""; - ImplicitGrantSettings=""; - LogoutUrl=""; - RedirectUriSettings=@{}; + HomePageUrl=""; + LogoutUrl=""; RedirectUris=@{}; + Oauth2AllowImplicitFlow="" } "RequiredResourceAccess" = $null - "AppId" = "9c17362d-20b6-4572-bb6f-600e57c840e5" - "AppManagementPolicies" = $null - "ApplicationTemplateId" = $null - "Certification" = {} - "CreatedDateTime" = "12/12/2023 11:28:45 AM" - "CreatedOnBehalfOf" = {} - "DefaultRedirectUri" = $null - "DeletedDateTime" = "12/12/2023 12:23:39 PM" - "Description" = $null - "DisabledByMicrosoftStatus" = $null "DisplayName" = "Mock-test-App" - "ExtensionProperties" = $null - "FederatedIdentityCredentials" = $null - "HomeRealmDiscoveryPolicies" = $null - "Id" = "01157307-373c-47b0-889a-3bc57033d73e" - "IsFallbackPublicClient" = $null + "Id" = "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" "Logo" = $null - "Notes" = $null - "Oauth2RequirePostResponse" = $null - "Owners" = $null - "RequestSignatureVerification" = {} - "SamlMetadataUrl" = $null - "ServiceManagementReference" = $null - "ServicePrincipalLockConfiguration" = {} - "SignInAudience" = "AzureADandPersonalMicrosoftAccount" - "Spa" = {} - "Synchronization" = {} - "Tags" = {} - "TokenEncryptionKeyId" = $null - "TokenIssuancePolicies" = $null - "TokenLifetimePolicies" = $null - "UniqueName" = $null - "VerifiedPublisher" = {} - "AdditionalProperties" = {} "Parameters" = $args } ) @@ -90,12 +51,12 @@ BeforeAll { Describe "Get-EntraDeletedApplication" { Context "Test for Get-EntraDeletedApplication" { It "Should return all applications" { - $result = Get-EntraDeletedApplication | ConvertTo-Json | ConvertFrom-Json + $result = Get-EntraDeletedApplication | ConvertTo-Json -Depth 10 | ConvertFrom-Json $result | Should -Not -BeNullOrEmpty Should -Invoke -CommandName Get-MgDirectoryDeletedItemAsApplication -ModuleName Microsoft.Graph.Entra -Times 1 } It "Should fail when All is empty" { - { Get-EntraDeletedApplication -All } | Should -Throw "Missing an argument for parameter 'All'*" + { Get-EntraDeletedApplication -All $true} | Should -Throw "A positional parameter cannot be found that accepts argument 'True'.*" } It "Should fail when invalid parameter is passed" { { Get-EntraDeletedApplication -DisplayName "abc" } | Should -Throw "A parameter cannot be found that matches parameter name 'DisplayName'*" @@ -122,7 +83,7 @@ Describe "Get-EntraDeletedApplication" { } It "Result should Contain ObjectId" { $result = Get-EntraDeletedApplication -Filter "DisplayName -eq 'Mock-test-App'" | ConvertTo-Json | ConvertFrom-Json - $result.ObjectId | should -Be "01157307-373c-47b0-889a-3bc57033d73e" + $result.ObjectId | should -Be "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" } It "Should contain Filter in parameters when passed SearchString to it" { $result = Get-EntraDeletedApplication -SearchString 'Mock-test-App' | ConvertTo-Json | ConvertFrom-Json @@ -136,5 +97,18 @@ Describe "Get-EntraDeletedApplication" { $a= $params | ConvertTo-json | ConvertFrom-Json $a.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 + { Get-EntraDeletedApplication -Debug } | Should -Not -Throw + } finally { + # Restore original confirmation preference + $DebugPreference = $originalDebugPreference + } + } } } \ No newline at end of file From e383969914c8df93072a8df20c10a21caf16d473 Mon Sep 17 00:00:00 2001 From: "Snehal Kotwal (Perennial Systems Inc)" Date: Tue, 24 Sep 2024 16:13:44 +0530 Subject: [PATCH 12/15] updated --- .../Entra/Get-EntraApplicationOwner.Tests.ps1 | 11 +++++++---- .../Entra/Get-EntraDeletedApplication.Tests.ps1 | 16 +++++++++++----- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/test/module/Entra/Get-EntraApplicationOwner.Tests.ps1 b/test/module/Entra/Get-EntraApplicationOwner.Tests.ps1 index 8d65d362d..ebde421cd 100644 --- a/test/module/Entra/Get-EntraApplicationOwner.Tests.ps1 +++ b/test/module/Entra/Get-EntraApplicationOwner.Tests.ps1 @@ -61,10 +61,13 @@ Describe "Get-EntraApplicationOwner"{ } It "Should contain 'User-Agent' header" { $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraApplicationOwner" - $result = Get-EntraApplicationOwner -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" - $params = Get-Parameters -data $result.Parameters - $a= $params | ConvertTo-json | ConvertFrom-Json - $a.headers.'User-Agent' | Should -Be $userAgentHeaderValue + $result = Get-EntraApplicationOwner -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + $result | Should -Not -BeNullOrEmpty + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraApplicationOwner" + 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 diff --git a/test/module/Entra/Get-EntraDeletedApplication.Tests.ps1 b/test/module/Entra/Get-EntraDeletedApplication.Tests.ps1 index 48fc51893..e50cf85c5 100644 --- a/test/module/Entra/Get-EntraDeletedApplication.Tests.ps1 +++ b/test/module/Entra/Get-EntraDeletedApplication.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 @@ -29,7 +32,7 @@ BeforeAll { "PublicClient" = @{ RedirectUris=@{}; } - "PublisherDomain" = "M365x99297270.onmicrosoft.com" + "PublisherDomain" = "contoso.com" "Web" = @{ HomePageUrl=""; LogoutUrl=""; @@ -92,10 +95,13 @@ Describe "Get-EntraDeletedApplication" { } It "Should contain 'User-Agent' header" { $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraDeletedApplication" - $result = Get-EntraDeletedApplication -Top 1 | ConvertTo-Json | ConvertFrom-Json - $params = Get-Parameters -data $result.Parameters - $a= $params | ConvertTo-json | ConvertFrom-Json - $a.headers.'User-Agent' | Should -Be $userAgentHeaderValue + $result = Get-EntraDeletedApplication -Filter "DisplayName -eq 'Mock-test-App'" | ConvertTo-Json | ConvertFrom-Json + $result | Should -Not -BeNullOrEmpty + $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraDeletedApplication" + Should -Invoke -CommandName Get-MgDirectoryDeletedItemAsApplication -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 From eb912602bb001d19e51c252a9fde9ca323419750 Mon Sep 17 00:00:00 2001 From: "Snehal Kotwal (Perennial Systems Inc)" Date: Tue, 24 Sep 2024 19:30:13 +0530 Subject: [PATCH 13/15] update --- ...ntraApplicationPasswordCredential.Tests.ps1 | 16 ++++++++++++++-- .../Get-EntraDeletedApplication.Tests.ps1 | 11 +++++++++++ .../Get-EntraDeletedDirectoryObject.Tests.ps1 | 11 +++++++++++ ...-EntraPermissionGrantConditionSet.Tests.ps1 | 13 ++++++++++++- ...ServicePrincipalAppRoleAssignment.Tests.ps1 | 18 +++++++++--------- 5 files changed, 57 insertions(+), 12 deletions(-) diff --git a/test/module/Entra/Get-EntraApplicationPasswordCredential.Tests.ps1 b/test/module/Entra/Get-EntraApplicationPasswordCredential.Tests.ps1 index 0ca03b400..115c04eeb 100644 --- a/test/module/Entra/Get-EntraApplicationPasswordCredential.Tests.ps1 +++ b/test/module/Entra/Get-EntraApplicationPasswordCredential.Tests.ps1 @@ -9,7 +9,7 @@ BeforeAll { "PasswordCredentials" = @( @{ "CustomKeyIdentifier" = {116, 101, 115, 116} - "DisplayName" = "" + "DisplayName" = "Test" "EndDateTime" = "10/23/2024 11:36:56 AM" "KeyId" = "aaaaaaaa-0b0b-1c1c-2d2d-333333333333" "StartDateTime" = "11/22/2023 11:35:16 AM" @@ -40,6 +40,18 @@ BeforeAll { It "Should fail when invalid parameter is passed" { { Get-EntraApplicationPasswordCredential -DisplayName "abc" } | Should -Throw "A parameter cannot be found that matches parameter name 'DisplayName'*" } + It "Property parameter should work" { + $result = Get-EntraApplicationPasswordCredential -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -Property DisplayName + $result | Should -Not -BeNullOrEmpty + $result.DisplayName | Should -Be "Test" + + Should -Invoke -CommandName Get-MgApplication -ModuleName Microsoft.Graph.Entra -Times 1 + } + + It "Should fail when Property is empty" { + { Get-EntraApplicationPasswordCredential -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -Property } | Should -Throw "Missing an argument for parameter 'Property'*" + } + It "Should contain 'User-Agent' header" { $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraApplicationPasswordCredential" $result = Get-EntraApplicationPasswordCredential -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" @@ -57,7 +69,7 @@ BeforeAll { try { # Act & Assert: Ensure the function doesn't throw an exception - { Get-EntraApplicationPasswordCredential -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb"-Debug } | Should -Not -Throw + { Get-EntraApplicationPasswordCredential -ObjectId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -Debug } | Should -Not -Throw } finally { # Restore original confirmation preference $DebugPreference = $originalDebugPreference diff --git a/test/module/Entra/Get-EntraDeletedApplication.Tests.ps1 b/test/module/Entra/Get-EntraDeletedApplication.Tests.ps1 index e50cf85c5..f2c478d4a 100644 --- a/test/module/Entra/Get-EntraDeletedApplication.Tests.ps1 +++ b/test/module/Entra/Get-EntraDeletedApplication.Tests.ps1 @@ -93,6 +93,17 @@ Describe "Get-EntraDeletedApplication" { $params = Get-Parameters -data $result.Parameters $params.Filter | Should -Match "Mock-test-App" } + It "Property parameter should work" { + $result = Get-EntraDeletedApplication -Property DisplayName + $result | Should -Not -BeNullOrEmpty + $result.DisplayName | Should -Be "Mock-test-App" + + Should -Invoke -CommandName Get-MgDirectoryDeletedItemAsApplication -ModuleName Microsoft.Graph.Entra -Times 1 + } + + It "Should fail when Property is empty" { + { Get-EntraDeletedApplication -Property } | Should -Throw "Missing an argument for parameter 'Property'*" + } It "Should contain 'User-Agent' header" { $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraDeletedApplication" $result = Get-EntraDeletedApplication -Filter "DisplayName -eq 'Mock-test-App'" | ConvertTo-Json | ConvertFrom-Json diff --git a/test/module/Entra/Get-EntraDeletedDirectoryObject.Tests.ps1 b/test/module/Entra/Get-EntraDeletedDirectoryObject.Tests.ps1 index 767d92c46..bc05403bc 100644 --- a/test/module/Entra/Get-EntraDeletedDirectoryObject.Tests.ps1 +++ b/test/module/Entra/Get-EntraDeletedDirectoryObject.Tests.ps1 @@ -40,6 +40,17 @@ Describe "Get-EntraDeletedDirectoryObject"{ $params = Get-Parameters -data $result.Parameters $params.DirectoryObjectId | Should -Be "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" } + It "Property parameter should work" { + $result = Get-EntraDeletedDirectoryObject -Id "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -Property Id + $result | Should -Not -BeNullOrEmpty + $result.Id | Should -Be "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + + Should -Invoke -CommandName Get-MgDirectoryDeletedItem -ModuleName Microsoft.Graph.Entra -Times 1 + } + + It "Should fail when Property is empty" { + {Get-EntraDeletedDirectoryObject -Id "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -Property } | Should -Throw "Missing an argument for parameter 'Property'*" + } It "Should contain 'User-Agent' header" { $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraDeletedDirectoryObject" $result = Get-EntraDeletedDirectoryObject -Id "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" diff --git a/test/module/Entra/Get-EntraPermissionGrantConditionSet.Tests.ps1 b/test/module/Entra/Get-EntraPermissionGrantConditionSet.Tests.ps1 index fb1262047..3d73bd2a3 100644 --- a/test/module/Entra/Get-EntraPermissionGrantConditionSet.Tests.ps1 +++ b/test/module/Entra/Get-EntraPermissionGrantConditionSet.Tests.ps1 @@ -24,7 +24,7 @@ BeforeAll { Mock -CommandName Get-MgPolicyPermissionGrantPolicyExclude -MockWith $scriptblock -ModuleName Microsoft.Graph.Entra } -Describe "Get-EntraMSPermissionGrantConditionSet"{ +Describe "Get-EntraPermissionGrantConditionSet"{ It "Should not return empty object for condition set 'includes'"{ $result = Get-EntraPermissionGrantConditionSet -PolicyId "policy1" -ConditionSetType "includes" -Id "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" $result | Should -Not -BeNullOrEmpty @@ -51,6 +51,17 @@ Describe "Get-EntraMSPermissionGrantConditionSet"{ $params = Get-Parameters -data $result.Parameters $params.PermissionGrantPolicyId | Should -Be "policy1" } + It "Property parameter should work" { + $result = Get-EntraPermissionGrantConditionSet -PolicyId "policy1" -ConditionSetType "includes" -Id "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -Property Id + $result | Should -Not -BeNullOrEmpty + $result.Id | Should -Be "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" + + Should -Invoke -CommandName Get-MgPolicyPermissionGrantPolicyInclude -ModuleName Microsoft.Graph.Entra -Times 1 + } + + It "Should fail when Property is empty" { + {Get-EntraPermissionGrantConditionSet -PolicyId "policy1" -ConditionSetType "includes" -Id "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -Property } | Should -Throw "Missing an argument for parameter 'Property'*" + } It "Should contain 'User-Agent' header" { $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraPermissionGrantConditionSet" $result = Get-EntraPermissionGrantConditionSet -PolicyId "policy1" -ConditionSetType "includes" -Id "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" diff --git a/test/module/Entra/New-EntraServicePrincipalAppRoleAssignment.Tests.ps1 b/test/module/Entra/New-EntraServicePrincipalAppRoleAssignment.Tests.ps1 index 1ea4e9273..0a6302d43 100644 --- a/test/module/Entra/New-EntraServicePrincipalAppRoleAssignment.Tests.ps1 +++ b/test/module/Entra/New-EntraServicePrincipalAppRoleAssignment.Tests.ps1 @@ -12,9 +12,9 @@ BeforeAll { return @( [PSCustomObject]@{ "DeletedDateTime" = $null - "Id" = "gsx_zBushUevRyyjtwUohm_RMYjcGsJIjXwKOVMr3ww" + "Id" = "00aa00aa-bb11-cc22-dd33-44ee44ee44ee" "PrincipalDisplayName" = "Mock-App" - "AppRoleId" = "e18f0405-fdec-4ae8-a8a0-d8edb98b061f" + "AppRoleId" = "bbbb1b1b-cc2c-dd3d-ee4e-ffffff5f5f5f" "CreatedDateTime" = "3/12/2024 11:05:29 AM" "PrincipalId" = "aaaaaaaa-bbbb-cccc-1111-222222222222" "Parameters" = $args @@ -28,7 +28,7 @@ BeforeAll { Describe "New-EntraServicePrincipalAppRoleAssignment"{ Context "Test for New-EntraServicePrincipalAppRoleAssignment" { It "Should return New-EntraServicePrincipalAppRoleAssignment"{ - $result = New-EntraServicePrincipalAppRoleAssignment -ObjectId "02ed943d-6eca-4f99-83d6-e6fbf9dc63ae" -ResourceId "02ed943d-6eca-4f99-83d6-e6fbf9dc63ae" -Id "e18f0405-fdec-4ae8-a8a0-d8edb98b061f" -PrincipalId "d2d0a585-0c52-4bab-8c64-a096b98b061f" + $result = New-EntraServicePrincipalAppRoleAssignment -ObjectId "00001111-aaaa-2222-bbbb-3333cccc4444" -ResourceId "a0a0a0a0-bbbb-cccc-dddd-e1e1e1e1e1e1" -Id "bbbb1b1b-cc2c-dd3d-ee4e-ffffff5f5f5f" -PrincipalId "aaaaaaaa-bbbb-cccc-1111-222222222222" $result | Should -Not -BeNullOrEmpty $result.PrincipalDisplayName | should -Be "Mock-App" $result.PrincipalId | should -Be "aaaaaaaa-bbbb-cccc-1111-222222222222" @@ -63,18 +63,18 @@ Describe "New-EntraServicePrincipalAppRoleAssignment"{ { New-EntraServicePrincipalAppRoleAssignment -DisplayName "abc" } | Should -Throw "A parameter cannot be found that matches parameter name 'DisplayName'." } It "Should contain AppRoleId in parameters when passed Id to it" { - $result = New-EntraServicePrincipalAppRoleAssignment -ObjectId "02ed943d-6eca-4f99-83d6-e6fbf9dc63ae" -ResourceId "02ed943d-6eca-4f99-83d6-e6fbf9dc63ae" -Id "e18f0405-fdec-4ae8-a8a0-d8edb98b061f" -PrincipalId "d2d0a585-0c52-4bab-8c64-a096b98b061f" + $result = New-EntraServicePrincipalAppRoleAssignment -ObjectId "00001111-aaaa-2222-bbbb-3333cccc4444" -ResourceId "a0a0a0a0-bbbb-cccc-dddd-e1e1e1e1e1e1" -Id "bbbb1b1b-cc2c-dd3d-ee4e-ffffff5f5f5f" -PrincipalId "aaaaaaaa-bbbb-cccc-1111-222222222222" $params = Get-Parameters -data $result.Parameters - $params.AppRoleId | Should -Be "e18f0405-fdec-4ae8-a8a0-d8edb98b061f" + $params.AppRoleId | Should -Be "bbbb1b1b-cc2c-dd3d-ee4e-ffffff5f5f5f" } It "Should contain ServicePrincipalId in parameters when passed ObjectId to it" { - $result = New-EntraServicePrincipalAppRoleAssignment -ObjectId "02ed943d-6eca-4f99-83d6-e6fbf9dc63ae" -ResourceId "02ed943d-6eca-4f99-83d6-e6fbf9dc63ae" -Id "e18f0405-fdec-4ae8-a8a0-d8edb98b061f" -PrincipalId "d2d0a585-0c52-4bab-8c64-a096b98b061f" + $result = New-EntraServicePrincipalAppRoleAssignment -ObjectId "00001111-aaaa-2222-bbbb-3333cccc4444" -ResourceId "a0a0a0a0-bbbb-cccc-dddd-e1e1e1e1e1e1" -Id "bbbb1b1b-cc2c-dd3d-ee4e-ffffff5f5f5f" -PrincipalId "aaaaaaaa-bbbb-cccc-1111-222222222222" $params = Get-Parameters -data $result.Parameters - $params.ServicePrincipalId | Should -Be "02ed943d-6eca-4f99-83d6-e6fbf9dc63ae" + $params.ServicePrincipalId | Should -Be "00001111-aaaa-2222-bbbb-3333cccc4444" } It "Should contain 'User-Agent' header" { $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion New-EntraServicePrincipalAppRoleAssignment" - $result = New-EntraServicePrincipalAppRoleAssignment -ObjectId "02ed943d-6eca-4f99-83d6-e6fbf9dc63ae" -ResourceId "02ed943d-6eca-4f99-83d6-e6fbf9dc63ae" -Id "e18f0405-fdec-4ae8-a8a0-d8edb98b061f" -PrincipalId "d2d0a585-0c52-4bab-8c64-a096b98b061f" + $result = New-EntraServicePrincipalAppRoleAssignment -ObjectId "00001111-aaaa-2222-bbbb-3333cccc4444" -ResourceId "a0a0a0a0-bbbb-cccc-dddd-e1e1e1e1e1e1" -Id "bbbb1b1b-cc2c-dd3d-ee4e-ffffff5f5f5f" -PrincipalId "aaaaaaaa-bbbb-cccc-1111-222222222222" $result | Should -Not -BeNullOrEmpty $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion New-EntraServicePrincipalAppRoleAssignment" Should -Invoke -CommandName New-MgServicePrincipalAppRoleAssignment -ModuleName Microsoft.Graph.Entra -Times 1 -ParameterFilter { @@ -89,7 +89,7 @@ Describe "New-EntraServicePrincipalAppRoleAssignment"{ try { # Act & Assert: Ensure the function doesn't throw an exception - { New-EntraServicePrincipalAppRoleAssignment -ObjectId "02ed943d-6eca-4f99-83d6-e6fbf9dc63ae" -ResourceId "02ed943d-6eca-4f99-83d6-e6fbf9dc63ae" -Id "e18f0405-fdec-4ae8-a8a0-d8edb98b061f" -PrincipalId "d2d0a585-0c52-4bab-8c64-a096b98b061f" -Debug } | Should -Not -Throw + { New-EntraServicePrincipalAppRoleAssignment -ObjectId "00001111-aaaa-2222-bbbb-3333cccc4444" -ResourceId "a0a0a0a0-bbbb-cccc-dddd-e1e1e1e1e1e1" -Id "bbbb1b1b-cc2c-dd3d-ee4e-ffffff5f5f5f" -PrincipalId "aaaaaaaa-bbbb-cccc-1111-222222222222" -Debug } | Should -Not -Throw } finally { # Restore original confirmation preference $DebugPreference = $originalDebugPreference From 269f460c255bb24f74c313dd562522e1ef1aa4ba Mon Sep 17 00:00:00 2001 From: v-uansari Date: Wed, 25 Sep 2024 11:32:16 +0530 Subject: [PATCH 14/15] added json convert and depth --- .../Entra/Get-EntraDeletedApplication.Tests.ps1 | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/test/module/Entra/Get-EntraDeletedApplication.Tests.ps1 b/test/module/Entra/Get-EntraDeletedApplication.Tests.ps1 index f2c478d4a..36f50b989 100644 --- a/test/module/Entra/Get-EntraDeletedApplication.Tests.ps1 +++ b/test/module/Entra/Get-EntraDeletedApplication.Tests.ps1 @@ -65,36 +65,36 @@ Describe "Get-EntraDeletedApplication" { { Get-EntraDeletedApplication -DisplayName "abc" } | Should -Throw "A parameter cannot be found that matches parameter name 'DisplayName'*" } It "Should return specific application by searchstring" { - $result = Get-EntraDeletedApplication -SearchString 'Mock-test-App' | ConvertTo-Json | ConvertFrom-Json + $result = Get-EntraDeletedApplication -SearchString 'Mock-test-App' | ConvertTo-Json -Depth 10 | ConvertFrom-Json $result | Should -Not -BeNullOrEmpty $result.DisplayName | should -Be 'Mock-test-App' Should -Invoke -CommandName Get-MgDirectoryDeletedItemAsApplication -ModuleName Microsoft.Graph.Entra -Times 1 } It "Should return specific application by filter" { - $result = Get-EntraDeletedApplication -Filter "DisplayName -eq 'Mock-test-App'" | ConvertTo-Json | ConvertFrom-Json + $result = Get-EntraDeletedApplication -Filter "DisplayName -eq 'Mock-test-App'" | ConvertTo-Json -Depth 10 | ConvertFrom-Json $result | Should -Not -BeNullOrEmpty $result.DisplayName | should -Be 'Mock-test-App' Should -Invoke -CommandName Get-MgDirectoryDeletedItemAsApplication -ModuleName Microsoft.Graph.Entra -Times 1 } It "Should return top application" { - $result = Get-EntraDeletedApplication -Top 1 | ConvertTo-Json | ConvertFrom-Json + $result = Get-EntraDeletedApplication -Top 1 | ConvertTo-Json -Depth 10 | ConvertFrom-Json $result | Should -Not -BeNullOrEmpty Should -Invoke -CommandName Get-MgDirectoryDeletedItemAsApplication -ModuleName Microsoft.Graph.Entra -Times 1 } It "Result should Contain ObjectId" { - $result = Get-EntraDeletedApplication -Filter "DisplayName -eq 'Mock-test-App'" | ConvertTo-Json | ConvertFrom-Json + $result = Get-EntraDeletedApplication -Filter "DisplayName -eq 'Mock-test-App'" | ConvertTo-Json -Depth 10 | ConvertFrom-Json $result.ObjectId | should -Be "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" } It "Should contain Filter in parameters when passed SearchString to it" { - $result = Get-EntraDeletedApplication -SearchString 'Mock-test-App' | ConvertTo-Json | ConvertFrom-Json + $result = Get-EntraDeletedApplication -SearchString 'Mock-test-App' | ConvertTo-Json -Depth 10| ConvertFrom-Json $params = Get-Parameters -data $result.Parameters $params.Filter | Should -Match "Mock-test-App" } It "Property parameter should work" { - $result = Get-EntraDeletedApplication -Property DisplayName + $result = Get-EntraDeletedApplication -Property "DisplayName" | ConvertTo-Json | ConvertFrom-Json $result | Should -Not -BeNullOrEmpty $result.DisplayName | Should -Be "Mock-test-App" @@ -106,7 +106,7 @@ Describe "Get-EntraDeletedApplication" { } It "Should contain 'User-Agent' header" { $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraDeletedApplication" - $result = Get-EntraDeletedApplication -Filter "DisplayName -eq 'Mock-test-App'" | ConvertTo-Json | ConvertFrom-Json + $result = Get-EntraDeletedApplication -Filter "DisplayName -eq 'Mock-test-App'" | ConvertTo-Json -Depth 10 | ConvertFrom-Json $result | Should -Not -BeNullOrEmpty $userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion Get-EntraDeletedApplication" Should -Invoke -CommandName Get-MgDirectoryDeletedItemAsApplication -ModuleName Microsoft.Graph.Entra -Times 1 -ParameterFilter { From 70f737edd921e2739fdae073d950ba1cd5fa56a2 Mon Sep 17 00:00:00 2001 From: v-uansari Date: Wed, 25 Sep 2024 15:38:02 +0530 Subject: [PATCH 15/15] added additional property --- test/module/EntraBeta/New-EntraBetaApplication.Tests.ps1 | 1 + 1 file changed, 1 insertion(+) diff --git a/test/module/EntraBeta/New-EntraBetaApplication.Tests.ps1 b/test/module/EntraBeta/New-EntraBetaApplication.Tests.ps1 index 6c3f27336..cd9590573 100644 --- a/test/module/EntraBeta/New-EntraBetaApplication.Tests.ps1 +++ b/test/module/EntraBeta/New-EntraBetaApplication.Tests.ps1 @@ -27,6 +27,7 @@ BeforeAll { "SignInAudience" = "AzureADandPersonalMicrosoftAccount" "Web" = @{HomePageUrl="https://localhost/demoapp"; ImplicitGrantSettings=""; LogoutUrl="";} "Parameters" = $args + "AdditionalProperties" = @{CountriesBlockedForMinors = $null; LegalAgeGroupRule = "Allow" } } ) }