Skip to content

feat: generate golden files for notification templates #14537

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 24 commits into from
Sep 4, 2024
Merged
59 changes: 44 additions & 15 deletions coderd/notifications/notifications_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@ import (
"context"
_ "embed"
"encoding/json"
"flag"
"fmt"
"go/ast"
"go/parser"
"go/token"
"net/http"
"net/http/httptest"
"net/url"
"os"
"path/filepath"
"slices"
"sort"
"strings"
"sync"
"sync/atomic"
"testing"
Expand Down Expand Up @@ -46,6 +50,9 @@ import (
"github.com/coder/coder/v2/testutil"
)

// updateGoldenFiles is a flag that can be set to update golden files.
var updateGoldenFiles = flag.Bool("update", false, "Update golden files")

func TestMain(m *testing.M) {
goleak.VerifyTestMain(m)
}
Expand Down Expand Up @@ -693,7 +700,7 @@ func TestNotificationTemplatesCanRender(t *testing.T) {
name: "TemplateWorkspaceDeleted",
id: notifications.TemplateWorkspaceDeleted,
payload: types.MessagePayload{
UserName: "bobby",
UserName: "Bobby",
Labels: map[string]string{
"name": "bobby-workspace",
"reason": "autodeleted due to dormancy",
Expand All @@ -705,7 +712,7 @@ func TestNotificationTemplatesCanRender(t *testing.T) {
name: "TemplateWorkspaceAutobuildFailed",
id: notifications.TemplateWorkspaceAutobuildFailed,
payload: types.MessagePayload{
UserName: "bobby",
UserName: "Bobby",
Labels: map[string]string{
"name": "bobby-workspace",
"reason": "autostart",
Expand All @@ -716,7 +723,7 @@ func TestNotificationTemplatesCanRender(t *testing.T) {
name: "TemplateWorkspaceDormant",
id: notifications.TemplateWorkspaceDormant,
payload: types.MessagePayload{
UserName: "bobby",
UserName: "Bobby",
Labels: map[string]string{
"name": "bobby-workspace",
"reason": "breached the template's threshold for inactivity",
Expand All @@ -730,7 +737,7 @@ func TestNotificationTemplatesCanRender(t *testing.T) {
name: "TemplateWorkspaceAutoUpdated",
id: notifications.TemplateWorkspaceAutoUpdated,
payload: types.MessagePayload{
UserName: "bobby",
UserName: "Bobby",
Labels: map[string]string{
"name": "bobby-workspace",
"template_version_name": "1.0",
Expand All @@ -742,7 +749,7 @@ func TestNotificationTemplatesCanRender(t *testing.T) {
name: "TemplateWorkspaceMarkedForDeletion",
id: notifications.TemplateWorkspaceMarkedForDeletion,
payload: types.MessagePayload{
UserName: "bobby",
UserName: "Bobby",
Labels: map[string]string{
"name": "bobby-workspace",
"reason": "template updated to new dormancy policy",
Expand All @@ -755,7 +762,7 @@ func TestNotificationTemplatesCanRender(t *testing.T) {
name: "TemplateUserAccountCreated",
id: notifications.TemplateUserAccountCreated,
payload: types.MessagePayload{
UserName: "bobby",
UserName: "Bobby",
Labels: map[string]string{
"created_account_name": "bobby",
},
Expand All @@ -765,7 +772,7 @@ func TestNotificationTemplatesCanRender(t *testing.T) {
name: "TemplateUserAccountDeleted",
id: notifications.TemplateUserAccountDeleted,
payload: types.MessagePayload{
UserName: "bobby",
UserName: "Bobby",
Labels: map[string]string{
"deleted_account_name": "bobby",
},
Expand All @@ -775,7 +782,7 @@ func TestNotificationTemplatesCanRender(t *testing.T) {
name: "TemplateUserAccountSuspended",
id: notifications.TemplateUserAccountSuspended,
payload: types.MessagePayload{
UserName: "bobby",
UserName: "Bobby",
Labels: map[string]string{
"suspended_account_name": "bobby",
},
Expand All @@ -785,7 +792,7 @@ func TestNotificationTemplatesCanRender(t *testing.T) {
name: "TemplateUserAccountActivated",
id: notifications.TemplateUserAccountActivated,
payload: types.MessagePayload{
UserName: "bobby",
UserName: "Bobby",
Labels: map[string]string{
"activated_account_name": "bobby",
},
Expand All @@ -795,7 +802,7 @@ func TestNotificationTemplatesCanRender(t *testing.T) {
name: "TemplateYourAccountSuspended",
id: notifications.TemplateYourAccountSuspended,
payload: types.MessagePayload{
UserName: "bobby",
UserName: "Bobby",
Labels: map[string]string{
"suspended_account_name": "bobby",
},
Expand All @@ -805,7 +812,7 @@ func TestNotificationTemplatesCanRender(t *testing.T) {
name: "TemplateYourAccountActivated",
id: notifications.TemplateYourAccountActivated,
payload: types.MessagePayload{
UserName: "bobby",
UserName: "Bobby",
Labels: map[string]string{
"activated_account_name": "bobby",
},
Expand All @@ -815,7 +822,7 @@ func TestNotificationTemplatesCanRender(t *testing.T) {
name: "TemplateTemplateDeleted",
id: notifications.TemplateTemplateDeleted,
payload: types.MessagePayload{
UserName: "bobby",
UserName: "Bobby",
Labels: map[string]string{
"name": "bobby-template",
"initiator": "rob",
Expand All @@ -826,7 +833,7 @@ func TestNotificationTemplatesCanRender(t *testing.T) {
name: "TemplateWorkspaceManualBuildFailed",
id: notifications.TemplateWorkspaceManualBuildFailed,
payload: types.MessagePayload{
UserName: "bobby",
UserName: "Bobby",
Labels: map[string]string{
"name": "bobby-workspace",
"template_name": "bobby-template",
Expand Down Expand Up @@ -869,14 +876,36 @@ func TestNotificationTemplatesCanRender(t *testing.T) {
Scan(&titleTmpl, &bodyTmpl)
require.NoError(t, err, "failed to query body template for template:", tc.id)

title, err := render.GoTemplate(titleTmpl, tc.payload, nil)
title, err := render.GoTemplate(titleTmpl, tc.payload, defaultHelpers())
require.NotContainsf(t, title, render.NoValue, "template %q is missing a label value", tc.name)
require.NoError(t, err, "failed to render notification title template")
require.NotEmpty(t, title, "title should not be empty")

body, err := render.GoTemplate(bodyTmpl, tc.payload, nil)
body, err := render.GoTemplate(bodyTmpl, tc.payload, defaultHelpers())
require.NoError(t, err, "failed to render notification body template")
require.NotEmpty(t, body, "body should not be empty")

partialName := strings.Split(t.Name(), "/")[1]
bodyGoldenFile := filepath.Join("testdata", "rendered-templates", partialName+"-body.md.golden")
titleGoldenFile := filepath.Join("testdata", "rendered-templates", partialName+"-title.md.golden")

if *updateGoldenFiles {
err = os.MkdirAll(filepath.Dir(bodyGoldenFile), 0o755)
require.NoError(t, err, "want no error creating golden file directory")
err = os.WriteFile(bodyGoldenFile, []byte(body), 0o600)
require.NoError(t, err, "want no error writing body golden file")
err = os.WriteFile(titleGoldenFile, []byte(title), 0o600)
require.NoError(t, err, "want no error writing title golden file")
return
}

wantBody, err := os.ReadFile(bodyGoldenFile)
require.NoError(t, err, "open golden file, run \"DB=ci make update-golden-files\" and commit the changes")
wantTitle, err := os.ReadFile(titleGoldenFile)
require.NoError(t, err, "open golden file, run \"DB=ci make update-golden-files\" and commit the changes")

require.Equal(t, string(wantBody), body, "body should be equal")
require.Equal(t, string(wantTitle), title, "title should be equal")
})
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Hi Bobby

The template **bobby-template** was deleted by **rob**.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Template "bobby-template" deleted
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Hi Bobby,
User account **bobby** has been activated.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
User account "bobby" activated
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Hi Bobby,

New user account **bobby** has been created.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
User account "bobby" created
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Hi Bobby,

User account **bobby** has been deleted.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
User account "bobby" deleted
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Hi Bobby,
User account **bobby** has been suspended.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
User account "bobby" suspended
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Hi Bobby
Your workspace **bobby-workspace** has been updated automatically to the latest template version (1.0).
Reason for update: **template now includes catnip**
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Workspace "bobby-workspace" updated automatically
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Hi Bobby
Automatic build of your workspace **bobby-workspace** failed.
The specified reason was "**autostart**".
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Workspace "bobby-workspace" autobuild failed
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Hi Bobby

Your workspace **bobby-workspace** was deleted.
The specified reason was "**autodeleted due to dormancy (autobuild)**".
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Workspace "bobby-workspace" deleted
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Hi Bobby

Your workspace **bobby-workspace** has been marked as [**dormant**](https://coder.com/docs/templates/schedule#dormancy-threshold-enterprise) because of breached the template's threshold for inactivity.
Dormant workspaces are [automatically deleted](https://coder.com/docs/templates/schedule#dormancy-auto-deletion-enterprise) after 24 hours of inactivity.
To prevent deletion, use your workspace with the link below.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Workspace "bobby-workspace" marked as dormant
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Hi Bobby,

A manual build of the workspace **bobby-workspace** using the template **bobby-template** failed (version: **bobby-template-version**).
The workspace build was initiated by **joe**.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Workspace "bobby-workspace" manual build failed
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Hi Bobby

Your workspace **bobby-workspace** has been marked for **deletion** after 24 hours of [dormancy](https://coder.com/docs/templates/schedule#dormancy-auto-deletion-enterprise) because of template updated to new dormancy policy.
To prevent deletion, use your workspace with the link below.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Workspace "bobby-workspace" marked for deletion
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Hi Bobby,
Your account **bobby** has been activated.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Your account "bobby" has been activated
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Hi Bobby,
Your account **bobby** has been suspended.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Your account "bobby" has been suspended
Loading