@@ -658,21 +658,25 @@ function Test-NewADServicePrincipalWithCustomScope
658658
659659<#
660660. SYNOPSIS
661- Tests Creating and deleting application using Password Credentials.
661+ Tests Creating and deleting application using App Credentials.
662662#>
663- function Test-CreateDeleteAppPasswordCredentials
663+ function Test-CreateDeleteAppCredentials
664664{
665665 # Setup
666- $displayName = getAssetName
666+ $getAssetName = ConvertTo-SecureString " test" - AsPlainText - Force
667+ $displayName = " test"
667668 $identifierUri = " http://" + $displayName
668- $password = getAssetName
669+ $password = $getAssetName
670+ $keyId1 = " 316af45c-83ff-42a5-a1d1-8fe9b2de3ac1"
671+ $keyId2 = " 9b7fda23-cb39-4504-8aa6-3570c4239620"
672+ $keyId3 = " 4141b479-4ca0-4919-8451-7e155de6aa0f"
669673
670674 # Test - Add application with a password cred
671675 $application = New-AzADApplication - DisplayName $displayName - IdentifierUris $identifierUri - Password $password
672676
673677 # Assert
674678 Assert-NotNull $application
675-
679+ Try {
676680 # Get Application by ObjectId
677681 $app1 = Get-AzADApplication - ObjectId $application.ObjectId
678682 Assert-NotNull $app1
@@ -685,7 +689,7 @@ function Test-CreateDeleteAppPasswordCredentials
685689 # Add 1 more password credential to the same app
686690 $start = (Get-Date ).ToUniversalTime()
687691 $end = $start.AddYears (1 )
688- $cred = New-AzADAppCredential - ObjectId $application.ObjectId - Password $password - StartDate $start - EndDate $end
692+ $cred = New-AzADAppCredentialWithId - ObjectId $application.ObjectId - Password $password - StartDate $start - EndDate $end - KeyId $keyId1
689693 Assert-NotNull $cred
690694
691695 # Get credential should fetch 2 credentials
@@ -694,39 +698,83 @@ function Test-CreateDeleteAppPasswordCredentials
694698 Assert-AreEqual $cred2.Count 2
695699 $credCount = $cred2 | where {$_.KeyId -in $cred1.KeyId , $cred.KeyId }
696700 Assert-AreEqual $credCount.Count 2
701+ $cred2 = $cred
702+
703+ # Add 1 key credential to the same app
704+ $certPath = Join-Path $ResourcesPath " certificate.pfx"
705+ $cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($certPath )
706+
707+ $binCert = $cert.GetRawCertData ()
708+ $credValue = [System.Convert ]::ToBase64String($binCert )
709+ $start = (Get-Date ).ToUniversalTime()
710+ $end = $start.AddDays (1 )
711+ $cred = New-AzADAppCredentialWithId - ObjectId $application.ObjectId - CertValue $credValue - StartDate $start - EndDate $end - KeyId $keyId2
712+ Assert-NotNull $cred
713+
714+ # Get credential should fetch 3 credentials
715+ $cred3 = Get-AzADAppCredential - ObjectId $application.ObjectId
716+ Assert-NotNull $cred3
717+ Assert-AreEqual $cred3.Count 3
718+ $credCount = $cred3 | where {$_.KeyId -in $cred1.KeyId , $cred2.KeyId , $cred.KeyId }
719+ Assert-AreEqual $credCount.Count 3
720+ $cred3 = $cred
721+
722+ # Add 1 more key credential to the same app
723+ $binCert = $cert.GetRawCertData ()
724+ $credValue = [System.Convert ]::ToBase64String($binCert )
725+ $start = (Get-Date ).ToUniversalTime()
726+ $end = $start.AddDays (1 )
727+ $cred = New-AzADAppCredentialWithId - ObjectId $application.ObjectId - CertValue $credValue - StartDate $start - EndDate $end - KeyId $keyId3
728+ Assert-NotNull $cred
729+
730+ # Get credential should fetch 4 credentials
731+ $cred4 = Get-AzADAppCredential - ObjectId $application.ObjectId
732+ Assert-NotNull $cred4
733+ Assert-AreEqual $cred4.Count 4
734+ $credCount = $cred4 | where {$_.KeyId -in $cred1.KeyId , $cred2.KeyId , $cred3.KeyId , $cred.KeyId }
735+ Assert-AreEqual $credCount.Count 4
697736
698737 # Remove cred by KeyId
699738 Remove-AzADAppCredential - ApplicationId $application.ApplicationId - KeyId $cred.KeyId - Force
700- $cred3 = Get-AzADAppCredential - ApplicationId $application.ApplicationId
701- Assert-NotNull $cred3
702- Assert-AreEqual $cred3 .Count 1
703- Assert-AreEqual $cred3 [ 0 ].KeyId $cred1.KeyId
739+ $cred5 = Get-AzADAppCredential - ApplicationId $application.ApplicationId
740+ Assert-NotNull $cred5
741+ Assert-AreEqual $cred5 .Count 3
742+ Assert-AreEqual $cred5 [ 2 ].KeyId $cred1.KeyId
704743
705744 # Remove All creds
706- Remove-AzADAppCredential - ObjectId $application.ObjectId - All - Force
707- $cred3 = Get-AzADAppCredential - ObjectId $application.ObjectId
708- Assert-Null $cred3
709-
745+ Remove-AzADAppCredential - ObjectId $application.ObjectId - Force
746+ $cred5 = Get-AzADAppCredential - ObjectId $application.ObjectId
747+ Assert-Null $cred5
748+
710749 $newApplication = Get-AzADApplication - DisplayNameStartWith " PowershellTestingApp"
711750 Assert-Throws { New-AzADAppCredential - ApplicationId $newApplication.ApplicationId - Password " Somedummypwd" }
712-
713- # Remove App
714- Remove-AzADApplication - ObjectId $application.ObjectId - Force
751+ }
752+ Finally {
753+ # Remove App
754+ Remove-AzADApplication - ObjectId $application.ObjectId - Force
755+ }
715756}
716757
717758
718759<#
719760. SYNOPSIS
720761Tests Creating and deleting application using Service Principal Credentials.
721762#>
722- function Test-CreateDeleteSpPasswordCredentials
763+ function Test-CreateDeleteSpCredentials
723764{
765+ param ([string ]$applicationId )
766+
724767 # Setup
725- $displayName = getAssetName
726- $password = getAssetName
768+ $getAssetName = ConvertTo-SecureString " test" - AsPlainText - Force
769+ $displayName = " test"
770+ $identifierUri = " http://" + $displayName
771+ $password = $getAssetName
772+ $keyId1 = " 316af45c-83ff-42a5-a1d1-8fe9b2de3ac1"
773+ $keyId2 = " 9b7fda23-cb39-4504-8aa6-3570c4239620"
774+ $keyId3 = " 4141b479-4ca0-4919-8451-7e155de6aa0f"
727775
728- # Test - Add SP with a password cred
729- $servicePrincipal = New-AzADServicePrincipal - DisplayName $displayName - Password $password
776+ # Test - Add SP
777+ $servicePrincipal = New-AzADServicePrincipal - DisplayName $displayName - ApplicationId $applicationId
730778
731779 # Assert
732780 Assert-NotNull $servicePrincipal
@@ -742,10 +790,10 @@ function Test-CreateDeleteSpPasswordCredentials
742790 Assert-NotNull $cred1
743791 Assert-AreEqual $cred1.Count 1
744792
745- # Add 1 more passowrd credential to the same app
793+ # Add 1 more password credential to the same app
746794 $start = (Get-Date ).ToUniversalTime()
747795 $end = $start.AddYears (1 )
748- $cred = New-AzADSpCredential - ObjectId $servicePrincipal.Id - Password $password - StartDate $start - EndDate $end
796+ $cred = New-AzADSpCredentialWithId - ObjectId $servicePrincipal.Id - StartDate $start - EndDate $end - KeyId $keyId1
749797 Assert-NotNull $cred
750798
751799 # Get credential should fetch 2 credentials
@@ -754,23 +802,58 @@ function Test-CreateDeleteSpPasswordCredentials
754802 Assert-AreEqual $cred2.Count 2
755803 $credCount = $cred2 | where {$_.KeyId -in $cred1.KeyId , $cred.KeyId }
756804 Assert-AreEqual $credCount.Count 2
805+ $cred2 = $cred
806+
807+ # Add 1 key credential to the same app
808+ $certPath = Join-Path $ResourcesPath " certificate.pfx"
809+ $cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($certPath )
810+
811+ $binCert = $cert.GetRawCertData ()
812+ $credValue = [System.Convert ]::ToBase64String($binCert )
813+ $start = (Get-Date ).ToUniversalTime()
814+ $end = $start.AddDays (1 )
815+ $cred = New-AzADSpCredentialWithId - ObjectId $servicePrincipal.Id - CertValue $credValue - StartDate $start - EndDate $end - KeyId $keyId2
816+ Assert-NotNull $cred
817+
818+ # Get credential should fetch 3 credentials
819+ $cred3 = Get-AzADSpCredential - ObjectId $servicePrincipal.Id
820+ Assert-NotNull $cred3
821+ Assert-AreEqual $cred3.Count 3
822+ $credCount = $cred3 | where {$_.KeyId -in $cred1.KeyId , $cred2.KeyId , $cred.KeyId }
823+ Assert-AreEqual $credCount.Count 3
824+ $cred3 = $cred
825+
826+ # Add 1 more key credential to the same app
827+ $binCert = $cert.GetRawCertData ()
828+ $credValue = [System.Convert ]::ToBase64String($binCert )
829+ $start = (Get-Date ).ToUniversalTime()
830+ $end = $start.AddDays (1 )
831+ $cred = New-AzADSpCredentialWithId - ObjectId $servicePrincipal.Id - CertValue $credValue - StartDate $start - EndDate $end - KeyId $keyId3
832+ Assert-NotNull $cred
833+
834+ # Get credential should fetch 4 credentials
835+ $cred4 = Get-AzADSpCredential - ObjectId $servicePrincipal.Id
836+ Assert-NotNull $cred4
837+ Assert-AreEqual $cred4.Count 4
838+ $credCount = $cred4 | where {$_.KeyId -in $cred1.KeyId , $cred2.KeyId , $cred3.KeyId , $cred.KeyId }
839+ Assert-AreEqual $credCount.Count 4
840+
757841
758842 # Remove cred by KeyId
759843 Remove-AzADSpCredential - ServicePrincipalName $servicePrincipal.ServicePrincipalNames [0 ] - KeyId $cred.KeyId - Force
760- $cred3 = Get-AzADSpCredential - ServicePrincipalName $servicePrincipal.ServicePrincipalNames [0 ]
761- Assert-NotNull $cred3
762- Assert-AreEqual $cred3 .Count 1
763- Assert-AreEqual $cred3 [ 0 ].KeyId $cred1.KeyId
844+ $cred5 = Get-AzADSpCredential - ServicePrincipalName $servicePrincipal.ServicePrincipalNames [0 ]
845+ Assert-NotNull $cred5
846+ Assert-AreEqual $cred5 .Count 3
847+ Assert-AreEqual $cred5 [ 2 ].KeyId $cred1.KeyId
764848
765849 # Remove All creds
766- Remove-AzADSpCredential - ObjectId $servicePrincipal.Id - All - Force
767- $cred3 = Get-AzADSpCredential - ObjectId $servicePrincipal.Id
768- Assert-Null $cred3
850+ Remove-AzADSpCredential - ObjectId $servicePrincipal.Id - Force
851+ $cred5 = Get-AzADSpCredential - ObjectId $servicePrincipal.Id
852+ Assert-Null $cred5
769853 }
770854 Finally
771855 {
772- # Remove App
773- $app = Get-AzADApplication - ApplicationId $servicePrincipal.ApplicationId
774- Remove-AzADApplication - ObjectId $app.ObjectId - Force
856+ # Remove Service Principal
857+ Remove-AzADServicePrincipal - ObjectId $servicePrincipal.Id - Force
775858 }
776859}
0 commit comments