From 3588aa611fffdafdf4507e9acaf392ab98d370b2 Mon Sep 17 00:00:00 2001 From: Kiko Beats Date: Sun, 5 May 2024 11:14:45 +0200 Subject: [PATCH] test: add more cases (#20) * test: add update plan with limit * test: add create key with metadata * refactor: reuse timestamp --- src/usage.js | 7 +++--- test/keys.js | 3 ++- test/plans.js | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 65 insertions(+), 5 deletions(-) diff --git a/src/usage.js b/src/usage.js index 85a8f06..bb13bfe 100644 --- a/src/usage.js +++ b/src/usage.js @@ -30,15 +30,16 @@ module.exports = ({ plans, keys, redis, stats, prefix, serialize, deserialize }) const plan = await plans.retrieve(key.plan, { throwError }) let usage = await deserialize(await redis.get(prefixKey(keyValue))) + const now = Date.now() if (usage === null) { usage = { count: Math.min(quantity, plan.limit), - reset: Date.now() + ms(plan.period) + reset: now + ms(plan.period) } - } else if (Date.now() > usage.reset) { + } else if (now > usage.reset) { usage.count = quantity - usage.reset = Date.now() + ms(plan.period) + usage.reset = now + ms(plan.period) } else { if (usage.count < plan.limit) { usage.count = Math.min(usage.count + quantity, plan.limit) diff --git a/test/keys.js b/test/keys.js index 9cd7be7..5c4e6d2 100644 --- a/test/keys.js +++ b/test/keys.js @@ -37,11 +37,12 @@ test('.create # error if plan does not exist', async t => { }) test('.create', async t => { - const key = await openkey.keys.create() + const key = await openkey.keys.create({ metadata: { email: 'hello@microlink.io' } }) t.truthy(key.createdAt) t.is(key.createdAt, key.updatedAt) t.is(key.value.length, 16) t.true(key.enabled) + t.deepEqual(key.metadata, { email: 'hello@microlink.io' }) }) test('.create # associate a plan', async t => { diff --git a/test/plans.js b/test/plans.js index a554274..3256b1f 100644 --- a/test/plans.js +++ b/test/plans.js @@ -251,7 +251,65 @@ test(".update # don't update invalid `period`", async t => { t.deepEqual(await openkey.plans.retrieve(id), { ...plan, updatedAt }) }) -test('.update # add metadata', async t => { +test('.update # limit', async t => { + let plan = await openkey.plans.create({ + id: randomUUID(), + limit: 1, + period: '1d' + }) + + const key = await openkey.keys.create({ plan: plan.id }) + + let usage = await openkey.usage(key.value) + + t.is(usage.limit, 1) + t.is(usage.remaining, 1) + + usage = await openkey.usage.increment(key.value) + + t.is(usage.limit, 1) + t.is(usage.remaining, 0) + + plan = await openkey.plans.update(plan.id, { limit: 2 }) + usage = await openkey.usage(key.value) + + t.is(usage.limit, 2) + t.is(usage.remaining, 1) +}) + +test('.update # period', async t => { + let plan = await openkey.plans.create({ + id: randomUUID(), + limit: 1, + period: '1ms' + }) + + const key = await openkey.keys.create({ plan: plan.id }) + + let usage = await openkey.usage(key.value) + + t.is(usage.limit, 1) + t.is(usage.remaining, 1) + + usage = await openkey.usage.increment(key.value) + + const oldReset = usage.reset + + t.is(usage.limit, 1) + t.is(usage.remaining, 0) + + const now = Date.now() + plan = await openkey.plans.update(plan.id, { period: '3s', limit: 2 }) + + await setTimeout(0) // ensure time move forward + usage = await openkey.usage(key.value) + + const acceptableDelta = 100 // 100 milliseconds tolerance + t.true(Math.abs(usage.reset - (now + 3000)) <= acceptableDelta) + t.true(oldReset < usage.reset) +}) + +test('.update # metadata', async t => { { const { id } = await openkey.plans.create({ id: randomUUID(),