backend-manager 5.1.2 → 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.
Files changed (97) hide show
  1. package/.claude/settings.local.json +12 -0
  2. package/CHANGELOG.md +52 -0
  3. package/CLAUDE.md +2 -1
  4. package/README.md +30 -0
  5. package/docs/common-mistakes.md +1 -0
  6. package/docs/consent.md +333 -0
  7. package/docs/marketing-campaigns.md +41 -4
  8. package/docs/testing.md +81 -0
  9. package/package.json +1 -1
  10. package/src/cli/commands/emulator.js +62 -9
  11. package/src/cli/commands/serve.js +73 -7
  12. package/src/cli/commands/test.js +65 -1
  13. package/src/cli/commands/watch.js +15 -3
  14. package/src/defaults/CLAUDE.md +7 -5
  15. package/src/manager/events/cron/daily/marketing-newsletter-generate.js +24 -8
  16. package/src/manager/helpers/user.js +29 -0
  17. package/src/manager/index.js +111 -5
  18. package/src/manager/libraries/ai/index.js +21 -0
  19. package/src/manager/libraries/ai/providers/openai.js +75 -0
  20. package/src/manager/libraries/email/data/disposable-domains.json +20 -0
  21. package/src/manager/libraries/email/generators/lib/image-host.js +58 -6
  22. package/src/manager/libraries/email/generators/lib/markdown-renderer.js +285 -0
  23. package/src/manager/libraries/email/generators/lib/structure.js +19 -2
  24. package/src/manager/libraries/email/generators/lib/svg-illustrator.js +12 -3
  25. package/src/manager/libraries/email/generators/lib/templates/classic-schema.js +9 -13
  26. package/src/manager/libraries/email/generators/lib/templates/clean.js +1 -1
  27. package/src/manager/libraries/email/generators/lib/templates/editorial.js +1 -1
  28. package/src/manager/libraries/email/generators/lib/templates/field-report.js +10 -14
  29. package/src/manager/libraries/email/generators/newsletter.js +154 -7
  30. package/src/manager/libraries/email/providers/beehiiv.js +8 -1
  31. package/src/manager/libraries/payment/processors/stripe.js +12 -0
  32. package/src/manager/libraries/payment/processors/test.js +8 -1
  33. package/src/manager/routes/admin/infer-contact/post.js +3 -2
  34. package/src/manager/routes/admin/post/post.js +3 -3
  35. package/src/manager/routes/marketing/email-preferences/post.js +165 -37
  36. package/src/manager/routes/marketing/webhook/forward/post.js +168 -0
  37. package/src/manager/routes/marketing/webhook/post.js +180 -0
  38. package/src/manager/routes/marketing/webhook/processors/beehiiv.js +196 -0
  39. package/src/manager/routes/marketing/webhook/processors/sendgrid.js +171 -0
  40. package/src/manager/routes/payments/cancel/post.js +2 -2
  41. package/src/manager/routes/payments/cancel/processors/test.js +5 -2
  42. package/src/manager/routes/payments/intent/processors/test.js +7 -3
  43. package/src/manager/routes/payments/refund/processors/test.js +4 -1
  44. package/src/manager/routes/test/health/get.js +17 -0
  45. package/src/manager/routes/user/signup/post.js +65 -1
  46. package/src/manager/schemas/marketing/email-preferences/post.js +8 -4
  47. package/src/manager/schemas/marketing/webhook/forward/post.js +8 -0
  48. package/src/manager/schemas/marketing/webhook/post.js +7 -0
  49. package/src/manager/schemas/payments/cancel/post.js +5 -0
  50. package/src/manager/schemas/user/signup/post.js +5 -0
  51. package/src/test/run-tests.js +30 -0
  52. package/src/test/runner.js +72 -26
  53. package/src/test/test-accounts.js +94 -12
  54. package/src/test/utils/http-client.js +4 -3
  55. package/src/test/utils/test-mode-file.js +192 -0
  56. package/test/events/payments/journey-payments-cancel-endpoint.js +3 -12
  57. package/test/events/payments/journey-payments-cancel.js +4 -5
  58. package/test/events/payments/journey-payments-failure.js +0 -1
  59. package/test/events/payments/journey-payments-one-time-failure.js +6 -3
  60. package/test/events/payments/journey-payments-one-time.js +6 -3
  61. package/test/events/payments/journey-payments-plan-change.js +5 -5
  62. package/test/events/payments/journey-payments-refund-webhook.js +2 -3
  63. package/test/events/payments/journey-payments-suspend.js +4 -5
  64. package/test/events/payments/journey-payments-trial-cancel.js +3 -12
  65. package/test/events/payments/journey-payments-trial.js +2 -3
  66. package/test/events/payments/journey-payments-uid-resolution.js +2 -3
  67. package/test/functions/admin/database-read.js +0 -14
  68. package/test/functions/admin/database-write.js +0 -14
  69. package/test/functions/admin/firestore-query.js +0 -14
  70. package/test/functions/admin/firestore-read.js +0 -15
  71. package/test/functions/admin/firestore-write.js +0 -11
  72. package/test/functions/general/add-marketing-contact.js +16 -14
  73. package/test/helpers/email.js +1 -1
  74. package/test/helpers/infer-contact.js +3 -3
  75. package/test/helpers/user.js +241 -2
  76. package/test/helpers/webhook-forward.js +392 -0
  77. package/test/marketing/fixtures/clean.json +2 -3
  78. package/test/marketing/fixtures/editorial.json +2 -3
  79. package/test/marketing/fixtures/field-report.json +3 -4
  80. package/test/marketing/newsletter-generate.js +78 -54
  81. package/test/marketing/newsletter-templates.js +12 -33
  82. package/test/routes/admin/create-post.js +2 -2
  83. package/test/routes/admin/database.js +0 -13
  84. package/test/routes/admin/firestore-query.js +0 -13
  85. package/test/routes/admin/firestore.js +0 -14
  86. package/test/routes/admin/infer-contact.js +6 -3
  87. package/test/routes/admin/post.js +4 -2
  88. package/test/routes/marketing/contact.js +60 -26
  89. package/test/routes/marketing/email-preferences.js +145 -69
  90. package/test/routes/marketing/webhook-forward.js +54 -0
  91. package/test/routes/marketing/webhook.js +582 -0
  92. package/test/routes/payments/cancel.js +2 -7
  93. package/test/routes/payments/dispute-alert.js +0 -39
  94. package/test/routes/payments/refund.js +3 -1
  95. package/test/routes/payments/webhook.js +5 -26
  96. package/test/routes/test/usage.js +2 -2
  97. package/test/routes/user/signup.js +114 -0
