Skip to content

Commit 3803af1

Browse files
test: added tests for individual store new implementation
1 parent a36c775 commit 3803af1

File tree

6 files changed

+67
-65
lines changed

6 files changed

+67
-65
lines changed

db/tests/performance/individual_ip.test.sql

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,31 +28,31 @@ VALUES ('test-count', '00000000-0000-0000-0000-000000000000'::uuid),
2828

2929
SELECT performs_ok(
3030
$bd$
31-
SELECT ind_increment as count FROM rate_limit.ind_increment('test-count', '00000000-0000-0000-0000-000000000000')
31+
SELECT * FROM rate_limit.ind_increment('test-count', 'dedicated-test', 1000) AS (count int, expires_at timestamptz);
3232
$bd$,
3333
250,
3434
'inserting record should execute under 250ms'
3535
);
3636

3737
SELECT performs_ok(
3838
$bd$
39-
SELECT * FROM rate_limit.ind_decrement('test-decrement', '00000000-0000-0000-0000-000000000000');
39+
SELECT * FROM rate_limit.ind_decrement('test-decrement', 'dedicated-test');
4040
$bd$,
4141
250,
4242
'decrementing query execute under 250ms'
4343
);
4444

4545
SELECT performs_ok(
4646
$bd$
47-
SELECT * FROM rate_limit.ind_reset_key('test-reset-key', '00000000-0000-0000-0000-000000000000');
47+
SELECT * FROM rate_limit.ind_reset_key('test-reset-key', 'dedicated-test');
4848
$bd$,
4949
250,
5050
'resetting a key should execute under 250ms'
5151
);
5252

