Skip to content

Commit

Permalink
Add whitelist automatic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zguig52 committed Nov 28, 2024
1 parent 848cacb commit 524b003
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 1 deletion.
64 changes: 63 additions & 1 deletion test/fixtures/rateLimiter/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,68 @@ export default defineNuxtConfig({
tokensPerInterval: 10,
}
}
}
},
'/whitelistBase': {
security: {
rateLimiter: {
tokensPerInterval: 1,
interval: 300000,
whiteList: [
'127.0.0.1',
'192.168.0.1',
'172.16.0.1',
'10.0.0.1',
],
}
}
},
'/whitelistEmpty': {
security: {
rateLimiter: {
tokensPerInterval: 1,
interval: 300000,
whiteList: [],
}
}
},
'/whitelistNotListed': {
security: {
rateLimiter: {
tokensPerInterval: 1,
interval: 300000,
whiteList: [
'10.0.0.1',
'10.0.1.1',
'10.0.2.1',
'10.0.3.1',
'10.0.4.1',
'10.0.5.1',
'10.0.6.1',
'10.0.7.1',
'10.0.8.1',
'10.0.9.1',
'10.1.0.1',
'10.2.0.1',
'10.3.0.1',
'10.4.0.1',
'10.5.0.1',
'10.6.0.1',
'10.7.0.1',
'10.8.0.1',
'10.9.0.1',
'192.168.0.1',
'192.168.1.1',
'192.168.2.1',
'192.168.3.1',
'192.168.4.1',
'192.168.5.1',
'192.168.6.1',
'192.168.7.1',
'192.168.8.1',
'192.168.9.1',
],
}
}
},
}
})
3 changes: 3 additions & 0 deletions test/fixtures/rateLimiter/pages/whitelistBase.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template>
<div>whitelist base test</div>
</template>
3 changes: 3 additions & 0 deletions test/fixtures/rateLimiter/pages/whitelistEmpty.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template>
<div>whitelist empty test</div>
</template>
3 changes: 3 additions & 0 deletions test/fixtures/rateLimiter/pages/whitelistNotListed.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template>
<div>whitelist not listed test</div>
</template>
39 changes: 39 additions & 0 deletions test/rateLimiter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,43 @@ describe('[nuxt-security] Rate Limiter', async () => {
expect(res6.status).toBe(200)
expect(res6.statusText).toBe('OK')
})

it ('should return 200 OK after multiple requests for a route with localhost ip whitelisted', async () => {
const res1 = await fetch('/whitelistBase')
await fetch('/whitelistBase')
await fetch('/whitelistBase')
await fetch('/whitelistBase')
const res5 = await fetch('/whitelistBase')

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

it ('should return 429 when limit reached with an empty whitelist array', async () => {
const res1 = await fetch('/whitelistEmpty')
await fetch('/whitelistEmpty')
await fetch('/whitelistEmpty')
await fetch('/whitelistEmpty')
const res5 = await fetch('/whitelistEmpty')

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

it ('should return 429 when limit reached as localhost ip is not whitelisted', async () => {
const res1 = await fetch('/whitelistNotListed')
await fetch('/whitelistNotListed')
await fetch('/whitelistNotListed')
await fetch('/whitelistNotListed')
const res5 = await fetch('/whitelistNotListed')

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

0 comments on commit 524b003

Please sign in to comment.