backend-manager 5.9.7 → 5.9.8

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 (209) hide show
  1. package/package.json +5 -3
  2. package/templates/_.env +42 -0
  3. package/templates/_.gitignore +60 -0
  4. package/templates/backend-manager-config.json +249 -0
  5. package/templates/database.rules.json +83 -0
  6. package/templates/firebase.json +66 -0
  7. package/templates/firestore.indexes.json +4 -0
  8. package/templates/firestore.rules +112 -0
  9. package/templates/public/404.html +26 -0
  10. package/templates/public/index.html +24 -0
  11. package/templates/remoteconfig.template.json +1 -0
  12. package/templates/storage-lifecycle-config-1-day.json +9 -0
  13. package/templates/storage-lifecycle-config-30-days.json +9 -0
  14. package/templates/storage.rules +8 -0
  15. package/test/_init/accounts-validation.js +58 -0
  16. package/test/_legacy/ai/index.js +231 -0
  17. package/test/_legacy/ai/test.jpg +0 -0
  18. package/test/_legacy/ai/test.pdf +0 -0
  19. package/test/_legacy/index.js +31 -0
  20. package/test/_legacy/payment-resolver/chargebee/orders/refunded.json +0 -0
  21. package/test/_legacy/payment-resolver/chargebee/orders/unpaid.json +98 -0
  22. package/test/_legacy/payment-resolver/chargebee/subscriptions/active.json +578 -0
  23. package/test/_legacy/payment-resolver/chargebee/subscriptions/trial-in-trial.json +38 -0
  24. package/test/_legacy/payment-resolver/chargebee/subscriptions/trial-skipped-to-active.json +135 -0
  25. package/test/_legacy/payment-resolver/chargebee/subscriptions/trial-to-active-to-cancelled.json +42 -0
  26. package/test/_legacy/payment-resolver/chargebee/subscriptions/trial-to-active.json +44 -0
  27. package/test/_legacy/payment-resolver/chargebee/subscriptions/trial-to-cancelled.json +39 -0
  28. package/test/_legacy/payment-resolver/chargebee/subscriptions/trial-to-refund.json +143 -0
  29. package/test/_legacy/payment-resolver/chargebee/subscriptions/trial-to-suspended.json +48 -0
  30. package/test/_legacy/payment-resolver/coinbase/orders/regular.json +204 -0
  31. package/test/_legacy/payment-resolver/coinbase/subscriptions/cancelled.json +204 -0
  32. package/test/_legacy/payment-resolver/coinbase/subscriptions/paid-2.json +125 -0
  33. package/test/_legacy/payment-resolver/index.js +1558 -0
  34. package/test/_legacy/payment-resolver/paypal/orders/refunded.json +0 -0
  35. package/test/_legacy/payment-resolver/paypal/orders/regular.json +120 -0
  36. package/test/_legacy/payment-resolver/paypal/subscriptions/active-refund-previous-stmnt.json +323 -0
  37. package/test/_legacy/payment-resolver/paypal/subscriptions/active.json +192 -0
  38. package/test/_legacy/payment-resolver/paypal/subscriptions/trial-in-trial.json +125 -0
  39. package/test/_legacy/payment-resolver/paypal/subscriptions/trial-payment-not-complete.json +76 -0
  40. package/test/_legacy/payment-resolver/paypal/subscriptions/trial-payment-overdue-2.json +114 -0
  41. package/test/_legacy/payment-resolver/paypal/subscriptions/trial-payment-overdue.json +114 -0
  42. package/test/_legacy/payment-resolver/paypal/subscriptions/trial-to-active-to-cancelled.json +111 -0
  43. package/test/_legacy/payment-resolver/paypal/subscriptions/trial-to-active.json +132 -0
  44. package/test/_legacy/payment-resolver/paypal/subscriptions/trial-to-cancelled.json +107 -0
  45. package/test/_legacy/payment-resolver/paypal/subscriptions/trial-to-expired.json +91 -0
  46. package/test/_legacy/payment-resolver/paypal/subscriptions/trial-to-refund.json +147 -0
  47. package/test/_legacy/payment-resolver/paypal/subscriptions/trial-to-suspended.json +120 -0
  48. package/test/_legacy/payment-resolver/stripe/orders/refunded.json +0 -0
  49. package/test/_legacy/payment-resolver/stripe/orders/regular.json +91 -0
  50. package/test/_legacy/payment-resolver/stripe/subscriptions/active.json +421 -0
  51. package/test/_legacy/payment-resolver/stripe/subscriptions/trial-in-trial.json +349 -0
  52. package/test/_legacy/payment-resolver/stripe/subscriptions/trial-to-active-failed-authorization.json +430 -0
  53. package/test/_legacy/payment-resolver/stripe/subscriptions/trial-to-active.json +319 -0
  54. package/test/_legacy/payment-resolver/stripe/subscriptions/trial-to-cancelled.json +319 -0
  55. package/test/_legacy/payment-resolver/stripe/subscriptions/trial-to-refund.json +414 -0
  56. package/test/_legacy/payment-resolver/stripe/subscriptions/trial-to-suspended.json +319 -0
  57. package/test/_legacy/payment-resolver/stripe/subscriptions/unsure.json +339 -0
  58. package/test/_legacy/test-new +1178 -0
  59. package/test/_legacy/test.md +89 -0
  60. package/test/_legacy/usage.js +175 -0
  61. package/test/_legacy/user.js +31 -0
  62. package/test/ai/tools-live.js +170 -0
  63. package/test/boot/emulator-boots.js +37 -0
  64. package/test/content/blog-generate.js +160 -0
  65. package/test/email/campaign-send.js +45 -0
  66. package/test/email/consent-lifecycle.js +257 -0
  67. package/test/email/feedback-and-plain-send.js +52 -0
  68. package/test/email/fixtures/clean.json +30 -0
  69. package/test/email/fixtures/editorial.json +30 -0
  70. package/test/email/fixtures/field-report.json +53 -0
  71. package/test/email/marketing-lifecycle.js +132 -0
  72. package/test/email/newsletter-generate.js +819 -0
  73. package/test/email/newsletter-templates.js +491 -0
  74. package/test/email/templates.js +207 -0
  75. package/test/email/transactional-send.js +34 -0
  76. package/test/email/transactional.js +560 -0
  77. package/test/email/validation.js +710 -0
  78. package/test/events/auth-delete-race.js +201 -0
  79. package/test/events/payments/journey-payments-cancel-endpoint.js +100 -0
  80. package/test/events/payments/journey-payments-cancel.js +182 -0
  81. package/test/events/payments/journey-payments-discount.js +89 -0
  82. package/test/events/payments/journey-payments-failure.js +146 -0
  83. package/test/events/payments/journey-payments-legacy-product.js +149 -0
  84. package/test/events/payments/journey-payments-one-time-failure.js +111 -0
  85. package/test/events/payments/journey-payments-one-time.js +136 -0
  86. package/test/events/payments/journey-payments-plan-change.js +144 -0
  87. package/test/events/payments/journey-payments-refund-webhook.js +180 -0
  88. package/test/events/payments/journey-payments-suspend.js +181 -0
  89. package/test/events/payments/journey-payments-trial-cancel.js +130 -0
  90. package/test/events/payments/journey-payments-trial.js +171 -0
  91. package/test/events/payments/journey-payments-uid-resolution.js +132 -0
  92. package/test/events/payments/journey-payments-upgrade.js +135 -0
  93. package/test/fixtures/chargebee/invoice-one-time.json +27 -0
  94. package/test/fixtures/chargebee/subscription-active.json +44 -0
  95. package/test/fixtures/chargebee/subscription-cancelled.json +42 -0
  96. package/test/fixtures/chargebee/subscription-in-trial.json +41 -0
  97. package/test/fixtures/chargebee/subscription-legacy-plan.json +41 -0
  98. package/test/fixtures/chargebee/subscription-non-renewing.json +41 -0
  99. package/test/fixtures/chargebee/subscription-paused.json +42 -0
  100. package/test/fixtures/chargebee/webhook-payment-failed.json +51 -0
  101. package/test/fixtures/chargebee/webhook-subscription-created.json +47 -0
  102. package/test/fixtures/paypal/order-approved.json +62 -0
  103. package/test/fixtures/paypal/order-completed.json +110 -0
  104. package/test/fixtures/paypal/subscription-active.json +76 -0
  105. package/test/fixtures/paypal/subscription-cancelled.json +50 -0
  106. package/test/fixtures/paypal/subscription-suspended.json +65 -0
  107. package/test/fixtures/stripe/checkout-session-completed.json +130 -0
  108. package/test/fixtures/stripe/invoice-payment-failed.json +148 -0
  109. package/test/fixtures/stripe/invoice-subscription-payment-failed.json +28 -0
  110. package/test/fixtures/stripe/subscription-active.json +161 -0
  111. package/test/fixtures/stripe/subscription-canceled.json +161 -0
  112. package/test/fixtures/stripe/subscription-trialing.json +161 -0
  113. package/test/functions/admin/database-read.js +134 -0
  114. package/test/functions/admin/database-write.js +159 -0
  115. package/test/functions/admin/edit-post.js +366 -0
  116. package/test/functions/admin/firestore-query.js +207 -0
  117. package/test/functions/admin/firestore-read.js +129 -0
  118. package/test/functions/admin/firestore-write.js +105 -0
  119. package/test/functions/admin/get-stats.js +115 -0
  120. package/test/functions/admin/send-email.js +119 -0
  121. package/test/functions/admin/send-notification.js +208 -0
  122. package/test/functions/admin/write-repo-content.js +223 -0
  123. package/test/functions/general/add-marketing-contact.js +335 -0
  124. package/test/functions/general/fetch-post.js +54 -0
  125. package/test/functions/general/generate-uuid.js +114 -0
  126. package/test/functions/test/authenticate.js +64 -0
  127. package/test/functions/user/create-custom-token.js +73 -0
  128. package/test/functions/user/delete.js +135 -0
  129. package/test/functions/user/get-active-sessions.js +111 -0
  130. package/test/functions/user/get-subscription-info.js +99 -0
  131. package/test/functions/user/regenerate-api-keys.js +156 -0
  132. package/test/functions/user/resolve.js +52 -0
  133. package/test/functions/user/sign-out-all-sessions.js +94 -0
  134. package/test/functions/user/sign-up.js +252 -0
  135. package/test/functions/user/submit-feedback.js +104 -0
  136. package/test/functions/user/validate-settings.js +82 -0
  137. package/test/helpers/ai-request-payload.js +300 -0
  138. package/test/helpers/ai-test-provider.js +202 -0
  139. package/test/helpers/ai-tools-format.js +383 -0
  140. package/test/helpers/content/blog-auto-publisher.js +484 -0
  141. package/test/helpers/content/feed-parser.js +528 -0
  142. package/test/helpers/content/ghostii-blocks.js +134 -0
  143. package/test/helpers/content/ghostii-feed-integration.js +404 -0
  144. package/test/helpers/content/ghostii-write-article.js +243 -0
  145. package/test/helpers/environment.js +230 -0
  146. package/test/helpers/infer-contact.js +113 -0
  147. package/test/helpers/merge-line-files.js +271 -0
  148. package/test/helpers/payment/chargebee/parse-webhook.js +413 -0
  149. package/test/helpers/payment/chargebee/to-unified-one-time.js +147 -0
  150. package/test/helpers/payment/chargebee/to-unified-subscription.js +648 -0
  151. package/test/helpers/payment/paypal/parse-webhook.js +539 -0
  152. package/test/helpers/payment/paypal/to-unified-one-time.js +382 -0
  153. package/test/helpers/payment/paypal/to-unified-subscription.js +820 -0
  154. package/test/helpers/payment/stripe/parse-webhook.js +447 -0
  155. package/test/helpers/payment/stripe/to-unified-one-time.js +306 -0
  156. package/test/helpers/payment/stripe/to-unified-subscription.js +708 -0
  157. package/test/helpers/sanitize.js +222 -0
  158. package/test/helpers/slugify.js +394 -0
  159. package/test/helpers/storage.js +194 -0
  160. package/test/helpers/user.js +755 -0
  161. package/test/helpers/webhook-forward.js +418 -0
  162. package/test/mcp/discovery.js +53 -0
  163. package/test/mcp/oauth.js +161 -0
  164. package/test/mcp/protocol.js +268 -0
  165. package/test/mcp/roles.js +188 -0
  166. package/test/mcp/utils.js +245 -0
  167. package/test/routes/admin/create-post.js +364 -0
  168. package/test/routes/admin/database.js +131 -0
  169. package/test/routes/admin/deduplicate-image-alts.js +190 -0
  170. package/test/routes/admin/email.js +114 -0
  171. package/test/routes/admin/firestore-query.js +204 -0
  172. package/test/routes/admin/firestore.js +127 -0
  173. package/test/routes/admin/infer-contact.js +218 -0
  174. package/test/routes/admin/notification.js +198 -0
  175. package/test/routes/admin/post-resize-image.js +184 -0
  176. package/test/routes/admin/post.js +368 -0
  177. package/test/routes/admin/repo-content.js +223 -0
  178. package/test/routes/admin/stats.js +112 -0
  179. package/test/routes/content/post.js +54 -0
  180. package/test/routes/general/uuid.js +115 -0
  181. package/test/routes/marketing/campaign.js +125 -0
  182. package/test/routes/marketing/contact.js +370 -0
  183. package/test/routes/marketing/email-preferences.js +292 -0
  184. package/test/routes/marketing/push-send.js +32 -0
  185. package/test/routes/marketing/webhook-forward.js +61 -0
  186. package/test/routes/marketing/webhook.js +639 -0
  187. package/test/routes/payments/cancel.js +163 -0
  188. package/test/routes/payments/discount.js +80 -0
  189. package/test/routes/payments/dispute-alert.js +320 -0
  190. package/test/routes/payments/intent.js +336 -0
  191. package/test/routes/payments/portal.js +92 -0
  192. package/test/routes/payments/refund.js +179 -0
  193. package/test/routes/payments/trial-eligibility.js +71 -0
  194. package/test/routes/payments/webhook.js +107 -0
  195. package/test/routes/test/authenticate.js +59 -0
  196. package/test/routes/test/schema.js +554 -0
  197. package/test/routes/test/usage.js +342 -0
  198. package/test/routes/user/api-keys.js +156 -0
  199. package/test/routes/user/delete.js +136 -0
  200. package/test/routes/user/feedback.js +104 -0
  201. package/test/routes/user/sessions.js +199 -0
  202. package/test/routes/user/settings-validate.js +82 -0
  203. package/test/routes/user/signup.js +542 -0
  204. package/test/routes/user/subscription.js +99 -0
  205. package/test/routes/user/token.js +73 -0
  206. package/test/routes/user/user.js +157 -0
  207. package/test/rules/notifications.js +410 -0
  208. package/test/rules/payments-carts.js +371 -0
  209. package/test/rules/user.js +396 -0
