MCPcopy Index your code
hub / github.com/coder/coder / TestSMTPDispatch

Function TestSMTPDispatch

coderd/notifications/notifications_test.go:128–191  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

126}
127
128func TestSMTPDispatch(t *testing.T) {
129 t.Parallel()
130
131 // SETUP
132
133 ctx := dbauthz.AsNotifier(testutil.Context(t, testutil.WaitSuperLong))
134 store, pubsub := dbtestutil.NewDB(t)
135 logger := testutil.Logger(t)
136
137 // start mock SMTP server
138 mockSMTPSrv := smtpmock.New(smtpmock.ConfigurationAttr{
139 LogToStdout: false,
140 LogServerActivity: true,
141 })
142 require.NoError(t, mockSMTPSrv.Start())
143 t.Cleanup(func() {
144 assert.NoError(t, mockSMTPSrv.Stop())
145 })
146
147 // GIVEN: an SMTP setup referencing a mock SMTP server
148 const from = "danny@coder.com"
149 method := database.NotificationMethodSmtp
150 cfg := defaultNotificationsConfig(method)
151 cfg.SMTP = codersdk.NotificationsEmailConfig{
152 From: from,
153 Smarthost: serpent.String(fmt.Sprintf("localhost:%d", mockSMTPSrv.PortNumber())),
154 Hello: "localhost",
155 }
156 handler := newDispatchInterceptor(dispatch.NewSMTPHandler(cfg.SMTP, logger.Named("smtp")))
157 mgr, err := notifications.NewManager(cfg, store, pubsub, defaultHelpers(), createMetrics(), logger.Named("manager"))
158 require.NoError(t, err)
159 mgr.WithHandlers(map[database.NotificationMethod]notifications.Handler{
160 method: handler,
161 database.NotificationMethodInbox: &fakeHandler{},
162 })
163 t.Cleanup(func() {
164 assert.NoError(t, mgr.Stop(ctx))
165 })
166 enq, err := notifications.NewStoreEnqueuer(cfg, store, defaultHelpers(), logger.Named("enqueuer"), quartz.NewReal())
167 require.NoError(t, err)
168
169 user := createSampleUser(t, store)
170
171 // WHEN: a message is enqueued
172 msgID, err := enq.Enqueue(ctx, user.ID, notifications.TemplateWorkspaceDeleted, map[string]string{}, "test")
173 require.NoError(t, err)
174 require.Len(t, msgID, 2)
175
176 mgr.Run(ctx)
177
178 // THEN: wait until the dispatch interceptor validates that the messages were dispatched
179 require.Eventually(t, func() bool {
180 assert.Nil(t, handler.lastErr.Load())
181 assert.True(t, handler.retryable.Load() == 0)
182 return handler.sent.Load() == 1
183 }, testutil.WaitLong, testutil.IntervalMedium)
184
185 // THEN: we verify that the expected message was received by the mock SMTP server

Callers

nothing calls this directly

Calls 15

WithHandlersMethod · 0.95
StopMethod · 0.95
EnqueueMethod · 0.95
RunMethod · 0.95
AsNotifierFunction · 0.92
ContextFunction · 0.92
NewDBFunction · 0.92
LoggerFunction · 0.92
NewSMTPHandlerFunction · 0.92
NewManagerFunction · 0.92
NewStoreEnqueuerFunction · 0.92

Tested by

no test coverage detected