Skip to content

Commit 83eeca5

Browse files
Added unit tests for store_aggregated_ip
1 parent c94fe4b commit 83eeca5

File tree

2 files changed

+167
-1
lines changed

2 files changed

+167
-1
lines changed
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
import 'mocha'
2+
3+
import { Pool } from 'pg'
4+
import sinon, { SinonMock, SinonStub } from 'sinon'
5+
import { PostgresStore } from '../../source'
6+
import { Session } from '../../source/models/session'
7+
const session_handler = require('../../source/util/session_handler')
8+
const migration_handler = require('../../source/util/migration_handler')
9+
10+
class ClientMock {
11+
query() {}
12+
release() {}
13+
}
14+
15+
describe('Postgres Store Aggregated IP', () => {
16+
let query: SinonStub
17+
let connect: SinonStub
18+
let client: SinonMock
19+
let getSessionStub: SinonStub
20+
let applyMigrationsStub: SinonStub
21+
let isSessionValidSpy: SinonStub
22+
let newCreatedSession: Session
23+
24+
beforeEach(() => {
25+
query = sinon.stub(Pool.prototype, 'query')
26+
connect = sinon.stub(Pool.prototype, 'connect')
27+
client = sinon.mock(ClientMock.prototype)
28+
getSessionStub = sinon.stub(session_handler, 'getSession')
29+
isSessionValidSpy = sinon.stub(session_handler, 'isSessionValid')
30+
applyMigrationsStub = sinon.stub(migration_handler, 'applyMigrations')
31+
const futureTime = new Date()
32+
const timeOffset = 5000
33+
futureTime.setMilliseconds(futureTime.getMilliseconds() + timeOffset)
34+
35+
newCreatedSession = {
36+
id: '1',
37+
name: 'test-name',
38+
type: 'test-type',
39+
expires_at: futureTime,
40+
}
41+
})
42+
43+
afterEach(() => {
44+
query.restore() // reset stub/mock
45+
connect.restore()
46+
client.restore()
47+
getSessionStub.restore()
48+
applyMigrationsStub.restore()
49+
isSessionValidSpy.restore()
50+
})
51+
52+
it('constructor should call correct functions', async () => {
53+
new PostgresStore({}, 'test')
54+
sinon.assert.callCount(applyMigrationsStub, 1)
55+
})
56+
57+
it('increment function should follow expected business logic', async () => {
58+
let pool = new Pool()
59+
60+
isSessionValidSpy.returns(true)
61+
query.onFirstCall().returns({
62+
rows: [],
63+
})
64+
65+
query.onSecondCall().returns({
66+
rows: [
67+
{
68+
count: 1,
69+
},
70+
],
71+
})
72+
let testStore = new PostgresStore({}, 'test')
73+
testStore.pool = pool
74+
testStore.session = newCreatedSession
75+
76+
await testStore.increment('key')
77+
sinon.assert.callCount(isSessionValidSpy, 1)
78+
sinon.assert.calledWith(
79+
query,
80+
`
81+
INSERT INTO rate_limit.records_aggregated(key, session_id) VALUES ($1, $2)
82+
ON CONFLICT ON CONSTRAINT unique_session_key DO UPDATE
83+
SET count = records_aggregated.count + 1
84+
RETURNING count
85+
`,
86+
['key', newCreatedSession.id],
87+
)
88+
})
89+
90+
it('decrement function should follow expected business logic', async () => {
91+
let pool = new Pool()
92+
query.onFirstCall().returns({
93+
rows: [],
94+
})
95+
96+
query.onSecondCall().returns({
97+
rows: [
98+
{
99+
count: 1,
100+
},
101+
],
102+
})
103+
let testStore = new PostgresStore({}, 'test')
104+
testStore.pool = pool
105+
testStore.session = newCreatedSession
106+
107+
await testStore.decrement('key')
108+
let decrementQuery = `
109+
UPDATE rate_limit.records_aggregated
110+
SET count = greatest(0, count-1)
111+
WHERE key = $1 and session_id = $2
112+
`
113+
sinon.assert.calledWith(query, decrementQuery, [
114+
'key',
115+
newCreatedSession.id,
116+
])
117+
})
118+
119+
it('resetKey function should follow expected business logic', async () => {
120+
let pool = new Pool()
121+
query.onFirstCall().returns({
122+
rows: [],
123+
})
124+
125+
query.onSecondCall().returns({
126+
rows: [
127+
{
128+
count: 1,
129+
},
130+
],
131+
})
132+
let testStore = new PostgresStore({}, 'test')
133+
testStore.pool = pool
134+
testStore.session = newCreatedSession
135+
136+
await testStore.resetKey('key')
137+
let resetQuery = `
138+
DELETE FROM rate_limit.records_aggregated
139+
WHERE key = $1 and session_id = $2
140+
`
141+
sinon.assert.calledWith(query, resetQuery, ['key', newCreatedSession.id])
142+
})
143+
144+
it('resetAll function should follow expected business logic', async () => {
145+
let pool = new Pool()
146+
query.onFirstCall().returns({
147+
rows: [],
148+
})
149+
150+
query.onSecondCall().returns({
151+
rows: [
152+
{
153+
count: 1,
154+
},
155+
],
156+
})
157+
let testStore = new PostgresStore({}, 'test')
158+
testStore.pool = pool
159+
testStore.session = newCreatedSession
160+
161+
await testStore.resetAll()
162+
let resetAllQuery = `
163+
DELETE FROM rate_limit.records_aggregated WHERE session_id = $1
164+
`
165+
sinon.assert.calledWith(query, resetAllQuery, [newCreatedSession.id])
166+
})
167+
})

test/stores/store_individual_ip.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import 'mocha'
2-
import { assert, expect } from 'chai'
32

43
import { Pool } from 'pg'
54
import sinon, { SinonMock, SinonStub } from 'sinon'

0 commit comments

Comments
 (0)