forked from adonisjs/limiter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdefine_config.spec.ts
More file actions
178 lines (148 loc) · 5.94 KB
/
define_config.spec.ts
File metadata and controls
178 lines (148 loc) · 5.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
* @adonisjs/limiter
*
* (c) AdonisJS
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
import { test } from '@japa/runner'
import { RedisService } from '@adonisjs/redis/types'
import { ApplicationService } from '@adonisjs/core/types'
import { AppFactory } from '@adonisjs/core/factories/app'
import { Limiter } from '../src/limiter.js'
import LimiterRedisStore from '../src/stores/redis.js'
import LimiterMemoryStore from '../src/stores/memory.js'
import { LimiterManager } from '../src/limiter_manager.js'
import LimiterDatabaseStore from '../src/stores/database.js'
import { defineConfig, stores } from '../src/define_config.js'
import type { LimiterConsumptionOptions } from '../src/types.js'
import { createDatabase, createRedis, createTables } from './helpers.js'
test.group('Define config', () => {
test('define redis store', async ({ assert }) => {
const redis = createRedis() as unknown as RedisService
const app = new AppFactory().create(new URL('./', import.meta.url)) as ApplicationService
await app.init()
app.container.singleton('redis', () => redis)
const redisProvider = stores.redis({
connectionName: 'main',
})
const storeFactory = await redisProvider.resolver(app)
const store = storeFactory({ duration: '1mins', requests: 5 })
assert.instanceOf(store, LimiterRedisStore)
assert.isNull(await store.get('ip_localhost'))
})
test('define database store', async ({ assert }) => {
const database = createDatabase()
await createTables(database)
const app = new AppFactory().create(new URL('./', import.meta.url)) as ApplicationService
await app.init()
app.container.singleton('lucid.db', () => database)
const dbProvider = stores.database({
connectionName: process.env.DB as any,
dbName: 'limiter',
tableName: 'rate_limits',
})
const storeFactory = await dbProvider.resolver(app)
const store = storeFactory({ duration: '1mins', requests: 5 })
assert.instanceOf(store, LimiterDatabaseStore)
assert.isNull(await store.get('ip_localhost'))
})
test('use default database when no explicit database is configured', async ({ assert }) => {
const database = createDatabase()
await createTables(database)
const app = new AppFactory().create(new URL('./', import.meta.url)) as ApplicationService
await app.init()
app.container.singleton('lucid.db', () => database)
const dbProvider = stores.database({
tableName: 'rate_limits',
})
const storeFactory = await dbProvider.resolver(app)
const store = storeFactory({ duration: '1mins', requests: 5 })
assert.instanceOf(store, LimiterDatabaseStore)
assert.isNull(await store.get('ip_localhost'))
})
test('throw error when unregistered db connection is used', async () => {
const database = createDatabase()
await createTables(database)
const app = new AppFactory().create(new URL('./', import.meta.url)) as ApplicationService
await app.init()
app.container.singleton('lucid.db', () => database)
const dbProvider = stores.database({
connectionName: 'foo',
tableName: 'rate_limits',
})
await dbProvider.resolver(app)
}).throws(
'Invalid connection name "foo" referenced by "config/limiter.ts" file. First register the connection inside "config/database.ts" file'
)
test('define memory store', async ({ assert }) => {
const storeFactory = stores.memory({})
const store = storeFactory({ duration: '1mins', requests: 5 })
assert.instanceOf(store, LimiterMemoryStore)
assert.isNull(await store.get('ip_localhost'))
})
test('throw error when config is invalid', async ({ assert }) => {
const redis = createRedis() as unknown as RedisService
const database = createDatabase()
await createTables(database)
const app = new AppFactory().create(new URL('./', import.meta.url)) as ApplicationService
await app.init()
app.container.singleton('redis', () => redis)
app.container.singleton('lucid.db', () => database)
assert.throws(
() =>
defineConfig({
// @ts-expect-error
default: 'redis',
stores: {},
}),
'Missing "stores.redis" in limiter config. It is referenced by the "default" property'
)
assert.throws(
// @ts-expect-error
() => defineConfig({}),
'Missing "stores" property in limiter config'
)
assert.throws(
// @ts-expect-error
() => defineConfig({ stores: {} }),
'Missing "default" store in limiter config'
)
})
test('create manager from define config output', async ({ assert, expectTypeOf }) => {
const redis = createRedis() as unknown as RedisService
const database = createDatabase()
await createTables(database)
const app = new AppFactory().create(new URL('./', import.meta.url)) as ApplicationService
await app.init()
app.container.singleton('redis', () => redis)
app.container.singleton('lucid.db', () => database)
const config = defineConfig({
default: 'redis',
stores: {
redis: stores.redis({
connectionName: 'main',
}),
db: stores.database({
connectionName: process.env.DB as any,
dbName: 'limiter',
tableName: 'rate_limits',
}),
memory: stores.memory({}),
},
})
const limiter = new LimiterManager(await config.resolver(app))
expectTypeOf(limiter.use).parameters.toEqualTypeOf<
[LimiterConsumptionOptions] | ['redis' | 'db' | 'memory', LimiterConsumptionOptions]
>()
expectTypeOf(limiter.use).returns.toEqualTypeOf<Limiter>()
assert.isNull(
await limiter.use('redis', { duration: '1 min', requests: 5 }).get('ip_localhost')
)
assert.isNull(await limiter.use('db', { duration: '1 min', requests: 5 }).get('ip_localhost'))
assert.isNull(
await limiter.use('memory', { duration: '1 min', requests: 5 }).get('ip_localhost')
)
})
})