File: webpush_store_test.go

package info (click to toggle)
ntfy 2.11.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 19,360 kB
  • sloc: javascript: 16,782; makefile: 282; sh: 105; php: 21; python: 19
file content (200 lines) | stat: -rw-r--r-- 7,685 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
//go:build ignore
package server

import (
	"fmt"
	"github.com/stretchr/testify/require"
	"net/netip"
	"path/filepath"
	"testing"
	"time"
)

func TestWebPushStore_UpsertSubscription_SubscriptionsForTopic(t *testing.T) {
	webPush := newTestWebPushStore(t)
	defer webPush.Close()

	require.Nil(t, webPush.UpsertSubscription(testWebPushEndpoint, "auth-key", "p256dh-key", "u_1234", netip.MustParseAddr("1.2.3.4"), []string{"test-topic", "mytopic"}))

	subs, err := webPush.SubscriptionsForTopic("test-topic")
	require.Nil(t, err)
	require.Len(t, subs, 1)
	require.Equal(t, subs[0].Endpoint, testWebPushEndpoint)
	require.Equal(t, subs[0].P256dh, "p256dh-key")
	require.Equal(t, subs[0].Auth, "auth-key")
	require.Equal(t, subs[0].UserID, "u_1234")

	subs2, err := webPush.SubscriptionsForTopic("mytopic")
	require.Nil(t, err)
	require.Len(t, subs2, 1)
	require.Equal(t, subs[0].Endpoint, subs2[0].Endpoint)
}

func TestWebPushStore_UpsertSubscription_SubscriberIPLimitReached(t *testing.T) {
	webPush := newTestWebPushStore(t)
	defer webPush.Close()

	// Insert 10 subscriptions with the same IP address
	for i := 0; i < 10; i++ {
		endpoint := fmt.Sprintf(testWebPushEndpoint+"%d", i)
		require.Nil(t, webPush.UpsertSubscription(endpoint, "auth-key", "p256dh-key", "u_1234", netip.MustParseAddr("1.2.3.4"), []string{"test-topic", "mytopic"}))
	}

	// Another one for the same endpoint should be fine
	require.Nil(t, webPush.UpsertSubscription(testWebPushEndpoint+"0", "auth-key", "p256dh-key", "u_1234", netip.MustParseAddr("1.2.3.4"), []string{"test-topic", "mytopic"}))

	// But with a different endpoint it should fail
	require.Equal(t, errWebPushTooManySubscriptions, webPush.UpsertSubscription(testWebPushEndpoint+"11", "auth-key", "p256dh-key", "u_1234", netip.MustParseAddr("1.2.3.4"), []string{"test-topic", "mytopic"}))

	// But with a different IP address it should be fine again
	require.Nil(t, webPush.UpsertSubscription(testWebPushEndpoint+"99", "auth-key", "p256dh-key", "u_1234", netip.MustParseAddr("9.9.9.9"), []string{"test-topic", "mytopic"}))
}

func TestWebPushStore_UpsertSubscription_UpdateTopics(t *testing.T) {
	webPush := newTestWebPushStore(t)
	defer webPush.Close()

	// Insert subscription with two topics, and another with one topic
	require.Nil(t, webPush.UpsertSubscription(testWebPushEndpoint+"0", "auth-key", "p256dh-key", "u_1234", netip.MustParseAddr("1.2.3.4"), []string{"topic1", "topic2"}))
	require.Nil(t, webPush.UpsertSubscription(testWebPushEndpoint+"1", "auth-key", "p256dh-key", "", netip.MustParseAddr("9.9.9.9"), []string{"topic1"}))

	subs, err := webPush.SubscriptionsForTopic("topic1")
	require.Nil(t, err)
	require.Len(t, subs, 2)
	require.Equal(t, testWebPushEndpoint+"0", subs[0].Endpoint)
	require.Equal(t, testWebPushEndpoint+"1", subs[1].Endpoint)

	subs, err = webPush.SubscriptionsForTopic("topic2")
	require.Nil(t, err)
	require.Len(t, subs, 1)
	require.Equal(t, testWebPushEndpoint+"0", subs[0].Endpoint)

	// Update the first subscription to have only one topic
	require.Nil(t, webPush.UpsertSubscription(testWebPushEndpoint+"0", "auth-key", "p256dh-key", "u_1234", netip.MustParseAddr("1.2.3.4"), []string{"topic1"}))

	subs, err = webPush.SubscriptionsForTopic("topic1")
	require.Nil(t, err)
	require.Len(t, subs, 2)
	require.Equal(t, testWebPushEndpoint+"0", subs[0].Endpoint)

	subs, err = webPush.SubscriptionsForTopic("topic2")
	require.Nil(t, err)
	require.Len(t, subs, 0)
}

