@@ -460,4 +460,61 @@ var _ = Describe("KeycloakRealmUser controller", Ordered, func() {
460
460
g .Expect (createdUser .Status .Value ).Should (ContainSubstring ("unable to sync realm user" ))
461
461
}, time .Second * 3 , time .Second ).Should (Succeed ())
462
462
})
463
+ It ("Should delete KeycloakRealmUser if user not found" , func () {
464
+ By ("Creating a KeycloakRealmUser" )
465
+ user := & keycloakApi.KeycloakRealmUser {
466
+ ObjectMeta : metav1.ObjectMeta {
467
+ Name : "test-keycloak-realm-user-not-found" ,
468
+ Namespace : ns ,
469
+ },
470
+ Spec : keycloakApi.KeycloakRealmUserSpec {
471
+ RealmRef : common.RealmRef {
472
+ Kind : keycloakApi .KeycloakRealmKind ,
473
+ Name : KeycloakRealmCR ,
474
+ },
475
+ Username : "test-user-not-found" ,
476
+ PasswordSecret : keycloakApi.PasswordSecret {
477
+ Name : userSecretName ,
478
+ Key : "password" ,
479
+ },
480
+ KeepResource : true ,
481
+ },
482
+ }
483
+ Expect (k8sClient .Create (ctx , user )).Should (Succeed ())
484
+ Eventually (func (g Gomega ) {
485
+ createdUser := & keycloakApi.KeycloakRealmUser {}
486
+ err := k8sClient .Get (ctx , types.NamespacedName {Name : user .Name , Namespace : ns }, createdUser )
487
+ g .Expect (err ).ShouldNot (HaveOccurred ())
488
+ g .Expect (createdUser .Status .Value ).Should (Equal (helper .StatusOK ))
489
+ }).WithTimeout (time .Second * 20 ).WithPolling (time .Second ).Should (Succeed ())
490
+
491
+ By ("Manually deleting the user from Keycloak to simulate user not found scenario" )
492
+ users , err := keycloakApiClient .GetUsers (ctx , getKeyCloakToken (), KeycloakRealmCR , gocloak.GetUsersParams {
493
+ Username : gocloak .StringP (user .Spec .Username ),
494
+ Exact : gocloak .BoolP (true ),
495
+ })
496
+ Expect (err ).ShouldNot (HaveOccurred ())
497
+ Expect (users ).Should (HaveLen (1 ))
498
+
499
+ err = keycloakApiClient .DeleteUser (ctx , getKeyCloakToken (), KeycloakRealmCR , * users [0 ].ID )
500
+ Expect (err ).ShouldNot (HaveOccurred ())
501
+
502
+ By ("Verifying user is deleted from Keycloak" )
503
+ Eventually (func (g Gomega ) {
504
+ users , err := keycloakApiClient .GetUsers (ctx , getKeyCloakToken (), KeycloakRealmCR , gocloak.GetUsersParams {
505
+ Username : gocloak .StringP (user .Spec .Username ),
506
+ Exact : gocloak .BoolP (true ),
507
+ })
508
+ g .Expect (err ).ShouldNot (HaveOccurred ())
509
+ g .Expect (users ).Should (HaveLen (0 ))
510
+ }, time .Minute , time .Second * 5 ).Should (Succeed ())
511
+
512
+ By ("Deleting KeycloakRealmUser CR - should succeed even though user doesn't exist in Keycloak" )
513
+ Expect (k8sClient .Delete (ctx , user )).Should (Succeed ())
514
+ Eventually (func (g Gomega ) {
515
+ deletedUser := & keycloakApi.KeycloakRealmUser {}
516
+ err := k8sClient .Get (ctx , types.NamespacedName {Name : user .Name , Namespace : ns }, deletedUser )
517
+ g .Expect (k8sErrors .IsNotFound (err )).Should (BeTrue ())
518
+ }, timeout , interval ).Should (Succeed ())
519
+ })
463
520
})
0 commit comments