@@ -1,9 +1,15 @@
1
1
  /**
2
2
  * Test: POST /marketing/email-preferences
3
- * Tests the email preferences endpoint for unsubscribe/resubscribe via SendGrid ASM
4
3
  *
5
- * Set TEST_EXTENDED_MODE=true to run tests against real SendGrid ASM API
6
- * (requires SENDGRID_API_KEY env var)
4
+ * Two modes:
5
+ * - Anonymous HMAC: from email-footer unsubscribe link. Requires email + asmId + sig.
6
+ * Hits SendGrid ASM and (NEW) mirrors to user doc if email maps to a user.
7
+ * - Authenticated: from account-page toggle. Requires only `action`.
8
+ * Writes consent.marketing to user doc with source='account' and hits SendGrid + Beehiiv
9
+ * via the email library.
10
+ *
11
+ * Set TEST_EXTENDED_MODE=true to hit real SendGrid + Beehiiv. Otherwise provider calls
12
+ * are skipped but user-doc mutations still happen.
7
13
  */
8
14
  const crypto = require('crypto');
9
15
 
@@ -15,201 +21,271 @@ function generateSig(email) {
15
21
  }
16
22
 
17
23
  module.exports = {
18
- description: 'Marketing email-preferences (POST unsubscribe/resubscribe)',
24
+ description: 'Marketing email-preferences (anonymous HMAC + authenticated)',
19
25
  type: 'group',
20
26
  tests: [
21
- // Test 1: Successful unsubscribe with valid sig
27
+ // ─── Anonymous HMAC flow ───
28
+
22
29
  {
23
- name: 'unsubscribe-valid-sig-succeeds',
30
+ name: 'anon-unsubscribe-valid-sig-succeeds',
24
31
  auth: 'none',
25
32
  timeout: 15000,
26
-
27
33
  async run({ http, assert }) {
28
34
  const sig = generateSig(TEST_EMAIL);
29
-
30
35
  const response = await http.post('marketing/email-preferences', {
31
36
  email: TEST_EMAIL,
32
37
  asmId: TEST_ASM_ID,
33
38
  action: 'unsubscribe',
34
- sig: sig,
39
+ sig,
35
40
  });
36
-
37
41
  assert.isSuccess(response, 'Unsubscribe with valid sig should succeed');
38
42
  assert.propertyEquals(response, 'data.success', true, 'success should be true');
39
43
  },
40
44
  },
41
45
 
42
- // Test 2: Successful resubscribe with valid sig
43
46
  {
44
- name: 'resubscribe-valid-sig-succeeds',
47
+ name: 'anon-subscribe-valid-sig-succeeds',
45
48
  auth: 'none',
46
49
  timeout: 15000,
47
-
48
50
  async run({ http, assert }) {
49
51
  const sig = generateSig(TEST_EMAIL);
50
-
51
52
  const response = await http.post('marketing/email-preferences', {
52
53
  email: TEST_EMAIL,
53
54
  asmId: TEST_ASM_ID,
54
- action: 'resubscribe',
55
- sig: sig,
55
+ action: 'subscribe',
56
+ sig,
56
57
  });
57
-
58
- assert.isSuccess(response, 'Resubscribe with valid sig should succeed');
58
+ assert.isSuccess(response, 'Subscribe with valid sig should succeed');
59
59
  assert.propertyEquals(response, 'data.success', true, 'success should be true');
60
60
  },
61
61
  },
62
62
 
63
- // Test 3: Invalid sig rejected
64
63
  {
65
- name: 'invalid-sig-rejected',
64
+ name: 'anon-resubscribe-rejected',
66
65
  auth: 'none',
67
66
  timeout: 15000,
68
-
69
67
  async run({ http, assert }) {
68
+ // Old 'resubscribe' action is no longer accepted — must use 'subscribe'
69
+ const sig = generateSig(TEST_EMAIL);
70
70
  const response = await http.post('marketing/email-preferences', {
71
71
  email: TEST_EMAIL,
72
72
  asmId: TEST_ASM_ID,
73
- action: 'unsubscribe',
74
- sig: 'invalid-signature-value',
73
+ action: 'resubscribe',
74
+ sig,
75
75
  });
76
-
77
- assert.isError(response, 403, 'Invalid sig should return 403');
76
+ assert.isError(response, 400, 'Old "resubscribe" action should be rejected');
78
77
  },
79
78
  },
80
79
 
81
- // Test 4: Missing sig rejected (schema requires it)
82
80
  {
83
- name: 'missing-sig-rejected',
81
+ name: 'anon-invalid-sig-rejected',
84
82
  auth: 'none',
85
83
  timeout: 15000,
86
-
87
84
  async run({ http, assert }) {
88
85
  const response = await http.post('marketing/email-preferences', {
89
86
  email: TEST_EMAIL,
90
87
  asmId: TEST_ASM_ID,
91
88
  action: 'unsubscribe',
89
+ sig: 'invalid-signature-value',
92
90
  });
93
-
94
- assert.isError(response, 400, 'Missing sig should return 400');
91
+ assert.isError(response, 403, 'Invalid sig should return 403');
95
92
  },
96
93
  },
97
94
 
98
- // Test 5: Missing email rejected
99
95
  {
100
- name: 'missing-email-rejected',
96
+ name: 'anon-missing-email-rejected',
101
97
  auth: 'none',
102
98
  timeout: 15000,
103
-
104
99
  async run({ http, assert }) {
105
100
  const response = await http.post('marketing/email-preferences', {
106
101
  asmId: TEST_ASM_ID,
107
102
  action: 'unsubscribe',
108
103
  sig: 'anything',
109
104
  });
110
-
111
105
  assert.isError(response, 400, 'Missing email should return 400');
112
106
  },
113
107
  },
114
108
 
115
- // Test 6: Invalid email format rejected
116
109
  {
117
- name: 'invalid-email-rejected',
110
+ name: 'anon-invalid-email-rejected',
118
111
  auth: 'none',
119
112
  timeout: 15000,
120
-
121
113
  async run({ http, assert }) {
122
114
  const sig = generateSig('not-an-email');
123
-
124
115
  const response = await http.post('marketing/email-preferences', {
125
116
  email: 'not-an-email',
126
117
  asmId: TEST_ASM_ID,
127
118
  action: 'unsubscribe',
128
- sig: sig,
119
+ sig,
129
120
  });
130
-
131
121
  assert.isError(response, 400, 'Invalid email format should return 400');
132
122
  },
133
123
  },
134
124
 
135
- // Test 7: Missing asmId rejected
136
125
  {
137
- name: 'missing-asmid-rejected',
126
+ name: 'anon-missing-asmid-rejected',
138
127
  auth: 'none',
139
128
  timeout: 15000,
140
-
141
129
  async run({ http, assert }) {
142
130
  const sig = generateSig(TEST_EMAIL);
143
-
144
131
  const response = await http.post('marketing/email-preferences', {
145
132
  email: TEST_EMAIL,
146
133
  action: 'unsubscribe',
147
- sig: sig,
134
+ sig,
148
135
  });
149
-
150
136
  assert.isError(response, 400, 'Missing asmId should return 400');
151
137
  },
152
138
  },
153
139
 
154
- // Test 8: Invalid action rejected
155
140
  {
156
- name: 'invalid-action-rejected',
141
+ name: 'anon-invalid-action-rejected',
157
142
  auth: 'none',
158
143
  timeout: 15000,
159
-
160
144
  async run({ http, assert }) {
161
145
  const sig = generateSig(TEST_EMAIL);
162
-
163
146
  const response = await http.post('marketing/email-preferences', {
164
147
  email: TEST_EMAIL,
165
148
  asmId: TEST_ASM_ID,
166
149
  action: 'delete',
167
- sig: sig,
150
+ sig,
168
151
  });
169
-
170
152
  assert.isError(response, 400, 'Invalid action should return 400');
171
153
  },
172
154
  },
173
155
 
174
- // Test 9: Sig for different email rejected (proves per-email sig)
175
156
  {
176
- name: 'wrong-email-sig-rejected',
157
+ name: 'anon-wrong-email-sig-rejected',
177
158
  auth: 'none',
178
159
  timeout: 15000,
179
-
180
160
  async run({ http, assert }) {
181
- // Generate sig for a different email
161
+ // sig generated for a different email — must not validate against TEST_EMAIL
182
162
  const sig = generateSig('someone-else@gmail.com');
183
-
184
163
  const response = await http.post('marketing/email-preferences', {
185
164
  email: TEST_EMAIL,
186
165
  asmId: TEST_ASM_ID,
187
166
  action: 'unsubscribe',
188
- sig: sig,
167
+ sig,
189
168
  });
190
-
191
169
  assert.isError(response, 403, 'Sig for different email should return 403');
192
170
  },
193
171
  },
194
172
 
195
- // Test 10: Authenticated user also works (sig is checked regardless of auth)
173
+ // ─── Authenticated mode ───
174
+
175
+ {
176
+ name: 'auth-unsubscribe-writes-consent-and-records-source-account',
177
+ auth: 'basic',
178
+ timeout: 15000,
179
+ async run({ http, firestore, assert, accounts }) {
180
+ const uid = accounts.basic.uid;
181
+
182
+ const beforeMs = Date.now();
183
+ const response = await http.as('basic').post('marketing/email-preferences', {
184
+ action: 'unsubscribe',
185
+ });
186
+ const afterMs = Date.now();
187
+
188
+ assert.isSuccess(response, `Authenticated unsubscribe should succeed: ${JSON.stringify(response, null, 2)}`);
189
+ assert.propertyEquals(response, 'data.success', true, 'success should be true');
190
+ assert.propertyEquals(response, 'data.action', 'unsubscribe', 'action echoed in response');
191
+
192
+ const userDoc = await firestore.get(`users/${uid}`);
193
+
194
+ assert.equal(userDoc?.consent?.marketing?.status, 'revoked', 'consent.marketing.status should be revoked');
195
+ assert.equal(userDoc?.consent?.marketing?.revokedAt?.source, 'account', 'revokedAt.source should be account');
196
+ assert.ok(userDoc?.consent?.marketing?.revokedAt?.timestamp, 'revokedAt.timestamp should be set');
197
+ assert.equal(typeof userDoc?.consent?.marketing?.revokedAt?.timestampUNIX, 'number', 'revokedAt.timestampUNIX should be number');
198
+
199
+ // Server time used (defense against clock manipulation).
200
+ // Server uses Math.round, so the stamped value can be 1 second past Math.floor(afterMs/1000)
201
+ // when the request takes >500ms. Use Math.round on the upper bound + a small fudge.
202
+ const revokedUNIX = userDoc.consent.marketing.revokedAt.timestampUNIX;
203
+ const beforeUNIX = Math.floor(beforeMs / 1000);
204
+ const afterUNIX = Math.round(afterMs / 1000) + 1;
205
+ assert.ok(
206
+ revokedUNIX >= beforeUNIX && revokedUNIX <= afterUNIX,
207
+ `revokedAt.timestampUNIX (${revokedUNIX}) should be server time, between ${beforeUNIX} and ${afterUNIX}`
208
+ );
209
+ },
210
+ },
211
+
196
212
  {
197
- name: 'authenticated-user-with-valid-sig-succeeds',
198
- auth: 'user',
213
+ name: 'auth-subscribe-after-unsubscribe-flips-status-keeps-prior-revokedAt',
214
+ auth: 'basic',
199
215
  timeout: 15000,
216
+ async run({ http, firestore, assert, accounts }) {
217
+ const uid = accounts.basic.uid;
218
+
219
+ // Capture revokedAt from the previous test
220
+ const beforeDoc = await firestore.get(`users/${uid}`);
221
+ const priorRevokedAt = beforeDoc?.consent?.marketing?.revokedAt;
222
+ assert.ok(priorRevokedAt?.timestamp, 'Prior test should have left a revokedAt timestamp');
223
+
224
+ const response = await http.as('basic').post('marketing/email-preferences', {
225
+ action: 'subscribe',
226
+ });
227
+
228
+ assert.isSuccess(response, `Authenticated subscribe should succeed: ${JSON.stringify(response, null, 2)}`);
200
229
 
230
+ const userDoc = await firestore.get(`users/${uid}`);
231
+
232
+ // status flips
233
+ assert.equal(userDoc?.consent?.marketing?.status, 'granted', 'status should flip to granted');
234
+
235
+ // grantedAt populated with new server time + source=account
236
+ assert.equal(userDoc?.consent?.marketing?.grantedAt?.source, 'account', 'grantedAt.source should be account');
237
+ assert.ok(userDoc?.consent?.marketing?.grantedAt?.timestamp, 'grantedAt.timestamp should be set');
238
+
239
+ // revokedAt UNTOUCHED — still reflects the most recent revoke
240
+ assert.equal(
241
+ userDoc?.consent?.marketing?.revokedAt?.timestamp,
242
+ priorRevokedAt.timestamp,
243
+ 'revokedAt.timestamp should be preserved from prior revoke'
244
+ );
245
+ assert.equal(
246
+ userDoc?.consent?.marketing?.revokedAt?.source,
247
+ priorRevokedAt.source,
248
+ 'revokedAt.source should be preserved from prior revoke'
249
+ );
250
+ },
251
+ },
252
+
253
+ {
254
+ name: 'auth-invalid-action-rejected',
255
+ auth: 'basic',
256
+ timeout: 15000,
201
257
  async run({ http, assert }) {
202
- const sig = generateSig(TEST_EMAIL);
258
+ const response = await http.as('basic').post('marketing/email-preferences', {
259
+ action: 'delete',
260
+ });
261
+ assert.isError(response, 400, 'Invalid action should return 400');
262
+ },
263
+ },
203
264
 
265
+ {
266
+ name: 'auth-opt-in-old-name-rejected',
267
+ auth: 'basic',
268
+ timeout: 15000,
269
+ async run({ http, assert }) {
270
+ // Old proposed 'opt-in' is NOT accepted — must use 'subscribe'
271
+ const response = await http.as('basic').post('marketing/email-preferences', {
272
+ action: 'opt-in',
273
+ });
274
+ assert.isError(response, 400, 'Old "opt-in" name should be rejected (use "subscribe")');
275
+ },
276
+ },
277
+
278
+ {
279
+ name: 'unauthenticated-without-sig-rejected',
280
+ auth: 'none',
281
+ timeout: 15000,
282
+ async run({ http, assert }) {
283
+ // Unauthenticated + no sig → email field is required for HMAC path → 400.
284
+ // (No auth means we hit the anonymous path; no email/asmId means missing-required.)
204
285
  const response = await http.post('marketing/email-preferences', {
205
- email: TEST_EMAIL,
206
- asmId: TEST_ASM_ID,
207
286
  action: 'unsubscribe',
208
- sig: sig,
209
287
  });
210
-
211
- assert.isSuccess(response, 'Authenticated user with valid sig should succeed');
212
- assert.propertyEquals(response, 'data.success', true, 'success should be true');
288
+ assert.isError(response, 400, 'Unauthenticated request without HMAC fields should 400');
213
289
  },
214
290
  },
215
291
  ],
@@ -0,0 +1,54 @@
1
+ /**
2
+ * Test: POST /marketing/webhook/forward (parent forwarder)
3
+ *
4
+ * This route is gated to only work when Manager.config.parent === 'self'.
5
+ * Most test runs happen on a CHILD brand (e.g. Somiibo's backend-manager-config.json
6
+ * has `parent: 'https://api.itwcreativeworks.com'`), so the route should return 404.
7
+ *
8
+ * The actual fan-out behavior (reading brands collection, derive API URLs,
9
+ * POST to each child) is verified by unit-style tests in test/helpers/webhook-forward.js
10
+ * which exercise the forwarder logic against a mock admin + mock fetch — no emulator
11
+ * round-trip required.
12
+ *
13
+ * This file only verifies the GATE: on a non-parent BEM, the route is invisible.
14
+ */
15
+ module.exports = {
16
+ description: 'Marketing webhook forwarder gating (parent-only)',
17
+ type: 'group',
18
+ timeout: 15000,
19
+
20
+ tests: [
21
+ {
22
+ name: 'forwarder-returns-404-on-non-parent-brand',
23
+ auth: 'none',
24
+ async run({ http, assert, config }) {
25
+ // Sanity check: this brand should NOT be configured as the parent
26
+ assert.ok(
27
+ config.parent && config.parent !== 'self',
28
+ `Test brand should have config.parent set to a URL (got: ${config.parent})`
29
+ );
30
+
31
+ const response = await http.as('none').post(
32
+ `marketing/webhook/forward?provider=sendgrid&key=${process.env.BACKEND_MANAGER_WEBHOOK_KEY}`,
33
+ [{ sg_event_id: 'should-not-process', event: 'group_unsubscribe', email: 'test@example.com' }]
34
+ );
35
+
36
+ assert.isError(response, 404, 'Forwarder should be invisible (404) on non-parent BEMs');
37
+ },
38
+ },
39
+
40
+ {
41
+ name: 'forwarder-returns-404-even-with-valid-key',
42
+ auth: 'none',
43
+ async run({ http, assert }) {
44
+ // A valid key shouldn't unlock the forwarder — gate is on config.parent, not key.
45
+ const response = await http.as('none').post(
46
+ `marketing/webhook/forward?provider=beehiiv&key=${process.env.BACKEND_MANAGER_WEBHOOK_KEY}`,
47
+ { id: 'should-not-process', event: 'subscription.unsubscribed', email: 'test@example.com' }
48
+ );
49
+
50
+ assert.isError(response, 404, 'Even with valid key, non-parent returns 404');
51
+ },
52
+ },
53
+ ],
54
+ };