Skip to content

Commit cbff3ca

Browse files
committed
test push notifications endpoint
1 parent fc59c70 commit cbff3ca

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

coderd/notifications_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,10 @@ func TestPushNotificationSubscription(t *testing.T) {
412412
require.NoError(t, err, "create notification push subscription")
413413
require.True(t, <-handlerCalled, "handler should have been called")
414414

415+
err = memberClient.TestPushNotification(ctx)
416+
require.NoError(t, err, "test push notification")
417+
require.True(t, <-handlerCalled, "handler should have been called again")
418+
415419
err = memberClient.DeleteNotificationPushSubscription(ctx, "me", codersdk.DeletePushNotificationSubscription{
416420
Endpoint: server.URL,
417421
})

codersdk/deployment.go

+22
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,12 @@ type NotificationsConfig struct {
700700
Webhook NotificationsWebhookConfig `json:"webhook" typescript:",notnull"`
701701
// Inbox settings.
702702
Inbox NotificationsInboxConfig `json:"inbox" typescript:",notnull"`
703+
// Push notification settings.
704+
Push NotificationsPushConfig `json:"push" typescript:",notnull"`
705+
}
706+
707+
type NotificationsPushConfig struct {
708+
Enabled serpent.Bool `json:"enabled" typescript:",notnull"`
703709
}
704710

705711
// Are either of the notification methods enabled?
@@ -1001,6 +1007,11 @@ func (c *DeploymentValues) Options() serpent.OptionSet {
10011007
Parent: &deploymentGroupNotifications,
10021008
YAML: "inbox",
10031009
}
1010+
deploymentGroupNotificationsPush = serpent.Group{
1011+
Name: "Push",
1012+
Parent: &deploymentGroupNotifications,
1013+
YAML: "push",
1014+
}
10041015
)
10051016

10061017
httpAddress := serpent.Option{
@@ -2968,6 +2979,17 @@ Write out the current server config as YAML to stdout.`,
29682979
Annotations: serpent.Annotations{}.Mark(annotationFormatDuration, "true"),
29692980
Hidden: true, // Hidden because most operators should not need to modify this.
29702981
},
2982+
// Push notifications.
2983+
{
2984+
Name: "Notifications: Push: Enabled",
2985+
Description: "Enable push notifications using VAPID.",
2986+
Flag: "notifications-push-enabled",
2987+
Env: "CODER_NOTIFICATIONS_PUSH_ENABLED",
2988+
Value: &c.Notifications.Push.Enabled,
2989+
Default: "false",
2990+
Group: &deploymentGroupNotificationsPush,
2991+
YAML: "enabled",
2992+
},
29712993
}
29722994

29732995
return opts

codersdk/notifications.go

+16
Original file line numberDiff line numberDiff line change
@@ -265,3 +265,19 @@ func (c *Client) DeleteNotificationPushSubscription(ctx context.Context, user st
265265
}
266266
return nil
267267
}
268+
269+
func (c *Client) TestPushNotification(ctx context.Context) error {
270+
res, err := c.Request(ctx, http.MethodPost, fmt.Sprintf("/api/v2/users/%s/notifications/push/test", Me), PushNotification{
271+
Title: "It's working!",
272+
Body: "You've subscribed to push notifications.",
273+
})
274+
if err != nil {
275+
return err
276+
}
277+
defer res.Body.Close()
278+
279+
if res.StatusCode != http.StatusNoContent {
280+
return ReadBodyAsError(res)
281+
}
282+
return nil
283+
}

0 commit comments

Comments
 (0)