|
| 1 | +import { mkdtemp } from 'node:fs/promises' |
| 2 | +import { tmpdir } from 'node:os' |
| 3 | +import { join } from 'node:path' |
| 4 | +import { runWithNuxtContext } from '@nuxt/kit' |
| 5 | +import { createHooks } from 'hookable' |
| 6 | +import { beforeAll, describe, expect, it } from 'vitest' |
| 7 | +import { defaultOptions } from '../src/constant' |
| 8 | +import { getDevAuthToken } from '../src/dev-auth' |
| 9 | +import { setupRPC } from '../src/server-rpc' |
| 10 | + |
| 11 | +const SECRET = 'synthetic-secret' |
| 12 | + |
| 13 | +// Exercise the composed RPC table the HMR socket dispatches to, so a later |
| 14 | +// module re-registering a method without the token guard is caught. |
| 15 | +async function setupServer() { |
| 16 | + const hooks = createHooks() |
| 17 | + const rootDir = await mkdtemp(join(tmpdir(), 'nuxt-devtools-test-')) |
| 18 | + const nuxt = { |
| 19 | + hooks, |
| 20 | + hook: hooks.hook.bind(hooks), |
| 21 | + callHook: hooks.callHook.bind(hooks), |
| 22 | + options: { |
| 23 | + rootDir, |
| 24 | + srcDir: rootDir, |
| 25 | + dir: { public: 'public' }, |
| 26 | + app: { baseURL: '/' }, |
| 27 | + runtimeConfig: { secret: SECRET, public: {} }, |
| 28 | + nitro: { runtimeConfig: { secret: SECRET } }, |
| 29 | + _layers: [{ config: { runtimeConfig: { secret: SECRET } } }], |
| 30 | + }, |
| 31 | + } as any |
| 32 | + const { rpc } = runWithNuxtContext(nuxt, () => setupRPC(nuxt, defaultOptions)) |
| 33 | + await nuxt.callHook('nitro:init', { |
| 34 | + storage: {}, |
| 35 | + options: { storage: { db: { driver: 'redis', password: SECRET } }, devStorage: {} }, |
| 36 | + }) |
| 37 | + return { nuxt, functions: rpc.functions } |
| 38 | +} |
| 39 | + |
| 40 | +describe('private server data rpc', () => { |
| 41 | + let server: Awaited<ReturnType<typeof setupServer>> |
| 42 | + |
| 43 | + beforeAll(async () => { |
| 44 | + // sandbox where the dev auth token is persisted (see getHomeDir) |
| 45 | + process.env.XDG_CONFIG_HOME = await mkdtemp(join(tmpdir(), 'nuxt-devtools-home-')) |
| 46 | + server = await setupServer() |
| 47 | + }) |
| 48 | + |
| 49 | + it('getServerConfig rejects a missing or invalid token', async () => { |
| 50 | + // @ts-expect-error simulating a tokenless client |
| 51 | + await expect(server.functions.getServerConfig()).rejects.toThrow(/invalid dev auth token/i) |
| 52 | + await expect(server.functions.getServerConfig('invalid')).rejects.toThrow(/invalid dev auth token/i) |
| 53 | + }) |
| 54 | + |
| 55 | + it('getServerConfig returns the options with a valid token', async () => { |
| 56 | + await expect(server.functions.getServerConfig(await getDevAuthToken())).resolves.toBe(server.nuxt.options) |
| 57 | + }) |
| 58 | + |
| 59 | + it('getStorageMounts and getStorageKeys reject an invalid token', async () => { |
| 60 | + await expect(server.functions.getStorageMounts('invalid')).rejects.toThrow(/invalid dev auth token/i) |
| 61 | + await expect(server.functions.getStorageKeys('invalid', 'db')).rejects.toThrow(/invalid dev auth token/i) |
| 62 | + }) |
| 63 | + |
| 64 | + it('getStorageMounts returns mounts with a valid token', async () => { |
| 65 | + await expect(server.functions.getStorageMounts(await getDevAuthToken())) |
| 66 | + .resolves |
| 67 | + .toEqual({ db: { driver: 'redis', password: SECRET } }) |
| 68 | + }) |
| 69 | +}) |
0 commit comments