Skip to content

Commit b5dd367

Browse files
committed
fix tests
1 parent 587efaf commit b5dd367

File tree

3 files changed

+24
-32
lines changed

3 files changed

+24
-32
lines changed

coderd/notifications/push/push.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ import (
2727
// and push notifications at time of implementation are being used
2828
// for updates inside of a workspace, which we want to be immediate.
2929
//
30-
// There should be refactor of the core abstraction to merge dispatch
31-
// and queue, and then we can integrate this.
30+
// See: https://github.com/coder/internal/issues/528
3231
func New(ctx context.Context, log *slog.Logger, db database.Store) (*Notifier, error) {
3332
keys, err := db.GetNotificationVAPIDKeys(ctx)
3433
if err != nil {

coderd/notifications_test.go

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -379,59 +379,42 @@ func TestNotificationTest(t *testing.T) {
379379
}
380380

381381
const (
382+
// These are valid keys for a push notification subscription.
383+
// DO NOT REUSE THESE IN ANY REAL CODE.
382384
validEndpointAuthKey = "zqbxT6JKstKSY9JKibZLSQ=="
383385
validEndpointP256dhKey = "BNNL5ZaTfK81qhXOx23+wewhigUeFb632jN6LvRWCFH1ubQr77FE/9qV1FuojuRmHP42zmf34rXgW80OvUVDgTk="
384386
)
385387

386388
func TestPushNotificationSubscription(t *testing.T) {
387389
t.Parallel()
388390

389-
t.Run("Create", func(t *testing.T) {
390-
t.Parallel()
391-
392-
ctx := testutil.Context(t, testutil.WaitShort)
393-
394-
notificationSent := false
395-
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
396-
notificationSent = true
397-
w.WriteHeader(http.StatusOK)
398-
}))
399-
defer server.Close()
400-
401-
client := coderdtest.New(t, nil)
402-
coderdtest.CreateFirstUser(t, client)
403-
404-
err := client.CreateNotificationPushSubscription(ctx, "me", codersdk.PushNotificationSubscription{
405-
Endpoint: server.URL,
406-
AuthKey: validEndpointAuthKey,
407-
P256DHKey: validEndpointP256dhKey,
408-
})
409-
require.NoError(t, err)
410-
require.True(t, notificationSent)
411-
})
412-
t.Run("Delete", func(t *testing.T) {
391+
t.Run("CRUD", func(t *testing.T) {
413392
t.Parallel()
414393

415394
ctx := testutil.Context(t, testutil.WaitShort)
416395

417396
client := coderdtest.New(t, nil)
418-
coderdtest.CreateFirstUser(t, client)
397+
owner := coderdtest.CreateFirstUser(t, client)
398+
memberClient, _ := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID)
419399

400+
handlerCalled := make(chan bool, 1)
420401
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
421-
w.WriteHeader(http.StatusOK)
402+
w.WriteHeader(http.StatusCreated)
403+
handlerCalled <- true
422404
}))
423405
defer server.Close()
424406

425-
err := client.CreateNotificationPushSubscription(ctx, "me", codersdk.PushNotificationSubscription{
407+
err := memberClient.CreateNotificationPushSubscription(ctx, "me", codersdk.PushNotificationSubscription{
426408
Endpoint: server.URL,
427409
AuthKey: validEndpointAuthKey,
428410
P256DHKey: validEndpointP256dhKey,
429411
})
430-
require.NoError(t, err)
412+
require.NoError(t, err, "create notification push subscription")
413+
require.True(t, <-handlerCalled, "handler should have been called")
431414

432-
err = client.DeleteNotificationPushSubscription(ctx, "me", codersdk.DeletePushNotificationSubscription{
415+
err = memberClient.DeleteNotificationPushSubscription(ctx, "me", codersdk.DeletePushNotificationSubscription{
433416
Endpoint: server.URL,
434417
})
435-
require.NoError(t, err)
418+
require.NoError(t, err, "delete notification push subscription")
436419
})
437420
}

coderd/rbac/roles_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,16 @@ func TestRolePermissions(t *testing.T) {
713713
},
714714
},
715715
},
716+
// All users can create, read, and delete their own push notification subscriptions.
717+
{
718+
Name: "NotificationPushSubscription",
719+
Actions: []policy.Action{policy.ActionCreate, policy.ActionRead, policy.ActionDelete},
720+
Resource: rbac.ResourceNotificationPushSubscription.WithOwner(currentUser.String()),
721+
AuthorizeMap: map[bool][]hasAuthSubjects{
722+
true: {owner, memberMe, orgMemberMe},
723+
false: {otherOrgMember, orgAdmin, otherOrgAdmin, orgAuditor, otherOrgAuditor, templateAdmin, orgTemplateAdmin, otherOrgTemplateAdmin, userAdmin, orgUserAdmin, otherOrgUserAdmin},
724+
},
725+
},
716726
// AnyOrganization tests
717727
{
718728
Name: "CreateOrgMember",

0 commit comments

Comments
 (0)