Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): spread storage options #452

Merged
merged 2 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,12 @@ function registerRateLimiterStorage(nuxt: Nuxt, securityOptions: ModuleOptions)
securityOptions.rateLimiter ? securityOptions.rateLimiter.driver : undefined,
{ name: 'lruCache' }
)
const { name, options } = driver
const { name, options = {} } = driver
config.storage = defu(
{
'#rate-limiter-storage': {
driver: name,
options
...options
}
},
config.storage
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/storageOptions/app.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<template>
<div>
<NuxtPage />
</div>
</template>
17 changes: 17 additions & 0 deletions test/fixtures/storageOptions/nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export default defineNuxtConfig({
modules: [
'../../../src/module'
],
security: {
rateLimiter: {
tokensPerInterval: 3,
interval: 1000000,
driver: {
name: 'fsLite',
options: {
base: './test/fixtures/storageOptions/.nuxt/test/.data/db'
}
}
}
}
})
5 changes: 5 additions & 0 deletions test/fixtures/storageOptions/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"private": true,
"name": "basic",
"type": "module"
}
3 changes: 3 additions & 0 deletions test/fixtures/storageOptions/pages/index.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template>
<div>basic</div>
</template>
36 changes: 36 additions & 0 deletions test/storageOptions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { describe, it, expect } from 'vitest'
import { fileURLToPath } from 'node:url'
import { rm } from 'node:fs/promises'
import { setup, fetch } from '@nuxt/test-utils'

describe('[nuxt-security] Storage options', async () => {
await setup({
rootDir: fileURLToPath(new URL('./fixtures/storageOptions', import.meta.url)),
})

const dbPath = './test/fixtures/storageOptions/.nuxt/test/.data/db'
await rm(dbPath, { recursive: true, force: true })

it('should pass options to a custom driver', async () => {
const res1 = await fetch('/')
const res2 = await fetch('/')

expect(res1).toBeDefined()
expect(res1).toBeTruthy()
expect(res2.status).toBe(200)
expect(res2.statusText).toBe('OK')
})

it('should return 429 with the custom driver', async () => {
const res1 = await fetch('/')
await fetch('/')
await fetch('/')
await fetch('/')
const res5 = await fetch('/')

expect(res1).toBeDefined()
expect(res1).toBeTruthy()
expect(res5.status).toBe(429)
expect(res5.statusText).toBe('Too Many Requests')
})
})
Loading