mirror of
https://github.com/coder/coder.git
synced 2025-07-03 16:13:58 +00:00
feat: notify about created user account (#14010)
This commit is contained in:
@ -10,6 +10,7 @@ import (
|
||||
|
||||
"github.com/coder/coder/v2/coderd"
|
||||
"github.com/coder/coder/v2/coderd/coderdtest/oidctest"
|
||||
"github.com/coder/coder/v2/coderd/notifications"
|
||||
"github.com/coder/coder/v2/coderd/rbac/policy"
|
||||
"github.com/coder/serpent"
|
||||
|
||||
@ -598,6 +599,99 @@ func TestPostUsers(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestNotifyCreatedUser(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
t.Run("OwnerNotified", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
// given
|
||||
notifyEnq := &testutil.FakeNotificationsEnqueuer{}
|
||||
adminClient := coderdtest.New(t, &coderdtest.Options{
|
||||
NotificationsEnqueuer: notifyEnq,
|
||||
})
|
||||
firstUser := coderdtest.CreateFirstUser(t, adminClient)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
|
||||
defer cancel()
|
||||
|
||||
// when
|
||||
user, err := adminClient.CreateUser(ctx, codersdk.CreateUserRequest{
|
||||
OrganizationID: firstUser.OrganizationID,
|
||||
Email: "another@user.org",
|
||||
Username: "someone-else",
|
||||
Password: "SomeSecurePassword!",
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
// then
|
||||
require.Len(t, notifyEnq.Sent, 1)
|
||||
require.Equal(t, notifications.TemplateUserAccountCreated, notifyEnq.Sent[0].TemplateID)
|
||||
require.Equal(t, firstUser.UserID, notifyEnq.Sent[0].UserID)
|
||||
require.Contains(t, notifyEnq.Sent[0].Targets, user.ID)
|
||||
require.Equal(t, user.Username, notifyEnq.Sent[0].Labels["created_account_name"])
|
||||
})
|
||||
|
||||
t.Run("UserAdminNotified", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
// given
|
||||
notifyEnq := &testutil.FakeNotificationsEnqueuer{}
|
||||
adminClient := coderdtest.New(t, &coderdtest.Options{
|
||||
NotificationsEnqueuer: notifyEnq,
|
||||
})
|
||||
firstUser := coderdtest.CreateFirstUser(t, adminClient)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
|
||||
defer cancel()
|
||||
|
||||
userAdmin, err := adminClient.CreateUser(ctx, codersdk.CreateUserRequest{
|
||||
OrganizationID: firstUser.OrganizationID,
|
||||
Email: "user-admin@user.org",
|
||||
Username: "mr-user-admin",
|
||||
Password: "SomeSecurePassword!",
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
_, err = adminClient.UpdateUserRoles(ctx, userAdmin.Username, codersdk.UpdateRoles{
|
||||
Roles: []string{
|
||||
rbac.RoleUserAdmin().String(),
|
||||
},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
// when
|
||||
member, err := adminClient.CreateUser(ctx, codersdk.CreateUserRequest{
|
||||
OrganizationID: firstUser.OrganizationID,
|
||||
Email: "another@user.org",
|
||||
Username: "someone-else",
|
||||
Password: "SomeSecurePassword!",
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
// then
|
||||
require.Len(t, notifyEnq.Sent, 3)
|
||||
|
||||
// "User admin" account created, "owner" notified
|
||||
require.Equal(t, notifications.TemplateUserAccountCreated, notifyEnq.Sent[0].TemplateID)
|
||||
require.Equal(t, firstUser.UserID, notifyEnq.Sent[0].UserID)
|
||||
require.Contains(t, notifyEnq.Sent[0].Targets, userAdmin.ID)
|
||||
require.Equal(t, userAdmin.Username, notifyEnq.Sent[0].Labels["created_account_name"])
|
||||
|
||||
// "Member" account created, "owner" notified
|
||||
require.Equal(t, notifications.TemplateUserAccountCreated, notifyEnq.Sent[1].TemplateID)
|
||||
require.Equal(t, firstUser.UserID, notifyEnq.Sent[1].UserID)
|
||||
require.Contains(t, notifyEnq.Sent[1].Targets, member.ID)
|
||||
require.Equal(t, member.Username, notifyEnq.Sent[1].Labels["created_account_name"])
|
||||
|
||||
// "Member" account created, "user admin" notified
|
||||
require.Equal(t, notifications.TemplateUserAccountCreated, notifyEnq.Sent[1].TemplateID)
|
||||
require.Equal(t, userAdmin.ID, notifyEnq.Sent[2].UserID)
|
||||
require.Contains(t, notifyEnq.Sent[2].Targets, member.ID)
|
||||
require.Equal(t, member.Username, notifyEnq.Sent[2].Labels["created_account_name"])
|
||||
})
|
||||
}
|
||||
|
||||
func TestUpdateUserProfile(t *testing.T) {
|
||||
t.Parallel()
|
||||
t.Run("UserNotFound", func(t *testing.T) {
|
||||
|
Reference in New Issue
Block a user