11import { 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'
35import { $fetch , setup } from '@nuxt/test-utils/e2e'
46import type { RuntimeConfig } from '@nuxt/schema'
57import { ensureConfiguration } from '../src/runtime/server/lib/helpers'
6- import nuxtConfig from './fixtures/basic/nuxt.config'
7- import * as events from './events'
88
99const 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
1124await 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
1627describe ( '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 ( / ^ s i m u l a t e ( .* ) E v e n t $ / )
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} )
0 commit comments