(t *testing.T)
| 126 | } |
| 127 | |
| 128 | func 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 |
nothing calls this directly
no test coverage detected