Skip to content

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

Merged
merged 13 commits into from
Aug 14, 2024
Merged

Conversation

BrunoQuaresma
Copy link
Collaborator

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

Related to: coder/internal#16

@BrunoQuaresma BrunoQuaresma self-assigned this Aug 12, 2024
Copy link
Contributor

@dannykopping dannykopping left a comment

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.

Copy link
Contributor

@dannykopping dannykopping left a comment

Choose a reason for hiding this comment

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

Almost there 👍

require.NoError(t, err)

// Then: the template owners and template admins should receive the notification
shouldBeNotified := []uuid.UUID{anotherOwner.ID, tmplAdmin.ID}
Copy link
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
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.

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

t.Run("OnlyNotifyOwnersAndTemplateAdmins", func(t *testing.T) {
Copy link
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
Collaborator 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?

Copy link
Contributor

@dannykopping dannykopping left a comment

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

@@ -1334,9 +1334,50 @@ func TestTemplateNotifications(t *testing.T) {
t.Run("Delete", func(t *testing.T) {
t.Parallel()

t.Run("SendNotification", func(t *testing.T) {
Copy link
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?

_, 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: template is deleted
// When: the template is deleted by the owner
Copy link
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 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
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.

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

t.Run("SendNotification", func(t *testing.T) {
Copy link
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
27 checks passed
@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