Skip to content

Commit

Permalink
feat: add test suite for RateLimitsStorage implementations
Browse files Browse the repository at this point in the history
so we can use it in w3infra
  • Loading branch information
travis committed Aug 3, 2023
1 parent 80fbbae commit 1142f36
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/upload-api/test/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as Store from './store.js'
import * as Upload from './upload.js'
import { test as delegationsStorageTests } from './delegations-storage-tests.js'
import { test as provisionsStorageTests } from './provisions-storage-tests.js'
import { test as rateLimitsStorageTests } from './rate-limits-storage-tests.js'
import { DebugEmail } from '../src/utils/email.js'

export * from './util.js'
Expand All @@ -16,5 +17,6 @@ export {
Upload,
delegationsStorageTests,
provisionsStorageTests,
rateLimitsStorageTests,
DebugEmail,
}
70 changes: 70 additions & 0 deletions packages/upload-api/test/rate-limits-storage-tests.js
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)
},
}
29 changes: 29 additions & 0 deletions packages/upload-api/test/rate-limits-storage.spec.js
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)
}
})
}
})

0 comments on commit 1142f36

Please sign in to comment.