Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9f5332f

Browse files
committed
Added a test
Signed-off-by: Danny Kopping <[email protected]>
1 parent 3b9098a commit 9f5332f

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

coderd/notifications.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ func (api *API) systemNotificationTemplates(rw http.ResponseWriter, r *http.Requ
146146

147147
// @Summary Get notification dispatch methods
148148
// @ID get-notification-dispatch-methods
149+
// @Security CoderSessionToken
149150
// @Produce json
150151
// @Tags Notifications
151152
// @Success 200 {array} codersdk.NotificationMethodsResponse

coderd/notifications_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@ import (
55
"testing"
66

77
"github.com/stretchr/testify/require"
8+
"golang.org/x/exp/slices"
9+
10+
"github.com/coder/serpent"
811

912
"github.com/coder/coder/v2/coderd/coderdtest"
13+
"github.com/coder/coder/v2/coderd/database"
1014
"github.com/coder/coder/v2/coderd/notifications"
1115
"github.com/coder/coder/v2/codersdk"
1216
"github.com/coder/coder/v2/testutil"
@@ -265,3 +269,52 @@ func TestNotificationPreferences(t *testing.T) {
265269
require.True(t, found, "dormant notification preference was not found")
266270
})
267271
}
272+
273+
func TestNotificationDispatchMethods(t *testing.T) {
274+
t.Parallel()
275+
276+
defaultOpts := createOpts(t)
277+
webhookOpts := createOpts(t)
278+
webhookOpts.DeploymentValues.Notifications.Method = serpent.String(database.NotificationMethodWebhook)
279+
280+
tests := []struct {
281+
name string
282+
opts *coderdtest.Options
283+
expectedDefault string
284+
}{
285+
{
286+
name: "default",
287+
opts: defaultOpts,
288+
expectedDefault: string(database.NotificationMethodSmtp),
289+
},
290+
{
291+
name: "non-default",
292+
opts: webhookOpts,
293+
expectedDefault: string(database.NotificationMethodWebhook),
294+
},
295+
}
296+
297+
var allMethods []string
298+
for _, nm := range database.AllNotificationMethodValues() {
299+
allMethods = append(allMethods, string(nm))
300+
}
301+
slices.Sort(allMethods)
302+
303+
// nolint:paralleltest // Not since Go v1.22.
304+
for _, tc := range tests {
305+
t.Run(tc.name, func(t *testing.T) {
306+
t.Parallel()
307+
308+
ctx := testutil.Context(t, testutil.WaitShort)
309+
api := coderdtest.New(t, tc.opts)
310+
_ = coderdtest.CreateFirstUser(t, api)
311+
312+
resp, err := api.GetNotificationDispatchMethods(ctx)
313+
require.NoError(t, err)
314+
315+
slices.Sort(resp.AvailableNotificationMethods)
316+
require.EqualValues(t, resp.AvailableNotificationMethods, allMethods)
317+
require.Equal(t, tc.expectedDefault, resp.DefaultNotificationMethod)
318+
})
319+
}
320+
}

codersdk/notifications.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,31 @@ func (c *Client) UpdateUserNotificationPreferences(ctx context.Context, userID u
167167
return prefs, nil
168168
}
169169

170+
// GetNotificationDispatchMethods the available and default notification dispatch methods.
171+
func (c *Client) GetNotificationDispatchMethods(ctx context.Context) (NotificationMethodsResponse, error) {
172+
res, err := c.Request(ctx, http.MethodGet, "/api/v2/notifications/dispatch-methods", nil)
173+
if err != nil {
174+
return NotificationMethodsResponse{}, err
175+
}
176+
defer res.Body.Close()
177+
178+
if res.StatusCode != http.StatusOK {
179+
return NotificationMethodsResponse{}, ReadBodyAsError(res)
180+
}
181+
182+
var resp NotificationMethodsResponse
183+
body, err := io.ReadAll(res.Body)
184+
if err != nil {
185+
return NotificationMethodsResponse{}, xerrors.Errorf("read response body: %w", err)
186+
}
187+
188+
if err := json.Unmarshal(body, &resp); err != nil {
189+
return NotificationMethodsResponse{}, xerrors.Errorf("unmarshal response body: %w", err)
190+
}
191+
192+
return resp, nil
193+
}
194+
170195
type UpdateNotificationTemplateMethod struct {
171196
Method string `json:"method,omitempty" example:"webhook"`
172197
}

0 commit comments

Comments
 (0)