-
Notifications
You must be signed in to change notification settings - Fork 974
feat: add template delete notification #14250
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
Changes from 10 commits
5915daa
76ee1f6
1f7046c
30faf4f
1e11cb7
99c1950
be31ed8
9e6462d
7fb0fa2
92c11de
450235e
eaf87c5
4688765
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
INSERT INTO | ||
notification_templates ( | ||
id, | ||
name, | ||
title_template, | ||
body_template, | ||
"group", | ||
actions | ||
) | ||
VALUES ( | ||
'29a09665-2a4c-403f-9648-54301670e7be', | ||
'Template Deleted', | ||
E'Template "{{.Labels.name}}" deleted', | ||
E'Hi {{.UserName}}\n\nThe template **{{.Labels.name}}** was deleted by **{{ .Labels.initiator }}**.', | ||
'Template Events', | ||
'[ | ||
{ | ||
"label": "View templates", | ||
"url": "{{ base_url }}/templates" | ||
} | ||
]'::jsonb | ||
); |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -19,6 +19,7 @@ import ( | |||||
"github.com/coder/coder/v2/coderd/database" | ||||||
"github.com/coder/coder/v2/coderd/database/dbauthz" | ||||||
"github.com/coder/coder/v2/coderd/database/dbtime" | ||||||
"github.com/coder/coder/v2/coderd/notifications" | ||||||
"github.com/coder/coder/v2/coderd/rbac" | ||||||
"github.com/coder/coder/v2/coderd/schedule" | ||||||
"github.com/coder/coder/v2/coderd/util/ptr" | ||||||
|
@@ -1326,3 +1327,93 @@ func TestTemplateMetrics(t *testing.T) { | |||||
dbtime.Now(), res.Workspaces[0].LastUsedAt, time.Minute, | ||||||
) | ||||||
} | ||||||
|
||||||
func TestTemplateNotifications(t *testing.T) { | ||||||
t.Parallel() | ||||||
|
||||||
t.Run("Delete", func(t *testing.T) { | ||||||
t.Parallel() | ||||||
|
||||||
t.Run("SendNotification", func(t *testing.T) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now this is a great test! Needs a little work but it's very nice. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
t.Parallel() | ||||||
|
||||||
// Given: an owner and a template admin | ||||||
var ( | ||||||
notifyEnq = &testutil.FakeNotificationsEnqueuer{} | ||||||
client = coderdtest.New(t, &coderdtest.Options{ | ||||||
IncludeProvisionerDaemon: true, | ||||||
NotificationsEnqueuer: notifyEnq, | ||||||
}) | ||||||
owner = coderdtest.CreateFirstUser(t, client) | ||||||
version = coderdtest.CreateTemplateVersion(t, client, owner.OrganizationID, nil) | ||||||
_ = coderdtest.AwaitTemplateVersionJobCompleted(t, client, version.ID) | ||||||
template = coderdtest.CreateTemplate(t, client, owner.OrganizationID, version.ID) | ||||||
ctx = testutil.Context(t, testutil.WaitLong) | ||||||
_, templateAdmin = coderdtest.CreateAnotherUser(t, client, owner.OrganizationID, rbac.RoleTemplateAdmin()) | ||||||
) | ||||||
|
||||||
// When: the template is deleted | ||||||
err := client.DeleteTemplate(ctx, template.ID) | ||||||
require.NoError(t, err) | ||||||
|
||||||
// Then: verify that the notification is sent to the correct user | ||||||
// (template admin) and targets, using the appropriate labels. Note that | ||||||
// the owner, being the initiator, will not receive the notification. | ||||||
deleteNotifications := make([]*testutil.Notification, 0) | ||||||
for _, n := range notifyEnq.Sent { | ||||||
if n.TemplateID == notifications.TemplateTemplateDeleted { | ||||||
deleteNotifications = append(deleteNotifications, n) | ||||||
} | ||||||
} | ||||||
require.Len(t, deleteNotifications, 1) | ||||||
require.Contains(t, deleteNotifications[0].TemplateID, notifications.TemplateTemplateDeleted) | ||||||
require.Contains(t, deleteNotifications[0].UserID, templateAdmin.ID) | ||||||
require.Contains(t, deleteNotifications[0].Targets, template.ID) | ||||||
require.Contains(t, deleteNotifications[0].Targets, template.OrganizationID) | ||||||
require.Equal(t, deleteNotifications[0].Labels["name"], template.Name) | ||||||
require.Equal(t, deleteNotifications[0].Labels["initiator"], coderdtest.FirstUserParams.Username) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you could simplify this to show that the notification is not received at all in this test if there's only one owner. |
||||||
}) | ||||||
|
||||||
t.Run("OnlyNotifyOwnersAndTemplateAdmins", func(t *testing.T) { | ||||||
t.Parallel() | ||||||
|
||||||
// Given: multiple users with different roles | ||||||
var ( | ||||||
notifyEnq = &testutil.FakeNotificationsEnqueuer{} | ||||||
client = coderdtest.New(t, &coderdtest.Options{ | ||||||
IncludeProvisionerDaemon: true, | ||||||
NotificationsEnqueuer: notifyEnq, | ||||||
}) | ||||||
firstUser = coderdtest.CreateFirstUser(t, client) | ||||||
version = coderdtest.CreateTemplateVersion(t, client, firstUser.OrganizationID, nil) | ||||||
_ = coderdtest.AwaitTemplateVersionJobCompleted(t, client, version.ID) | ||||||
template = coderdtest.CreateTemplate(t, client, firstUser.OrganizationID, version.ID) | ||||||
ctx = testutil.Context(t, testutil.WaitLong) | ||||||
) | ||||||
_, anotherOwner := coderdtest.CreateAnotherUser(t, client, firstUser.OrganizationID, rbac.RoleOwner()) | ||||||
_, tmplAdmin := coderdtest.CreateAnotherUser(t, client, firstUser.OrganizationID, rbac.RoleTemplateAdmin()) | ||||||
coderdtest.CreateAnotherUser(t, client, firstUser.OrganizationID, rbac.RoleMember()) | ||||||
coderdtest.CreateAnotherUser(t, client, firstUser.OrganizationID, rbac.RoleUserAdmin()) | ||||||
|
||||||
// When: the template is deleted by the owner | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Which owner? You have the first user, which is performing this action, and
Suggested change
|
||||||
err := client.DeleteTemplate(ctx, template.ID) | ||||||
require.NoError(t, err) | ||||||
|
||||||
// Then: only the template owners and template admins should receive the | ||||||
// notification. Since the first user is the initiator, it should not | ||||||
// receive the notification. | ||||||
shouldBeNotified := []uuid.UUID{anotherOwner.ID, tmplAdmin.ID} | ||||||
var deleteTemplateNotifications []*testutil.Notification | ||||||
for _, n := range notifyEnq.Sent { | ||||||
if n.TemplateID == notifications.TemplateTemplateDeleted { | ||||||
deleteTemplateNotifications = append(deleteTemplateNotifications, n) | ||||||
} | ||||||
} | ||||||
notifiedUsers := make([]uuid.UUID, 0, len(deleteTemplateNotifications)) | ||||||
for _, n := range deleteTemplateNotifications { | ||||||
notifiedUsers = append(notifiedUsers, n.UserID) | ||||||
} | ||||||
require.ElementsMatch(t, shouldBeNotified, notifiedUsers) | ||||||
}) | ||||||
}) | ||||||
} |
Uh oh!
There was an error while loading. Please reload this page.