Skip to content

feat: allow admins to modify notification template delivery method #14116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix notification settings test
Signed-off-by: Danny Kopping <danny@coder.com>
  • Loading branch information
dannykopping committed Aug 5, 2024
commit 0546f24bec2a818f5ac53d11283b5c9723418c0e
20 changes: 13 additions & 7 deletions coderd/notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/coder/coder/v2/coderd/audit"
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/httpapi"
"github.com/coder/coder/v2/coderd/rbac"
"github.com/coder/coder/v2/codersdk"
)

Expand Down Expand Up @@ -71,9 +72,9 @@ func (api *API) putNotificationsSettings(rw http.ResponseWriter, r *http.Request
return
}

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

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

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

err = api.Database.UpsertNotificationsSettings(ctx, string(settingsJSON))
if err != nil {
httpapi.Write(r.Context(), rw, http.StatusInternalServerError, codersdk.Response{
Message: "Failed to update notifications settings.",
Detail: err.Error(),
})
if rbac.IsUnauthorizedError(err) {
httpapi.Forbidden(rw)
} else {
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
Message: "Failed to update notifications settings.",
Detail: err.Error(),
})
}

return
}

Expand Down
16 changes: 13 additions & 3 deletions coderd/notifications_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,23 @@ import (
"github.com/coder/coder/v2/testutil"
)

func createOpts(t *testing.T) *coderdtest.Options {
t.Helper()

dt := coderdtest.DeploymentValues(t)
dt.Experiments = []string{string(codersdk.ExperimentNotifications)}
return &coderdtest.Options{
DeploymentValues: dt,
}
}

func TestUpdateNotificationsSettings(t *testing.T) {
t.Parallel()

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

api := coderdtest.New(t, nil)
api := coderdtest.New(t, createOpts(t))
firstUser := coderdtest.CreateFirstUser(t, api)
anotherClient, _ := coderdtest.CreateAnotherUser(t, api, firstUser.OrganizationID)

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

client := coderdtest.New(t, nil)
client := coderdtest.New(t, createOpts(t))
_ = coderdtest.CreateFirstUser(t, client)

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

// Empty state: notifications Settings are undefined now (default).
client := coderdtest.New(t, nil)
client := coderdtest.New(t, createOpts(t))
_ = coderdtest.CreateFirstUser(t, client)
ctx := testutil.Context(t, testutil.WaitShort)

Expand Down