Skip to content

feat: add template delete notification - #14250

Merged
BrunoQuaresma merged 13 commits into
mainfrom
bq/notify-template-delete
Aug 14, 2024
Merged

feat: add template delete notification#14250
BrunoQuaresma merged 13 commits into
mainfrom
bq/notify-template-delete

Conversation

@BrunoQuaresma

Copy link
Copy Markdown
Contributor

Preview:
Screenshot 2024-08-12 at 15 54 29

Related to: coder/internal#16

@BrunoQuaresma BrunoQuaresma self-assigned this Aug 12, 2024

@dannykopping dannykopping left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good, but we need to send to all template admins & owners.

Comment thread coderd/notifications/events.go Outdated
Comment thread coderd/templates.go Outdated
Comment thread coderd/workspaces_test.go

@dannykopping dannykopping left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost there 👍

Comment thread coderd/templates.go
Comment thread coderd/templates.go Outdated
Comment thread coderd/templates_test.go Outdated
Comment thread coderd/templates_test.go Outdated
require.NoError(t, err)

// Then: the template owners and template admins should receive the notification
shouldBeNotified := []uuid.UUID{anotherOwner.ID, tmplAdmin.ID}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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.

Comment thread coderd/templates_test.go Outdated
Comment thread coderd/templates_test.go Outdated
func TestNotifyDeletedTemplate(t *testing.T) {
t.Parallel()

t.Run("OnlyNotifyOwnersAndTemplateAdmins", func(t *testing.T) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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?

@dannykopping dannykopping left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost there! Thanks for taking the feedback onboard so well

Comment thread coderd/templates_test.go Outdated
t.Run("Delete", func(t *testing.T) {
t.Parallel()

t.Run("SendNotification", func(t *testing.T) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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.
Doesn't this feel better to read now?

Comment thread coderd/templates_test.go Outdated
coderdtest.CreateAnotherUser(t, client, firstUser.OrganizationID, rbac.RoleUserAdmin())

// When: template is deleted
// When: the template is deleted by the owner

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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 anotherOwner.

Suggested change
// When: the template is deleted by the owner
// When: the template is deleted by firstUser

Comment thread coderd/templates_test.go Outdated
Comment on lines +1362 to +1374
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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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.
In the other test you're checking for exactly the same thing.
Tests should validate one (or as close to one) aspect of business logic at a time, IMHO.

Comment thread coderd/templates_test.go Outdated
t.Run("Delete", func(t *testing.T) {
t.Parallel()

t.Run("SendNotification", func(t *testing.T) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
t.Run("SendNotification", func(t *testing.T) {
t.Run("InitiatorIsNotNotified", func(t *testing.T) {

@BrunoQuaresma
BrunoQuaresma merged commit 6f1951e into main Aug 14, 2024
@BrunoQuaresma
BrunoQuaresma deleted the bq/notify-template-delete branch August 14, 2024 17:22
@github-actions github-actions Bot locked and limited conversation to collaborators Aug 14, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants