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

Commit9f66e1a

Browse files
committed
first tests
1 parent7687c06 commit9f66e1a

File tree

4 files changed

+111
-2
lines changed

4 files changed

+111
-2
lines changed

‎coderd/coderd.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,6 +1243,7 @@ func New(options *Options) *API {
12431243
})
12441244
})
12451245
r.Route("/notifications",func(r chi.Router) {
1246+
r.Use(apiKeyMiddleware)
12461247
r.Get("/settings",api.notificationsSettings)
12471248
r.Put("/settings",api.putNotificationsSettings)
12481249
})

‎coderd/notifications.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package coderd
22

33
import (
4+
"bytes"
45
"encoding/json"
56
"net/http"
67

@@ -78,6 +79,21 @@ func (api *API) putNotificationsSettings(rw http.ResponseWriter, r *http.Request
7879
return
7980
}
8081

82+
currentSettingsJSON,err:=api.Database.GetNotificationsSettings(r.Context())
83+
iferr!=nil {
84+
httpapi.Write(r.Context(),rw,http.StatusInternalServerError, codersdk.Response{
85+
Message:"Failed to fetch current notification settings.",
86+
Detail:err.Error(),
87+
})
88+
return
89+
}
90+
91+
ifbytes.Equal(settingsJSON, []byte(currentSettingsJSON)) {
92+
// See: https://www.rfc-editor.org/rfc/rfc7231#section-6.3.5
93+
httpapi.Write(r.Context(),rw,http.StatusNoContent,nil)
94+
return
95+
}
96+
8197
auditor:=api.Auditor.Load()
8298
aReq,commitAudit:=audit.InitRequest[database.NotificationsSettings](rw,&audit.RequestParams{
8399
Audit:*auditor,

‎coderd/notifications_test.go

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,58 @@
11
package coderd_test
22

3-
// TODO
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
8+
"github.com/coder/coder/v2/coderd/coderdtest"
9+
"github.com/coder/coder/v2/codersdk"
10+
"github.com/coder/coder/v2/testutil"
11+
)
12+
13+
funcTestUpdateNotificationsSettings(t*testing.T) {
14+
t.Parallel()
15+
16+
t.Run("Settings modified",func(t*testing.T) {
17+
t.Parallel()
18+
19+
client:=coderdtest.New(t,nil)
20+
_=coderdtest.CreateFirstUser(t,client)
21+
22+
// given
23+
expected:= codersdk.NotificationsSettings{
24+
NotifierPaused:true,
25+
}
26+
27+
ctx:=testutil.Context(t,testutil.WaitShort)
28+
29+
// when
30+
err:=client.PutNotificationsSettings(ctx,expected)
31+
require.NoError(t,err)
32+
33+
// then
34+
actual,err:=client.GetNotificationsSettings(ctx)
35+
require.NoError(t,err)
36+
require.Equal(t,expected,actual)
37+
})
38+
39+
t.Run("Settings not modified",func(t*testing.T) {
40+
t.Parallel()
41+
42+
client:=coderdtest.New(t,nil)
43+
_=coderdtest.CreateFirstUser(t,client)
44+
45+
ctx:=testutil.Context(t,testutil.WaitShort)
46+
47+
// given
48+
expected:= codersdk.NotificationsSettings{
49+
NotifierPaused:false,
50+
}
51+
err:=client.PutNotificationsSettings(ctx,expected)
52+
require.NoError(t,err)
53+
54+
// then
55+
err=client.PutNotificationsSettings(ctx,expected)
56+
require.Error(t,err)// Error: notifications settings not modified
57+
})
58+
}

‎codersdk/notifications.go

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,42 @@
11
package codersdk
22

3+
import (
4+
"context"
5+
"encoding/json"
6+
"net/http"
7+
8+
"golang.org/x/xerrors"
9+
)
10+
311
typeNotificationsSettingsstruct {
4-
NotifierPausedbool`json:"notifier_paused" validate:"required"`
12+
NotifierPausedbool`json:"notifier_paused"`
13+
}
14+
15+
func (c*Client)GetNotificationsSettings(ctx context.Context) (NotificationsSettings,error) {
16+
res,err:=c.Request(ctx,http.MethodGet,"/api/v2/notifications/settings",nil)
17+
iferr!=nil {
18+
returnNotificationsSettings{},err
19+
}
20+
deferres.Body.Close()
21+
ifres.StatusCode!=http.StatusOK {
22+
returnNotificationsSettings{},ReadBodyAsError(res)
23+
}
24+
varsettingsNotificationsSettings
25+
returnsettings,json.NewDecoder(res.Body).Decode(&settings)
26+
}
27+
28+
func (c*Client)PutNotificationsSettings(ctx context.Context,settingsNotificationsSettings)error {
29+
res,err:=c.Request(ctx,http.MethodPut,"/api/v2/notifications/settings",settings)
30+
iferr!=nil {
31+
returnerr
32+
}
33+
deferres.Body.Close()
34+
35+
ifres.StatusCode==http.StatusNoContent {
36+
returnxerrors.New("notifications settings not modified")
37+
}
38+
ifres.StatusCode!=http.StatusOK {
39+
returnReadBodyAsError(res)
40+
}
41+
returnnil
542
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp