backend-manager 5.1.4 → 5.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude/settings.local.json +12 -0
- package/CHANGELOG.md +23 -0
- package/CLAUDE.md +2 -1
- package/README.md +15 -0
- package/docs/common-mistakes.md +1 -0
- package/docs/consent.md +333 -0
- package/docs/testing.md +36 -0
- package/package.json +1 -1
- package/src/cli/commands/emulator.js +44 -8
- package/src/cli/commands/serve.js +73 -7
- package/src/cli/commands/test.js +47 -1
- package/src/cli/commands/watch.js +15 -3
- package/src/manager/helpers/user.js +29 -0
- package/src/manager/index.js +29 -0
- package/src/manager/libraries/email/data/disposable-domains.json +8 -0
- package/src/manager/libraries/email/generators/newsletter.js +2 -2
- package/src/manager/libraries/email/providers/beehiiv.js +1 -0
- package/src/manager/libraries/payment/processors/stripe.js +12 -0
- package/src/manager/libraries/payment/processors/test.js +8 -1
- package/src/manager/routes/admin/infer-contact/post.js +3 -2
- package/src/manager/routes/marketing/email-preferences/post.js +165 -37
- package/src/manager/routes/marketing/webhook/forward/post.js +168 -0
- package/src/manager/routes/marketing/webhook/post.js +180 -0
- package/src/manager/routes/marketing/webhook/processors/beehiiv.js +196 -0
- package/src/manager/routes/marketing/webhook/processors/sendgrid.js +171 -0
- package/src/manager/routes/payments/cancel/post.js +2 -2
- package/src/manager/routes/payments/cancel/processors/test.js +5 -2
- package/src/manager/routes/payments/intent/processors/test.js +7 -3
- package/src/manager/routes/payments/refund/processors/test.js +4 -1
- package/src/manager/routes/user/signup/post.js +65 -1
- package/src/manager/schemas/marketing/email-preferences/post.js +8 -4
- package/src/manager/schemas/marketing/webhook/forward/post.js +8 -0
- package/src/manager/schemas/marketing/webhook/post.js +7 -0
- package/src/manager/schemas/payments/cancel/post.js +5 -0
- package/src/manager/schemas/user/signup/post.js +5 -0
- package/src/test/runner.js +61 -18
- package/src/test/test-accounts.js +94 -12
- package/src/test/utils/http-client.js +4 -3
- package/test/events/payments/journey-payments-cancel-endpoint.js +3 -12
- package/test/events/payments/journey-payments-cancel.js +4 -5
- package/test/events/payments/journey-payments-failure.js +0 -1
- package/test/events/payments/journey-payments-one-time-failure.js +6 -3
- package/test/events/payments/journey-payments-one-time.js +6 -3
- package/test/events/payments/journey-payments-plan-change.js +5 -5
- package/test/events/payments/journey-payments-refund-webhook.js +2 -3
- package/test/events/payments/journey-payments-suspend.js +4 -5
- package/test/events/payments/journey-payments-trial-cancel.js +3 -12
- package/test/events/payments/journey-payments-trial.js +2 -3
- package/test/events/payments/journey-payments-uid-resolution.js +2 -3
- package/test/functions/admin/database-read.js +0 -14
- package/test/functions/admin/database-write.js +0 -14
- package/test/functions/admin/firestore-query.js +0 -14
- package/test/functions/admin/firestore-read.js +0 -15
- package/test/functions/admin/firestore-write.js +0 -11
- package/test/functions/general/add-marketing-contact.js +16 -14
- package/test/helpers/email.js +1 -1
- package/test/helpers/infer-contact.js +3 -3
- package/test/helpers/user.js +241 -2
- package/test/helpers/webhook-forward.js +392 -0
- package/test/marketing/newsletter-generate.js +17 -7
- package/test/routes/admin/database.js +0 -13
- package/test/routes/admin/firestore-query.js +0 -13
- package/test/routes/admin/firestore.js +0 -14
- package/test/routes/admin/infer-contact.js +6 -3
- package/test/routes/admin/post.js +4 -2
- package/test/routes/marketing/contact.js +60 -26
- package/test/routes/marketing/email-preferences.js +145 -69
- package/test/routes/marketing/webhook-forward.js +54 -0
- package/test/routes/marketing/webhook.js +582 -0
- package/test/routes/payments/cancel.js +2 -7
- package/test/routes/payments/dispute-alert.js +0 -39
- package/test/routes/payments/refund.js +3 -1
- package/test/routes/payments/webhook.js +5 -26
- package/test/routes/test/usage.js +2 -2
- package/test/routes/user/signup.js +114 -0
|
@@ -0,0 +1,582 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Test: POST /marketing/webhook
|
|
3
|
+
*
|
|
4
|
+
* Cross-provider unsubscribe webhook receiver. Phase E covers SendGrid;
|
|
5
|
+
* Beehiiv will be added in a separate file.
|
|
6
|
+
*
|
|
7
|
+
* Dispatcher tests:
|
|
8
|
+
* - Auth via ?key= query param
|
|
9
|
+
* - Provider validation
|
|
10
|
+
* - Brand filter (ignore mismatched brand)
|
|
11
|
+
* - Idempotency via marketing-webhooks/{eventId} doc
|
|
12
|
+
*
|
|
13
|
+
* SendGrid processor tests:
|
|
14
|
+
* - Various event types (group_unsubscribe, unsubscribe, spamreport, bounce, dropped)
|
|
15
|
+
* - Email lookup → user doc mutation with source='sendgrid'
|
|
16
|
+
* - Silent skip when email doesn't map to a user (shared SendGrid account scenario)
|
|
17
|
+
* - Batched events processed independently
|
|
18
|
+
* - Unsupported event types ignored
|
|
19
|
+
*/
|
|
20
|
+
const { TEST_ACCOUNTS } = require('../../../src/test/test-accounts.js');
|
|
21
|
+
|
|
22
|
+
// Helper — generate a unique sg_event_id per test
|
|
23
|
+
function sgEventId(name) {
|
|
24
|
+
return `_test-sg-${name}-${Date.now()}`;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// Helper — build a SendGrid event payload
|
|
28
|
+
function sgEvent({ id, type, email, timestamp, asmGroupId }) {
|
|
29
|
+
return {
|
|
30
|
+
sg_event_id: id,
|
|
31
|
+
event: type,
|
|
32
|
+
email,
|
|
33
|
+
timestamp: timestamp || Math.floor(Date.now() / 1000),
|
|
34
|
+
...(asmGroupId !== undefined ? { asm_group_id: asmGroupId } : {}),
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
module.exports = {
|
|
39
|
+
description: 'Marketing webhook endpoint (SendGrid)',
|
|
40
|
+
type: 'group',
|
|
41
|
+
timeout: 30000,
|
|
42
|
+
|
|
43
|
+
tests: [
|
|
44
|
+
// ─── Dispatcher auth + validation ───
|
|
45
|
+
|
|
46
|
+
{
|
|
47
|
+
name: 'rejects-missing-provider',
|
|
48
|
+
auth: 'none',
|
|
49
|
+
async run({ http, assert }) {
|
|
50
|
+
const response = await http.as('none').post('marketing/webhook', []);
|
|
51
|
+
assert.isError(response, 400, 'Should reject missing provider');
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
|
|
55
|
+
{
|
|
56
|
+
name: 'rejects-missing-key',
|
|
57
|
+
auth: 'none',
|
|
58
|
+
async run({ http, assert }) {
|
|
59
|
+
const response = await http.as('none').post('marketing/webhook?provider=sendgrid', []);
|
|
60
|
+
assert.isError(response, 401, 'Should reject missing key');
|
|
61
|
+
},
|
|
62
|
+
},
|
|
63
|
+
|
|
64
|
+
{
|
|
65
|
+
name: 'rejects-invalid-key',
|
|
66
|
+
auth: 'none',
|
|
67
|
+
async run({ http, assert }) {
|
|
68
|
+
const response = await http.as('none').post('marketing/webhook?provider=sendgrid&key=wrong-key', []);
|
|
69
|
+
assert.isError(response, 401, 'Should reject invalid key');
|
|
70
|
+
},
|
|
71
|
+
},
|
|
72
|
+
|
|
73
|
+
{
|
|
74
|
+
name: 'rejects-unknown-provider',
|
|
75
|
+
auth: 'none',
|
|
76
|
+
async run({ http, assert }) {
|
|
77
|
+
const response = await http.as('none').post(
|
|
78
|
+
`marketing/webhook?provider=unknown&key=${process.env.BACKEND_MANAGER_WEBHOOK_KEY}`,
|
|
79
|
+
[]
|
|
80
|
+
);
|
|
81
|
+
assert.isError(response, 400, 'Should reject unknown provider');
|
|
82
|
+
},
|
|
83
|
+
},
|
|
84
|
+
|
|
85
|
+
{
|
|
86
|
+
name: 'ignores-mismatched-brand',
|
|
87
|
+
auth: 'none',
|
|
88
|
+
async run({ http, assert }) {
|
|
89
|
+
// Brand filter should silently ignore (200 with ignored: true), not error
|
|
90
|
+
const response = await http.as('none').post(
|
|
91
|
+
`marketing/webhook?provider=sendgrid&key=${process.env.BACKEND_MANAGER_WEBHOOK_KEY}&brand=some-other-brand-that-does-not-exist`,
|
|
92
|
+
[]
|
|
93
|
+
);
|
|
94
|
+
assert.isSuccess(response, 'Should silently ignore mismatched brand (200 OK)');
|
|
95
|
+
assert.propertyEquals(response, 'data.ignored', true, 'Response should indicate ignored');
|
|
96
|
+
},
|
|
97
|
+
},
|
|
98
|
+
|
|
99
|
+
// ─── SendGrid processor — supported events ───
|
|
100
|
+
|
|
101
|
+
{
|
|
102
|
+
name: 'sendgrid-group-unsubscribe-writes-consent',
|
|
103
|
+
auth: 'none',
|
|
104
|
+
async run({ http, firestore, assert, accounts }) {
|
|
105
|
+
const uid = accounts.basic.uid;
|
|
106
|
+
const email = accounts.basic.email;
|
|
107
|
+
const eventId = sgEventId('group-unsub');
|
|
108
|
+
const eventTimestamp = Math.floor(Date.now() / 1000);
|
|
109
|
+
|
|
110
|
+
const response = await http.as('none').post(
|
|
111
|
+
`marketing/webhook?provider=sendgrid&key=${process.env.BACKEND_MANAGER_WEBHOOK_KEY}`,
|
|
112
|
+
[sgEvent({ id: eventId, type: 'group_unsubscribe', email, timestamp: eventTimestamp, asmGroupId: 25928 })]
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
assert.isSuccess(response, `Webhook should accept group_unsubscribe: ${JSON.stringify(response, null, 2)}`);
|
|
116
|
+
assert.propertyEquals(response, 'data.processed', 1, 'Should report 1 event processed');
|
|
117
|
+
|
|
118
|
+
// User doc should now show revoked marketing with source=sendgrid
|
|
119
|
+
const userDoc = await firestore.get(`users/${uid}`);
|
|
120
|
+
assert.equal(userDoc?.consent?.marketing?.status, 'revoked', 'marketing.status should be revoked');
|
|
121
|
+
assert.equal(userDoc?.consent?.marketing?.revokedAt?.source, 'sendgrid', 'revokedAt.source should be sendgrid');
|
|
122
|
+
assert.equal(userDoc?.consent?.marketing?.revokedAt?.timestampUNIX, eventTimestamp, 'revokedAt.timestampUNIX should match event timestamp');
|
|
123
|
+
|
|
124
|
+
// Idempotency doc should exist with status=completed
|
|
125
|
+
const webhookDoc = await firestore.get(`marketing-webhooks/${eventId}`);
|
|
126
|
+
assert.ok(webhookDoc, 'Idempotency doc should exist');
|
|
127
|
+
assert.equal(webhookDoc?.status, 'completed', 'Idempotency doc should be marked completed');
|
|
128
|
+
assert.equal(webhookDoc?.provider, 'sendgrid', 'Idempotency doc should record provider');
|
|
129
|
+
},
|
|
130
|
+
},
|
|
131
|
+
|
|
132
|
+
{
|
|
133
|
+
name: 'sendgrid-unsubscribe-event-handled',
|
|
134
|
+
auth: 'none',
|
|
135
|
+
async run({ http, firestore, assert, accounts }) {
|
|
136
|
+
const uid = accounts.basic.uid;
|
|
137
|
+
const email = accounts.basic.email;
|
|
138
|
+
const eventId = sgEventId('unsub');
|
|
139
|
+
|
|
140
|
+
const response = await http.as('none').post(
|
|
141
|
+
`marketing/webhook?provider=sendgrid&key=${process.env.BACKEND_MANAGER_WEBHOOK_KEY}`,
|
|
142
|
+
[sgEvent({ id: eventId, type: 'unsubscribe', email })]
|
|
143
|
+
);
|
|
144
|
+
|
|
145
|
+
assert.isSuccess(response);
|
|
146
|
+
assert.propertyEquals(response, 'data.processed', 1, 'Should process the unsubscribe event');
|
|
147
|
+
|
|
148
|
+
const userDoc = await firestore.get(`users/${uid}`);
|
|
149
|
+
assert.equal(userDoc?.consent?.marketing?.status, 'revoked', 'marketing.status should be revoked');
|
|
150
|
+
assert.equal(userDoc?.consent?.marketing?.revokedAt?.source, 'sendgrid', 'source should be sendgrid');
|
|
151
|
+
},
|
|
152
|
+
},
|
|
153
|
+
|
|
154
|
+
{
|
|
155
|
+
name: 'sendgrid-spamreport-event-handled',
|
|
156
|
+
auth: 'none',
|
|
157
|
+
async run({ http, firestore, assert, accounts }) {
|
|
158
|
+
const uid = accounts.basic.uid;
|
|
159
|
+
const email = accounts.basic.email;
|
|
160
|
+
const eventId = sgEventId('spamreport');
|
|
161
|
+
|
|
162
|
+
const response = await http.as('none').post(
|
|
163
|
+
`marketing/webhook?provider=sendgrid&key=${process.env.BACKEND_MANAGER_WEBHOOK_KEY}`,
|
|
164
|
+
[sgEvent({ id: eventId, type: 'spamreport', email })]
|
|
165
|
+
);
|
|
166
|
+
|
|
167
|
+
assert.isSuccess(response);
|
|
168
|
+
assert.propertyEquals(response, 'data.processed', 1, 'Spamreport should be treated as a revoke');
|
|
169
|
+
|
|
170
|
+
const userDoc = await firestore.get(`users/${uid}`);
|
|
171
|
+
assert.equal(userDoc?.consent?.marketing?.status, 'revoked');
|
|
172
|
+
},
|
|
173
|
+
},
|
|
174
|
+
|
|
175
|
+
{
|
|
176
|
+
name: 'sendgrid-bounce-event-handled',
|
|
177
|
+
auth: 'none',
|
|
178
|
+
async run({ http, firestore, assert, accounts }) {
|
|
179
|
+
const uid = accounts.basic.uid;
|
|
180
|
+
const email = accounts.basic.email;
|
|
181
|
+
const eventId = sgEventId('bounce');
|
|
182
|
+
|
|
183
|
+
const response = await http.as('none').post(
|
|
184
|
+
`marketing/webhook?provider=sendgrid&key=${process.env.BACKEND_MANAGER_WEBHOOK_KEY}`,
|
|
185
|
+
[sgEvent({ id: eventId, type: 'bounce', email })]
|
|
186
|
+
);
|
|
187
|
+
|
|
188
|
+
assert.isSuccess(response);
|
|
189
|
+
assert.propertyEquals(response, 'data.processed', 1, 'Bounce should be treated as a revoke');
|
|
190
|
+
|
|
191
|
+
const userDoc = await firestore.get(`users/${uid}`);
|
|
192
|
+
assert.equal(userDoc?.consent?.marketing?.status, 'revoked');
|
|
193
|
+
},
|
|
194
|
+
},
|
|
195
|
+
|
|
196
|
+
// ─── SendGrid processor — events we ignore ───
|
|
197
|
+
|
|
198
|
+
{
|
|
199
|
+
name: 'sendgrid-delivered-event-ignored',
|
|
200
|
+
auth: 'none',
|
|
201
|
+
async run({ http, firestore, assert, accounts }) {
|
|
202
|
+
const email = accounts.basic.email;
|
|
203
|
+
const eventId = sgEventId('delivered');
|
|
204
|
+
|
|
205
|
+
const response = await http.as('none').post(
|
|
206
|
+
`marketing/webhook?provider=sendgrid&key=${process.env.BACKEND_MANAGER_WEBHOOK_KEY}`,
|
|
207
|
+
[sgEvent({ id: eventId, type: 'delivered', email })]
|
|
208
|
+
);
|
|
209
|
+
|
|
210
|
+
assert.isSuccess(response, 'Should accept the request (not error) but ignore the event');
|
|
211
|
+
assert.propertyEquals(response, 'data.processed', 0, 'No events should be processed');
|
|
212
|
+
assert.propertyEquals(response, 'data.skipped', 1, '1 event should be skipped');
|
|
213
|
+
},
|
|
214
|
+
},
|
|
215
|
+
|
|
216
|
+
{
|
|
217
|
+
name: 'sendgrid-open-event-ignored',
|
|
218
|
+
auth: 'none',
|
|
219
|
+
async run({ http, assert, accounts }) {
|
|
220
|
+
const email = accounts.basic.email;
|
|
221
|
+
const eventId = sgEventId('open');
|
|
222
|
+
|
|
223
|
+
const response = await http.as('none').post(
|
|
224
|
+
`marketing/webhook?provider=sendgrid&key=${process.env.BACKEND_MANAGER_WEBHOOK_KEY}`,
|
|
225
|
+
[sgEvent({ id: eventId, type: 'open', email })]
|
|
226
|
+
);
|
|
227
|
+
|
|
228
|
+
assert.isSuccess(response);
|
|
229
|
+
assert.propertyEquals(response, 'data.processed', 0, 'Open events should be ignored');
|
|
230
|
+
},
|
|
231
|
+
},
|
|
232
|
+
|
|
233
|
+
// ─── Email not mapped to a user ───
|
|
234
|
+
|
|
235
|
+
{
|
|
236
|
+
name: 'sendgrid-unknown-email-silent-skip',
|
|
237
|
+
auth: 'none',
|
|
238
|
+
async run({ http, assert }) {
|
|
239
|
+
// Email that doesn't match any user in this brand's Firestore — shared SendGrid scenario
|
|
240
|
+
const eventId = sgEventId('unknown-email');
|
|
241
|
+
|
|
242
|
+
const response = await http.as('none').post(
|
|
243
|
+
`marketing/webhook?provider=sendgrid&key=${process.env.BACKEND_MANAGER_WEBHOOK_KEY}`,
|
|
244
|
+
[sgEvent({ id: eventId, type: 'group_unsubscribe', email: '_test.never-existed@example.com' })]
|
|
245
|
+
);
|
|
246
|
+
|
|
247
|
+
// Dispatcher still processes the event (idempotency doc written, handler runs and returns
|
|
248
|
+
// handled:false). From the dispatcher's POV this counts as 'processed=1' since the handler
|
|
249
|
+
// didn't throw. The handler's internal "user-not-found" branch is silent by design.
|
|
250
|
+
assert.isSuccess(response, 'Should accept unknown-email gracefully');
|
|
251
|
+
assert.propertyEquals(response, 'data.failed', 0, 'No failures for unknown email');
|
|
252
|
+
},
|
|
253
|
+
},
|
|
254
|
+
|
|
255
|
+
// ─── Batched events ───
|
|
256
|
+
|
|
257
|
+
{
|
|
258
|
+
name: 'sendgrid-batched-events-processed-independently',
|
|
259
|
+
auth: 'none',
|
|
260
|
+
async run({ http, firestore, assert, accounts }) {
|
|
261
|
+
const uid = accounts.basic.uid;
|
|
262
|
+
const email = accounts.basic.email;
|
|
263
|
+
const e1 = sgEventId('batch-1');
|
|
264
|
+
const e2 = sgEventId('batch-2');
|
|
265
|
+
const e3 = sgEventId('batch-3');
|
|
266
|
+
|
|
267
|
+
const response = await http.as('none').post(
|
|
268
|
+
`marketing/webhook?provider=sendgrid&key=${process.env.BACKEND_MANAGER_WEBHOOK_KEY}`,
|
|
269
|
+
[
|
|
270
|
+
sgEvent({ id: e1, type: 'group_unsubscribe', email }),
|
|
271
|
+
sgEvent({ id: e2, type: 'open', email }), // ignored — unsupported type
|
|
272
|
+
sgEvent({ id: e3, type: 'spamreport', email }),
|
|
273
|
+
]
|
|
274
|
+
);
|
|
275
|
+
|
|
276
|
+
assert.isSuccess(response);
|
|
277
|
+
assert.propertyEquals(response, 'data.processed', 2, '2 supported events should be processed');
|
|
278
|
+
assert.propertyEquals(response, 'data.skipped', 1, '1 unsupported event should be skipped');
|
|
279
|
+
|
|
280
|
+
// Each processed event gets its own idempotency doc
|
|
281
|
+
const doc1 = await firestore.get(`marketing-webhooks/${e1}`);
|
|
282
|
+
const doc3 = await firestore.get(`marketing-webhooks/${e3}`);
|
|
283
|
+
assert.ok(doc1 && doc1.status === 'completed', 'First event idempotency doc completed');
|
|
284
|
+
assert.ok(doc3 && doc3.status === 'completed', 'Third event idempotency doc completed');
|
|
285
|
+
|
|
286
|
+
// The skipped event should NOT have an idempotency doc (we filter by isSupported before writing)
|
|
287
|
+
const doc2 = await firestore.get(`marketing-webhooks/${e2}`);
|
|
288
|
+
assert.ok(!doc2, 'Unsupported event should NOT have an idempotency doc');
|
|
289
|
+
|
|
290
|
+
// User doc should be revoked
|
|
291
|
+
const userDoc = await firestore.get(`users/${uid}`);
|
|
292
|
+
assert.equal(userDoc?.consent?.marketing?.status, 'revoked');
|
|
293
|
+
},
|
|
294
|
+
},
|
|
295
|
+
|
|
296
|
+
// ─── Idempotency ───
|
|
297
|
+
|
|
298
|
+
{
|
|
299
|
+
name: 'sendgrid-duplicate-event-skipped',
|
|
300
|
+
auth: 'none',
|
|
301
|
+
async run({ http, firestore, assert, accounts }) {
|
|
302
|
+
const email = accounts.basic.email;
|
|
303
|
+
const eventId = sgEventId('duplicate');
|
|
304
|
+
|
|
305
|
+
// First delivery
|
|
306
|
+
const response1 = await http.as('none').post(
|
|
307
|
+
`marketing/webhook?provider=sendgrid&key=${process.env.BACKEND_MANAGER_WEBHOOK_KEY}`,
|
|
308
|
+
[sgEvent({ id: eventId, type: 'group_unsubscribe', email })]
|
|
309
|
+
);
|
|
310
|
+
assert.isSuccess(response1);
|
|
311
|
+
assert.propertyEquals(response1, 'data.processed', 1, 'First delivery should process');
|
|
312
|
+
|
|
313
|
+
// Second delivery — same eventId
|
|
314
|
+
const response2 = await http.as('none').post(
|
|
315
|
+
`marketing/webhook?provider=sendgrid&key=${process.env.BACKEND_MANAGER_WEBHOOK_KEY}`,
|
|
316
|
+
[sgEvent({ id: eventId, type: 'group_unsubscribe', email })]
|
|
317
|
+
);
|
|
318
|
+
assert.isSuccess(response2);
|
|
319
|
+
assert.propertyEquals(response2, 'data.processed', 0, 'Duplicate should NOT reprocess');
|
|
320
|
+
assert.propertyEquals(response2, 'data.skipped', 1, 'Duplicate should be skipped');
|
|
321
|
+
|
|
322
|
+
// Idempotency doc should still be there and completed
|
|
323
|
+
const webhookDoc = await firestore.get(`marketing-webhooks/${eventId}`);
|
|
324
|
+
assert.equal(webhookDoc?.status, 'completed');
|
|
325
|
+
},
|
|
326
|
+
},
|
|
327
|
+
|
|
328
|
+
// ─── Missing event ID ───
|
|
329
|
+
|
|
330
|
+
{
|
|
331
|
+
name: 'sendgrid-event-without-eventId-skipped',
|
|
332
|
+
auth: 'none',
|
|
333
|
+
async run({ http, assert, accounts }) {
|
|
334
|
+
const email = accounts.basic.email;
|
|
335
|
+
|
|
336
|
+
const response = await http.as('none').post(
|
|
337
|
+
`marketing/webhook?provider=sendgrid&key=${process.env.BACKEND_MANAGER_WEBHOOK_KEY}`,
|
|
338
|
+
[{ event: 'group_unsubscribe', email, timestamp: Math.floor(Date.now() / 1000) }] // NO sg_event_id
|
|
339
|
+
);
|
|
340
|
+
|
|
341
|
+
assert.isSuccess(response, 'Should accept the request');
|
|
342
|
+
assert.propertyEquals(response, 'data.processed', 0, 'Event without eventId cannot be deduped, so it is skipped');
|
|
343
|
+
assert.propertyEquals(response, 'data.skipped', 1, 'Event should be skipped');
|
|
344
|
+
},
|
|
345
|
+
},
|
|
346
|
+
|
|
347
|
+
// ──────────────────────────────────────────────────────────────────────
|
|
348
|
+
// Beehiiv processor tests
|
|
349
|
+
// ──────────────────────────────────────────────────────────────────────
|
|
350
|
+
|
|
351
|
+
{
|
|
352
|
+
name: 'beehiiv-subscription-unsubscribed-writes-consent',
|
|
353
|
+
auth: 'none',
|
|
354
|
+
async run({ http, firestore, assert, accounts, config }) {
|
|
355
|
+
const uid = accounts.basic.uid;
|
|
356
|
+
const email = accounts.basic.email;
|
|
357
|
+
const eventId = `_test-bh-unsub-${Date.now()}`;
|
|
358
|
+
const eventISO = new Date().toISOString();
|
|
359
|
+
const publicationId = config.marketing?.beehiiv?.publicationId;
|
|
360
|
+
|
|
361
|
+
assert.ok(publicationId, 'Test brand must have a Beehiiv publication ID configured');
|
|
362
|
+
|
|
363
|
+
const response = await http.as('none').post(
|
|
364
|
+
`marketing/webhook?provider=beehiiv&key=${process.env.BACKEND_MANAGER_WEBHOOK_KEY}`,
|
|
365
|
+
{
|
|
366
|
+
id: eventId,
|
|
367
|
+
event: 'subscription.unsubscribed',
|
|
368
|
+
email,
|
|
369
|
+
publication_id: publicationId,
|
|
370
|
+
created_at: eventISO,
|
|
371
|
+
}
|
|
372
|
+
);
|
|
373
|
+
|
|
374
|
+
assert.isSuccess(response, `Beehiiv unsub should succeed: ${JSON.stringify(response, null, 2)}`);
|
|
375
|
+
assert.propertyEquals(response, 'data.processed', 1, 'Should process 1 event');
|
|
376
|
+
|
|
377
|
+
const userDoc = await firestore.get(`users/${uid}`);
|
|
378
|
+
assert.equal(userDoc?.consent?.marketing?.status, 'revoked', 'marketing.status should be revoked');
|
|
379
|
+
assert.equal(userDoc?.consent?.marketing?.revokedAt?.source, 'beehiiv', 'revokedAt.source should be beehiiv');
|
|
380
|
+
assert.ok(userDoc?.consent?.marketing?.revokedAt?.timestamp, 'revokedAt.timestamp should be set');
|
|
381
|
+
|
|
382
|
+
// Idempotency doc should exist
|
|
383
|
+
const webhookDoc = await firestore.get(`marketing-webhooks/${eventId}`);
|
|
384
|
+
assert.ok(webhookDoc, 'Idempotency doc should exist');
|
|
385
|
+
assert.equal(webhookDoc?.status, 'completed');
|
|
386
|
+
assert.equal(webhookDoc?.provider, 'beehiiv');
|
|
387
|
+
},
|
|
388
|
+
},
|
|
389
|
+
|
|
390
|
+
{
|
|
391
|
+
name: 'beehiiv-subscription-deleted-handled',
|
|
392
|
+
auth: 'none',
|
|
393
|
+
async run({ http, firestore, assert, accounts, config }) {
|
|
394
|
+
const uid = accounts.basic.uid;
|
|
395
|
+
const email = accounts.basic.email;
|
|
396
|
+
const eventId = `_test-bh-deleted-${Date.now()}`;
|
|
397
|
+
const publicationId = config.marketing?.beehiiv?.publicationId;
|
|
398
|
+
|
|
399
|
+
const response = await http.as('none').post(
|
|
400
|
+
`marketing/webhook?provider=beehiiv&key=${process.env.BACKEND_MANAGER_WEBHOOK_KEY}`,
|
|
401
|
+
{
|
|
402
|
+
id: eventId,
|
|
403
|
+
event: 'subscription.deleted',
|
|
404
|
+
email,
|
|
405
|
+
publication_id: publicationId,
|
|
406
|
+
created_at: new Date().toISOString(),
|
|
407
|
+
}
|
|
408
|
+
);
|
|
409
|
+
|
|
410
|
+
assert.isSuccess(response);
|
|
411
|
+
assert.propertyEquals(response, 'data.processed', 1, 'subscription.deleted should be processed as a revoke');
|
|
412
|
+
|
|
413
|
+
const userDoc = await firestore.get(`users/${uid}`);
|
|
414
|
+
assert.equal(userDoc?.consent?.marketing?.status, 'revoked');
|
|
415
|
+
assert.equal(userDoc?.consent?.marketing?.revokedAt?.source, 'beehiiv');
|
|
416
|
+
},
|
|
417
|
+
},
|
|
418
|
+
|
|
419
|
+
{
|
|
420
|
+
name: 'beehiiv-subscription-paused-handled',
|
|
421
|
+
auth: 'none',
|
|
422
|
+
async run({ http, firestore, assert, accounts, config }) {
|
|
423
|
+
const uid = accounts.basic.uid;
|
|
424
|
+
const email = accounts.basic.email;
|
|
425
|
+
const eventId = `_test-bh-paused-${Date.now()}`;
|
|
426
|
+
const publicationId = config.marketing?.beehiiv?.publicationId;
|
|
427
|
+
|
|
428
|
+
const response = await http.as('none').post(
|
|
429
|
+
`marketing/webhook?provider=beehiiv&key=${process.env.BACKEND_MANAGER_WEBHOOK_KEY}`,
|
|
430
|
+
{
|
|
431
|
+
id: eventId,
|
|
432
|
+
event: 'subscription.paused',
|
|
433
|
+
email,
|
|
434
|
+
publication_id: publicationId,
|
|
435
|
+
created_at: new Date().toISOString(),
|
|
436
|
+
}
|
|
437
|
+
);
|
|
438
|
+
|
|
439
|
+
assert.isSuccess(response);
|
|
440
|
+
assert.propertyEquals(response, 'data.processed', 1, 'subscription.paused should be processed as a revoke');
|
|
441
|
+
|
|
442
|
+
const userDoc = await firestore.get(`users/${uid}`);
|
|
443
|
+
assert.equal(userDoc?.consent?.marketing?.status, 'revoked');
|
|
444
|
+
},
|
|
445
|
+
},
|
|
446
|
+
|
|
447
|
+
{
|
|
448
|
+
name: 'beehiiv-publication-mismatch-silent-skip',
|
|
449
|
+
auth: 'none',
|
|
450
|
+
async run({ http, firestore, assert, accounts }) {
|
|
451
|
+
// Send an event with a publication_id that does NOT match this brand's pub.
|
|
452
|
+
// Simulates the shared-devbeans scenario where the parent forwarder fans
|
|
453
|
+
// an event to brands that don't share the publication — they silent-skip.
|
|
454
|
+
const email = accounts.basic.email;
|
|
455
|
+
const eventId = `_test-bh-pubmismatch-${Date.now()}`;
|
|
456
|
+
|
|
457
|
+
// Snapshot revokedAt BEFORE the request so we can prove the pub-mismatch
|
|
458
|
+
// handler didn't write anything new. (The basic account may already have
|
|
459
|
+
// a beehiiv-sourced revoke from a prior test that legitimately fired.)
|
|
460
|
+
const beforeDoc = await firestore.get(`users/${accounts.basic.uid}`);
|
|
461
|
+
const beforeRevokedAt = beforeDoc?.consent?.marketing?.revokedAt || null;
|
|
462
|
+
|
|
463
|
+
const response = await http.as('none').post(
|
|
464
|
+
`marketing/webhook?provider=beehiiv&key=${process.env.BACKEND_MANAGER_WEBHOOK_KEY}`,
|
|
465
|
+
{
|
|
466
|
+
id: eventId,
|
|
467
|
+
event: 'subscription.unsubscribed',
|
|
468
|
+
email,
|
|
469
|
+
publication_id: 'pub_does-not-belong-to-this-brand-xxxxxxxxxxxxxx',
|
|
470
|
+
created_at: new Date().toISOString(),
|
|
471
|
+
}
|
|
472
|
+
);
|
|
473
|
+
|
|
474
|
+
// The dispatcher counts this as 'processed' from its POV (the handler
|
|
475
|
+
// ran without error and the idempotency doc was written), but the
|
|
476
|
+
// handler returned { handled: false, reason: 'publication-mismatch' }.
|
|
477
|
+
// What matters: the user doc should NOT have been mutated.
|
|
478
|
+
assert.isSuccess(response, 'Pub-mismatch event should be accepted gracefully');
|
|
479
|
+
|
|
480
|
+
// Reload the user doc and verify revokedAt is byte-equivalent to before —
|
|
481
|
+
// pub-mismatch must not write a new revoke entry.
|
|
482
|
+
const afterDoc = await firestore.get(`users/${accounts.basic.uid}`);
|
|
483
|
+
const afterRevokedAt = afterDoc?.consent?.marketing?.revokedAt || null;
|
|
484
|
+
|
|
485
|
+
assert.deepEqual(
|
|
486
|
+
afterRevokedAt,
|
|
487
|
+
beforeRevokedAt,
|
|
488
|
+
'consent.marketing.revokedAt must be UNCHANGED after a pub-mismatch event (handler should silent-skip)'
|
|
489
|
+
);
|
|
490
|
+
},
|
|
491
|
+
},
|
|
492
|
+
|
|
493
|
+
{
|
|
494
|
+
name: 'beehiiv-unknown-email-silent-skip',
|
|
495
|
+
auth: 'none',
|
|
496
|
+
async run({ http, assert, config }) {
|
|
497
|
+
// Email that doesn't map to any user — shared publication scenario where
|
|
498
|
+
// multiple brands receive the same event but only one has the user.
|
|
499
|
+
const publicationId = config.marketing?.beehiiv?.publicationId;
|
|
500
|
+
const eventId = `_test-bh-unknown-${Date.now()}`;
|
|
501
|
+
|
|
502
|
+
const response = await http.as('none').post(
|
|
503
|
+
`marketing/webhook?provider=beehiiv&key=${process.env.BACKEND_MANAGER_WEBHOOK_KEY}`,
|
|
504
|
+
{
|
|
505
|
+
id: eventId,
|
|
506
|
+
event: 'subscription.unsubscribed',
|
|
507
|
+
email: '_test.no-such-user@example.com',
|
|
508
|
+
publication_id: publicationId,
|
|
509
|
+
created_at: new Date().toISOString(),
|
|
510
|
+
}
|
|
511
|
+
);
|
|
512
|
+
|
|
513
|
+
assert.isSuccess(response, 'Unknown email should not error');
|
|
514
|
+
assert.propertyEquals(response, 'data.failed', 0, 'No failures for unknown email');
|
|
515
|
+
},
|
|
516
|
+
},
|
|
517
|
+
|
|
518
|
+
{
|
|
519
|
+
name: 'beehiiv-unsupported-event-ignored',
|
|
520
|
+
auth: 'none',
|
|
521
|
+
async run({ http, assert, accounts, config }) {
|
|
522
|
+
// 'subscription.created' (new signup) is NOT a revoke — should be ignored.
|
|
523
|
+
const email = accounts.basic.email;
|
|
524
|
+
const publicationId = config.marketing?.beehiiv?.publicationId;
|
|
525
|
+
const eventId = `_test-bh-created-${Date.now()}`;
|
|
526
|
+
|
|
527
|
+
const response = await http.as('none').post(
|
|
528
|
+
`marketing/webhook?provider=beehiiv&key=${process.env.BACKEND_MANAGER_WEBHOOK_KEY}`,
|
|
529
|
+
{
|
|
530
|
+
id: eventId,
|
|
531
|
+
event: 'subscription.created',
|
|
532
|
+
email,
|
|
533
|
+
publication_id: publicationId,
|
|
534
|
+
created_at: new Date().toISOString(),
|
|
535
|
+
}
|
|
536
|
+
);
|
|
537
|
+
|
|
538
|
+
assert.isSuccess(response);
|
|
539
|
+
assert.propertyEquals(response, 'data.processed', 0, 'Unsupported events should not be processed');
|
|
540
|
+
assert.propertyEquals(response, 'data.skipped', 1, 'Unsupported events should be skipped');
|
|
541
|
+
},
|
|
542
|
+
},
|
|
543
|
+
|
|
544
|
+
{
|
|
545
|
+
name: 'beehiiv-duplicate-event-skipped',
|
|
546
|
+
auth: 'none',
|
|
547
|
+
async run({ http, firestore, assert, accounts, config }) {
|
|
548
|
+
const email = accounts.basic.email;
|
|
549
|
+
const publicationId = config.marketing?.beehiiv?.publicationId;
|
|
550
|
+
const eventId = `_test-bh-dup-${Date.now()}`;
|
|
551
|
+
|
|
552
|
+
const payload = {
|
|
553
|
+
id: eventId,
|
|
554
|
+
event: 'subscription.unsubscribed',
|
|
555
|
+
email,
|
|
556
|
+
publication_id: publicationId,
|
|
557
|
+
created_at: new Date().toISOString(),
|
|
558
|
+
};
|
|
559
|
+
|
|
560
|
+
// First delivery
|
|
561
|
+
const r1 = await http.as('none').post(
|
|
562
|
+
`marketing/webhook?provider=beehiiv&key=${process.env.BACKEND_MANAGER_WEBHOOK_KEY}`,
|
|
563
|
+
payload
|
|
564
|
+
);
|
|
565
|
+
assert.isSuccess(r1);
|
|
566
|
+
assert.propertyEquals(r1, 'data.processed', 1, 'First delivery should process');
|
|
567
|
+
|
|
568
|
+
// Second delivery — same id
|
|
569
|
+
const r2 = await http.as('none').post(
|
|
570
|
+
`marketing/webhook?provider=beehiiv&key=${process.env.BACKEND_MANAGER_WEBHOOK_KEY}`,
|
|
571
|
+
payload
|
|
572
|
+
);
|
|
573
|
+
assert.isSuccess(r2);
|
|
574
|
+
assert.propertyEquals(r2, 'data.processed', 0, 'Duplicate should NOT reprocess');
|
|
575
|
+
assert.propertyEquals(r2, 'data.skipped', 1, 'Duplicate should be skipped');
|
|
576
|
+
|
|
577
|
+
const webhookDoc = await firestore.get(`marketing-webhooks/${eventId}`);
|
|
578
|
+
assert.equal(webhookDoc?.status, 'completed');
|
|
579
|
+
},
|
|
580
|
+
},
|
|
581
|
+
],
|
|
582
|
+
};
|
|
@@ -113,16 +113,11 @@ module.exports = {
|
|
|
113
113
|
&& userDoc?.subscription?.status === 'active';
|
|
114
114
|
}, 15000, 500);
|
|
115
115
|
|
|
116
|
-
//
|
|
117
|
-
const twoDaysAgo = new Date(Date.now() - 2 * 24 * 60 * 60 * 1000);
|
|
118
|
-
await firestore.set(`users/${uid}`, {
|
|
119
|
-
subscription: { payment: { startDate: { timestamp: twoDaysAgo.toISOString(), timestampUNIX: twoDaysAgo.getTime() } } },
|
|
120
|
-
}, { merge: true });
|
|
121
|
-
|
|
122
|
-
// Step 2: Call the cancel endpoint
|
|
116
|
+
// Step 2: Call the cancel endpoint (skipGuards bypasses the 24-hour age guard)
|
|
123
117
|
const cancelResponse = await http.as('route-cancel-success').post('payments/cancel', {
|
|
124
118
|
confirmed: true,
|
|
125
119
|
reason: 'Too expensive',
|
|
120
|
+
skipGuards: true,
|
|
126
121
|
});
|
|
127
122
|
|
|
128
123
|
assert.isSuccess(cancelResponse, 'Cancel should succeed');
|