Skip to content

refactor: refactor notification email templates #14208

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 8 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Pass extra data to template
  • Loading branch information
BrunoQuaresma committed Aug 7, 2024
commit a17381f76c4ab95574498d9c659cafacfa7601c1
13 changes: 11 additions & 2 deletions coderd/notifications/dispatch/smtp/html.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

.logo {
width: 60px;
display: block;
margin: auto;
}

.title {
Expand Down Expand Up @@ -82,9 +84,16 @@

<div class="footer">
<!-- TODO: dynamic copyright -->
<p>&copy; 2024 Coder. All rights reserved.</p>
<p>
<a href="" class="link"
&copy; {{ .Labels._year }} Coder. All rights reserved -
<a
href="{{ .Labels._accessURL }}"
class="link"
>{{ .Labels._accessURL }}</a
>
</p>
<p>
<a href="{{ .Labels._accessURL }}/settings/notifications" class="link"
>Click here to manage your notification settings</a
>
</p>
Expand Down
3 changes: 2 additions & 1 deletion coderd/notifications/dispatch/smtp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,8 +416,9 @@ func TestSMTP(t *testing.T) {
var hp serpent.HostPort
require.NoError(t, hp.Set(listen.Addr().String()))
tc.cfg.Smarthost = hp
accessURL := "http://localhost:8080"

handler := dispatch.NewSMTPHandler(tc.cfg, logger.Named("smtp"))
handler := dispatch.NewSMTPHandler(tc.cfg, accessURL, logger.Named("smtp"))

// Start mock SMTP server in the background.
var wg sync.WaitGroup
Expand Down
12 changes: 10 additions & 2 deletions coderd/notifications/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ func TestBufferedUpdates(t *testing.T) {
cfg.StoreSyncInterval = serpent.Duration(time.Hour) // Ensure we don't sync the store automatically.

// GIVEN: a manager which will pass or fail notifications based on their "nice" labels
mgr, err := notifications.NewManager(cfg, interceptor, createMetrics(), logger.Named("notifications-manager"))
accessURL := "http://localhost:8080"
mgr, err := notifications.NewManager(cfg, interceptor, accessURL, createMetrics(), logger.Named("notifications-manager"))
require.NoError(t, err)
mgr.WithHandlers(map[database.NotificationMethod]notifications.Handler{
database.NotificationMethodSmtp: santa,
Expand Down Expand Up @@ -150,7 +151,14 @@ func TestStopBeforeRun(t *testing.T) {
ctx, logger, db := setupInMemory(t)

// GIVEN: a standard manager
mgr, err := notifications.NewManager(defaultNotificationsConfig(database.NotificationMethodSmtp), db, createMetrics(), logger.Named("notifications-manager"))
accessURL := "http://localhost:8080"
mgr, err := notifications.NewManager(
defaultNotificationsConfig(database.NotificationMethodSmtp),
db,
accessURL,
createMetrics(),
logger.Named("notifications-manager"),
)
require.NoError(t, err)

// THEN: validate that the manager can be stopped safely without Run() having been called yet
Expand Down
12 changes: 8 additions & 4 deletions coderd/notifications/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ func TestMetrics(t *testing.T) {
cfg.RetryInterval = serpent.Duration(time.Millisecond * 50)
cfg.StoreSyncInterval = serpent.Duration(time.Millisecond * 100) // Twice as long as fetch interval to ensure we catch pending updates.

mgr, err := notifications.NewManager(cfg, store, metrics, logger.Named("manager"))
accessURL := "http://localhost:8080"
mgr, err := notifications.NewManager(cfg, store, accessURL, metrics, logger.Named("manager"))
require.NoError(t, err)
t.Cleanup(func() {
assert.NoError(t, mgr.Stop(ctx))
Expand Down Expand Up @@ -218,7 +219,8 @@ func TestPendingUpdatesMetric(t *testing.T) {

syncer := &syncInterceptor{Store: store}
interceptor := newUpdateSignallingInterceptor(syncer)
mgr, err := notifications.NewManager(cfg, interceptor, metrics, logger.Named("manager"))
accessURL := "http://localhost:8080"
mgr, err := notifications.NewManager(cfg, interceptor, accessURL, metrics, logger.Named("manager"))
require.NoError(t, err)
t.Cleanup(func() {
assert.NoError(t, mgr.Stop(ctx))
Expand Down Expand Up @@ -292,7 +294,8 @@ func TestInflightDispatchesMetric(t *testing.T) {
cfg.RetryInterval = serpent.Duration(time.Hour) // Delay retries so they don't interfere.
cfg.StoreSyncInterval = serpent.Duration(time.Millisecond * 100)

mgr, err := notifications.NewManager(cfg, store, metrics, logger.Named("manager"))
accessURL := "http://localhost:8080"
mgr, err := notifications.NewManager(cfg, store, accessURL, metrics, logger.Named("manager"))
require.NoError(t, err)
t.Cleanup(func() {
assert.NoError(t, mgr.Stop(ctx))
Expand Down Expand Up @@ -371,7 +374,8 @@ func TestCustomMethodMetricCollection(t *testing.T) {

// WHEN: two notifications (each with different templates) are enqueued.
cfg := defaultNotificationsConfig(defaultMethod)
mgr, err := notifications.NewManager(cfg, store, metrics, logger.Named("manager"))
accessURL := "http://localhost:8080"
mgr, err := notifications.NewManager(cfg, store, accessURL, metrics, logger.Named("manager"))
require.NoError(t, err)
t.Cleanup(func() {
assert.NoError(t, mgr.Stop(ctx))
Expand Down
34 changes: 22 additions & 12 deletions coderd/notifications/notifications_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ func TestBasicNotificationRoundtrip(t *testing.T) {
interceptor := &syncInterceptor{Store: db}
cfg := defaultNotificationsConfig(method)
cfg.RetryInterval = serpent.Duration(time.Hour) // Ensure retries don't interfere with the test
mgr, err := notifications.NewManager(cfg, interceptor, createMetrics(), logger.Named("manager"))
accessURL := "http://localhost:8080"
mgr, err := notifications.NewManager(cfg, interceptor, accessURL, createMetrics(), logger.Named("manager"))
require.NoError(t, err)
mgr.WithHandlers(map[database.NotificationMethod]notifications.Handler{method: handler})
t.Cleanup(func() {
Expand Down Expand Up @@ -138,8 +139,9 @@ func TestSMTPDispatch(t *testing.T) {
Smarthost: serpent.HostPort{Host: "localhost", Port: fmt.Sprintf("%d", mockSMTPSrv.PortNumber())},
Hello: "localhost",
}
handler := newDispatchInterceptor(dispatch.NewSMTPHandler(cfg.SMTP, logger.Named("smtp")))
mgr, err := notifications.NewManager(cfg, db, createMetrics(), logger.Named("manager"))
accessURL := "http://localhost:8080"
handler := newDispatchInterceptor(dispatch.NewSMTPHandler(cfg.SMTP, accessURL, logger.Named("smtp")))
mgr, err := notifications.NewManager(cfg, db, accessURL, createMetrics(), logger.Named("manager"))
require.NoError(t, err)
mgr.WithHandlers(map[database.NotificationMethod]notifications.Handler{method: handler})
t.Cleanup(func() {
Expand Down Expand Up @@ -200,7 +202,8 @@ func TestWebhookDispatch(t *testing.T) {
cfg.Webhook = codersdk.NotificationsWebhookConfig{
Endpoint: *serpent.URLOf(endpoint),
}
mgr, err := notifications.NewManager(cfg, db, createMetrics(), logger.Named("manager"))
accessURL := "http://localhost:8080"
mgr, err := notifications.NewManager(cfg, db, accessURL, createMetrics(), logger.Named("manager"))
require.NoError(t, err)
t.Cleanup(func() {
assert.NoError(t, mgr.Stop(ctx))
Expand Down Expand Up @@ -298,7 +301,8 @@ func TestBackpressure(t *testing.T) {
storeInterceptor := &syncInterceptor{Store: db}

// GIVEN: a notification manager whose updates will be intercepted
mgr, err := notifications.NewManager(cfg, storeInterceptor, createMetrics(), logger.Named("manager"))
accessURL := "http://localhost:8080"
mgr, err := notifications.NewManager(cfg, storeInterceptor, accessURL, createMetrics(), logger.Named("manager"))
require.NoError(t, err)
mgr.WithHandlers(map[database.NotificationMethod]notifications.Handler{method: handler})
enq, err := notifications.NewStoreEnqueuer(cfg, db, defaultHelpers(), logger.Named("enqueuer"))
Expand Down Expand Up @@ -393,7 +397,8 @@ func TestRetries(t *testing.T) {
// Intercept calls to submit the buffered updates to the store.
storeInterceptor := &syncInterceptor{Store: db}

mgr, err := notifications.NewManager(cfg, storeInterceptor, createMetrics(), logger.Named("manager"))
accessURL := "http://localhost:8080"
mgr, err := notifications.NewManager(cfg, storeInterceptor, accessURL, createMetrics(), logger.Named("manager"))
require.NoError(t, err)
t.Cleanup(func() {
assert.NoError(t, mgr.Stop(ctx))
Expand Down Expand Up @@ -454,7 +459,8 @@ func TestExpiredLeaseIsRequeued(t *testing.T) {
mgrCtx, cancelManagerCtx := context.WithCancel(context.Background())
t.Cleanup(cancelManagerCtx)

mgr, err := notifications.NewManager(cfg, noopInterceptor, createMetrics(), logger.Named("manager"))
accessURL := "http://localhost:8080"
mgr, err := notifications.NewManager(cfg, noopInterceptor, accessURL, createMetrics(), logger.Named("manager"))
require.NoError(t, err)
enq, err := notifications.NewStoreEnqueuer(cfg, db, defaultHelpers(), logger.Named("enqueuer"))
require.NoError(t, err)
Expand Down Expand Up @@ -501,7 +507,7 @@ func TestExpiredLeaseIsRequeued(t *testing.T) {
// Intercept calls to submit the buffered updates to the store.
storeInterceptor := &syncInterceptor{Store: db}
handler := newDispatchInterceptor(&fakeHandler{})
mgr, err = notifications.NewManager(cfg, storeInterceptor, createMetrics(), logger.Named("manager"))
mgr, err = notifications.NewManager(cfg, storeInterceptor, accessURL, createMetrics(), logger.Named("manager"))
require.NoError(t, err)
mgr.WithHandlers(map[database.NotificationMethod]notifications.Handler{method: handler})

Expand Down Expand Up @@ -542,7 +548,8 @@ func TestInvalidConfig(t *testing.T) {
cfg.DispatchTimeout = serpent.Duration(leasePeriod)

// WHEN: the manager is created with invalid config
_, err := notifications.NewManager(cfg, db, createMetrics(), logger.Named("manager"))
accessURL := "http://localhost:8080"
_, err := notifications.NewManager(cfg, db, accessURL, createMetrics(), logger.Named("manager"))

// THEN: the manager will fail to be created, citing invalid config as error
require.ErrorIs(t, err, notifications.ErrInvalidDispatchTimeout)
Expand All @@ -560,7 +567,8 @@ func TestNotifierPaused(t *testing.T) {
user := createSampleUser(t, db)

cfg := defaultNotificationsConfig(method)
mgr, err := notifications.NewManager(cfg, db, createMetrics(), logger.Named("manager"))
accessURL := "http://localhost:8080"
mgr, err := notifications.NewManager(cfg, db, accessURL, createMetrics(), logger.Named("manager"))
require.NoError(t, err)
mgr.WithHandlers(map[database.NotificationMethod]notifications.Handler{method: handler})
t.Cleanup(func() {
Expand Down Expand Up @@ -830,8 +838,9 @@ func TestDisabledAfterEnqueue(t *testing.T) {

method := database.NotificationMethodSmtp
cfg := defaultNotificationsConfig(method)
accessURL := "http://localhost:8080"

mgr, err := notifications.NewManager(cfg, db, createMetrics(), logger.Named("manager"))
mgr, err := notifications.NewManager(cfg, db, accessURL, createMetrics(), logger.Named("manager"))
require.NoError(t, err)
t.Cleanup(func() {
assert.NoError(t, mgr.Stop(ctx))
Expand Down Expand Up @@ -937,7 +946,8 @@ func TestCustomNotificationMethod(t *testing.T) {
Endpoint: *serpent.URLOf(endpoint),
}

mgr, err := notifications.NewManager(cfg, db, createMetrics(), logger.Named("manager"))
accessURL := "http://localhost:8080"
mgr, err := notifications.NewManager(cfg, db, accessURL, createMetrics(), logger.Named("manager"))
require.NoError(t, err)
t.Cleanup(func() {
_ = mgr.Stop(ctx)
Expand Down
Loading