@@ -10,6 +10,7 @@ import (
10
10
11
11
"github.com/coder/coder/v2/coderd"
12
12
"github.com/coder/coder/v2/coderd/coderdtest/oidctest"
13
+ "github.com/coder/coder/v2/coderd/notifications"
13
14
"github.com/coder/coder/v2/coderd/rbac/policy"
14
15
"github.com/coder/serpent"
15
16
@@ -598,6 +599,99 @@ func TestPostUsers(t *testing.T) {
598
599
})
599
600
}
600
601
602
+ func TestNotifyCreatedUser (t * testing.T ) {
603
+ t .Parallel ()
604
+
605
+ t .Run ("OwnerNotified" ,func (t * testing.T ) {
606
+ t .Parallel ()
607
+
608
+ // given
609
+ notifyEnq := & testutil.FakeNotificationsEnqueuer {}
610
+ adminClient := coderdtest .New (t ,& coderdtest.Options {
611
+ NotificationsEnqueuer :notifyEnq ,
612
+ })
613
+ firstUser := coderdtest .CreateFirstUser (t ,adminClient )
614
+
615
+ ctx ,cancel := context .WithTimeout (context .Background (),testutil .WaitLong )
616
+ defer cancel ()
617
+
618
+ // when
619
+ user ,err := adminClient .CreateUser (ctx , codersdk.CreateUserRequest {
620
+ OrganizationID :firstUser .OrganizationID ,
621
+ Email :"another@user.org" ,
622
+ Username :"someone-else" ,
623
+ Password :"SomeSecurePassword!" ,
624
+ })
625
+ require .NoError (t ,err )
626
+
627
+ // then
628
+ require .Len (t ,notifyEnq .Sent ,1 )
629
+ require .Equal (t ,notifications .TemplateUserAccountCreated ,notifyEnq .Sent [0 ].TemplateID )
630
+ require .Equal (t ,firstUser .UserID ,notifyEnq .Sent [0 ].UserID )
631
+ require .Contains (t ,notifyEnq .Sent [0 ].Targets ,user .ID )
632
+ require .Equal (t ,user .Username ,notifyEnq .Sent [0 ].Labels ["created_account_name" ])
633
+ })
634
+
635
+ t .Run ("UserAdminNotified" ,func (t * testing.T ) {
636
+ t .Parallel ()
637
+
638
+ // given
639
+ notifyEnq := & testutil.FakeNotificationsEnqueuer {}
640
+ adminClient := coderdtest .New (t ,& coderdtest.Options {
641
+ NotificationsEnqueuer :notifyEnq ,
642
+ })
643
+ firstUser := coderdtest .CreateFirstUser (t ,adminClient )
644
+
645
+ ctx ,cancel := context .WithTimeout (context .Background (),testutil .WaitLong )
646
+ defer cancel ()
647
+
648
+ userAdmin ,err := adminClient .CreateUser (ctx , codersdk.CreateUserRequest {
649
+ OrganizationID :firstUser .OrganizationID ,
650
+ Email :"user-admin@user.org" ,
651
+ Username :"mr-user-admin" ,
652
+ Password :"SomeSecurePassword!" ,
653
+ })
654
+ require .NoError (t ,err )
655
+
656
+ _ ,err = adminClient .UpdateUserRoles (ctx ,userAdmin .Username , codersdk.UpdateRoles {
657
+ Roles : []string {
658
+ rbac .RoleUserAdmin ().String (),
659
+ },
660
+ })
661
+ require .NoError (t ,err )
662
+
663
+ // when
664
+ member ,err := adminClient .CreateUser (ctx , codersdk.CreateUserRequest {
665
+ OrganizationID :firstUser .OrganizationID ,
666
+ Email :"another@user.org" ,
667
+ Username :"someone-else" ,
668
+ Password :"SomeSecurePassword!" ,
669
+ })
670
+ require .NoError (t ,err )
671
+
672
+ // then
673
+ require .Len (t ,notifyEnq .Sent ,3 )
674
+
675
+ // "User admin" account created, "owner" notified
676
+ require .Equal (t ,notifications .TemplateUserAccountCreated ,notifyEnq .Sent [0 ].TemplateID )
677
+ require .Equal (t ,firstUser .UserID ,notifyEnq .Sent [0 ].UserID )
678
+ require .Contains (t ,notifyEnq .Sent [0 ].Targets ,userAdmin .ID )
679
+ require .Equal (t ,userAdmin .Username ,notifyEnq .Sent [0 ].Labels ["created_account_name" ])
680
+
681
+ // "Member" account created, "owner" notified
682
+ require .Equal (t ,notifications .TemplateUserAccountCreated ,notifyEnq .Sent [1 ].TemplateID )
683
+ require .Equal (t ,firstUser .UserID ,notifyEnq .Sent [1 ].UserID )
684
+ require .Contains (t ,notifyEnq .Sent [1 ].Targets ,member .ID )
685
+ require .Equal (t ,member .Username ,notifyEnq .Sent [1 ].Labels ["created_account_name" ])
686
+
687
+ // "Member" account created, "user admin" notified
688
+ require .Equal (t ,notifications .TemplateUserAccountCreated ,notifyEnq .Sent [1 ].TemplateID )
689
+ require .Equal (t ,userAdmin .ID ,notifyEnq .Sent [2 ].UserID )
690
+ require .Contains (t ,notifyEnq .Sent [2 ].Targets ,member .ID )
691
+ require .Equal (t ,member .Username ,notifyEnq .Sent [2 ].Labels ["created_account_name" ])
692
+ })
693
+ }
694
+
601
695
func TestUpdateUserProfile (t * testing.T ) {
602
696
t .Parallel ()
603
697
t .Run ("UserNotFound" ,func (t * testing.T ) {