Skip to content

Commit

Permalink
test: add more cases (#20)
Browse files Browse the repository at this point in the history
* test: add update plan with limit

* test: add create key with metadata

* refactor: reuse timestamp
  • Loading branch information
Kikobeats authored May 5, 2024
1 parent 497fd3e commit 3588aa6
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 5 deletions.
7 changes: 4 additions & 3 deletions src/usage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion test/keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: '[email protected]' } })
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: '[email protected]' })
})

test('.create # associate a plan', async t => {
Expand Down
60 changes: 59 additions & 1 deletion test/plans.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit 3588aa6

Please sign in to comment.