@@ -565,30 +565,31 @@ func TestWorkspaceBuildResources(t *testing.T) {
565565func TestWorkspaceBuildWithUpdatedTemplateVersionSendsNotification (t * testing.T ) {
566566 t .Parallel ()
567567
568- t .Run ("OnlyOneNotification " , func (t * testing.T ) {
568+ t .Run ("NoRepeatedNotifications " , func (t * testing.T ) {
569569 t .Parallel ()
570570
571571 notify := & notificationstest.FakeEnqueuer {}
572572
573573 client := coderdtest .New (t , & coderdtest.Options {IncludeProvisionerDaemon : true , NotificationsEnqueuer : notify })
574574 first := coderdtest .CreateFirstUser (t , client )
575+ templateAdminClient , templateAdmin := coderdtest .CreateAnotherUser (t , client , first .OrganizationID , rbac .RoleTemplateAdmin ())
575576 userClient , user := coderdtest .CreateAnotherUser (t , client , first .OrganizationID )
576577
577578 // 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 )
581582
582583 // Create a workspace using this template
583584 workspace := coderdtest .CreateWorkspace (t , userClient , template .ID )
584585 coderdtest .AwaitWorkspaceBuildJobCompleted (t , userClient , workspace .LatestBuild .ID )
585586 coderdtest .MustTransitionWorkspace (t , userClient , workspace .ID , database .WorkspaceTransitionStart , database .WorkspaceTransitionStop )
586587
587588 // 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 ) {
589590 ctvr .TemplateID = template .ID
590591 })
591- coderdtest .AwaitTemplateVersionJobCompleted (t , client , newVersion .ID )
592+ coderdtest .AwaitTemplateVersionJobCompleted (t , templateAdminClient , newVersion .ID )
592593
593594 // Create a workspace build using this new template version
594595 build := coderdtest .CreateWorkspaceBuild (t , userClient , workspace , database .WorkspaceTransitionStart , func (cwbr * codersdk.CreateWorkspaceBuildRequest ) {
@@ -597,21 +598,45 @@ func TestWorkspaceBuildWithUpdatedTemplateVersionSendsNotification(t *testing.T)
597598 coderdtest .AwaitWorkspaceBuildJobCompleted (t , userClient , build .ID )
598599 coderdtest .MustTransitionWorkspace (t , userClient , workspace .ID , database .WorkspaceTransitionStart , database .WorkspaceTransitionStop )
599600
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.
601607 build = coderdtest .CreateWorkspaceBuild (t , userClient , workspace , database .WorkspaceTransitionStart , func (cwbr * codersdk.CreateWorkspaceBuildRequest ) {
602608 cwbr .TemplateVersionID = newVersion .ID
603609 })
604610 coderdtest .AwaitWorkspaceBuildJobCompleted (t , userClient , build .ID )
605611 coderdtest .MustTransitionWorkspace (t , userClient , workspace .ID , database .WorkspaceTransitionStart , database .WorkspaceTransitionStop )
606612
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.
608616 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+
611631 require .Contains (t , sent [0 ].Targets , template .ID )
612632 require .Contains (t , sent [0 ].Targets , workspace .ID )
613633 require .Contains (t , sent [0 ].Targets , workspace .OrganizationID )
614634 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 )
615640 })
616641
617642 t .Run ("ToCorrectUser" , func (t * testing.T ) {
@@ -621,23 +646,24 @@ func TestWorkspaceBuildWithUpdatedTemplateVersionSendsNotification(t *testing.T)
621646
622647 client := coderdtest .New (t , & coderdtest.Options {IncludeProvisionerDaemon : true , NotificationsEnqueuer : notify })
623648 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 )
625651
626652 // 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 )
630656
631657 // Create a workspace using this template
632658 workspace := coderdtest .CreateWorkspace (t , userClient , template .ID )
633659 coderdtest .AwaitWorkspaceBuildJobCompleted (t , userClient , workspace .LatestBuild .ID )
634660 coderdtest .MustTransitionWorkspace (t , userClient , workspace .ID , database .WorkspaceTransitionStart , database .WorkspaceTransitionStop )
635661
636662 // 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 ) {
638664 ctvr .TemplateID = template .ID
639665 })
640- coderdtest .AwaitTemplateVersionJobCompleted (t , client , newVersion .ID )
666+ coderdtest .AwaitTemplateVersionJobCompleted (t , templateAdminClient , newVersion .ID )
641667
642668 // Create a workspace build using this new template version from a different user
643669 ctx := testutil .Context (t , testutil .WaitShort )
@@ -652,7 +678,7 @@ func TestWorkspaceBuildWithUpdatedTemplateVersionSendsNotification(t *testing.T)
652678 // Ensure we receive only 1 workspace manually updated notification and to the right user
653679 sent := notify .Sent (notificationstest .WithTemplateID (notifications .TemplateWorkspaceManuallyUpdated ))
654680 require .Len (t , sent , 1 )
655- require .Equal (t , user .ID , sent [0 ].UserID )
681+ require .Equal (t , templateAdmin .ID , sent [0 ].UserID )
656682 require .Contains (t , sent [0 ].Targets , template .ID )
657683 require .Contains (t , sent [0 ].Targets , workspace .ID )
658684 require .Contains (t , sent [0 ].Targets , workspace .OrganizationID )
0 commit comments