5353
SELECT performs_ok(
5454
$bd$
55-
SELECT * FROM rate_limit.ind_reset_session('00000000-0000-0000-0000-000000000000');
55+
SELECT * FROM rate_limit.ind_reset_session('dedicated-test');
5656
$bd$,
5757
250,
5858
'resetting a session should execute under 250ms'

db/tests/unit/ind_decrement.test.sql

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ BEGIN;
22
-- Plan the tests.
33
SELECT plan(2);
44

5-
INSERT INTO rate_limit.sessions (id, name_, type_)
5+
INSERT INTO rate_limit.sessions (id, name_, type_, expires_at)
66
SELECT
77
'00000000-0000-0000-0000-000000000000'::uuid AS id,
88
'dedicated-test'::text AS name_,
9-
'individual'::text AS type_;
9+
'individual'::text AS type_,
10+
'2023-01-01 10:00:00+0'::timestamptz AS expires_at;
1011

1112
INSERT INTO rate_limit.individual_records (key, session_id)
1213
SELECT
@@ -23,7 +24,7 @@ SELECT
2324

2425
SELECT lives_ok(
2526
$have$
26-
SELECT * FROM rate_limit.ind_decrement('existing-key', '00000000-0000-0000-0000-000000000000')
27+
SELECT * FROM rate_limit.ind_decrement('existing-key', 'dedicated-test', '2023-01-01 09:00:00+0'::timestamptz)
2728
$have$,
2829
'rate_limit.ind_decrement does not throw an error'
2930
);

db/tests/unit/ind_increment.test.sql

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
BEGIN;
22
-- Plan the tests.
3-
SELECT plan(4);
3+
SELECT plan(6);
44

5-
INSERT INTO rate_limit.sessions (id, name_, type_)
5+
INSERT INTO rate_limit.sessions (id, name_, type_, expires_at)
66
SELECT
77
'00000000-0000-0000-0000-000000000000'::uuid AS id,
88
'dedicated-test'::text AS name_,
9-
'individual'::text AS type_
9+
'individual'::text AS type_,
10+
'2023-01-01 10:00:00+0'::timestamptz AS expires_at
1011
UNION
1112
SELECT
1213
'00000000-1111-1111-1111-000000000000'::uuid AS id,
1314
'dedicated-test-2'::text AS name_,
14-
'individual'::text AS type_;
15+
'individual'::text AS type_,
16+
'2023-01-01 10:00:00+0'::timestamptz AS expires_at
17+
UNION
18+
SELECT
19+
'00000000-2222-2222-2222-000000000000'::uuid AS id,
20+
'dedicated-test-expired'::text AS name_,
21+
'individual'::text AS type_,
22+
'2023-01-01 08:00:00+0'::timestamptz AS expires_at;;
1523

1624
INSERT INTO rate_limit.individual_records (key, session_id)
1725
SELECT
@@ -20,30 +28,30 @@ SELECT
2028

2129
SELECT results_eq(
2230
$have$
23-
SELECT ind_increment AS count FROM rate_limit.ind_increment('new-key', '00000000-0000-0000-0000-000000000000')
31+
SELECT * FROM rate_limit.ind_increment('new-key', 'dedicated-test', 1000, '2023-01-01 09:00:00+0'::timestamptz) AS (count int, expires_at timestamptz);
2432
$have$,
2533
$want$
26-
SELECT 1::int AS count;
34+
SELECT 1::int AS count, '2023-01-01 10:00:00+0'::timestamptz as expires_at;
2735
$want$,
2836
'rate_limit.ind_increment returns correct count for new key'
2937
);
3038

3139
SELECT results_eq(
3240
$have$
33-
SELECT ind_increment AS count FROM rate_limit.ind_increment('new-key', '00000000-1111-1111-1111-000000000000')
41+
SELECT * FROM rate_limit.ind_increment('new-key', 'dedicated-test-2', 1000, '2023-01-01 09:00:00+0'::timestamptz) AS (count int, expires_at timestamptz);
3442
$have$,
3543
$want$
36-
SELECT 1::int AS count;
44+
SELECT 1::int AS count, '2023-01-01 10:00:00+0'::timestamptz as expires_at;
3745
$want$,
3846
'rate_limit.ind_increment returns correct count for new key on another session'
3947
);
4048

4149
SELECT results_eq(
4250
$have$
43-
SELECT ind_increment AS count FROM rate_limit.ind_increment('existing-key', '00000000-0000-0000-0000-000000000000')
51+
SELECT * FROM rate_limit.ind_increment('existing-key', 'dedicated-test', 1000, '2023-01-01 09:00:00+0'::timestamptz) AS (count int, expires_at timestamptz);
4452
$have$,
4553
$want$
46-
SELECT 2::int AS count;
54+
SELECT 2::int AS count, '2023-01-01 10:00:00+0'::timestamptz as expires_at;
4755
$want$,
4856
'rate_limit.ind_increment returns correct count for existing key'
4957
);
@@ -63,6 +71,30 @@ SELECT bag_eq(
6371
'rate_limit.ind_increment applies correct logic on table rows'
6472
);
6573

74+
SELECT results_eq(
75+
$have$
76+
SELECT * FROM rate_limit.ind_increment('existing-key', 'dedicated-test-expired', 1000, '2023-01-01 09:00:00+0') AS (count int, expires_at timestamptz);
77+
$have$,
78+
$want$
79+
SELECT 1::int AS count, '2023-01-01 09:00:01+0'::timestamptz as expires_at;
80+
$want$,
81+
'rate_limit.ind_increment returns correct count for existing key for expired session'
82+
);
83+
84+
SELECT bag_hasnt(
85+
$have$
86+
SELECT id, name_, type_, expires_at FROM rate_limit.sessions
87+
$have$,
88+
$miss$
89+
SELECT
90+
'00000000-2222-2222-2222-000000000000'::uuid AS id,
91+
'dedicated-test-expired'::text AS name_,
92+
'individual'::text AS type_,
93+
'2023-01-01 08:00:00+0'::timestamptz as expires_at;
94+
$miss$,
95+
'expired session should not be reset after increment invoke'
96+
);
97+
6698
-- Finish the tests and clean up.
6799
SELECT finish FROM finish();
68100
ROLLBACK;

db/tests/unit/ind_reset_key.test.sql

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ BEGIN;
22
-- Plan the tests.
33
SELECT plan(2);
44

5-
INSERT INTO rate_limit.sessions (id, name_, type_)
5+
INSERT INTO rate_limit.sessions (id, name_, type_, expires_at)
66
SELECT
77
'00000000-0000-0000-0000-000000000000'::uuid AS id,
88
'dedicated-test'::text AS name_,
9-
'individual'::text AS type_;
9+
'individual'::text AS type_,
10+
'2023-01-01 10:00:00+0'::timestamptz AS expires_at;
1011

1112
INSERT INTO rate_limit.individual_records (key, session_id)
1213
SELECT
@@ -23,7 +24,7 @@ SELECT
2324

2425
SELECT lives_ok(
2526
$have$
26-
SELECT * FROM rate_limit.ind_reset_key('existing-key', '00000000-0000-0000-0000-000000000000')
27+
SELECT * FROM rate_limit.ind_reset_key('existing-key', 'dedicated-test', '2023-01-01 09:00:00+0'::timestamptz)
2728
$have$,
2829
'rate_limit.ind_reset_key does not throw an error'
2930
);

db/tests/unit/ind_reset_session.test.sql

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ BEGIN;
22
-- Plan the tests.
33
SELECT plan(2);
44

5-
INSERT INTO rate_limit.sessions (id, name_, type_)
5+
INSERT INTO rate_limit.sessions (id, name_, type_, expires_at)
66
SELECT
77
'00000000-0000-0000-0000-000000000000'::uuid AS id,
88
'dedicated-test'::text AS name_,
9-
'individual'::text AS type_;
9+
'individual'::text AS type_,
10+
'2023-01-01 10:00:00+0'::timestamptz AS expires_at;
1011

1112
INSERT INTO rate_limit.individual_records (key, session_id)
1213
SELECT
@@ -27,7 +28,7 @@ SELECT
2728

2829
SELECT lives_ok(
2930
$have$
30-
SELECT * FROM rate_limit.ind_reset_session('00000000-0000-0000-0000-000000000000')
31+
SELECT * FROM rate_limit.ind_reset_session('dedicated-test','2023-01-01 09:00:00+0'::timestamptz)
3132
$have$,
3233
'rate_limit.ind_reset_session does not throw an error'
3334
);

test/stores/store_individual_ip.spec.ts

Lines changed: 9 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import { assert } from 'chai'
44
import { Pool } from 'pg'
55
import sinon, { SinonMock, SinonStub } from 'sinon'
66
import { PostgresStoreIndividualIP } from '../../source'
7-
import { Session } from '../../source/models/session'
8-
const session_handler = require('../../source/util/session_handler')
97
const migration_handler = require('../../source/util/migration_handler')
108

119
class ClientMock {
@@ -17,37 +15,21 @@ describe('Postgres Store Individual IP', () => {
1715
let query: SinonStub
1816
let connect: SinonStub
1917
let client: SinonMock
20-
let getSessionStub: SinonStub
2118
let applyMigrationsStub: SinonStub
22-
let isSessionValidSpy: SinonStub
23-
let newCreatedSession: Session
19+
let expirationDate = new Date()
2420

2521
beforeEach(() => {
2622
query = sinon.stub(Pool.prototype, 'query')
2723
connect = sinon.stub(Pool.prototype, 'connect')
2824
client = sinon.mock(ClientMock.prototype)
29-
getSessionStub = sinon.stub(session_handler, 'getSession')
30-
isSessionValidSpy = sinon.stub(session_handler, 'isSessionValid')
3125
applyMigrationsStub = sinon.stub(migration_handler, 'applyMigrations')
32-
const futureTime = new Date()
33-
const timeOffset = 5000
34-
futureTime.setMilliseconds(futureTime.getMilliseconds() + timeOffset)
35-
36-
newCreatedSession = {
37-
id: '1',
38-
name: 'test-name',
39-
type: 'test-type',
40-
expires_at: futureTime,
41-
}
4226
})
4327

4428
afterEach(() => {
4529
query.restore() // reset stub/mock
4630
connect.restore()
4731
client.restore()
48-
getSessionStub.restore()
4932
applyMigrationsStub.restore()
50-
isSessionValidSpy.restore()
5133
})
5234

5335
it('constructor should call correct functions and populate correct fields', async () => {
@@ -60,35 +42,27 @@ describe('Postgres Store Individual IP', () => {
6042
let pool = new Pool()
6143
let dbCount = 1
6244

63-
isSessionValidSpy.returns(true)
64-
6545
query.onFirstCall().returns({
6646
rows: [
6747
{
6848
count: dbCount,
49+
expires_at: expirationDate,
6950
},
7051
],
7152
})
7253
let testStore = new PostgresStoreIndividualIP({}, 'test')
7354
testStore.pool = pool
74-
testStore.session = newCreatedSession
7555

7656
let incrementCount = await testStore.increment('key')
77-
sinon.assert.callCount(isSessionValidSpy, 1)
7857
let recordInsertQuery =
79-
'SELECT ind_increment as count FROM rate_limit.ind_increment($1, $2)'
58+
'SELECT * FROM rate_limit.ind_increment($1, $2, $3) AS (count int, expires_at timestamptz);'
8059
sinon.assert.calledWith(query, recordInsertQuery, [
8160
'key',
82-
newCreatedSession.id,
61+
testStore.prefix,
62+
undefined,
8363
])
8464
assert.equal(incrementCount.totalHits, dbCount)
85-
assert.isTrue(
86-
(incrementCount.resetTime?.getMilliseconds() ||
87-
new Date().getMilliseconds()) -
88-
(newCreatedSession.expires_at?.getMilliseconds() ||
89-
new Date().getMilliseconds()) <
90-
10,
91-
)
65+
assert.equal(incrementCount.resetTime, expirationDate)
9266
})
9367

9468
it('decrement function should follow expected business logic', async () => {
@@ -99,16 +73,12 @@ describe('Postgres Store Individual IP', () => {
9973

10074
let testStore = new PostgresStoreIndividualIP({}, 'test')
10175
testStore.pool = pool
102-
testStore.session = newCreatedSession
10376

10477
await testStore.decrement('key')
10578
let decrementQuery = `
10679
SELECT * FROM rate_limit.ind_decrement($1, $2);
10780
`
108-
sinon.assert.calledWith(query, decrementQuery, [
109-
'key',
110-
newCreatedSession.id,
111-
])
81+
sinon.assert.calledWith(query, decrementQuery, ['key', testStore.prefix])
11282
})
11383

11484
it('resetKey function should follow expected business logic', async () => {
@@ -119,13 +89,11 @@ describe('Postgres Store Individual IP', () => {
11989

12090
let testStore = new PostgresStoreIndividualIP({}, 'test')
12191
testStore.pool = pool
122-
testStore.session = newCreatedSession
123-
12492
await testStore.resetKey('key')
12593
let resetQuery = `
12694
SELECT * FROM rate_limit.ind_reset_key($1, $2);
12795
`
128-
sinon.assert.calledWith(query, resetQuery, ['key', newCreatedSession.id])
96+
sinon.assert.calledWith(query, resetQuery, ['key', testStore.prefix])
12997
})
13098

13199
it('resetAll function should follow expected business logic', async () => {
@@ -136,12 +104,11 @@ describe('Postgres Store Individual IP', () => {
136104

137105
let testStore = new PostgresStoreIndividualIP({}, 'test')
138106
testStore.pool = pool
139-
testStore.session = newCreatedSession
140107

141108
await testStore.resetAll()
142109
let resetAllQuery = `
143110
SELECT * FROM rate_limit.ind_reset_session($1);
144111
`
145-
sinon.assert.calledWith(query, resetAllQuery, [newCreatedSession.id])
112+
sinon.assert.calledWith(query, resetAllQuery, [testStore.prefix])
146113
})
147114
})

0 commit comments

Comments
 (0)