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

Skip to content

Commit 7c06846

Browse files
committed
Fix notification settings test
Signed-off-by: Danny Kopping <[email protected]>
1 parent 3e27046 commit 7c06846

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

coderd/notifications.go

+13-7
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/coder/coder/v2/coderd/audit"
1111
"github.com/coder/coder/v2/coderd/database"
1212
"github.com/coder/coder/v2/coderd/httpapi"
13+
"github.com/coder/coder/v2/coderd/rbac"
1314
"github.com/coder/coder/v2/codersdk"
1415
)
1516

@@ -71,9 +72,9 @@ func (api *API) putNotificationsSettings(rw http.ResponseWriter, r *http.Request
7172
return
7273
}
7374

74-
currentSettingsJSON, err := api.Database.GetNotificationsSettings(r.Context())
75+
currentSettingsJSON, err := api.Database.GetNotificationsSettings(ctx)
7576
if err != nil {
76-
httpapi.Write(r.Context(), rw, http.StatusInternalServerError, codersdk.Response{
77+
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
7778
Message: "Failed to fetch current notifications settings.",
7879
Detail: err.Error(),
7980
})
@@ -82,7 +83,7 @@ func (api *API) putNotificationsSettings(rw http.ResponseWriter, r *http.Request
8283

8384
if bytes.Equal(settingsJSON, []byte(currentSettingsJSON)) {
8485
// See: https://www.rfc-editor.org/rfc/rfc7232#section-4.1
85-
httpapi.Write(r.Context(), rw, http.StatusNotModified, nil)
86+
httpapi.Write(ctx, rw, http.StatusNotModified, nil)
8687
return
8788
}
8889

@@ -102,10 +103,15 @@ func (api *API) putNotificationsSettings(rw http.ResponseWriter, r *http.Request
102103

103104
err = api.Database.UpsertNotificationsSettings(ctx, string(settingsJSON))
104105
if err != nil {
105-
httpapi.Write(r.Context(), rw, http.StatusInternalServerError, codersdk.Response{
106-
Message: "Failed to update notifications settings.",
107-
Detail: err.Error(),
108-
})
106+
if rbac.IsUnauthorizedError(err) {
107+
httpapi.Forbidden(rw)
108+
} else {
109+
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
110+
Message: "Failed to update notifications settings.",
111+
Detail: err.Error(),
112+
})
113+
}
114+
109115
return
110116
}
111117

coderd/notifications_test.go

+13-3
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,23 @@ import (
1111
"github.com/coder/coder/v2/testutil"
1212
)
1313

14+
func createOpts(t *testing.T) *coderdtest.Options {
15+
t.Helper()
16+
17+
dt := coderdtest.DeploymentValues(t)
18+
dt.Experiments = []string{string(codersdk.ExperimentNotifications)}
19+
return &coderdtest.Options{
20+
DeploymentValues: dt,
21+
}
22+
}
23+
1424
func TestUpdateNotificationsSettings(t *testing.T) {
1525
t.Parallel()
1626

1727
t.Run("Permissions denied", func(t *testing.T) {
1828
t.Parallel()
1929

20-
api := coderdtest.New(t, nil)
30+
api := coderdtest.New(t, createOpts(t))
2131
firstUser := coderdtest.CreateFirstUser(t, api)
2232
anotherClient, _ := coderdtest.CreateAnotherUser(t, api, firstUser.OrganizationID)
2333

@@ -41,7 +51,7 @@ func TestUpdateNotificationsSettings(t *testing.T) {
4151
t.Run("Settings modified", func(t *testing.T) {
4252
t.Parallel()
4353

44-
client := coderdtest.New(t, nil)
54+
client := coderdtest.New(t, createOpts(t))
4555
_ = coderdtest.CreateFirstUser(t, client)
4656

4757
// given
@@ -65,7 +75,7 @@ func TestUpdateNotificationsSettings(t *testing.T) {
6575
t.Parallel()
6676

6777
// Empty state: notifications Settings are undefined now (default).
68-
client := coderdtest.New(t, nil)
78+
client := coderdtest.New(t, createOpts(t))
6979
_ = coderdtest.CreateFirstUser(t, client)
7080
ctx := testutil.Context(t, testutil.WaitShort)
7181

0 commit comments

Comments
 (0)