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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,37 @@ function New-EntraApplicationKeyCredential {
}
}

PROCESS {
$params = @{}
PROCESS {
$customHeaders = New-EntraCustomHeaders -Command $MyInvocation.MyCommand

$params = @{}
$keyCredential = @{}

if ($null -ne $PSBoundParameters["CustomKeyIdentifier"]) {
$params["CustomKeyIdentifier"] = $PSBoundParameters["CustomKeyIdentifier"]
}
if ($null -ne $PSBoundParameters["WarningVariable"]) {
$params["WarningVariable"] = $PSBoundParameters["WarningVariable"]
$keyCredential["CustomKeyIdentifier"] = [System.Text.Encoding]::ASCII.GetBytes($PSBoundParameters["CustomKeyIdentifier"])
}
if ($null -ne $PSBoundParameters["StartDate"]) {
$params["StartDate"] = $PSBoundParameters["StartDate"]
$keyCredential["StartDateTime"] = $PSBoundParameters["StartDate"]
}
if ($null -ne $PSBoundParameters["EndDate"]) {
$params["EndDate"] = $PSBoundParameters["EndDate"]
$keyCredential["EndDateTime"] = $PSBoundParameters["EndDate"]
}
if ($null -ne $PSBoundParameters["Value"]) {
$keyCredential["Key"] = [System.Text.Encoding]::ASCII.GetBytes($PSBoundParameters["Value"])
}
if ($null -ne $PSBoundParameters["Type"]) {
$keyCredential["Type"] = $PSBoundParameters["Type"]
}
if ($null -ne $PSBoundParameters["Usage"]) {
$keyCredential["Usage"] = $PSBoundParameters["Usage"]
}

$params["KeyCredential"] = $keyCredential

if ($null -ne $PSBoundParameters["ApplicationId"]) {
$params["ApplicationId"] = $PSBoundParameters["ApplicationId"]
}
if ($null -ne $PSBoundParameters["WarningVariable"]) {
$params["WarningVariable"] = $PSBoundParameters["WarningVariable"]
}
if ($PSBoundParameters.ContainsKey("Debug")) {
$params["Debug"] = $PSBoundParameters["Debug"]
Expand All @@ -63,15 +79,9 @@ function New-EntraApplicationKeyCredential {
if ($null -ne $PSBoundParameters["InformationVariable"]) {
$params["InformationVariable"] = $PSBoundParameters["InformationVariable"]
}
if ($null -ne $PSBoundParameters["Value"]) {
$params["Value"] = $PSBoundParameters["Value"]
}
if ($null -ne $PSBoundParameters["OutBuffer"]) {
$params["OutBuffer"] = $PSBoundParameters["OutBuffer"]
}
if ($null -ne $PSBoundParameters["Type"]) {
$params["Type"] = $PSBoundParameters["Type"]
}
if ($null -ne $PSBoundParameters["InformationAction"]) {
$params["InformationAction"] = $PSBoundParameters["InformationAction"]
}
Expand All @@ -84,18 +94,12 @@ function New-EntraApplicationKeyCredential {
if ($PSBoundParameters.ContainsKey("Verbose")) {
$params["Verbose"] = $PSBoundParameters["Verbose"]
}
if ($null -ne $PSBoundParameters["ApplicationId"]) {
$params["ApplicationId"] = $PSBoundParameters["ApplicationId"]
}
if ($null -ne $PSBoundParameters["WarningAction"]) {
$params["WarningAction"] = $PSBoundParameters["WarningAction"]
}
if ($null -ne $PSBoundParameters["ProgressAction"]) {
$params["ProgressAction"] = $PSBoundParameters["ProgressAction"]
}
if ($null -ne $PSBoundParameters["Usage"]) {
$params["Usage"] = $PSBoundParameters["Usage"]
}
if ($null -ne $PSBoundParameters["OutVariable"]) {
$params["OutVariable"] = $PSBoundParameters["OutVariable"]
}
Expand All @@ -107,7 +111,7 @@ function New-EntraApplicationKeyCredential {
$response = Add-MgApplicationKey @params -Headers $customHeaders
$response | ForEach-Object {
if ($null -ne $_) {
Add-Member -InputObject $_ -MemberType AliasProperty -Name ObjectId -Value Id
Add-Member -InputObject $_ -MemberType AliasProperty -Name ObjectId -Value ApplicationId

}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,65 +39,69 @@ function New-EntraBetaApplicationKeyCredential {
}

PROCESS {
$params = @{}
$customHeaders = New-EntraBetaCustomHeaders -Command $MyInvocation.MyCommand

if ($null -ne $PSBoundParameters["Value"]) {
$params["Value"] = $PSBoundParameters["Value"]
}
if ($null -ne $PSBoundParameters["ProgressAction"]) {
$params["ProgressAction"] = $PSBoundParameters["ProgressAction"]
$params = @{}
$keyCredential = @{}

if ($null -ne $PSBoundParameters["CustomKeyIdentifier"]) {
$keyCredential["CustomKeyIdentifier"] = [System.Text.Encoding]::ASCII.GetBytes($PSBoundParameters["CustomKeyIdentifier"])
}
if ($PSBoundParameters.ContainsKey("Debug")) {
$params["Debug"] = $PSBoundParameters["Debug"]
if ($null -ne $PSBoundParameters["StartDate"]) {
$keyCredential["StartDateTime"] = $PSBoundParameters["StartDate"]
}
if ($null -ne $PSBoundParameters["OutBuffer"]) {
$params["OutBuffer"] = $PSBoundParameters["OutBuffer"]
if ($null -ne $PSBoundParameters["EndDate"]) {
$keyCredential["EndDateTime"] = $PSBoundParameters["EndDate"]
}
if ($null -ne $PSBoundParameters["WarningVariable"]) {
$params["WarningVariable"] = $PSBoundParameters["WarningVariable"]
if ($null -ne $PSBoundParameters["Value"]) {
$keyCredential["Key"] = [System.Text.Encoding]::ASCII.GetBytes($PSBoundParameters["Value"])
}
if ($null -ne $PSBoundParameters["WarningAction"]) {
$params["WarningAction"] = $PSBoundParameters["WarningAction"]
if ($null -ne $PSBoundParameters["Type"]) {
$keyCredential["Type"] = $PSBoundParameters["Type"]
}
if ($null -ne $PSBoundParameters["CustomKeyIdentifier"]) {
$params["CustomKeyIdentifier"] = $PSBoundParameters["CustomKeyIdentifier"]
if ($null -ne $PSBoundParameters["Usage"]) {
$keyCredential["Usage"] = $PSBoundParameters["Usage"]
}

$params["KeyCredential"] = $keyCredential

if ($null -ne $PSBoundParameters["ApplicationId"]) {
$params["ApplicationId"] = $PSBoundParameters["ApplicationId"]
}
if ($null -ne $PSBoundParameters["OutVariable"]) {
$params["OutVariable"] = $PSBoundParameters["OutVariable"]
}
if ($null -ne $PSBoundParameters["StartDate"]) {
$params["StartDate"] = $PSBoundParameters["StartDate"]
if ($null -ne $PSBoundParameters["WarningVariable"]) {
$params["WarningVariable"] = $PSBoundParameters["WarningVariable"]
}
if ($PSBoundParameters.ContainsKey("Verbose")) {
$params["Verbose"] = $PSBoundParameters["Verbose"]
if ($PSBoundParameters.ContainsKey("Debug")) {
$params["Debug"] = $PSBoundParameters["Debug"]
}
if ($null -ne $PSBoundParameters["PipelineVariable"]) {
$params["PipelineVariable"] = $PSBoundParameters["PipelineVariable"]
}
if ($null -ne $PSBoundParameters["Type"]) {
$params["Type"] = $PSBoundParameters["Type"]
}
if ($null -ne $PSBoundParameters["InformationAction"]) {
$params["InformationAction"] = $PSBoundParameters["InformationAction"]
}
if ($null -ne $PSBoundParameters["InformationVariable"]) {
$params["InformationVariable"] = $PSBoundParameters["InformationVariable"]
}
if ($null -ne $PSBoundParameters["EndDate"]) {
$params["EndDate"] = $PSBoundParameters["EndDate"]
if ($null -ne $PSBoundParameters["OutBuffer"]) {
$params["OutBuffer"] = $PSBoundParameters["OutBuffer"]
}
if ($null -ne $PSBoundParameters["Usage"]) {
$params["Usage"] = $PSBoundParameters["Usage"]
if ($null -ne $PSBoundParameters["InformationAction"]) {
$params["InformationAction"] = $PSBoundParameters["InformationAction"]
}
if ($null -ne $PSBoundParameters["ErrorVariable"]) {
$params["ErrorVariable"] = $PSBoundParameters["ErrorVariable"]
}
if ($null -ne $PSBoundParameters["ErrorAction"]) {
$params["ErrorAction"] = $PSBoundParameters["ErrorAction"]
}
if ($null -ne $PSBoundParameters["ErrorVariable"]) {
$params["ErrorVariable"] = $PSBoundParameters["ErrorVariable"]
if ($PSBoundParameters.ContainsKey("Verbose")) {
$params["Verbose"] = $PSBoundParameters["Verbose"]
}
if ($null -ne $PSBoundParameters["WarningAction"]) {
$params["WarningAction"] = $PSBoundParameters["WarningAction"]
}
if ($null -ne $PSBoundParameters["ProgressAction"]) {
$params["ProgressAction"] = $PSBoundParameters["ProgressAction"]
}
if ($null -ne $PSBoundParameters["OutVariable"]) {
$params["OutVariable"] = $PSBoundParameters["OutVariable"]
}

Write-Debug("============================ TRANSFORMATIONS ============================")
Expand All @@ -107,7 +111,7 @@ function New-EntraBetaApplicationKeyCredential {
$response = Add-MgBetaApplicationKey @params -Headers $customHeaders
$response | ForEach-Object {
if ($null -ne $_) {
Add-Member -InputObject $_ -MemberType AliasProperty -Name ObjectId -Value Id
Add-Member -InputObject $_ -MemberType AliasProperty -Name ObjectId -Value ApplicationId

}
}
Expand Down
155 changes: 155 additions & 0 deletions test/Entra/Applications/New-EntraApplicationKeyCredential.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
# ------------------------------------------------------------------------------
# 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.Entra.Applications) -eq $null) {
Import-Module Microsoft.Entra.Applications
}
Import-Module (Join-Path $PSScriptRoot "..\..\Common-Functions.ps1") -Force

$scriptblock = {
return @(
[PSCustomObject]@{
"ApplicationId" = "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb"
"CustomKeyIdentifier" = "EntraPowerShellKey"
"Type" = "Symmetric"
"EndDate" = "2025-03-21T14:14:14Z"
"KeyId" = "aaaaaaaa-0b0b-1c1c-2d2d-333333333333"
"StartDate" = "2024-03-21T14:14:14Z"
"Usage" = "Sign"
"Value" = "wbBNW8kCuiPjNRg9NX98W_EaU6cqG"
"Parameters" = $args
}
)
}

Mock -CommandName Add-MgApplicationKey -MockWith $scriptblock -ModuleName Microsoft.Entra.Applications
Mock -CommandName Get-EntraContext -MockWith { @{Scopes = @("Application.ReadWrite.All") } } -ModuleName Microsoft.Entra.Applications
}
Describe "New-EntraApplicationKeyCredential" {
It "Should return created key credential" {
$params = @{
ApplicationId = "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb"
CustomKeyIdentifier = 'EntraPowerShellKey'
StartDate = '2024-03-21T14:14:14Z'
EndDate = '2025-03-21T14:14:14Z'
Type = 'Symmetric'
Usage = 'Sign'
Value = 'wbBNW8kCuiPjNRg9NX98W_EaU6cqG'
}
$result = New-EntraApplicationKeyCredential @params
$result | Should -Not -BeNullOrEmpty
$result.KeyId | should -Be "aaaaaaaa-0b0b-1c1c-2d2d-333333333333"
$result.Type | should -Be "Symmetric"
$result.Value | Should -Be "wbBNW8kCuiPjNRg9NX98W_EaU6cqG"
Should -Invoke -CommandName Add-MgApplicationKey -ModuleName Microsoft.Entra.Applications -Times 1
}
It "Should fail when ApplicationId is empty" {
{ New-EntraApplicationKeyCredential -ApplicationId "" } | Should -Throw "Cannot validate argument on parameter 'ApplicationId'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again."
}
It "Should fail when ApplicationId is null" {
{ New-EntraApplicationKeyCredential -ApplicationId } | Should -Throw "Missing an argument for parameter 'ApplicationId'*"
}
It "Should fail when StartDate is empty" {
{ New-EntraApplicationKeyCredential -ApplicationId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -StartDate "" } | Should -Throw "Cannot process argument transformation on parameter 'StartDate'*"
}
It "Should fail when StartDate is null" {
{ New-EntraApplicationKeyCredential -ApplicationId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -StartDate } | Should -Throw "Missing an argument for parameter 'StartDate'*"
}
It "Should fail when EndDate is empty" {
{ New-EntraApplicationKeyCredential -ApplicationId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -EndDate "" } | Should -Throw "Cannot process argument transformation on parameter 'EndDate'*"
}
It "Should fail when EndDate is null" {
{ New-EntraApplicationKeyCredential -ApplicationId "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" -EndDate } | Should -Throw "Missing an argument for parameter 'EndDate'*"
}
It "Should fail when invalid parameter is passed" {
{ New-EntraApplicationKeyCredential -DisplayName "abc" } | Should -Throw "A parameter cannot be found that matches parameter name 'DisplayName'."
}
It "Should contain ApplicationId in parameters when passed ApplicationId to it" {
$params = @{
ApplicationId = "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb"
CustomKeyIdentifier = 'EntraPowerShellKey'
StartDate = '2024-03-21T14:14:14Z'
EndDate = '2025-03-21T14:14:14Z'
Type = 'Symmetric'
Usage = 'Sign'
Value = 'wbBNW8kCuiPjNRg9NX98W_EaU6cqG'
}
$result = New-EntraApplicationKeyCredential @params
$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" {
$params = @{
ApplicationId = "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb"
CustomKeyIdentifier = 'EntraPowerShellKey'
StartDate = '2024-03-21T14:14:14Z'
EndDate = '2025-03-21T14:14:14Z'
Type = 'Symmetric'
Usage = 'Sign'
Value = 'wbBNW8kCuiPjNRg9NX98W_EaU6cqG'
}
$result = New-EntraApplicationKeyCredential @params
$params = Get-Parameters -data $result.Parameters
$a = $params.KeyCredential | ConvertTo-json | ConvertFrom-Json
$a.startDateTime | Should -Not -BeNullOrEmpty
}
It "should contain endDateTime in body parameter when passed EndDate to it" {
$params = @{
ApplicationId = "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb"
CustomKeyIdentifier = 'EntraPowerShellKey'
StartDate = '2024-03-21T14:14:14Z'
EndDate = '2025-03-21T14:14:14Z'
Type = 'Symmetric'
Usage = 'Sign'
Value = 'wbBNW8kCuiPjNRg9NX98W_EaU6cqG'
}
$result = New-EntraApplicationKeyCredential @params
$params = Get-Parameters -data $result.Parameters
$a = $params.KeyCredential | ConvertTo-json | ConvertFrom-Json
$a.endDateTime | Should -Not -BeNullOrEmpty
}
It "Should contain 'User-Agent' header" {
$userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion New-EntraApplicationKeyCredential"
$params = @{
ApplicationId = "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb"
CustomKeyIdentifier = 'EntraPowerShellKey'
StartDate = '2024-03-21T14:14:14Z'
EndDate = '2025-03-21T14:14:14Z'
Type = 'Symmetric'
Usage = 'Sign'
Value = 'wbBNW8kCuiPjNRg9NX98W_EaU6cqG'
}
$result = New-EntraApplicationKeyCredential @params
$result | Should -Not -BeNullOrEmpty
$userAgentHeaderValue = "PowerShell/$psVersion EntraPowershell/$entraVersion New-EntraApplicationKeyCredential"
Should -Invoke -CommandName Add-MgApplicationKey -ModuleName Microsoft.Entra.Applications -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
$params = @{
ApplicationId = "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb"
CustomKeyIdentifier = 'EntraPowerShellKey'
StartDate = '2024-03-21T14:14:14Z'
EndDate = '2025-03-21T14:14:14Z'
Type = 'Symmetric'
Usage = 'Sign'
Value = 'wbBNW8kCuiPjNRg9NX98W_EaU6cqG'
}
{ New-EntraApplicationKeyCredential @params -Debug } | Should -Not -Throw
}
finally {
# Restore original confirmation preference
$DebugPreference = $originalDebugPreference
}
}
}

Loading