Skip to content

Commit 524b003

Browse files
committed
Add whitelist automatic tests
1 parent 848cacb commit 524b003

File tree

5 files changed

+111
-1
lines changed

5 files changed

+111
-1
lines changed

test/fixtures/rateLimiter/nuxt.config.ts

+63-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,68 @@ export default defineNuxtConfig({
1515
tokensPerInterval: 10,
1616
}
1717
}
18-
}
18+
},
19+
'/whitelistBase': {
20+
security: {
21+
rateLimiter: {
22+
tokensPerInterval: 1,
23+
interval: 300000,
24+
whiteList: [
25+
'127.0.0.1',
26+
'192.168.0.1',
27+
'172.16.0.1',
28+
'10.0.0.1',
29+
],
30+
}
31+
}
32+
},
33+
'/whitelistEmpty': {
34+
security: {
35+
rateLimiter: {
36+
tokensPerInterval: 1,
37+
interval: 300000,
38+
whiteList: [],
39+
}
40+
}
41+
},
42+
'/whitelistNotListed': {
43+
security: {
44+
rateLimiter: {
45+
tokensPerInterval: 1,
46+
interval: 300000,
47+
whiteList: [
48+
'10.0.0.1',
49+
'10.0.1.1',
50+
'10.0.2.1',
51+
'10.0.3.1',
52+
'10.0.4.1',
53+
'10.0.5.1',
54+
'10.0.6.1',
55+
'10.0.7.1',
56+
'10.0.8.1',
57+
'10.0.9.1',
58+
'10.1.0.1',
59+
'10.2.0.1',
60+
'10.3.0.1',
61+
'10.4.0.1',
62+
'10.5.0.1',
63+
'10.6.0.1',
64+
'10.7.0.1',
65+
'10.8.0.1',
66+
'10.9.0.1',
67+
'192.168.0.1',
68+
'192.168.1.1',
69+
'192.168.2.1',
70+
'192.168.3.1',
71+
'192.168.4.1',
72+
'192.168.5.1',
73+
'192.168.6.1',
74+
'192.168.7.1',
75+
'192.168.8.1',
76+
'192.168.9.1',
77+
],
78+
}
79+
}
80+
},
1981
}
2082
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<template>
2+
<div>whitelist base test</div>
3+
</template>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<template>
2+
<div>whitelist empty test</div>
3+
</template>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<template>
2+
<div>whitelist not listed test</div>
3+
</template>

test/rateLimiter.test.ts

+39
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,43 @@ describe('[nuxt-security] Rate Limiter', async () => {
6363
expect(res6.status).toBe(200)
6464
expect(res6.statusText).toBe('OK')
6565
})
66+
67+
it ('should return 200 OK after multiple requests for a route with localhost ip whitelisted', async () => {
68+
const res1 = await fetch('/whitelistBase')
69+
await fetch('/whitelistBase')
70+
await fetch('/whitelistBase')
71+
await fetch('/whitelistBase')
72+
const res5 = await fetch('/whitelistBase')
73+
74+
expect(res1).toBeDefined()
75+
expect(res1).toBeTruthy()
76+
expect(res5.status).toBe(200)
77+
expect(res5.statusText).toBe('OK')
78+
})
79+
80+
it ('should return 429 when limit reached with an empty whitelist array', async () => {
81+
const res1 = await fetch('/whitelistEmpty')
82+
await fetch('/whitelistEmpty')
83+
await fetch('/whitelistEmpty')
84+
await fetch('/whitelistEmpty')
85+
const res5 = await fetch('/whitelistEmpty')
86+
87+
expect(res1).toBeDefined()
88+
expect(res1).toBeTruthy()
89+
expect(res5.status).toBe(429)
90+
expect(res5.statusText).toBe('Too Many Requests')
91+
})
92+
93+
it ('should return 429 when limit reached as localhost ip is not whitelisted', async () => {
94+
const res1 = await fetch('/whitelistNotListed')
95+
await fetch('/whitelistNotListed')
96+
await fetch('/whitelistNotListed')
97+
await fetch('/whitelistNotListed')
98+
const res5 = await fetch('/whitelistNotListed')
99+
100+
expect(res1).toBeDefined()
101+
expect(res1).toBeTruthy()
102+
expect(res5.status).toBe(429)
103+
expect(res5.statusText).toBe('Too Many Requests')
104+
})
66105
})

0 commit comments

Comments
 (0)