Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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