diff --git a/coderd/notifications/notifications_test.go b/coderd/notifications/notifications_test.go index 3e9a68f7207c6..567866a0aaf35 100644 --- a/coderd/notifications/notifications_test.go +++ b/coderd/notifications/notifications_test.go @@ -5,6 +5,7 @@ import ( "context" _ "embed" "encoding/json" + "flag" "fmt" "go/ast" "go/parser" @@ -12,8 +13,11 @@ import ( "net/http" "net/http/httptest" "net/url" + "os" + "path/filepath" "slices" "sort" + "strings" "sync" "sync/atomic" "testing" @@ -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) } @@ -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", @@ -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", @@ -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", @@ -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", @@ -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", @@ -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", }, @@ -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", }, @@ -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", }, @@ -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", }, @@ -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", }, @@ -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", }, @@ -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", @@ -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", @@ -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") }) } } diff --git a/coderd/notifications/testdata/rendered-templates/TemplateTemplateDeleted-body.md.golden b/coderd/notifications/testdata/rendered-templates/TemplateTemplateDeleted-body.md.golden new file mode 100644 index 0000000000000..be3a61e695652 --- /dev/null +++ b/coderd/notifications/testdata/rendered-templates/TemplateTemplateDeleted-body.md.golden @@ -0,0 +1,3 @@ +Hi Bobby + +The template **bobby-template** was deleted by **rob**. \ No newline at end of file diff --git a/coderd/notifications/testdata/rendered-templates/TemplateTemplateDeleted-title.md.golden b/coderd/notifications/testdata/rendered-templates/TemplateTemplateDeleted-title.md.golden new file mode 100644 index 0000000000000..c3f3db7645422 --- /dev/null +++ b/coderd/notifications/testdata/rendered-templates/TemplateTemplateDeleted-title.md.golden @@ -0,0 +1 @@ +Template "bobby-template" deleted \ No newline at end of file diff --git a/coderd/notifications/testdata/rendered-templates/TemplateUserAccountActivated-body.md.golden b/coderd/notifications/testdata/rendered-templates/TemplateUserAccountActivated-body.md.golden new file mode 100644 index 0000000000000..2665a781492ea --- /dev/null +++ b/coderd/notifications/testdata/rendered-templates/TemplateUserAccountActivated-body.md.golden @@ -0,0 +1,2 @@ +Hi Bobby, +User account **bobby** has been activated. \ No newline at end of file diff --git a/coderd/notifications/testdata/rendered-templates/TemplateUserAccountActivated-title.md.golden b/coderd/notifications/testdata/rendered-templates/TemplateUserAccountActivated-title.md.golden new file mode 100644 index 0000000000000..ebf8e9da36934 --- /dev/null +++ b/coderd/notifications/testdata/rendered-templates/TemplateUserAccountActivated-title.md.golden @@ -0,0 +1 @@ +User account "bobby" activated \ No newline at end of file diff --git a/coderd/notifications/testdata/rendered-templates/TemplateUserAccountCreated-body.md.golden b/coderd/notifications/testdata/rendered-templates/TemplateUserAccountCreated-body.md.golden new file mode 100644 index 0000000000000..e5a5be89c11e0 --- /dev/null +++ b/coderd/notifications/testdata/rendered-templates/TemplateUserAccountCreated-body.md.golden @@ -0,0 +1,3 @@ +Hi Bobby, + +New user account **bobby** has been created. \ No newline at end of file diff --git a/coderd/notifications/testdata/rendered-templates/TemplateUserAccountCreated-title.md.golden b/coderd/notifications/testdata/rendered-templates/TemplateUserAccountCreated-title.md.golden new file mode 100644 index 0000000000000..bfcdf6826f772 --- /dev/null +++ b/coderd/notifications/testdata/rendered-templates/TemplateUserAccountCreated-title.md.golden @@ -0,0 +1 @@ +User account "bobby" created \ No newline at end of file diff --git a/coderd/notifications/testdata/rendered-templates/TemplateUserAccountDeleted-body.md.golden b/coderd/notifications/testdata/rendered-templates/TemplateUserAccountDeleted-body.md.golden new file mode 100644 index 0000000000000..bd1066c25fb50 --- /dev/null +++ b/coderd/notifications/testdata/rendered-templates/TemplateUserAccountDeleted-body.md.golden @@ -0,0 +1,3 @@ +Hi Bobby, + +User account **bobby** has been deleted. \ No newline at end of file diff --git a/coderd/notifications/testdata/rendered-templates/TemplateUserAccountDeleted-title.md.golden b/coderd/notifications/testdata/rendered-templates/TemplateUserAccountDeleted-title.md.golden new file mode 100644 index 0000000000000..199d4ddd66d12 --- /dev/null +++ b/coderd/notifications/testdata/rendered-templates/TemplateUserAccountDeleted-title.md.golden @@ -0,0 +1 @@ +User account "bobby" deleted \ No newline at end of file diff --git a/coderd/notifications/testdata/rendered-templates/TemplateUserAccountSuspended-body.md.golden b/coderd/notifications/testdata/rendered-templates/TemplateUserAccountSuspended-body.md.golden new file mode 100644 index 0000000000000..70a43f2960ec0 --- /dev/null +++ b/coderd/notifications/testdata/rendered-templates/TemplateUserAccountSuspended-body.md.golden @@ -0,0 +1,2 @@ +Hi Bobby, +User account **bobby** has been suspended. \ No newline at end of file diff --git a/coderd/notifications/testdata/rendered-templates/TemplateUserAccountSuspended-title.md.golden b/coderd/notifications/testdata/rendered-templates/TemplateUserAccountSuspended-title.md.golden new file mode 100644 index 0000000000000..f2be8e201f0af --- /dev/null +++ b/coderd/notifications/testdata/rendered-templates/TemplateUserAccountSuspended-title.md.golden @@ -0,0 +1 @@ +User account "bobby" suspended \ No newline at end of file diff --git a/coderd/notifications/testdata/rendered-templates/TemplateWorkspaceAutoUpdated-body.md.golden b/coderd/notifications/testdata/rendered-templates/TemplateWorkspaceAutoUpdated-body.md.golden new file mode 100644 index 0000000000000..79248150987c2 --- /dev/null +++ b/coderd/notifications/testdata/rendered-templates/TemplateWorkspaceAutoUpdated-body.md.golden @@ -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** \ No newline at end of file diff --git a/coderd/notifications/testdata/rendered-templates/TemplateWorkspaceAutoUpdated-title.md.golden b/coderd/notifications/testdata/rendered-templates/TemplateWorkspaceAutoUpdated-title.md.golden new file mode 100644 index 0000000000000..fb62dcd0d3692 --- /dev/null +++ b/coderd/notifications/testdata/rendered-templates/TemplateWorkspaceAutoUpdated-title.md.golden @@ -0,0 +1 @@ +Workspace "bobby-workspace" updated automatically \ No newline at end of file diff --git a/coderd/notifications/testdata/rendered-templates/TemplateWorkspaceAutobuildFailed-body.md.golden b/coderd/notifications/testdata/rendered-templates/TemplateWorkspaceAutobuildFailed-body.md.golden new file mode 100644 index 0000000000000..731f71f22ae88 --- /dev/null +++ b/coderd/notifications/testdata/rendered-templates/TemplateWorkspaceAutobuildFailed-body.md.golden @@ -0,0 +1,3 @@ +Hi Bobby +Automatic build of your workspace **bobby-workspace** failed. +The specified reason was "**autostart**". \ No newline at end of file diff --git a/coderd/notifications/testdata/rendered-templates/TemplateWorkspaceAutobuildFailed-title.md.golden b/coderd/notifications/testdata/rendered-templates/TemplateWorkspaceAutobuildFailed-title.md.golden new file mode 100644 index 0000000000000..9cf98bc9e546a --- /dev/null +++ b/coderd/notifications/testdata/rendered-templates/TemplateWorkspaceAutobuildFailed-title.md.golden @@ -0,0 +1 @@ +Workspace "bobby-workspace" autobuild failed \ No newline at end of file diff --git a/coderd/notifications/testdata/rendered-templates/TemplateWorkspaceDeleted-body.md.golden b/coderd/notifications/testdata/rendered-templates/TemplateWorkspaceDeleted-body.md.golden new file mode 100644 index 0000000000000..06aec5692465f --- /dev/null +++ b/coderd/notifications/testdata/rendered-templates/TemplateWorkspaceDeleted-body.md.golden @@ -0,0 +1,4 @@ +Hi Bobby + +Your workspace **bobby-workspace** was deleted. +The specified reason was "**autodeleted due to dormancy (autobuild)**". \ No newline at end of file diff --git a/coderd/notifications/testdata/rendered-templates/TemplateWorkspaceDeleted-title.md.golden b/coderd/notifications/testdata/rendered-templates/TemplateWorkspaceDeleted-title.md.golden new file mode 100644 index 0000000000000..6806624053eb9 --- /dev/null +++ b/coderd/notifications/testdata/rendered-templates/TemplateWorkspaceDeleted-title.md.golden @@ -0,0 +1 @@ +Workspace "bobby-workspace" deleted \ No newline at end of file diff --git a/coderd/notifications/testdata/rendered-templates/TemplateWorkspaceDormant-body.md.golden b/coderd/notifications/testdata/rendered-templates/TemplateWorkspaceDormant-body.md.golden new file mode 100644 index 0000000000000..aa10f4864cf24 --- /dev/null +++ b/coderd/notifications/testdata/rendered-templates/TemplateWorkspaceDormant-body.md.golden @@ -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. \ No newline at end of file diff --git a/coderd/notifications/testdata/rendered-templates/TemplateWorkspaceDormant-title.md.golden b/coderd/notifications/testdata/rendered-templates/TemplateWorkspaceDormant-title.md.golden new file mode 100644 index 0000000000000..ce34a2a029ab4 --- /dev/null +++ b/coderd/notifications/testdata/rendered-templates/TemplateWorkspaceDormant-title.md.golden @@ -0,0 +1 @@ +Workspace "bobby-workspace" marked as dormant \ No newline at end of file diff --git a/coderd/notifications/testdata/rendered-templates/TemplateWorkspaceManualBuildFailed-body.md.golden b/coderd/notifications/testdata/rendered-templates/TemplateWorkspaceManualBuildFailed-body.md.golden new file mode 100644 index 0000000000000..45f8733dd2931 --- /dev/null +++ b/coderd/notifications/testdata/rendered-templates/TemplateWorkspaceManualBuildFailed-body.md.golden @@ -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**. \ No newline at end of file diff --git a/coderd/notifications/testdata/rendered-templates/TemplateWorkspaceManualBuildFailed-title.md.golden b/coderd/notifications/testdata/rendered-templates/TemplateWorkspaceManualBuildFailed-title.md.golden new file mode 100644 index 0000000000000..e786626b74672 --- /dev/null +++ b/coderd/notifications/testdata/rendered-templates/TemplateWorkspaceManualBuildFailed-title.md.golden @@ -0,0 +1 @@ +Workspace "bobby-workspace" manual build failed \ No newline at end of file diff --git a/coderd/notifications/testdata/rendered-templates/TemplateWorkspaceMarkedForDeletion-body.md.golden b/coderd/notifications/testdata/rendered-templates/TemplateWorkspaceMarkedForDeletion-body.md.golden new file mode 100644 index 0000000000000..3d9fe99accd94 --- /dev/null +++ b/coderd/notifications/testdata/rendered-templates/TemplateWorkspaceMarkedForDeletion-body.md.golden @@ -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. \ No newline at end of file diff --git a/coderd/notifications/testdata/rendered-templates/TemplateWorkspaceMarkedForDeletion-title.md.golden b/coderd/notifications/testdata/rendered-templates/TemplateWorkspaceMarkedForDeletion-title.md.golden new file mode 100644 index 0000000000000..1b561a73678de --- /dev/null +++ b/coderd/notifications/testdata/rendered-templates/TemplateWorkspaceMarkedForDeletion-title.md.golden @@ -0,0 +1 @@ +Workspace "bobby-workspace" marked for deletion \ No newline at end of file diff --git a/coderd/notifications/testdata/rendered-templates/TemplateYourAccountActivated-body.md.golden b/coderd/notifications/testdata/rendered-templates/TemplateYourAccountActivated-body.md.golden new file mode 100644 index 0000000000000..160fdc66e8990 --- /dev/null +++ b/coderd/notifications/testdata/rendered-templates/TemplateYourAccountActivated-body.md.golden @@ -0,0 +1,2 @@ +Hi Bobby, +Your account **bobby** has been activated. \ No newline at end of file diff --git a/coderd/notifications/testdata/rendered-templates/TemplateYourAccountActivated-title.md.golden b/coderd/notifications/testdata/rendered-templates/TemplateYourAccountActivated-title.md.golden new file mode 100644 index 0000000000000..90be1ef2dd63c --- /dev/null +++ b/coderd/notifications/testdata/rendered-templates/TemplateYourAccountActivated-title.md.golden @@ -0,0 +1 @@ +Your account "bobby" has been activated \ No newline at end of file diff --git a/coderd/notifications/testdata/rendered-templates/TemplateYourAccountSuspended-body.md.golden b/coderd/notifications/testdata/rendered-templates/TemplateYourAccountSuspended-body.md.golden new file mode 100644 index 0000000000000..ce30139213bb0 --- /dev/null +++ b/coderd/notifications/testdata/rendered-templates/TemplateYourAccountSuspended-body.md.golden @@ -0,0 +1,2 @@ +Hi Bobby, +Your account **bobby** has been suspended. \ No newline at end of file diff --git a/coderd/notifications/testdata/rendered-templates/TemplateYourAccountSuspended-title.md.golden b/coderd/notifications/testdata/rendered-templates/TemplateYourAccountSuspended-title.md.golden new file mode 100644 index 0000000000000..3a4cb57c8aac0 --- /dev/null +++ b/coderd/notifications/testdata/rendered-templates/TemplateYourAccountSuspended-title.md.golden @@ -0,0 +1 @@ +Your account "bobby" has been suspended \ No newline at end of file