-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add test suite for RateLimitsStorage implementations
so we can use it in w3infra
- Loading branch information
Showing
3 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import * as API from '../src/types.js' | ||
|
||
/** | ||
* @type {API.Tests} | ||
*/ | ||
export const test = { | ||
'should persist rate limits': async (assert, context) => { | ||
const storage = context.rateLimitsStorage | ||
const subject = 'travis@example.com' | ||
const result = await storage.add(subject, 0) | ||
assert.ok(result.ok) | ||
if (!result.ok){ | ||
throw new Error('storing rate limit failed!') | ||
} | ||
|
||
const limitsResult = await storage.list(subject) | ||
assert.ok(limitsResult.ok) | ||
if (!limitsResult.ok){ | ||
throw new Error('listing rate limits failed!') | ||
} | ||
|
||
assert.equal(limitsResult.ok.length, 1) | ||
}, | ||
|
||
'should list rate limits': async (assert, context) => { | ||
const storage = context.rateLimitsStorage | ||
const subject = 'travis@example.com' | ||
await storage.add(subject, 0) | ||
await storage.add(subject, 2) | ||
|
||
const limitsResult = await storage.list(subject) | ||
assert.ok(limitsResult.ok) | ||
if (!limitsResult.ok){ | ||
throw new Error('listing rate limits failed!') | ||
} | ||
|
||
assert.equal(limitsResult.ok.length, 2) | ||
}, | ||
|
||
'should allow rate limits to be deleted': async (assert, context) => { | ||
const storage = context.rateLimitsStorage | ||
const subject = 'travis@example.com' | ||
const result = await storage.add(subject, 0) | ||
assert.ok(result.ok) | ||
if (!result.ok){ | ||
throw new Error('storing rate limit failed!') | ||
} | ||
|
||
const limitsResult = await storage.list(subject) | ||
assert.ok(limitsResult.ok) | ||
if (!limitsResult.ok){ | ||
throw new Error('listing rate limits failed!') | ||
} | ||
|
||
assert.equal(limitsResult.ok.length, 1) | ||
|
||
const removeResult = await storage.remove(result.ok.id) | ||
assert.ok(removeResult.ok) | ||
if (!result.ok){ | ||
throw new Error('removing rate limit failed!') | ||
} | ||
|
||
const secondLimitsResult = await storage.list(subject) | ||
assert.ok(secondLimitsResult.ok) | ||
if (!secondLimitsResult.ok){ | ||
throw new Error('listing rate limits failed!') | ||
} | ||
assert.equal(secondLimitsResult.ok.length, 0) | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import * as RateLimitsStorage from './rate-limits-storage-tests.js' | ||
import * as assert from 'assert' | ||
import { cleanupContext, createContext } from './helpers/context.js' | ||
|
||
describe('in memory rate limits storage', async () => { | ||
for (const [name, test] of Object.entries(RateLimitsStorage.test)) { | ||
const define = name.startsWith('only ') | ||
? it.only | ||
: name.startsWith('skip ') | ||
? it.skip | ||
: it | ||
|
||
define(name, async () => { | ||
const context = await createContext() | ||
try { | ||
await test( | ||
{ | ||
equal: assert.strictEqual, | ||
deepEqual: assert.deepStrictEqual, | ||
ok: assert.ok, | ||
}, | ||
context | ||
) | ||
} finally { | ||
await cleanupContext(context) | ||
} | ||
}) | ||
} | ||
}) |