@@ -228,6 +228,14 @@ var _ = Describe("RabbitMQUser controller", func() {
228228 })
229229
230230 It ("should update status.VhostRef" , func () {
231+ // Simulate successful RabbitMQ API call by updating status
232+ Eventually (func (g Gomega ) {
233+ user := GetRabbitMQUser (userName )
234+ user .Status .Vhost = "test"
235+ user .Status .VhostRef = vhostName .Name
236+ g .Expect (k8sClient .Status ().Update (th .Ctx , user )).Should (Succeed ())
237+ }, timeout , interval ).Should (Succeed ())
238+
231239 Eventually (func (g Gomega ) {
232240 user := GetRabbitMQUser (userName )
233241 g .Expect (user .Status .VhostRef ).To (Equal (vhostName .Name ))
@@ -282,6 +290,14 @@ var _ = Describe("RabbitMQUser controller", func() {
282290 g .Expect (vhost .Finalizers ).To (ContainElement (expectedFinalizer ))
283291 }, timeout , interval ).Should (Succeed ())
284292
293+ // Simulate successful RabbitMQ API call by updating status
294+ Eventually (func (g Gomega ) {
295+ user := GetRabbitMQUser (userName )
296+ user .Status .Vhost = "test"
297+ user .Status .VhostRef = vhostName .Name
298+ g .Expect (k8sClient .Status ().Update (th .Ctx , user )).Should (Succeed ())
299+ }, timeout , interval ).Should (Succeed ())
300+
285301 // Wait for status.VhostRef to be set
286302 Eventually (func (g Gomega ) {
287303 user := GetRabbitMQUser (userName )
@@ -309,12 +325,129 @@ var _ = Describe("RabbitMQUser controller", func() {
309325 g .Expect (vhost .Finalizers ).To (ContainElement (expectedFinalizer ))
310326 }, timeout , interval ).Should (Succeed ())
311327
328+ // Simulate successful RabbitMQ API call by updating status
329+ // This mimics what the controller would do after successfully updating vhost permissions
330+ Eventually (func (g Gomega ) {
331+ user := GetRabbitMQUser (userName )
332+ user .Status .Vhost = "test2"
333+ user .Status .VhostRef = secondVhostName .Name
334+ g .Expect (k8sClient .Status ().Update (th .Ctx , user )).Should (Succeed ())
335+ }, timeout , interval ).Should (Succeed ())
336+
312337 // Verify status updated
313338 Eventually (func (g Gomega ) {
314339 user := GetRabbitMQUser (userName )
315340 g .Expect (user .Status .VhostRef ).To (Equal (secondVhostName .Name ))
316341 }, timeout , interval ).Should (Succeed ())
317342 })
343+
344+ It ("should update status.VhostRef when VhostRef is changed" , func () {
345+ expectedFinalizer := rabbitmqv1 .UserVhostFinalizerPrefix + userName .Name
346+
347+ // Verify initial VhostRef in status
348+ Eventually (func (g Gomega ) {
349+ user := GetRabbitMQUser (userName )
350+ g .Expect (user .Status .VhostRef ).To (Equal (vhostName .Name ))
351+ }, timeout , interval ).Should (Succeed ())
352+
353+ // Update user to reference second vhost
354+ Eventually (func (g Gomega ) {
355+ user := GetRabbitMQUser (userName )
356+ user .Spec .VhostRef = secondVhostName .Name
357+ g .Expect (th .K8sClient .Update (th .Ctx , user )).To (Succeed ())
358+ }, timeout , interval ).Should (Succeed ())
359+
360+ // Wait for controller to move finalizer from old to new vhost
361+ // This must happen BEFORE we update status, otherwise controller won't detect the change
362+ Eventually (func (g Gomega ) {
363+ vhost := GetRabbitMQVhost (vhostName )
364+ g .Expect (vhost .Finalizers ).NotTo (ContainElement (expectedFinalizer ))
365+ }, timeout , interval ).Should (Succeed ())
366+
367+ Eventually (func (g Gomega ) {
368+ vhost := GetRabbitMQVhost (secondVhostName )
369+ g .Expect (vhost .Finalizers ).To (ContainElement (expectedFinalizer ))
370+ }, timeout , interval ).Should (Succeed ())
371+
372+ // Simulate successful RabbitMQ API call by updating status
373+ Eventually (func (g Gomega ) {
374+ user := GetRabbitMQUser (userName )
375+ user .Status .Vhost = "test2"
376+ user .Status .VhostRef = secondVhostName .Name
377+ g .Expect (k8sClient .Status ().Update (th .Ctx , user )).Should (Succeed ())
378+ }, timeout , interval ).Should (Succeed ())
379+
380+ // Verify status.VhostRef is updated
381+ Eventually (func (g Gomega ) {
382+ user := GetRabbitMQUser (userName )
383+ g .Expect (user .Status .VhostRef ).To (Equal (secondVhostName .Name ))
384+ }, timeout , interval ).Should (Succeed ())
385+ })
386+ })
387+
388+ When ("a RabbitMQUser VhostRef is changed from default to custom vhost" , func () {
389+ var customVhostName types.NamespacedName
390+
391+ BeforeEach (func () {
392+ // Create custom vhost
393+ customVhostName = types.NamespacedName {Name : "custom-vhost" , Namespace : namespace }
394+ vhostCustom := CreateRabbitMQVhost (customVhostName , map [string ]any {
395+ "rabbitmqClusterName" : rabbitmqClusterName .Name ,
396+ "name" : "custom" ,
397+ })
398+ DeferCleanup (th .DeleteInstance , vhostCustom )
399+
400+ // Create user WITHOUT vhostRef (will use default "/")
401+ spec := map [string ]any {
402+ "rabbitmqClusterName" : rabbitmqClusterName .Name ,
403+ "username" : "default-user" ,
404+ }
405+ user := CreateRabbitMQUser (userName , spec )
406+ DeferCleanup (th .DeleteInstance , user )
407+
408+ // Wait for status.VhostRef to be empty (no custom vhost)
409+ Eventually (func (g Gomega ) {
410+ user := GetRabbitMQUser (userName )
411+ g .Expect (user .Status .VhostRef ).To (BeEmpty ())
412+ }, timeout , interval ).Should (Succeed ())
413+ })
414+
415+ It ("should update VhostRef from default to custom vhost" , func () {
416+ // Verify initial state - no custom vhost
417+ user := GetRabbitMQUser (userName )
418+ Expect (user .Status .VhostRef ).To (BeEmpty ())
419+
420+ // Update user to use custom vhost - THIS IS THE USER'S SCENARIO
421+ Eventually (func (g Gomega ) {
422+ user := GetRabbitMQUser (userName )
423+ user .Spec .VhostRef = customVhostName .Name
424+ g .Expect (th .K8sClient .Update (th .Ctx , user )).To (Succeed ())
425+ }, timeout , interval ).Should (Succeed ())
426+
427+ // Simulate successful RabbitMQ API call by updating status
428+ Eventually (func (g Gomega ) {
429+ user := GetRabbitMQUser (userName )
430+ user .Status .Vhost = "custom"
431+ user .Status .VhostRef = customVhostName .Name
432+ g .Expect (k8sClient .Status ().Update (th .Ctx , user )).Should (Succeed ())
433+ }, timeout , interval ).Should (Succeed ())
434+
435+ // Verify status.VhostRef updated
436+ // Note: We don't test status.Vhost because that requires RabbitMQ API
437+ // The real controller would detect: status.Vhost ("/") != new vhost ("custom")
438+ // and update RabbitMQ permissions accordingly
439+ Eventually (func (g Gomega ) {
440+ user := GetRabbitMQUser (userName )
441+ g .Expect (user .Status .VhostRef ).To (Equal (customVhostName .Name ))
442+ }, timeout , interval ).Should (Succeed ())
443+
444+ // Verify finalizer added to custom vhost
445+ expectedFinalizer := rabbitmqv1 .UserVhostFinalizerPrefix + userName .Name
446+ Eventually (func (g Gomega ) {
447+ vhost := GetRabbitMQVhost (customVhostName )
448+ g .Expect (vhost .Finalizers ).To (ContainElement (expectedFinalizer ))
449+ }, timeout , interval ).Should (Succeed ())
450+ })
318451 })
319452
320453 When ("multiple RabbitMQUsers reference the same vhost" , func () {
0 commit comments