-
Notifications
You must be signed in to change notification settings - Fork 929
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 3 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,57 @@ func TestTemplateMetrics(t *testing.T) { | |
dbtime.Now(), res.Workspaces[0].LastUsedAt, time.Minute, | ||
) | ||
} | ||
|
||
func TestNotifyDeletedTemplate(t *testing.T) { | ||
dannykopping marked this conversation as resolved.
Show resolved
Hide resolved
|
||
t.Parallel() | ||
|
||
t.Run("OnlyNotifyOwnersAndTemplateAdmins", 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. Please add a separate test to validate the functionality in isolation; you can then reduce some complexity in this test and keep it nicely scoped. 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. Not sure if I got it. What should I separate and what complexity does it solve? |
||
t.Parallel() | ||
|
||
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()) | ||
|
||
// When: template is deleted | ||
err := client.DeleteTemplate(ctx, template.ID) | ||
require.NoError(t, err) | ||
|
||
// Then: the template owners and template admins should receive the notification | ||
shouldBeNotified := []uuid.UUID{anotherOwner.ID, tmplAdmin.ID} | ||
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 it'd be good to explicitly specify that the first user is not receiving the notification because they initiated it. 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 it'd also be a good idea here to create another admin but with a different role like user admin, and validate that they don't receive the notification either. |
||
var deleteTemplateNotifications []*testutil.Notification | ||
for _, n := range notifyEnq.Sent { | ||
if n.TemplateID == notifications.TemplateTemplateDeleted { | ||
deleteTemplateNotifications = append(deleteTemplateNotifications, n) | ||
} | ||
} | ||
require.Len(t, deleteTemplateNotifications, len(shouldBeNotified)) | ||
|
||
for _, userID := range shouldBeNotified { | ||
dannykopping marked this conversation as resolved.
Show resolved
Hide resolved
|
||
var notification *testutil.Notification | ||
for _, n := range deleteTemplateNotifications { | ||
if n.UserID == userID { | ||
notification = n | ||
break | ||
} | ||
} | ||
require.NotNil(t, notification) | ||
require.Contains(t, notification.Targets, template.ID) | ||
require.Contains(t, notification.Targets, template.OrganizationID) | ||
require.Equal(t, notification.Labels["name"], template.Name) | ||
require.Equal(t, notification.Labels["initiator"], coderdtest.FirstUserParams.Username) | ||
} | ||
}) | ||
} |
Uh oh!
There was an error while loading. Please reload this page.