Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9f5332f

Browse files
committed
Added a test
Signed-off-by: Danny Kopping <danny@coder.com>
1 parent3b9098a commit9f5332f

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+
funcTestNotificationDispatchMethods(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+
namestring
282+
opts*coderdtest.Options
283+
expectedDefaultstring
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+
varallMethods []string
298+
for_,nm:=rangedatabase.AllNotificationMethodValues() {
299+
allMethods=append(allMethods,string(nm))
300+
}
301+
slices.Sort(allMethods)
302+
303+
// nolint:paralleltest // Not since Go v1.22.
304+
for_,tc:=rangetests {
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
returnprefs,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+
iferr!=nil {
174+
returnNotificationMethodsResponse{},err
175+
}
176+
deferres.Body.Close()
177+
178+
ifres.StatusCode!=http.StatusOK {
179+
returnNotificationMethodsResponse{},ReadBodyAsError(res)
180+
}
181+
182+
varrespNotificationMethodsResponse
183+
body,err:=io.ReadAll(res.Body)
184+
iferr!=nil {
185+
returnNotificationMethodsResponse{},xerrors.Errorf("read response body: %w",err)
186+
}
187+
188+
iferr:=json.Unmarshal(body,&resp);err!=nil {
189+
returnNotificationMethodsResponse{},xerrors.Errorf("unmarshal response body: %w",err)
190+
}
191+
192+
returnresp,nil
193+
}
194+
170195
typeUpdateNotificationTemplateMethodstruct {
171196
Methodstring`json:"method,omitempty" example:"webhook"`
172197
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp