| 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 { WebhookFanoutService } |
| 9 | import { InvalidWebhookUrlError } |
| 10 | import { WebhooksService } |
| 11 | |
| 12 | const CONNECTION = process |
| 13 | const RUN = Date |
| 14 | const OWNER = ` |
| 15 | const USERNAME = ` |
| 16 | |
| 17 | // Touches only the user it creates; deleting that user cascades to the repository, its issue, endpoints and jobs. |
| 18 | describe.skipIf(!CONNECTION |
| 19 | let db: |
| 20 | let pool: |
| 21 | let fanout: |
| 22 | let webhooks: |
| 23 | let repository: |
| 24 | let issue: |
| 25 | const ref = |
| 26 | |
| 27 | const jobsFor = |
| 28 | db |
| 29 | .select() |
| 30 | .from(schema.deliveryJob |
| 31 | .where(eq(schema |
| 32 | .orderBy(desc(schema |
| 33 | |
| 34 | beforeAll(async () |
| 35 | ({ db, pool |
| 36 | const config = |
| 37 | get: (name: |
| 38 | ({ |
| 39 | WEBHOOK_SECRET_KEY: randomBytes(32) |
| 40 | WEB_APP_URL: 'https://ghost.test', |
| 41 | })[name], |
| 42 | } as unknown as |
| 43 | fanout = new |
| 44 | webhooks = new |
| 45 | db, |
| 46 | new RepositoryAccessService(db |
| 47 | fanout, |
| 48 | config, |
| 49 | ); |
| 50 | |
| 51 | await db.insert |
| 52 | id: OWNER, |
| 53 | name: 'Webhook owner', |
| 54 | email: `${USERNAME} |
| 55 | username: USERNAME, |
| 56 | emailVerified: true, |
| 57 | }); |
| 58 | [repository] = await |
| 59 | .insert(schema.repository |
| 60 | .values({ |
| 61 | name: 'app', |
| 62 | slug: 'app', |
| 63 | ownerId: OWNER, |
| 64 | visibility: 'public', |
| 65 | }) |
| 66 | .returning(); |
| 67 | [issue] = await |
| 68 | .insert(schema.issue |
| 69 | .values({ |
| 70 | repositoryId: repository.id |
| 71 | number: 1, |
| 72 | title: 'Bell count', |
| 73 | body: 'The bell should show a count', |
| 74 | authorId: OWNER, |
| 75 | }) |
| 76 | .returning(); |
| 77 | }); |
| 78 | |
| 79 | afterAll(async () |
| 80 | await db.delete |
| 81 | await pool?.end |
| 82 | }); |
| 83 | |
| 84 | it('creates an endpoint, returns its secret once, and pings it', |
| 85 | const created = |
| 86 | ...ref, |
| 87 | url: 'https://93.184.216.34/hook', |
| 88 | events: ['issue.opened', |
| 89 | }); |
| 90 | expect(created.secret |
| 91 | expect(created.events |
| 92 | |
| 93 | const [stored] |
| 94 | .select({ secret: |
| 95 | .from(schema.webhookEndpoint |
| 96 | .where(eq(schema |
| 97 | expect(stored.secret |
| 98 | expect(stored.secret |
| 99 | |
| 100 | const [ping] |
| 101 | expect(ping.kind |
| 102 | const payload = |
| 103 | expect(payload.event |
| 104 | expect(JSON.parse |
| 105 | repository: { full_name: ` |
| 106 | }); |
| 107 | |
| 108 | const listed = |
| 109 | expect(listed.webhooks |
| 110 | expect(listed.webhooks |
| 111 | }); |
| 112 | |
| 113 | it('refuses a URL that points inside the network', |
| 114 | await expect( |
| 115 | webhooks.create({ |
| 116 | ...ref, |
| 117 | url: 'http://127.0.0.1:9000', |
| 118 | events: ['issue.opened'], |
| 119 | }), |
| 120 | ).rejects.toBeInstanceOf( |
| 121 | }); |
| 122 | |
| 123 | it('fans an event out once to the endpoints that chose it', |
| 124 | const wanted = |
| 125 | ...ref, |
| 126 | url: 'https://93.184.216.34/wanted', |
| 127 | events: ['issue.opened'], |
| 128 | }); |
| 129 | const other = |
| 130 | ...ref, |
| 131 | url: 'https://93.184.216.34/other', |
| 132 | events: ['issue.closed'], |
| 133 | }); |
| 134 | const event = |
| 135 | id: `evt_whk_${RUN |
| 136 | type: 'issue.opened' as |
| 137 | repositoryId: repository.id |
| 138 | actorId: OWNER, |
| 139 | payload: { issueId: issue |
| 140 | createdAt: new Date |
| 141 | }; |
| 142 | |
| 143 | await fanout.handle |
| 144 | await fanout.handle |
| 145 | |
| 146 | const jobs = |
| 147 | (job) => |
| 148 | ); |
| 149 | expect(jobs).toHaveLength |
| 150 | const body = |
| 151 | expect(body).toMatchObject |
| 152 | event: 'issue.opened', |
| 153 | repository: { full_name: ` |
| 154 | sender: { username: USERNAME |
| 155 | issue: { |
| 156 | number: 1, |
| 157 | title: 'Bell count', |
| 158 | html_url: `https://ghost.test/${USERNAME |
| 159 | }, |
| 160 | }); |
| 161 | expect( |
| 162 | (await jobsFor(other |
| 163 | (job) => |
| 164 | ), |
| 165 | ).toEqual(['ping' |
| 166 | }); |
| 167 | |
| 168 | it('sends a push with its ref and commits', |
| 169 | const webhook = |
| 170 | ...ref, |
| 171 | url: 'https://93.184.216.34/push', |
| 172 | events: ['push'], |
| 173 | }); |
| 174 | const sha = |
| 175 | await fanout.handle |
| 176 | id: `evt_whk_push_${RUN |
| 177 | type: 'push', |
| 178 | repositoryId: repository.id |
| 179 | actorId: OWNER, |
| 180 | payload: { |
| 181 | ref: 'refs/heads/main', |
| 182 | before: '0'.repeat |
| 183 | after: sha, |
| 184 | commits: [ |
| 185 | { |
| 186 | sha, |
| 187 | message: 'Count the bell', |
| 188 | author: { name: ' |
| 189 | timestamp: '2026-09-29T12:00:00.000Z', |
| 190 | }, |
| 191 | ], |
| 192 | }, |
| 193 | createdAt: new Date |
| 194 | }); |
| 195 | |
| 196 | const [job] |
| 197 | (job) => |
| 198 | ); |
| 199 | expect(JSON.parse |
| 200 | event: 'push', |
| 201 | ref: 'refs/heads/main', |
| 202 | created: true, |
| 203 | deleted: false, |
| 204 | repository: { full_name: ` |
| 205 | sender: { username: USERNAME |
| 206 | commits: [ |
| 207 | { |
| 208 | sha, |
| 209 | message: 'Count the bell', |
| 210 | html_url: `https://ghost.test/${USERNAME |
| 211 | }, |
| 212 | ], |
| 213 | }); |
| 214 | }); |
| 215 | |
| 216 | it('turns a disabled endpoint back on and redelivers as a new delivery', |
| 217 | const webhook = |
| 218 | ...ref, |
| 219 | url: 'https://93.184.216.34/again', |
| 220 | events: ['issue.opened'], |
| 221 | }); |
| 222 | await db |
| 223 | .update(schema.webhookEndpoint |
| 224 | .set({ |
| 225 | active: false, |
| 226 | disabledReason: 'The endpoint responded 410 Gone.', |
| 227 | }) |
| 228 | .where(eq(schema |
| 229 | |
| 230 | const updated = |
| 231 | ...ref, |
| 232 | webhookId: webhook.id |
| 233 | active: true, |
| 234 | }); |
| 235 | expect(updated).toMatchObject |
| 236 | |
| 237 | const { deliveries |
| 238 | ...ref, |
| 239 | webhookId: webhook.id |
| 240 | }); |
| 241 | expect(deliveries).toHaveLength |
| 242 | await webhooks.redeliver |
| 243 | ...ref, |
| 244 | webhookId: webhook.id |
| 245 | deliveryId: deliveries[0] |
| 246 | }); |
| 247 | const after = |
| 248 | expect(after.deliveries |
| 249 | expect(after.deliveries |
| 250 | expect(after.deliveries |
| 251 | }); |
| 252 | }); |