| 1 | import { randomBytes } from 'node:crypto'; |
| 2 | import { createDatabase, type |
| 3 | import type { ConfigService |
| 4 | import { desc, eq |
| 5 | import { afterAll, beforeAll |
| 6 | |
| 7 | import { RepositoryAccessService } |
| 8 | import { WebhookBreakerService } |
| 9 | import { WebhookFanoutService } |
| 10 | import type { MailService |
| 11 | import { InvalidWebhookUrlError } |
| 12 | import { WebhooksService } |
| 13 | |
| 14 | const CONNECTION = process |
| 15 | const RUN = Date |
| 16 | const OWNER = ` |
| 17 | const USERNAME = ` |
| 18 | |
| 19 | // Touches only the user it creates; deleting that user cascades to the repository, its issue, endpoints and jobs. |
| 20 | describe.skipIf(!CONNECTION |
| 21 | let db: |
| 22 | let pool: |
| 23 | let fanout: |
| 24 | let webhooks: |
| 25 | let repository: |
| 26 | let issue: |
| 27 | const ref = |
| 28 | |
| 29 | const jobsFor = |
| 30 | db |
| 31 | .select() |
| 32 | .from(schema.deliveryJob |
| 33 | .where(eq(schema |
| 34 | .orderBy(desc(schema |
| 35 | |
| 36 | beforeAll(async () |
| 37 | ({ db, pool |
| 38 | const config = |
| 39 | get: (name: |
| 40 | ({ |
| 41 | WEBHOOK_SECRET_KEY: randomBytes(32) |
| 42 | WEB_APP_URL: 'https://ghost.test', |
| 43 | })[name], |
| 44 | } as unknown as |
| 45 | fanout = new |
| 46 | webhooks = new |
| 47 | db, |
| 48 | new RepositoryAccessService(db |
| 49 | fanout, |
| 50 | config, |
| 51 | ); |
| 52 | |
| 53 | await db.insert |
| 54 | id: OWNER, |
| 55 | name: 'Webhook owner', |
| 56 | email: `${USERNAME} |
| 57 | username: USERNAME, |
| 58 | emailVerified: true, |
| 59 | }); |
| 60 | [repository] = await |
| 61 | .insert(schema.repository |
| 62 | .values({ |
| 63 | name: 'app', |
| 64 | slug: 'app', |
| 65 | ownerId: OWNER, |
| 66 | visibility: 'public', |
| 67 | }) |
| 68 | .returning(); |
| 69 | [issue] = await |
| 70 | .insert(schema.issue |
| 71 | .values({ |
| 72 | repositoryId: repository.id |
| 73 | number: 1, |
| 74 | title: 'Bell count', |
| 75 | body: 'The bell should show a count', |
| 76 | authorId: OWNER, |
| 77 | }) |
| 78 | .returning(); |
| 79 | }); |
| 80 | |
| 81 | afterAll(async () |
| 82 | await db.delete |
| 83 | await pool?.end |
| 84 | }); |
| 85 | |
| 86 | it('creates an endpoint, returns its secret once, and pings it', |
| 87 | const created = |
| 88 | ...ref, |
| 89 | url: 'https://93.184.216.34/hook', |
| 90 | events: ['issue.opened', |
| 91 | }); |
| 92 | expect(created.secret |
| 93 | expect(created.events |
| 94 | |
| 95 | const [stored] |
| 96 | .select({ secret: |
| 97 | .from(schema.webhookEndpoint |
| 98 | .where(eq(schema |
| 99 | expect(stored.secret |
| 100 | expect(stored.secret |
| 101 | |
| 102 | const [ping] |
| 103 | expect(ping.kind |
| 104 | const payload = |
| 105 | expect(payload.event |
| 106 | expect(JSON.parse |
| 107 | repository: { full_name: ` |
| 108 | }); |
| 109 | |
| 110 | const listed = |
| 111 | expect(listed.webhooks |
| 112 | expect(listed.webhooks |
| 113 | }); |
| 114 | |
| 115 | it('refuses a URL that points inside the network', |
| 116 | await expect( |
| 117 | webhooks.create({ |
| 118 | ...ref, |
| 119 | url: 'http://127.0.0.1:9000', |
| 120 | events: ['issue.opened'], |
| 121 | }), |
| 122 | ).rejects.toBeInstanceOf( |
| 123 | }); |
| 124 | |
| 125 | it('fans an event out once to the endpoints that chose it', |
| 126 | const wanted = |
| 127 | ...ref, |
| 128 | url: 'https://93.184.216.34/wanted', |
| 129 | events: ['issue.opened'], |
| 130 | }); |
| 131 | const other = |
| 132 | ...ref, |
| 133 | url: 'https://93.184.216.34/other', |
| 134 | events: ['issue.closed'], |
| 135 | }); |
| 136 | const event = |
| 137 | id: `evt_whk_${RUN |
| 138 | type: 'issue.opened' as |
| 139 | repositoryId: repository.id |
| 140 | actorId: OWNER, |
| 141 | payload: { issueId: issue |
| 142 | createdAt: new Date |
| 143 | }; |
| 144 | |
| 145 | await fanout.handle |
| 146 | await fanout.handle |
| 147 | |
| 148 | const jobs = |
| 149 | (job) => |
| 150 | ); |
| 151 | expect(jobs).toHaveLength |
| 152 | const body = |
| 153 | expect(body).toMatchObject |
| 154 | event: 'issue.opened', |
| 155 | repository: { full_name: ` |
| 156 | sender: { username: USERNAME |
| 157 | issue: { |
| 158 | number: 1, |
| 159 | title: 'Bell count', |
| 160 | html_url: `https://ghost.test/${USERNAME |
| 161 | }, |
| 162 | }); |
| 163 | expect( |
| 164 | (await jobsFor(other |
| 165 | (job) => |
| 166 | ), |
| 167 | ).toEqual(['ping' |
| 168 | }); |
| 169 | |
| 170 | it('sends a push with its ref and commits', |
| 171 | const webhook = |
| 172 | ...ref, |
| 173 | url: 'https://93.184.216.34/push', |
| 174 | events: ['push'], |
| 175 | }); |
| 176 | const sha = |
| 177 | await fanout.handle |
| 178 | id: `evt_whk_push_${RUN |
| 179 | type: 'push', |
| 180 | repositoryId: repository.id |
| 181 | actorId: OWNER, |
| 182 | payload: { |
| 183 | ref: 'refs/heads/main', |
| 184 | before: '0'.repeat |
| 185 | after: sha, |
| 186 | commits: [ |
| 187 | { |
| 188 | sha, |
| 189 | message: 'Count the bell', |
| 190 | author: { name: ' |
| 191 | timestamp: '2026-09-29T12:00:00.000Z', |
| 192 | }, |
| 193 | ], |
| 194 | }, |
| 195 | createdAt: new Date |
| 196 | }); |
| 197 | |
| 198 | const [job] |
| 199 | (job) => |
| 200 | ); |
| 201 | expect(JSON.parse |
| 202 | event: 'push', |
| 203 | ref: 'refs/heads/main', |
| 204 | created: true, |
| 205 | deleted: false, |
| 206 | repository: { full_name: ` |
| 207 | sender: { username: USERNAME |
| 208 | commits: [ |
| 209 | { |
| 210 | sha, |
| 211 | message: 'Count the bell', |
| 212 | html_url: `https://ghost.test/${USERNAME |
| 213 | }, |
| 214 | ], |
| 215 | }); |
| 216 | }); |
| 217 | |
| 218 | it('turns off an endpoint that only failed for three days and emails the owner', |
| 219 | // The sweep covers every endpoint; only run where all of them are this test's. |
| 220 | const [others] |
| 221 | .select({ total: |
| 222 | .from(schema.webhookEndpoint |
| 223 | .where(ne(schema |
| 224 | if (others.total |
| 225 | |
| 226 | const endpoint = |
| 227 | const webhook = |
| 228 | ...ref, |
| 229 | url: `https://93.184.216.34/${path |
| 230 | events: ['issue.opened'], |
| 231 | }); |
| 232 | await db |
| 233 | .update(schema.webhookEndpoint |
| 234 | .set({ updatedAt: |
| 235 | .where(eq(schema |
| 236 | await db.insert |
| 237 | kind: 'webhook', |
| 238 | idempotencyKey: `webhook:test-breaker-${path |
| 239 | endpointId: webhook.id |
| 240 | payload: { event: ' |
| 241 | status: finished, |
| 242 | }); |
| 243 | return webhook.id |
| 244 | }; |
| 245 | const failing = |
| 246 | const healthy = |
| 247 | await db.insert |
| 248 | kind: 'webhook', |
| 249 | idempotencyKey: `webhook:test-breaker-healthy-dead-${RUN |
| 250 | endpointId: healthy, |
| 251 | payload: { event: ' |
| 252 | status: 'dead', |
| 253 | }); |
| 254 | |
| 255 | const sendWebhookDisabledEmail = |
| 256 | const breaker = |
| 257 | sendWebhookDisabledEmail, |
| 258 | } as unknown as |
| 259 | const disabled = |
| 260 | |
| 261 | expect(disabled.map |
| 262 | const [row] |
| 263 | .select() |
| 264 | .from(schema.webhookEndpoint |
| 265 | .where(eq(schema |
| 266 | expect(row).toMatchObject |
| 267 | active: false, |
| 268 | disabledReason: 'Every delivery failed for three days.', |
| 269 | }); |
| 270 | expect(sendWebhookDisabledEmail).toHaveBeenCalledWith |
| 271 | `${USERNAME}@example.com`, |
| 272 | expect.objectContaining({ |
| 273 | repository: `${USERNAME} |
| 274 | webhookId: failing, |
| 275 | }), |
| 276 | ); |
| 277 | |
| 278 | // turning it back on is a fresh start: the old failure does not turn it off again |
| 279 | await webhooks.update |
| 280 | expect(await breaker |
| 281 | }); |
| 282 | |
| 283 | it('turns a disabled endpoint back on and redelivers as a new delivery', |
| 284 | const webhook = |
| 285 | ...ref, |
| 286 | url: 'https://93.184.216.34/again', |
| 287 | events: ['issue.opened'], |
| 288 | }); |
| 289 | await db |
| 290 | .update(schema.webhookEndpoint |
| 291 | .set({ |
| 292 | active: false, |
| 293 | disabledReason: 'The endpoint responded 410 Gone.', |
| 294 | }) |
| 295 | .where(eq(schema |
| 296 | |
| 297 | const updated = |
| 298 | ...ref, |
| 299 | webhookId: webhook.id |
| 300 | active: true, |
| 301 | }); |
| 302 | expect(updated).toMatchObject |
| 303 | |
| 304 | const { deliveries |
| 305 | ...ref, |
| 306 | webhookId: webhook.id |
| 307 | }); |
| 308 | expect(deliveries).toHaveLength |
| 309 | await webhooks.redeliver |
| 310 | ...ref, |
| 311 | webhookId: webhook.id |
| 312 | deliveryId: deliveries[0] |
| 313 | }); |
| 314 | const after = |
| 315 | expect(after.deliveries |
| 316 | expect(after.deliveries |
| 317 | expect(after.deliveries |
| 318 | }); |
| 319 | }); |