MCPcopy Index your code
hub / github.com/coder/coder / TestWebpushSubscribeOverwritesKeys

Function TestWebpushSubscribeOverwritesKeys

coderd/webpush_test.go:103–146  ·  view source on GitHub ↗

TestWebpushSubscribeOverwritesKeys verifies that re-subscribing with the same endpoint and rotated keys overwrites the existing row in place rather than inserting a duplicate. This is the reinstall path: on iOS, deleting the PWA from the home screen and reinstalling can yield the same endpoint with

(t *testing.T)

Source from the content-addressed store, hash-verified

101// with new p256dh / auth keys, and Coder must replace the stored keys so
102// dispatch encrypts with the keys the device can decrypt.
103func TestWebpushSubscribeOverwritesKeys(t *testing.T) {
104 t.Parallel()
105
106 ctx := testutil.Context(t, testutil.WaitShort)
107 store, ps := dbtestutil.NewDB(t)
108 client := coderdtest.New(t, &coderdtest.Options{
109 WebpushDispatcher: &testWebpushDispatcher{},
110 Database: store,
111 Pubsub: ps,
112 })
113 owner := coderdtest.CreateFirstUser(t, client)
114 memberClient, member := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID)
115
116 const endpoint = "https://push.example.com/subscription/reinstall"
117 const secondAuthKey = "AnotherAuthKey/yV1FuojuRmHP42=="
118 const secondP256dhKey = "BNNL5ZaTfK81qhXOx23+wewhigUeFb632jN6LvRWCFH1ubQr77FE/9qV1FuojuRmHP42zmf34rXgW80OvUVDgABc="
119
120 // First subscribe with the original keys.
121 err := memberClient.PostWebpushSubscription(ctx, "me", codersdk.WebpushSubscription{
122 Endpoint: endpoint,
123 AuthKey: validEndpointAuthKey,
124 P256DHKey: validEndpointP256dhKey,
125 })
126 require.NoError(t, err, "initial subscribe")
127
128 // Re-subscribe with the same endpoint but rotated keys. This
129 // simulates the post-reinstall path on iOS where the browser
130 // retains the endpoint but rotates p256dh / auth.
131 err = memberClient.PostWebpushSubscription(ctx, "me", codersdk.WebpushSubscription{
132 Endpoint: endpoint,
133 AuthKey: secondAuthKey,
134 P256DHKey: secondP256dhKey,
135 })
136 require.NoError(t, err, "re-subscribe with rotated keys")
137
138 // The second subscribe must replace the keys in place; we should
139 // see exactly one row carrying the new keys.
140 subs, err := store.GetWebpushSubscriptionsByUserID(dbauthz.AsSystemRestricted(ctx), member.ID)
141 require.NoError(t, err)
142 require.Len(t, subs, 1, "re-subscribe should overwrite the row, not append a duplicate")
143 require.Equal(t, endpoint, subs[0].Endpoint)
144 require.Equal(t, secondAuthKey, subs[0].EndpointAuthKey, "auth key should be the latest one")
145 require.Equal(t, secondP256dhKey, subs[0].EndpointP256dhKey, "p256dh key should be the latest one")
146}
147
148func TestWebpushSubscribeRejectsInvalidEndpoint(t *testing.T) {
149 t.Parallel()

Callers

nothing calls this directly

Calls 10

ContextFunction · 0.92
NewDBFunction · 0.92
NewFunction · 0.92
CreateFirstUserFunction · 0.92
CreateAnotherUserFunction · 0.92
AsSystemRestrictedFunction · 0.92
LenMethod · 0.45
EqualMethod · 0.45

Tested by

no test coverage detected