func TestWebPushStore_RemoveSubscriptionsByEndpoint(t *testing.T) {
	webPush := newTestWebPushStore(t)
	defer webPush.Close()

	// Insert subscription with two topics
	require.Nil(t, webPush.UpsertSubscription(testWebPushEndpoint, "auth-key", "p256dh-key", "u_1234", netip.MustParseAddr("1.2.3.4"), []string{"topic1", "topic2"}))
	subs, err := webPush.SubscriptionsForTopic("topic1")
	require.Nil(t, err)
	require.Len(t, subs, 1)

	// And remove it again
	require.Nil(t, webPush.RemoveSubscriptionsByEndpoint(testWebPushEndpoint))
	subs, err = webPush.SubscriptionsForTopic("topic1")
	require.Nil(t, err)
	require.Len(t, subs, 0)
}

func TestWebPushStore_RemoveSubscriptionsByUserID(t *testing.T) {
	webPush := newTestWebPushStore(t)
	defer webPush.Close()

	// Insert subscription with two topics
	require.Nil(t, webPush.UpsertSubscription(testWebPushEndpoint, "auth-key", "p256dh-key", "u_1234", netip.MustParseAddr("1.2.3.4"), []string{"topic1", "topic2"}))
	subs, err := webPush.SubscriptionsForTopic("topic1")
	require.Nil(t, err)
	require.Len(t, subs, 1)

	// And remove it again
	require.Nil(t, webPush.RemoveSubscriptionsByUserID("u_1234"))
	subs, err = webPush.SubscriptionsForTopic("topic1")
	require.Nil(t, err)
	require.Len(t, subs, 0)
}

func TestWebPushStore_RemoveSubscriptionsByUserID_Empty(t *testing.T) {
	webPush := newTestWebPushStore(t)
	defer webPush.Close()
	require.Equal(t, errWebPushUserIDCannotBeEmpty, webPush.RemoveSubscriptionsByUserID(""))
}

func TestWebPushStore_MarkExpiryWarningSent(t *testing.T) {
	webPush := newTestWebPushStore(t)
	defer webPush.Close()

	// Insert subscription with two topics
	require.Nil(t, webPush.UpsertSubscription(testWebPushEndpoint, "auth-key", "p256dh-key", "u_1234", netip.MustParseAddr("1.2.3.4"), []string{"topic1", "topic2"}))
	subs, err := webPush.SubscriptionsForTopic("topic1")
	require.Nil(t, err)
	require.Len(t, subs, 1)

	// Mark them as warning sent
	require.Nil(t, webPush.MarkExpiryWarningSent(subs))

	rows, err := webPush.db.Query("SELECT endpoint FROM subscription WHERE warned_at > 0")
	require.Nil(t, err)
	defer rows.Close()
	var endpoint string
	require.True(t, rows.Next())
	require.Nil(t, rows.Scan(&endpoint))
	require.Nil(t, err)
	require.Equal(t, testWebPushEndpoint, endpoint)
	require.False(t, rows.Next())
}

func TestWebPushStore_SubscriptionsExpiring(t *testing.T) {
	webPush := newTestWebPushStore(t)
	defer webPush.Close()

	// Insert subscription with two topics
	require.Nil(t, webPush.UpsertSubscription(testWebPushEndpoint, "auth-key", "p256dh-key", "u_1234", netip.MustParseAddr("1.2.3.4"), []string{"topic1", "topic2"}))
	subs, err := webPush.SubscriptionsForTopic("topic1")
	require.Nil(t, err)
	require.Len(t, subs, 1)

	// Fake-mark them as soon-to-expire
	_, err = webPush.db.Exec("UPDATE subscription SET updated_at = ? WHERE endpoint = ?", time.Now().Add(-8*24*time.Hour).Unix(), testWebPushEndpoint)
	require.Nil(t, err)

	// Should not be cleaned up yet
	require.Nil(t, webPush.RemoveExpiredSubscriptions(9*24*time.Hour))

	// Run expiration
	subs, err = webPush.SubscriptionsExpiring(7 * 24 * time.Hour)
	require.Nil(t, err)
	require.Len(t, subs, 1)
	require.Equal(t, testWebPushEndpoint, subs[0].Endpoint)
}

func TestWebPushStore_RemoveExpiredSubscriptions(t *testing.T) {
	webPush := newTestWebPushStore(t)
	defer webPush.Close()

	// Insert subscription with two topics
	require.Nil(t, webPush.UpsertSubscription(testWebPushEndpoint, "auth-key", "p256dh-key", "u_1234", netip.MustParseAddr("1.2.3.4"), []string{"topic1", "topic2"}))
	subs, err := webPush.SubscriptionsForTopic("topic1")
	require.Nil(t, err)
	require.Len(t, subs, 1)

	// Fake-mark them as expired
	_, err = webPush.db.Exec("UPDATE subscription SET updated_at = ? WHERE endpoint = ?", time.Now().Add(-10*24*time.Hour).Unix(), testWebPushEndpoint)
	require.Nil(t, err)

	// Run expiration
	require.Nil(t, webPush.RemoveExpiredSubscriptions(9*24*time.Hour))

	// List again, should be 0
	subs, err = webPush.SubscriptionsForTopic("topic1")
	require.Nil(t, err)
	require.Len(t, subs, 0)
}

func newTestWebPushStore(t *testing.T) *webPushStore {
	webPush, err := newWebPushStore(filepath.Join(t.TempDir(), "webpush.db"), "")
	require.Nil(t, err)
	return webPush
}