(t *testing.T)
| 27 | ) |
| 28 | |
| 29 | func TestWebpushSubscribeUnsubscribe(t *testing.T) { |
| 30 | t.Parallel() |
| 31 | |
| 32 | ctx := testutil.Context(t, testutil.WaitShort) |
| 33 | |
| 34 | dispatcher := &testWebpushDispatcher{} |
| 35 | client := coderdtest.New(t, &coderdtest.Options{ |
| 36 | WebpushDispatcher: dispatcher, |
| 37 | }) |
| 38 | owner := coderdtest.CreateFirstUser(t, client) |
| 39 | memberClient, _ := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID) |
| 40 | _, anotherMember := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID) |
| 41 | endpoint := "https://push.example.com/subscription/abc123" |
| 42 | |
| 43 | // Seed the dispatcher cache with an empty subscription set. Creating the |
| 44 | // subscription should invalidate that entry so the next dispatch sees the new |
| 45 | // subscription immediately. |
| 46 | err := memberClient.PostTestWebpushMessage(ctx) |
| 47 | require.NoError(t, err, "test webpush message without a subscription") |
| 48 | require.Equal(t, int32(1), dispatcher.dispatchCalls.Load(), "dispatch should be called even with no subscriptions") |
| 49 | |
| 50 | err = memberClient.PostWebpushSubscription(ctx, "me", codersdk.WebpushSubscription{ |
| 51 | Endpoint: endpoint, |
| 52 | AuthKey: validEndpointAuthKey, |
| 53 | P256DHKey: validEndpointP256dhKey, |
| 54 | }) |
| 55 | require.NoError(t, err, "create webpush subscription") |
| 56 | require.Equal(t, int32(1), dispatcher.testCalls.Load(), "subscription validation should call dispatcher test once") |
| 57 | require.Equal(t, 1, dispatcher.invalidateCount(), "subscribing should invalidate the user's cached subscriptions") |
| 58 | |
| 59 | err = memberClient.PostTestWebpushMessage(ctx) |
| 60 | require.NoError(t, err, "test webpush message after subscribing") |
| 61 | require.Equal(t, int32(2), dispatcher.dispatchCalls.Load(), "dispatch should be called after subscribing") |
| 62 | |
| 63 | err = memberClient.DeleteWebpushSubscription(ctx, "me", codersdk.DeleteWebpushSubscription{ |
| 64 | Endpoint: endpoint, |
| 65 | }) |
| 66 | require.NoError(t, err, "delete webpush subscription") |
| 67 | require.Equal(t, 2, dispatcher.invalidateCount(), "unsubscribing should invalidate the user's cached subscriptions") |
| 68 | |
| 69 | err = memberClient.PostTestWebpushMessage(ctx) |
| 70 | require.NoError(t, err, "test webpush message after unsubscribing") |
| 71 | require.Equal(t, int32(3), dispatcher.dispatchCalls.Load(), "dispatch should be called after unsubscribing") |
| 72 | |
| 73 | // Deleting the subscription for a non-existent endpoint should return a 404. |
| 74 | err = memberClient.DeleteWebpushSubscription(ctx, "me", codersdk.DeleteWebpushSubscription{ |
| 75 | Endpoint: endpoint, |
| 76 | }) |
| 77 | var sdkError *codersdk.Error |
| 78 | require.Error(t, err) |
| 79 | require.ErrorAsf(t, err, &sdkError, "error should be of type *codersdk.Error") |
| 80 | require.Equal(t, http.StatusNotFound, sdkError.StatusCode()) |
| 81 | |
| 82 | // Creating a subscription for another user should not be allowed. |
| 83 | err = memberClient.PostWebpushSubscription(ctx, anotherMember.ID.String(), codersdk.WebpushSubscription{ |
| 84 | Endpoint: endpoint, |
| 85 | AuthKey: validEndpointAuthKey, |
| 86 | P256DHKey: validEndpointP256dhKey, |
nothing calls this directly
no test coverage detected