@@ -0,0 +1,368 @@
1
+ /**
2
+ * Test: PUT /admin/post
3
+ * Tests the admin edit post endpoint
4
+ * Edits blog post content in a GitHub repository
5
+ * Requires admin/blogger role, GitHub API key, and repo_website config
6
+ *
7
+ * This is a suite because we need to:
8
+ * 1. Create a test post file via Octokit
9
+ * 2. Wait for GitHub to index it (for code search)
10
+ * 3. Edit it via the API
11
+ * 4. Clean up the test file
12
+ */
13
+ const { Octokit } = require('@octokit/rest');
14
+
15
+ // Test post configuration
16
+ const TEST_POST_SLUG = 'bem-test-post';
17
+ const TEST_POST_ID = Date.now();
18
+
19
+ // Generate frontmatter for the test post
20
+ function generatePostContent(body) {
21
+ const frontmatter = `layout: blueprint/blog/post
22
+
23
+ meta:
24
+ title: BEM Test Post
25
+ description: This is a test post created by BEM test suite.
26
+ og:image: ''
27
+
28
+ post:
29
+ title: BEM Test Post
30
+ id: ${TEST_POST_ID}
31
+ description: This is a test post created by BEM test suite.
32
+ author: guest
33
+ tags: []
34
+ categories: []`;
35
+
36
+ return `---\n${frontmatter}\n---\n\n${body}`;
37
+ }
38
+
39
+ module.exports = {
40
+ description: 'Admin edit post on GitHub',
41
+ type: 'suite',
42
+ timeout: 300000, // 5 minutes total for the suite
43
+ skip: !process.env.TEST_EXTENDED_MODE ? 'TEST_EXTENDED_MODE env var not set (skipping GitHub edit tests)' : false,
44
+
45
+ tests: [
46
+ // Test 1: Missing URL returns 400 error
47
+ {
48
+ name: 'missing-url-rejected',
49
+ auth: 'admin',
50
+ timeout: 15000,
51
+
52
+ async run({ http, assert }) {
53
+ const response = await http.put('backend-manager/admin/post', {
54
+ body: 'Test content',
55
+ });
56
+
57
+ assert.isError(response, 400, 'Missing URL should return 400');
58
+ },
59
+ },
60
+
61
+ // Test 2: Missing body returns 400 error
62
+ {
63
+ name: 'missing-body-rejected',
64
+ auth: 'admin',
65
+ timeout: 15000,
66
+
67
+ async run({ http, assert }) {
68
+ const response = await http.put('backend-manager/admin/post', {
69
+ url: 'https://example.com/blog/test-post',
70
+ });
71
+
72
+ assert.isError(response, 400, 'Missing body should return 400');
73
+ },
74
+ },
75
+
76
+ // Test 3: Non-existent post returns 404
77
+ {
78
+ name: 'nonexistent-post-returns-404',
79
+ auth: 'admin',
80
+ timeout: 60000,
81
+
82
+ async run({ http, assert, config }) {
83
+ const response = await http.put('backend-manager/admin/post', {
84
+ url: `https://${config.domain}/blog/nonexistent-test-post-12345`,
85
+ body: 'This is test content from BEM test suite.',
86
+ });
87
+
88
+ assert.isError(response, 404, 'Non-existent post should return 404');
89
+ },
90
+ },
91
+
92
+ // Test 4: Non-existent repo returns 404
93
+ // PUT first calls content/post to fetch the existing post; with a unique URL that's never been
94
+ // created, the fetch itself returns 404 before we ever try to push to the nonexistent repo.
95
+ {
96
+ name: 'nonexistent-repo-returns-404',
97
+ auth: 'admin',
98
+ timeout: 30000,
99
+
100
+ async run({ http, assert }) {
101
+ const response = await http.put('backend-manager/admin/post', {
102
+ url: `https://example.com/blog/never-created-${Date.now()}`,
103
+ body: 'Test content',
104
+ githubUser: 'nonexistent-user-12345',
105
+ githubRepo: 'nonexistent-repo-12345',
106
+ });
107
+
108
+ assert.isError(response, 404, 'Non-existent repo or post should return 404');
109
+ },
110
+ },
111
+
112
+ // Test 5: Create a test post via Octokit
113
+ {
114
+ name: 'create-test-post',
115
+ auth: 'admin',
116
+ timeout: 60000,
117
+ skip: !process.env.GH_TOKEN ? 'GH_TOKEN env var not set' : false,
118
+
119
+ async run({ assert, state, config }) {
120
+ if (!config.github?.repo_website) {
121
+ assert.fail('githubRepoWebsite not configured');
122
+ return;
123
+ }
124
+
125
+ const octokit = new Octokit({ auth: process.env.GH_TOKEN });
126
+
127
+ // Parse owner/repo from githubRepoWebsite
128
+ const repoMatch = config.github?.repo_website.match(/github\.com\/([^/]+)\/([^/]+)/);
129
+ if (!repoMatch) {
130
+ assert.fail('Could not parse githubRepoWebsite');
131
+ return;
132
+ }
133
+
134
+ state.owner = repoMatch[1];
135
+ state.repo = repoMatch[2];
136
+
137
+ // Build the post path (matches the structure expected by fetch-post)
138
+ const date = new Date().toISOString().split('T')[0]; // YYYY-MM-DD
139
+ const year = date.split('-')[0];
140
+ state.postPath = `src/_posts/${year}/guest/${date}-${TEST_POST_SLUG}.md`;
141
+ state.postSlug = TEST_POST_SLUG;
142
+
143
+ // Create the post content
144
+ const originalBody = 'This is the original content of the test post.\n\nIt will be edited by the BEM test suite.';
145
+ state.originalBody = originalBody;
146
+ const content = generatePostContent(originalBody);
147
+
148
+ // Check if the file already exists (from a previous failed run)
149
+ let existingSha;
150
+ try {
151
+ const existing = await octokit.rest.repos.getContent({
152
+ owner: state.owner,
153
+ repo: state.repo,
154
+ path: state.postPath,
155
+ });
156
+ existingSha = existing.data.sha;
157
+ } catch (e) {
158
+ // File doesn't exist, that's fine
159
+ }
160
+
161
+ // Create or update the file via Octokit
162
+ const createResult = await octokit.rest.repos.createOrUpdateFileContents({
163
+ owner: state.owner,
164
+ repo: state.repo,
165
+ path: state.postPath,
166
+ message: `🧪 BEM test: create test post for edit-post test`,
167
+ content: Buffer.from(content).toString('base64'),
168
+ sha: existingSha, // Include SHA if file exists
169
+ }).catch(e => e);
170
+
171
+ if (createResult instanceof Error) {
172
+ assert.fail(`Failed to create test post: ${createResult.message}`);
173
+ return;
174
+ }
175
+
176
+ assert.ok(createResult.data.content.sha, 'Post should be created with a SHA');
177
+ state.postSha = createResult.data.content.sha;
178
+ },
179
+ },
180
+
181
+ // Test 6: Verify the file exists via GitHub API (not code search)
182
+ {
183
+ name: 'verify-file-exists',
184
+ auth: 'admin',
185
+ timeout: 30000,
186
+ skip: !process.env.GH_TOKEN ? 'GH_TOKEN env var not set' : false,
187
+
188
+ async run({ assert, state, config }) {
189
+ if (!state.postPath) {
190
+ return; // Previous test didn't run
191
+ }
192
+
193
+ state.postUrl = `https://${config.domain}/blog/${state.postSlug}`;
194
+
195
+ const octokit = new Octokit({ auth: process.env.GH_TOKEN });
196
+
197
+ // Verify the file exists via direct API (not code search)
198
+ const { data: fileData } = await octokit.rest.repos.getContent({
199
+ owner: state.owner,
200
+ repo: state.repo,
201
+ path: state.postPath,
202
+ });
203
+
204
+ assert.ok(fileData.sha, 'File should exist in GitHub repo');
205
+ state.currentSha = fileData.sha;
206
+ },
207
+ },
208
+
209
+ // Test 7: Edit the test post via PUT /admin/post
210
+ // NOTE: This may fail if GitHub's code search hasn't indexed the file yet
211
+ // The edit-post API uses fetch-post which relies on code search
212
+ {
213
+ name: 'edit-test-post',
214
+ auth: 'admin',
215
+ timeout: 60000,
216
+ skip: !process.env.GH_TOKEN ? 'GH_TOKEN env var not set' : false,
217
+
218
+ async run({ http, assert, state }) {
219
+ if (!state.postUrl) {
220
+ return; // Previous tests didn't run
221
+ }
222
+
223
+ const newBody = 'This content was EDITED by the BEM test suite.\n\nThe edit was successful!';
224
+ state.editedBody = newBody;
225
+
226
+ const response = await http.put('backend-manager/admin/post', {
227
+ url: state.postUrl,
228
+ body: newBody,
229
+ });
230
+
231
+ // If fetch-post can't find the file (not indexed yet), it returns 404
232
+ // This is a known limitation - the file exists but code search hasn't indexed it
233
+ if (response.status === 404 && response.error?.includes('not found')) {
234
+ state.editSkipped = true;
235
+ assert.ok(true, 'Edit skipped - GitHub code search has not indexed the file yet (this is expected for new files)');
236
+ return;
237
+ }
238
+
239
+ assert.isSuccess(response, 'Edit post should succeed');
240
+ assert.hasProperty(response, 'data.url', 'Response should have url');
241
+ },
242
+ },
243
+
244
+ // Test 8: Verify the edit was applied (using Octokit directly)
245
+ {
246
+ name: 'verify-edit',
247
+ auth: 'admin',
248
+ timeout: 30000,
249
+ skip: !process.env.GH_TOKEN ? 'GH_TOKEN env var not set' : false,
250
+
251
+ async run({ assert, state }) {
252
+ if (!state.postPath || state.editSkipped) {
253
+ assert.ok(true, 'Skipping verify - edit was not performed');
254
+ return;
255
+ }
256
+
257
+ const octokit = new Octokit({ auth: process.env.GH_TOKEN });
258
+
259
+ // Fetch the file directly from GitHub
260
+ const { data: fileData } = await octokit.rest.repos.getContent({
261
+ owner: state.owner,
262
+ repo: state.repo,
263
+ path: state.postPath,
264
+ });
265
+
266
+ // Decode the content
267
+ const content = Buffer.from(fileData.content, 'base64').toString();
268
+
269
+ assert.ok(
270
+ content.includes('EDITED by the BEM test suite'),
271
+ 'Post content should contain the edited text'
272
+ );
273
+ },
274
+ },
275
+
276
+ // --- Auth rejection tests (at end per convention) ---
277
+ {
278
+ name: 'unauthenticated-rejected',
279
+ auth: 'none',
280
+ timeout: 15000,
281
+
282
+ async run({ http, assert }) {
283
+ const response = await http.put('backend-manager/admin/post', {
284
+ url: 'https://example.com/blog/test-post',
285
+ body: 'Test content',
286
+ });
287
+
288
+ assert.isError(response, 401, 'Edit post should fail without authentication');
289
+ },
290
+ },
291
+
292
+ {
293
+ name: 'non-admin-rejected',
294
+ auth: 'basic',
295
+ timeout: 15000,
296
+
297
+ async run({ http, assert }) {
298
+ const response = await http.put('backend-manager/admin/post', {
299
+ url: 'https://example.com/blog/test-post',
300
+ body: 'Test content',
301
+ });
302
+
303
+ assert.isError(response, 403, 'Edit post should fail for non-admin user');
304
+ },
305
+ },
306
+
307
+ // Cleanup - delete test post and cancel workflows
308
+ {
309
+ name: 'cleanup',
310
+ auth: 'admin',
311
+ timeout: 60000,
312
+
313
+ async run({ state }) {
314
+ if (!process.env.GH_TOKEN || !state.postPath) {
315
+ return;
316
+ }
317
+
318
+ const octokit = new Octokit({ auth: process.env.GH_TOKEN });
319
+
320
+ // Get the current SHA (may have changed after edit)
321
+ try {
322
+ const { data: fileData } = await octokit.rest.repos.getContent({
323
+ owner: state.owner,
324
+ repo: state.repo,
325
+ path: state.postPath,
326
+ });
327
+
328
+ // Delete the test post
329
+ await octokit.rest.repos.deleteFile({
330
+ owner: state.owner,
331
+ repo: state.repo,
332
+ path: state.postPath,
333
+ message: `🧹 BEM test cleanup: delete test post`,
334
+ sha: fileData.sha,
335
+ });
336
+ } catch (e) {
337
+ // File might not exist, ignore
338
+ }
339
+
340
+ // Cancel any running workflows triggered by our test commits
341
+ try {
342
+ const { data: runs } = await octokit.rest.actions.listWorkflowRunsForRepo({
343
+ owner: state.owner,
344
+ repo: state.repo,
345
+ status: 'in_progress',
346
+ per_page: 10,
347
+ });
348
+
349
+ for (const run of runs.workflow_runs) {
350
+ if (run.head_commit?.message?.includes('BEM test')) {
351
+ try {
352
+ await octokit.rest.actions.cancelWorkflowRun({
353
+ owner: state.owner,
354
+ repo: state.repo,
355
+ run_id: run.id,
356
+ });
357
+ } catch (e) {
358
+ // May already be completed, ignore
359
+ }
360
+ }
361
+ }
362
+ } catch (e) {
363
+ // Workflow cancellation failed, not critical
364
+ }
365
+ },
366
+ },
367
+ ],
368
+ };
@@ -0,0 +1,223 @@
1
+ /**
2
+ * Test: POST /admin/repo/content
3
+ * Tests the admin write repo content endpoint
4
+ * Writes arbitrary content to a GitHub repository
5
+ * Requires admin/blogger role, GitHub API key, and repo_website config
6
+ *
7
+ * IMPORTANT: These tests require GH_TOKEN and github.repo_website to be configured.
8
+ * If GitHub is not configured, the tests will fail.
9
+ *
10
+ * This is a suite because we need to clean up created files and cancel workflows after tests.
11
+ */
12
+ const { Octokit } = require('@octokit/rest');
13
+
14
+ // Test file paths that will be created and need cleanup
15
+ const TEST_FILES = [
16
+ '_test/bem-write-test.txt',
17
+ '_test/type-test.txt',
18
+ ];
19
+
20
+ module.exports = {
21
+ description: 'Admin write content to GitHub repo',
22
+ type: 'suite',
23
+ timeout: 120000,
24
+ skip: !process.env.TEST_EXTENDED_MODE ? 'TEST_EXTENDED_MODE env var not set (skipping GitHub write tests)' : false,
25
+
26
+ tests: [
27
+ // Test 1: Missing path returns 400 error
28
+ {
29
+ name: 'missing-path-rejected',
30
+ auth: 'admin',
31
+ timeout: 15000,
32
+
33
+ async run({ http, assert }) {
34
+ const response = await http.post('backend-manager/admin/repo/content', {
35
+ content: 'Test content',
36
+ });
37
+
38
+ assert.isError(response, 400, 'Missing path should return 400');
39
+ },
40
+ },
41
+
42
+ // Test 2: Missing content returns 400 error
43
+ {
44
+ name: 'missing-content-rejected',
45
+ auth: 'admin',
46
+ timeout: 15000,
47
+
48
+ async run({ http, assert }) {
49
+ const response = await http.post('backend-manager/admin/repo/content', {
50
+ path: '_test/bem-test-file.txt',
51
+ });
52
+
53
+ assert.isError(response, 400, 'Missing content should return 400');
54
+ },
55
+ },
56
+
57
+ // Test 3: Admin can write content
58
+ {
59
+ name: 'admin-can-write-content',
60
+ auth: 'admin',
61
+ timeout: 60000,
62
+
63
+ async run({ http, assert, state }) {
64
+ const uniqueContent = `BEM Test - ${new Date().toISOString()}`;
65
+ const response = await http.post('backend-manager/admin/repo/content', {
66
+ path: '_test/bem-write-test.txt',
67
+ content: uniqueContent,
68
+ });
69
+
70
+ assert.isSuccess(response, 'Admin should be able to write content');
71
+ assert.hasProperty(response, 'data.path', 'Response should contain path');
72
+ assert.equal(response.data.path, '_test/bem-write-test.txt', 'Path should match');
73
+
74
+ // Store for cleanup
75
+ state.filesCreated = state.filesCreated || [];
76
+ state.filesCreated.push('_test/bem-write-test.txt');
77
+ },
78
+ },
79
+
80
+ // Test 4: Content type defaults to text
81
+ {
82
+ name: 'content-type-default',
83
+ auth: 'admin',
84
+ timeout: 30000,
85
+
86
+ async run({ http, assert, state }) {
87
+ const response = await http.post('backend-manager/admin/repo/content', {
88
+ path: '_test/type-test.txt',
89
+ content: 'Test content with default type',
90
+ });
91
+
92
+ assert.isSuccess(response, 'Write with default type should succeed');
93
+ assert.hasProperty(response, 'data.type', 'Response should contain type');
94
+ assert.equal(response.data.type, 'text', 'Default type should be text');
95
+
96
+ // Store for cleanup
97
+ state.filesCreated = state.filesCreated || [];
98
+ state.filesCreated.push('_test/type-test.txt');
99
+ },
100
+ },
101
+
102
+ // Test 5: Non-existent repo returns 404
103
+ {
104
+ name: 'nonexistent-repo-returns-404',
105
+ auth: 'admin',
106
+ timeout: 30000,
107
+
108
+ async run({ http, assert }) {
109
+ const response = await http.post('backend-manager/admin/repo/content', {
110
+ path: '_test/custom-repo-test.txt',
111
+ content: 'Test content',
112
+ githubUser: 'nonexistent-user-12345',
113
+ githubRepo: 'nonexistent-repo-12345',
114
+ });
115
+
116
+ assert.isError(response, 404, 'Non-existent repo should return 404');
117
+ },
118
+ },
119
+
120
+ // --- Auth rejection tests (at end per convention) ---
121
+ {
122
+ name: 'unauthenticated-rejected',
123
+ auth: 'none',
124
+ timeout: 15000,
125
+
126
+ async run({ http, assert }) {
127
+ const response = await http.post('backend-manager/admin/repo/content', {
128
+ path: '_test/bem-test-file.txt',
129
+ content: 'Test content',
130
+ });
131
+
132
+ assert.isError(response, 401, 'Write repo content should fail without authentication');
133
+ },
134
+ },
135
+
136
+ {
137
+ name: 'non-admin-rejected',
138
+ auth: 'basic',
139
+ timeout: 15000,
140
+
141
+ async run({ http, assert }) {
142
+ const response = await http.post('backend-manager/admin/repo/content', {
143
+ path: '_test/bem-test-file.txt',
144
+ content: 'Test content',
145
+ });
146
+
147
+ assert.isError(response, 403, 'Write repo content should fail for non-admin user');
148
+ },
149
+ },
150
+
151
+ // Cleanup - delete test files and cancel workflows
152
+ {
153
+ name: 'cleanup',
154
+ auth: 'admin',
155
+ timeout: 60000,
156
+
157
+ async run({ state, config }) {
158
+ if (!process.env.GH_TOKEN || !config.github?.repo_website) {
159
+ return;
160
+ }
161
+
162
+ const octokit = new Octokit({ auth: process.env.GH_TOKEN });
163
+
164
+ // Parse owner/repo from githubRepoWebsite (e.g., 'https://github.com/owner/repo')
165
+ const repoMatch = config.github?.repo_website.match(/github\.com\/([^/]+)\/([^/]+)/);
166
+ if (!repoMatch) {
167
+ return;
168
+ }
169
+ const owner = repoMatch[1];
170
+ const repo = repoMatch[2];
171
+
172
+ // Delete test files
173
+ const filesToDelete = state.filesCreated || TEST_FILES;
174
+ for (const filePath of filesToDelete) {
175
+ try {
176
+ const { data: fileData } = await octokit.rest.repos.getContent({
177
+ owner,
178
+ repo,
179
+ path: filePath,
180
+ });
181
+
182
+ await octokit.rest.repos.deleteFile({
183
+ owner,
184
+ repo,
185
+ path: filePath,
186
+ message: `🧹 BEM test cleanup: ${filePath}`,
187
+ sha: fileData.sha,
188
+ });
189
+ } catch (e) {
190
+ // File might not exist, ignore
191
+ }
192
+ }
193
+
194
+ // Cancel running workflows triggered by our test commits
195
+ try {
196
+ const { data: runs } = await octokit.rest.actions.listWorkflowRunsForRepo({
197
+ owner,
198
+ repo,
199
+ status: 'in_progress',
200
+ per_page: 10,
201
+ });
202
+
203
+ for (const run of runs.workflow_runs) {
204
+ if (run.head_commit?.message?.includes('admin:write-repo-content')
205
+ || run.head_commit?.message?.includes('BEM test cleanup')) {
206
+ try {
207
+ await octokit.rest.actions.cancelWorkflowRun({
208
+ owner,
209
+ repo,
210
+ run_id: run.id,
211
+ });
212
+ } catch (e) {
213
+ // May already be completed, ignore
214
+ }
215
+ }
216
+ }
217
+ } catch (e) {
218
+ // Workflow cancellation failed, not critical
219
+ }
220
+ },
221
+ },
222
+ ],
223
+ };
@@ -0,0 +1,112 @@
1
+ /**
2
+ * Test: GET /admin/stats
3
+ * Tests the admin get stats endpoint
4
+ * Requires admin authentication
5
+ */
6
+ module.exports = {
7
+ description: 'Admin get stats operation',
8
+ type: 'group',
9
+ tests: [
10
+ // Test 1: Admin can get stats
11
+ {
12
+ name: 'admin-get-stats-succeeds',
13
+ auth: 'admin',
14
+ timeout: 30000,
15
+
16
+ async run({ http, assert }) {
17
+ const response = await http.get('backend-manager/admin/stats', {});
18
+
19
+ assert.isSuccess(response, 'Get stats should succeed with admin auth');
20
+ assert.ok(
21
+ typeof response.data === 'object',
22
+ 'Response data should be an object'
23
+ );
24
+ },
25
+ },
26
+
27
+ // Test 2: Stats contains users field
28
+ {
29
+ name: 'stats-contains-users',
30
+ auth: 'admin',
31
+ timeout: 30000,
32
+
33
+ async run({ http, assert }) {
34
+ const response = await http.get('backend-manager/admin/stats', {});
35
+
36
+ assert.isSuccess(response, 'Get stats should succeed');
37
+ assert.hasProperty(response, 'data.users', 'Stats should contain users field');
38
+ assert.hasProperty(response, 'data.users.total', 'Users should have total field');
39
+ assert.ok(
40
+ typeof response.data.users.total === 'number',
41
+ 'users.total should be a number'
42
+ );
43
+ },
44
+ },
45
+
46
+ // Test 3: Stats with update flag refreshes data
47
+ {
48
+ name: 'stats-with-update',
49
+ auth: 'admin',
50
+ timeout: 60000,
51
+
52
+ async run({ http, assert }) {
53
+ const response = await http.get('backend-manager/admin/stats', {
54
+ update: { users: true },
55
+ });
56
+
57
+ assert.isSuccess(response, 'Get stats with update should succeed');
58
+ assert.hasProperty(response, 'data.users.total', 'Updated stats should have users.total');
59
+ assert.ok(
60
+ response.data.users.total >= 0,
61
+ 'users.total should be a non-negative number'
62
+ );
63
+ },
64
+ },
65
+
66
+ // Test 4: Stats contains metadata
67
+ {
68
+ name: 'stats-has-metadata',
69
+ auth: 'admin',
70
+ timeout: 30000,
71
+
72
+ async run({ http, assert }) {
73
+ await http.get('backend-manager/admin/stats', {
74
+ update: { users: true },
75
+ });
76
+
77
+ const response = await http.get('backend-manager/admin/stats', {});
78
+
79
+ assert.isSuccess(response, 'Get stats should succeed');
80
+ if (response.data.metadata) {
81
+ assert.hasProperty(response, 'data.metadata.updated', 'Metadata should have updated timestamp');
82
+ }
83
+ },
84
+ },
85
+
86
+ // Test 5: Unauthenticated request fails
87
+ {
88
+ name: 'unauthenticated-rejected',
89
+ auth: 'none',
90
+ timeout: 15000,
91
+
92
+ async run({ http, assert }) {
93
+ const response = await http.get('backend-manager/admin/stats', {});
94
+
95
+ assert.isError(response, 401, 'Get stats should fail without authentication');
96
+ },
97
+ },
98
+
99
+ // Test 6: Non-admin user fails
100
+ {
101
+ name: 'non-admin-rejected',
102
+ auth: 'user',
103
+ timeout: 15000,
104
+
105
+ async run({ http, assert }) {
106
+ const response = await http.get('backend-manager/admin/stats', {});
107
+
108
+ assert.isError(response, 403, 'Get stats should fail for non-admin user');
109
+ },
110
+ },
111
+ ],
112
+ };