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

Function TestPush

coderd/webpush/webpush_test.go:48–495  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

46}
47
48func TestPush(t *testing.T) {
49 t.Parallel()
50
51 t.Run("SuccessfulDelivery", func(t *testing.T) {
52 t.Parallel()
53 ctx := testutil.Context(t, testutil.WaitShort)
54 msg := randomWebpushMessage(t)
55 manager, store, serverURL := setupPushTest(ctx, t, func(w http.ResponseWriter, r *http.Request) {
56 assertWebpushPayload(t, r)
57 w.WriteHeader(http.StatusOK)
58 })
59 user := dbgen.User(t, store, database.User{})
60 sub, err := store.InsertWebpushSubscription(ctx, database.InsertWebpushSubscriptionParams{
61 UserID: user.ID,
62 Endpoint: serverURL,
63 EndpointAuthKey: validEndpointAuthKey,
64 EndpointP256dhKey: validEndpointP256dhKey,
65 CreatedAt: dbtime.Now(),
66 })
67 require.NoError(t, err)
68
69 err = manager.Dispatch(ctx, user.ID, msg)
70 require.NoError(t, err)
71
72 subscriptions, err := store.GetWebpushSubscriptionsByUserID(ctx, user.ID)
73 require.NoError(t, err)
74 assert.Len(t, subscriptions, 1, "One subscription should be returned")
75 assert.Equal(t, subscriptions[0].ID, sub.ID, "The subscription should not be deleted")
76 })
77
78 t.Run("ExpiredSubscription", func(t *testing.T) {
79 t.Parallel()
80 ctx := testutil.Context(t, testutil.WaitShort)
81 manager, store, serverURL := setupPushTest(ctx, t, func(w http.ResponseWriter, r *http.Request) {
82 assertWebpushPayload(t, r)
83 w.WriteHeader(http.StatusGone)
84 })
85 user := dbgen.User(t, store, database.User{})
86 _, err := store.InsertWebpushSubscription(ctx, database.InsertWebpushSubscriptionParams{
87 UserID: user.ID,
88 Endpoint: serverURL,
89 EndpointAuthKey: validEndpointAuthKey,
90 EndpointP256dhKey: validEndpointP256dhKey,
91 CreatedAt: dbtime.Now(),
92 })
93 require.NoError(t, err)
94
95 msg := randomWebpushMessage(t)
96 err = manager.Dispatch(ctx, user.ID, msg)
97 require.NoError(t, err)
98
99 subscriptions, err := store.GetWebpushSubscriptionsByUserID(ctx, user.ID)
100 require.NoError(t, err)
101 assert.Len(t, subscriptions, 0, "No subscriptions should be returned")
102 })
103
104 t.Run("FailedDelivery", func(t *testing.T) {
105 // 5xx responses are transient failures. The subscription should

Callers

nothing calls this directly

Calls 15

CloseMethod · 0.95
getCallCountMethod · 0.95
ContextFunction · 0.92
UserFunction · 0.92
NowFunction · 0.92
NewDBFunction · 0.92
WithClockFunction · 0.92
WithSubscriptionCacheTTLFunction · 0.92
randomWebpushMessageFunction · 0.85
setupPushTestFunction · 0.85
assertWebpushPayloadFunction · 0.85

Tested by

no test coverage detected