Skip to content

feat: add notification for suspended/activated account #14367

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
Aug 22, 2024
Prev Previous commit
Next Next commit
TestNotifyUserReactivate
  • Loading branch information
mtojek committed Aug 21, 2024
commit e1b6c289c0cbb5e8c86b679528f80a282c88d5a5
2 changes: 1 addition & 1 deletion coderd/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -868,7 +868,7 @@ func (api *API) putUserStatus(status database.UserStatus) func(rw http.ResponseW
templateID = notifications.TemplateUserAccountSuspended
case database.UserStatusActive:
key = "activated_account_name"
templateID = notifications.TemplateUserAccountSuspended
templateID = notifications.TemplateUserAccountActivated
default:
api.Logger.Error(ctx, "unable to notify admins as the user's status is unsupported", slog.F("username", user.Username), slog.F("user_status", string(status)))

Expand Down
57 changes: 56 additions & 1 deletion coderd/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ func TestNotifyUserSuspended(t *testing.T) {
require.NoError(t, err)

// then
require.Len(t, notifyEnq.Sent, 6)
require.Len(t, notifyEnq.Sent, 3+3) // 3 notifications due to acccount creation + 3 extra due to account suspension
// notifyEnq.Sent[0]: "User admin" account created, "owner" notified
// notifyEnq.Sent[1]: "Member" account created, "owner" notified
// notifyEnq.Sent[2]: "Member" account created, "user admin" notified
Expand All @@ -426,6 +426,61 @@ func TestNotifyUserSuspended(t *testing.T) {
require.Equal(t, member.Username, notifyEnq.Sent[5].Labels["suspended_account_name"])
}

func TestNotifyUserReactivate(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 := coderdtest.CreateAnotherUser(t, adminClient, firstUser.OrganizationID, rbac.RoleUserAdmin())

member, err := adminClient.CreateUser(ctx, codersdk.CreateUserRequest{
OrganizationID: firstUser.OrganizationID,
Email: "another@user.org",
Username: "someone-else",
Password: "SomeSecurePassword!",
})
require.NoError(t, err)

_, err = adminClient.UpdateUserStatus(context.Background(), member.Username, codersdk.UserStatusSuspended)
require.NoError(t, err)

// when
_, err = adminClient.UpdateUserStatus(context.Background(), member.Username, codersdk.UserStatusActive)
require.NoError(t, err)

// then
require.Len(t, notifyEnq.Sent, 6+3) // 6 notifications due to account suspension + 3 extra due to activation
// notifyEnq.Sent[0]: "User admin" account created, "owner" notified
// notifyEnq.Sent[1]: "Member" account created, "owner" notified
// notifyEnq.Sent[2]: "Member" account created, "user admin" notified

// "Member" account suspended, "owner" notified
require.Equal(t, notifications.TemplateUserAccountActivated, notifyEnq.Sent[6].TemplateID)
require.Equal(t, firstUser.UserID, notifyEnq.Sent[6].UserID)
require.Contains(t, notifyEnq.Sent[6].Targets, member.ID)
require.Equal(t, member.Username, notifyEnq.Sent[6].Labels["activated_account_name"])

// "Member" account suspended, "user admin" notified
require.Equal(t, notifications.TemplateUserAccountActivated, notifyEnq.Sent[7].TemplateID)
require.Equal(t, userAdmin.ID, notifyEnq.Sent[7].UserID)
require.Contains(t, notifyEnq.Sent[7].Targets, member.ID)
require.Equal(t, member.Username, notifyEnq.Sent[7].Labels["activated_account_name"])

// "Member" account suspended, "member" notified
require.Equal(t, notifications.TemplateUserAccountActivated, notifyEnq.Sent[8].TemplateID)
require.Equal(t, member.ID, notifyEnq.Sent[8].UserID)
require.Contains(t, notifyEnq.Sent[8].Targets, member.ID)
require.Equal(t, member.Username, notifyEnq.Sent[8].Labels["activated_account_name"])
}

func TestNotifyDeletedUser(t *testing.T) {
t.Parallel()

Expand Down
Loading