@@ -583,4 +583,115 @@ function Test-NewADServicePrincipalWithoutApp
583
583
Remove-AzureRmADApplication - ApplicationObjectId $app1.ApplicationObjectId - Force
584
584
585
585
Assert-Throws { Remove-AzureRmADServicePrincipal - ObjectId $servicePrincipal.Id }
586
+ }
587
+
588
+ <#
589
+ . SYNOPSIS
590
+ Tests Creating and deleting service principal without an exisitng application.
591
+ #>
592
+ function Test-CreateDeleteAppPasswordCredentials
593
+ {
594
+ # Setup
595
+ $displayName = getAssetName
596
+ $identifierUri = " http://" + $displayName
597
+ $password = getAssetName
598
+
599
+ # Test - Add application with a password cred
600
+ $application = New-AzureRmADApplication - DisplayName $displayName - IdentifierUris $identifierUri - Password $password
601
+
602
+ # Assert
603
+ Assert-NotNull $application
604
+
605
+ # Get Application by ApplicationObjectId
606
+ $app1 = Get-AzureRmADApplication - ApplicationObjectId $application.ApplicationObjectId
607
+ Assert-NotNull $app1
608
+
609
+ # Get credential should fetch 1 credential
610
+ $cred1 = Get-AzureRmADAppCredential - ApplicationObjectId $application.ApplicationObjectId
611
+ Assert-NotNull $cred1
612
+ Assert-AreEqual $cred1.Count 1
613
+
614
+ # Add 1 more password credential to the same app
615
+ $start = (Get-Date ).ToUniversalTime()
616
+ $end = $start.AddYears (1 )
617
+ $cred = New-AzureRmADAppCredential - ApplicationObjectId $application.ApplicationObjectId - Password $password - StartDate $start - EndDate $end
618
+ Assert-NotNull $cred
619
+
620
+ # Get credential should fetch 2 credentials
621
+ $cred2 = Get-AzureRmADAppCredential - ApplicationObjectId $application.ApplicationObjectId
622
+ Assert-NotNull $cred2
623
+ Assert-AreEqual $cred2.Count 2
624
+ $credCount = $cred2 | where {$_.KeyId -in $cred1.KeyId , $cred.KeyId }
625
+ Assert-AreEqual $credCount.Count 2
626
+
627
+ # Remove cred by KeyId
628
+ Remove-AzureRmADAppCredential - ApplicationId $application.ApplicationId - KeyId $cred.KeyId - Force
629
+ $cred3 = Get-AzureRmADAppCredential - ApplicationId $application.ApplicationId
630
+ Assert-NotNull $cred3
631
+ Assert-AreEqual $cred3.Count 1
632
+ Assert-AreEqual $cred3 [0 ].KeyId $cred1.KeyId
633
+
634
+ # Remove All creds
635
+ Remove-AzureRmADAppCredential - ApplicationObjectId $application.ApplicationObjectId - All - Force
636
+ $cred3 = Get-AzureRmADAppCredential - ApplicationObjectId $application.ApplicationObjectId
637
+ Assert-Null $cred3
638
+
639
+ # Remove App
640
+ Remove-AzureRmADApplication - ApplicationObjectId $application.ApplicationObjectId - Force
641
+ }
642
+
643
+
644
+ <#
645
+ . SYNOPSIS
646
+ Tests Creating and deleting service principal without an exisitng application.
647
+ #>
648
+ function Test-CreateDeleteSpPasswordCredentials
649
+ {
650
+ # Setup
651
+ $displayName = getAssetName
652
+ $password = getAssetName
653
+
654
+ # Test - Add SP with a password cred
655
+ $servicePrincipal = New-AzureRmADServicePrincipal - DisplayName $displayName - Password $password
656
+
657
+ # Assert
658
+ Assert-NotNull $servicePrincipal
659
+
660
+ # Get service principal by ObjectId
661
+ $sp1 = Get-AzureRmADServicePrincipal - ObjectId $servicePrincipal.Id
662
+ Assert-NotNull $sp1.Id
663
+
664
+ # Get credential should fetch 1 credential
665
+ $cred1 = Get-AzureRmADSpCredential - ObjectId $servicePrincipal.Id
666
+ Assert-NotNull $cred1
667
+ Assert-AreEqual $cred1.Count 1
668
+
669
+ # Add 1 more passowrd credential to the same app
670
+ $start = (Get-Date ).ToUniversalTime()
671
+ $end = $start.AddYears (1 )
672
+ $cred = New-AzureRmADSpCredential - ObjectId $servicePrincipal.Id - Password $password - StartDate $start - EndDate $end
673
+ Assert-NotNull $cred
674
+
675
+ # Get credential should fetch 2 credentials
676
+ $cred2 = Get-AzureRmADSpCredential - ObjectId $servicePrincipal.Id
677
+ Assert-NotNull $cred2
678
+ Assert-AreEqual $cred2.Count 2
679
+ $credCount = $cred2 | where {$_.KeyId -in $cred1.KeyId , $cred.KeyId }
680
+ Assert-AreEqual $credCount.Count 2
681
+
682
+ # Remove cred by KeyId
683
+ Remove-AzureRmADSpCredential - ServicePrincipalName $servicePrincipal.ServicePrincipalNames [0 ] - KeyId $cred.KeyId - Force
684
+ $cred3 = Get-AzureRmADSpCredential - ServicePrincipalName $servicePrincipal.ServicePrincipalNames [0 ]
685
+ Assert-NotNull $cred3
686
+ Assert-AreEqual $cred3.Count 1
687
+ Assert-AreEqual $cred3 [0 ].KeyId $cred1.KeyId
688
+
689
+ # Remove All creds
690
+ Remove-AzureRmADSpCredential - ObjectId $servicePrincipal.Id - All - Force
691
+ $cred3 = Get-AzureRmADSpCredential - ObjectId $servicePrincipal.Id
692
+ Assert-Null $cred3
693
+
694
+ # Remove App
695
+ $app = Get-AzureRmADApplication - ApplicationId $servicePrincipal.ApplicationId
696
+ Remove-AzureRmADApplication - ApplicationObjectId $app.ApplicationObjectId - Force
586
697
}
0 commit comments