@@ -565,30 +565,31 @@ func TestWorkspaceBuildResources(t *testing.T) {
565
565
func TestWorkspaceBuildWithUpdatedTemplateVersionSendsNotification (t * testing.T ) {
566
566
t .Parallel ()
567
567
568
- t .Run ("OnlyOneNotification " , func (t * testing.T ) {
568
+ t .Run ("NoRepeatedNotifications " , func (t * testing.T ) {
569
569
t .Parallel ()
570
570
571
571
notify := & notificationstest.FakeEnqueuer {}
572
572
573
573
client := coderdtest .New (t , & coderdtest.Options {IncludeProvisionerDaemon : true , NotificationsEnqueuer : notify })
574
574
first := coderdtest .CreateFirstUser (t , client )
575
+ templateAdminClient , templateAdmin := coderdtest .CreateAnotherUser (t , client , first .OrganizationID , rbac .RoleTemplateAdmin ())
575
576
userClient , user := coderdtest .CreateAnotherUser (t , client , first .OrganizationID )
576
577
577
578
// Create a template with an initial version
578
- version := coderdtest .CreateTemplateVersion (t , client , first .OrganizationID , nil )
579
- coderdtest .AwaitTemplateVersionJobCompleted (t , client , version .ID )
580
- template := coderdtest .CreateTemplate (t , client , first .OrganizationID , version .ID )
579
+ version := coderdtest .CreateTemplateVersion (t , templateAdminClient , first .OrganizationID , nil )
580
+ coderdtest .AwaitTemplateVersionJobCompleted (t , templateAdminClient , version .ID )
581
+ template := coderdtest .CreateTemplate (t , templateAdminClient , first .OrganizationID , version .ID )
581
582
582
583
// Create a workspace using this template
583
584
workspace := coderdtest .CreateWorkspace (t , userClient , template .ID )
584
585
coderdtest .AwaitWorkspaceBuildJobCompleted (t , userClient , workspace .LatestBuild .ID )
585
586
coderdtest .MustTransitionWorkspace (t , userClient , workspace .ID , database .WorkspaceTransitionStart , database .WorkspaceTransitionStop )
586
587
587
588
// Create a new version of the template
588
- newVersion := coderdtest .CreateTemplateVersion (t , client , first .OrganizationID , nil , func (ctvr * codersdk.CreateTemplateVersionRequest ) {
589
+ newVersion := coderdtest .CreateTemplateVersion (t , templateAdminClient , first .OrganizationID , nil , func (ctvr * codersdk.CreateTemplateVersionRequest ) {
589
590
ctvr .TemplateID = template .ID
590
591
})
591
- coderdtest .AwaitTemplateVersionJobCompleted (t , client , newVersion .ID )
592
+ coderdtest .AwaitTemplateVersionJobCompleted (t , templateAdminClient , newVersion .ID )
592
593
593
594
// Create a workspace build using this new template version
594
595
build := coderdtest .CreateWorkspaceBuild (t , userClient , workspace , database .WorkspaceTransitionStart , func (cwbr * codersdk.CreateWorkspaceBuildRequest ) {
@@ -597,21 +598,45 @@ func TestWorkspaceBuildWithUpdatedTemplateVersionSendsNotification(t *testing.T)
597
598
coderdtest .AwaitWorkspaceBuildJobCompleted (t , userClient , build .ID )
598
599
coderdtest .MustTransitionWorkspace (t , userClient , workspace .ID , database .WorkspaceTransitionStart , database .WorkspaceTransitionStop )
599
600
600
- // Create the workspace build _again_. We are doing this to ensure we only create 1 notification.
601
+ // Create the workspace build _again_. We are doing this to
602
+ // ensure we do not create _another_ notification. This is
603
+ // separate to the notifications subsystem dedupe mechanism
604
+ // as this build shouldn't create a notification. It shouldn't
605
+ // create another notification as this new build isn't changing
606
+ // the template version.
601
607
build = coderdtest .CreateWorkspaceBuild (t , userClient , workspace , database .WorkspaceTransitionStart , func (cwbr * codersdk.CreateWorkspaceBuildRequest ) {
602
608
cwbr .TemplateVersionID = newVersion .ID
603
609
})
604
610
coderdtest .AwaitWorkspaceBuildJobCompleted (t , userClient , build .ID )
605
611
coderdtest .MustTransitionWorkspace (t , userClient , workspace .ID , database .WorkspaceTransitionStart , database .WorkspaceTransitionStop )
606
612
607
- // Ensure we receive only 1 workspace manually updated notification
613
+ // We're going to have two notifications (one for the first user and one for the template admin)
614
+ // By ensuring we only have these two, we are sure the second build didn't trigger more
615
+ // notifications.
608
616
sent := notify .Sent (notificationstest .WithTemplateID (notifications .TemplateWorkspaceManuallyUpdated ))
609
- require .Len (t , sent , 1 )
610
- require .Equal (t , user .ID , sent [0 ].UserID )
617
+ require .Len (t , sent , 2 )
618
+
619
+ receivers := make ([]uuid.UUID , len (sent ))
620
+ for idx , notif := range sent {
621
+ receivers [idx ] = notif .UserID
622
+ }
623
+
624
+ // Check the notification was sent to the first user and template admin
625
+ // (both of whom have the "template admin" role), and explicitly not the
626
+ // workspace owner (since they initiated the workspace build).
627
+ require .Contains (t , receivers , templateAdmin .ID )
628
+ require .Contains (t , receivers , first .UserID )
629
+ require .NotContains (t , receivers , user .ID )
630
+
611
631
require .Contains (t , sent [0 ].Targets , template .ID )
612
632
require .Contains (t , sent [0 ].Targets , workspace .ID )
613
633
require .Contains (t , sent [0 ].Targets , workspace .OrganizationID )
614
634
require .Contains (t , sent [0 ].Targets , workspace .OwnerID )
635
+
636
+ require .Contains (t , sent [1 ].Targets , template .ID )
637
+ require .Contains (t , sent [1 ].Targets , workspace .ID )
638
+ require .Contains (t , sent [1 ].Targets , workspace .OrganizationID )
639
+ require .Contains (t , sent [1 ].Targets , workspace .OwnerID )
615
640
})
616
641
617
642
t .Run ("ToCorrectUser" , func (t * testing.T ) {
@@ -621,23 +646,24 @@ func TestWorkspaceBuildWithUpdatedTemplateVersionSendsNotification(t *testing.T)
621
646
622
647
client := coderdtest .New (t , & coderdtest.Options {IncludeProvisionerDaemon : true , NotificationsEnqueuer : notify })
623
648
first := coderdtest .CreateFirstUser (t , client )
624
- userClient , user := coderdtest .CreateAnotherUser (t , client , first .OrganizationID )
649
+ templateAdminClient , templateAdmin := coderdtest .CreateAnotherUser (t , client , first .OrganizationID , rbac .RoleTemplateAdmin ())
650
+ userClient , _ := coderdtest .CreateAnotherUser (t , client , first .OrganizationID )
625
651
626
652
// Create a template with an initial version
627
- version := coderdtest .CreateTemplateVersion (t , client , first .OrganizationID , nil )
628
- coderdtest .AwaitTemplateVersionJobCompleted (t , client , version .ID )
629
- template := coderdtest .CreateTemplate (t , client , first .OrganizationID , version .ID )
653
+ version := coderdtest .CreateTemplateVersion (t , templateAdminClient , first .OrganizationID , nil )
654
+ coderdtest .AwaitTemplateVersionJobCompleted (t , templateAdminClient , version .ID )
655
+ template := coderdtest .CreateTemplate (t , templateAdminClient , first .OrganizationID , version .ID )
630
656
631
657
// Create a workspace using this template
632
658
workspace := coderdtest .CreateWorkspace (t , userClient , template .ID )
633
659
coderdtest .AwaitWorkspaceBuildJobCompleted (t , userClient , workspace .LatestBuild .ID )
634
660
coderdtest .MustTransitionWorkspace (t , userClient , workspace .ID , database .WorkspaceTransitionStart , database .WorkspaceTransitionStop )
635
661
636
662
// Create a new version of the template
637
- newVersion := coderdtest .CreateTemplateVersion (t , client , first .OrganizationID , nil , func (ctvr * codersdk.CreateTemplateVersionRequest ) {
663
+ newVersion := coderdtest .CreateTemplateVersion (t , templateAdminClient , first .OrganizationID , nil , func (ctvr * codersdk.CreateTemplateVersionRequest ) {
638
664
ctvr .TemplateID = template .ID
639
665
})
640
- coderdtest .AwaitTemplateVersionJobCompleted (t , client , newVersion .ID )
666
+ coderdtest .AwaitTemplateVersionJobCompleted (t , templateAdminClient , newVersion .ID )
641
667
642
668
// Create a workspace build using this new template version from a different user
643
669
ctx := testutil .Context (t , testutil .WaitShort )
@@ -652,7 +678,7 @@ func TestWorkspaceBuildWithUpdatedTemplateVersionSendsNotification(t *testing.T)
652
678
// Ensure we receive only 1 workspace manually updated notification and to the right user
653
679
sent := notify .Sent (notificationstest .WithTemplateID (notifications .TemplateWorkspaceManuallyUpdated ))
654
680
require .Len (t , sent , 1 )
655
- require .Equal (t , user .ID , sent [0 ].UserID )
681
+ require .Equal (t , templateAdmin .ID , sent [0 ].UserID )
656
682
require .Contains (t , sent [0 ].Targets , template .ID )
657
683
require .Contains (t , sent [0 ].Targets , workspace .ID )
658
684
require .Contains (t , sent [0 ].Targets , workspace .OrganizationID )
0 commit comments