Skip to content

Commit 7373ea9

Browse files
committed
test: iterate simulation events + generate key pairs before testing
1 parent e0c7b02 commit 7373ea9

File tree

3 files changed

+43
-96
lines changed

3 files changed

+43
-96
lines changed

test/fixtures/basic/nuxt.config.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { defineNuxtConfig } from 'nuxt/config'
22
import myModule from '../../../src/module'
3+
// @ts-expect-error generated on test command
4+
import keys from './test-keys.json'
35

46
export default defineNuxtConfig({
57
modules: [myModule],
@@ -21,8 +23,8 @@ export default defineNuxtConfig({
2123
secretKey: 'testHerokuSecretKey',
2224
},
2325
kick: {
24-
// Generated key: type: 'spki' | format: 'pem'
25-
publicKey: 'MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAO4GZQc6hemxG93YR1bTu/kTmmycoqwpWudSAmTAW8cqyio6eZexPD5PGGp2WUEJr5DSPH4LCMjJMcKloLeZKEMCAwEAAQ==',
26+
// Generated on test setup
27+
publicKey: keys.publicKey,
2628
},
2729
meta: {
2830
appSecret: 'testMetaAppSecret',

test/module.test.ts

Lines changed: 35 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
11
import { fileURLToPath } from 'node:url'
2-
import { describe, it, expect, vi } from 'vitest'
2+
import { rm, writeFile } from 'node:fs/promises'
3+
import { generateKeyPairSync } from 'node:crypto'
4+
import { describe, it, expect, vi, afterAll } from 'vitest'
35
import { $fetch, setup } from '@nuxt/test-utils/e2e'
46
import type { RuntimeConfig } from '@nuxt/schema'
57
import { ensureConfiguration } from '../src/runtime/server/lib/helpers'
6-
import nuxtConfig from './fixtures/basic/nuxt.config'
7-
import * as events from './events'
88

99
const validWebhook = { isValidWebhook: true }
1010

11+
// Generate test keys
12+
const keysDir = fileURLToPath(new URL('./fixtures/basic/test-keys.json', import.meta.url))
13+
14+
const keys = generateKeyPairSync('rsa', {
15+
modulusLength: 512,
16+
publicKeyEncoding: { type: 'spki', format: 'pem' },
17+
privateKeyEncoding: { type: 'pkcs8', format: 'pem' },
18+
})
19+
20+
await writeFile(keysDir, JSON.stringify(keys))
21+
const nuxtConfig = (await import('./fixtures/basic/nuxt.config')).default
22+
23+
// Nuxt setup
1124
await setup({ rootDir: fileURLToPath(new URL('./fixtures/basic', import.meta.url)) })
12-
vi.mock('#imports', () => ({
13-
useRuntimeConfig: vi.fn(() => nuxtConfig.runtimeConfig),
14-
}))
1525

26+
// Start tests
1627
describe('ssr', () => {
1728
it('renders the index page', async () => {
1829
const html = await $fetch('/')
@@ -35,87 +46,22 @@ describe('ensureConfiguration method', () => {
3546
})
3647
})
3748

38-
describe('webhooks', () => {
39-
it('valid Discord webhook', async () => {
40-
const response = await events.simulateDiscordEvent()
41-
expect(response).toStrictEqual(validWebhook)
42-
})
43-
44-
it('valid Dropbox webhook', async () => {
45-
const response = await events.simulateDropboxEvent()
46-
expect(response).toStrictEqual(validWebhook)
47-
})
48-
49-
it('valid GitHub webhook', async () => {
50-
const response = await events.simulateGitHubEvent()
51-
expect(response).toStrictEqual(validWebhook)
52-
})
53-
54-
it('valid GitLab webhook', async () => {
55-
const response = await events.simulateGitLabEvent()
56-
expect(response).toStrictEqual(validWebhook)
57-
})
58-
59-
it('valid Heroku webhook', async () => {
60-
const response = await events.simulateHerokuEvent()
61-
expect(response).toStrictEqual(validWebhook)
62-
})
63-
64-
it('valid Kick webhook', async () => {
65-
const response = await events.simulateKickEvent()
66-
expect(response).toStrictEqual(validWebhook)
67-
})
68-
69-
it('valid Meta webhook', async () => {
70-
const response = await events.simulateMetaEvent()
71-
expect(response).toStrictEqual(validWebhook)
72-
})
73-
74-
it('valid NuxtHub webhook', async () => {
75-
const response = await events.simulateNuxthubEvent()
76-
expect(response).toStrictEqual(validWebhook)
77-
})
78-
79-
it('valid Paddle webhook', async () => {
80-
const response = await events.simulatePaddleEvent()
81-
expect(response).toStrictEqual(validWebhook)
82-
})
83-
84-
it('valid Paypal webhook', async () => {
85-
const response = await events.simulatePaypalEvent()
86-
expect(response).toStrictEqual(validWebhook)
87-
})
88-
89-
it('valid Resend webhook', async () => {
90-
const response = await events.simulateResendEvent()
91-
expect(response).toStrictEqual(validWebhook)
92-
})
93-
94-
it('valid Shopify webhook', async () => {
95-
const response = await events.simulateShopifyEvent()
96-
expect(response).toStrictEqual(validWebhook)
97-
})
98-
99-
it('valid Stripe webhook', async () => {
100-
const response = await events.simulateStripeEvent()
101-
expect(response).toStrictEqual(validWebhook)
102-
})
103-
104-
it('valid Svix Webhook', async () => {
105-
const response = await events.simulateSvixEvent()
106-
expect(response).toStrictEqual(validWebhook)
107-
})
108-
109-
it('valid Twitch webhook', async () => {
110-
const response = await events.simulateTwitchEvent()
111-
expect(response).toStrictEqual(validWebhook)
112-
})
113-
it('valid Hygraph webhook', async () => {
114-
const response = await events.simulateHygraphEvent()
115-
expect(response).toStrictEqual(validWebhook)
116-
})
117-
it('valid Polar webhook', async () => {
118-
const response = await events.simulatePolarEvent()
119-
expect(response).toStrictEqual(validWebhook)
120-
})
49+
describe('webhooks', async () => {
50+
vi.mock('#imports', () => ({
51+
useRuntimeConfig: vi.fn(() => nuxtConfig.runtimeConfig),
52+
}))
53+
54+
afterAll(() => rm(keysDir))
55+
56+
// Iterate over the `events` object dynamically
57+
const events = await import('./events')
58+
for (const [methodName, simulation] of Object.entries(events)) {
59+
const match = methodName.match(/^simulate(.*)Event$/)
60+
if (!match) continue
61+
const webhookName = match[1]
62+
it(`valid ${webhookName} webhook`, async () => {
63+
const response = await simulation()
64+
expect(response).toStrictEqual(validWebhook)
65+
})
66+
}
12167
})

test/simulations/kick.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
import { subtle } from 'node:crypto'
22
import { Buffer } from 'node:buffer'
33
import { $fetch } from '@nuxt/test-utils/e2e'
4-
import { RSASSA_PKCS1_v1_5_SHA256, encoder } from '../../src/runtime/server/lib/helpers'
4+
import { RSASSA_PKCS1_v1_5_SHA256, encoder, stripPemHeaders } from '../../src/runtime/server/lib/helpers'
5+
// @ts-expect-error generated on test command
6+
import keys from '../fixtures/basic/test-keys.json'
57

68
const body = 'testBody'
79
const messageId = 'testMessageId'
810

9-
// Generated key: type: 'pkcs8' | format: 'pem'
10-
const fakePrivateKey = 'MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEA7gZlBzqF6bEb3dhHVtO7+ROabJyirCla51ICZMBbxyrKKjp5l7E8Pk8YanZZQQmvkNI8fgsIyMkxwqWgt5koQwIDAQABAkBsQMBF71nkFMaluK2JUbbV6xJ6fyqEvjI3rlakV8/l3jGGL7hP6KItO3TgH4Zy0MUH5EnFBl7W2spjJt2p87rxAiEA/NdaQquie0kSPJPpfxyQ8m5fyZ4bMjuT8Buw2lDU5x0CIQDw/6f4ZfXCZuxYgCjsmwoHj2CEoBGcV8VvkZxR3E1O3wIhAJmz0Ir3C68mnI9221sKYpL9xf0qwB2pWiV8r+YHfWWBAiAt9XNA6aDOa/ZSgk5LoN1ux6buY+A34n0iY7Bd5BdSHQIgTLlJPuQ7e1KTYeJAD5F84uOKMlqMM+bbgFMkzDqqPY4='
11-
1211
export const simulateKickEvent = async () => {
13-
const privateKeyBuffer = Buffer.from(fakePrivateKey, 'base64')
12+
const privateKeyBuffer = Buffer.from(stripPemHeaders(keys.privateKey), 'base64')
1413
const privateKey = await subtle.importKey('pkcs8', privateKeyBuffer, RSASSA_PKCS1_v1_5_SHA256, false, ['sign'])
1514

1615
const timestamp = Math.floor(Date.now() / 1000)

0 commit comments

Comments
 (0)