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

Function TestCustomNotificationMethod

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

Source from the content-addressed store, hash-verified

1897}
1898
1899func TestCustomNotificationMethod(t *testing.T) {
1900 t.Parallel()
1901
1902 ctx := dbauthz.AsNotifier(testutil.Context(t, testutil.WaitSuperLong))
1903 store, pubsub := dbtestutil.NewDB(t)
1904 logger := testutil.Logger(t)
1905
1906 received := make(chan uuid.UUID, 1)
1907
1908 // SETUP:
1909 // Start mock server to simulate webhook endpoint.
1910 mockWebhookSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
1911 var payload dispatch.WebhookPayload
1912 err := json.NewDecoder(r.Body).Decode(&payload)
1913 assert.NoError(t, err)
1914
1915 received <- payload.MsgID
1916 close(received)
1917
1918 w.WriteHeader(http.StatusOK)
1919 _, err = w.Write([]byte("noted."))
1920 require.NoError(t, err)
1921 }))
1922 defer mockWebhookSrv.Close()
1923
1924 // Start mock SMTP server.
1925 mockSMTPSrv := smtpmock.New(smtpmock.ConfigurationAttr{
1926 LogToStdout: false,
1927 LogServerActivity: true,
1928 })
1929 require.NoError(t, mockSMTPSrv.Start())
1930 t.Cleanup(func() {
1931 assert.NoError(t, mockSMTPSrv.Stop())
1932 })
1933
1934 endpoint, err := url.Parse(mockWebhookSrv.URL)
1935 require.NoError(t, err)
1936
1937 // GIVEN: a notification template which has a method explicitly set
1938 var (
1939 tmpl = notifications.TemplateWorkspaceDormant
1940 defaultMethod = database.NotificationMethodSmtp
1941 customMethod = database.NotificationMethodWebhook
1942 )
1943 out, err := store.UpdateNotificationTemplateMethodByID(ctx, database.UpdateNotificationTemplateMethodByIDParams{
1944 ID: tmpl,
1945 Method: database.NullNotificationMethod{NotificationMethod: customMethod, Valid: true},
1946 })
1947 require.NoError(t, err)
1948 require.Equal(t, customMethod, out.Method.NotificationMethod)
1949
1950 // GIVEN: a manager configured with multiple dispatch methods
1951 cfg := defaultNotificationsConfig(defaultMethod)
1952 cfg.SMTP = codersdk.NotificationsEmailConfig{
1953 From: "danny@coder.com",
1954 Hello: "localhost",
1955 Smarthost: serpent.String(fmt.Sprintf("localhost:%d", mockSMTPSrv.PortNumber())),
1956 }

Callers

nothing calls this directly

Calls 15

CloseMethod · 0.95
StopMethod · 0.95
EnqueueMethod · 0.95
RunMethod · 0.95
AsNotifierFunction · 0.92
ContextFunction · 0.92
NewDBFunction · 0.92
LoggerFunction · 0.92
NewManagerFunction · 0.92
NewStoreEnqueuerFunction · 0.92
TryReceiveFunction · 0.92

Tested by

no test coverage detected