-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* test(keys): add metadata cases * test: improve coverage
- Loading branch information
Showing
5 changed files
with
106 additions
and
54 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
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 |
---|---|---|
|
@@ -13,7 +13,6 @@ const { keys, plans } = openkey({ redis: new Redis() }) | |
|
||
test.beforeEach(async () => { | ||
const keys = await redis.keys(`${KEY_PREFIX}*`) | ||
|
||
if (keys.length > 0) await redis.del(keys) | ||
}) | ||
|
||
|
@@ -24,6 +23,19 @@ test('.create # `name` is required', async t => { | |
t.is(error.name, 'TypeError') | ||
}) | ||
|
||
test('.create # `metadata` must be a flat object', async t => { | ||
const error = await t.throwsAsync(keys.create({ name: '[email protected]', metadata: { tier: { type: 'new' } } })) | ||
|
||
t.is(error.message, "The metadata field 'tier' can't be an object.") | ||
t.is(error.name, 'TypeError') | ||
}) | ||
|
||
test('.create # `metadata` as undefined is omitted', async t => { | ||
const key = keys.create({ name: '[email protected]', metadata: { cc: undefined } }) | ||
|
||
t.is(key.metadata, undefined) | ||
}) | ||
|
||
test('.create # error if plan is invalid', async t => { | ||
const error = await t.throwsAsync(keys.create({ name: '[email protected]', plan: 123 })) | ||
|
||
|
@@ -48,7 +60,22 @@ test('.create', async t => { | |
t.true(key.enabled) | ||
}) | ||
|
||
test('.retrieve', async t => { | ||
test('.create # associate a plan', async t => { | ||
const plan = await plans.create({ | ||
name: 'free tier', | ||
quota: { limit: 3000, period: 'day' } | ||
}) | ||
|
||
const key = await keys.create({ name: '[email protected]', plan: plan.id }) | ||
|
||
t.true(key.id.startsWith('key_')) | ||
t.truthy(key.createdAt) | ||
t.is(key.createdAt, key.updatedAt) | ||
t.is(key.value.length, 16) | ||
t.true(key.enabled) | ||
}) | ||
|
||
test('.retrieve # a key previously created', async t => { | ||
const { id, value } = await keys.create({ name: '[email protected]' }) | ||
|
||
const { createdAt, updatedAt, ...key } = await keys.retrieve(id) | ||
|
@@ -61,6 +88,10 @@ test('.retrieve', async t => { | |
}) | ||
}) | ||
|
||
test('.retrieve # a key not previously created', async t => { | ||
t.is(await keys.retrieve('key_1'), null) | ||
}) | ||
|
||
test('.update', async t => { | ||
const { id, value, createdAt } = await keys.create({ | ||
name: '[email protected]' | ||
|
@@ -86,47 +117,30 @@ test('.update', async t => { | |
t.deepEqual(await keys.retrieve(id), { ...key, updatedAt }) | ||
}) | ||
|
||
test('.update # error if plan is invalid', async t => { | ||
const { id } = await keys.create({ name: '[email protected]' }) | ||
|
||
const error = await t.throwsAsync( | ||
keys.update(id, { | ||
description: 'new description', | ||
enabled: false, | ||
plan: 123 | ||
}) | ||
) | ||
test('.update # error if key is invalid', async t => { | ||
const error = await t.throwsAsync(keys.update('id', { foo: 'bar' })) | ||
t.is(error.message, 'The id `id` must to start with `key_`.') | ||
t.is(error.name, 'TypeError') | ||
}) | ||
|
||
t.is(error.message, 'The id `123` must to start with `plan_`.') | ||
test('.update # error if key does not exist', async t => { | ||
const error = await t.throwsAsync(keys.update('key_id')) | ||
t.is(error.message, 'The key `key_id` does not exist.') | ||
t.is(error.name, 'TypeError') | ||
}) | ||
|
||
test('.update # error if plan does not exist', async t => { | ||
test('.update # error if plan is invalid', async t => { | ||
const { id } = await keys.create({ name: '[email protected]' }) | ||
|
||
const error = await t.throwsAsync( | ||
keys.update(id, { | ||
description: 'new description', | ||
enabled: false, | ||
plan: 'plan_123' | ||
}) | ||
) | ||
|
||
t.is(error.message, 'The plan `plan_123` does not exist.') | ||
const error = await t.throwsAsync(keys.update(id, { plan: 'id' })) | ||
t.is(error.message, 'The id `id` must to start with `plan_`.') | ||
t.is(error.name, 'TypeError') | ||
}) | ||
|
||
test('.update # error if key does not exist', async t => { | ||
{ | ||
const error = await t.throwsAsync(keys.update('id', { foo: 'bar' })) | ||
t.is(error.message, 'The id `id` must to start with `key_`.') | ||
t.is(error.name, 'TypeError') | ||
} | ||
{ | ||
const error = await t.throwsAsync(keys.update('key_id', { foo: 'bar' })) | ||
t.is(error.message, 'The key `key_id` does not exist.') | ||
t.is(error.name, 'TypeError') | ||
} | ||
test('.update # error if plan does not exist', async t => { | ||
const { id } = await keys.create({ name: '[email protected]' }) | ||
const error = await t.throwsAsync(keys.update(id, { plan: 'plan_id' })) | ||
t.is(error.message, 'The plan `plan_id` does not exist.') | ||
t.is(error.name, 'TypeError') | ||
}) | ||
|
||
test('.update # add a plan', async t => { | ||
|
@@ -142,10 +156,39 @@ test('.update # add a plan', async t => { | |
}) | ||
|
||
test('.update # add metadata', async t => { | ||
{ | ||
const { id } = await keys.create({ name: '[email protected]' }) | ||
const key = await keys.update(id, { metadata: { cc: '[email protected]' } }) | ||
t.is(key.metadata.cc, '[email protected]') | ||
} | ||
{ | ||
const { id } = await keys.create({ name: '[email protected]' }) | ||
await keys.update(id, { metadata: { cc: '[email protected]' } }) | ||
const key = await keys.update(id, { metadata: { cc: '[email protected]', version: 2 } }) | ||
|
||
t.is(key.metadata.cc, '[email protected]') | ||
t.is(key.metadata.version, 2) | ||
} | ||
}) | ||
|
||
test('.update # metadata must be a flat object', async t => { | ||
const { id } = await keys.create({ name: '[email protected]' }) | ||
const key = await keys.update(id, { metadata: { cc: '[email protected]' } }) | ||
const error = await t.throwsAsync(keys.update(id, { metadata: { email: { cc: '[email protected]' } } })) | ||
t.is(error.message, "The metadata field 'email' can't be an object.") | ||
t.is(error.name, 'TypeError') | ||
}) | ||
|
||
t.is(key.metadata.cc, '[email protected]') | ||
test('.update # metadata as undefined is omitted', async t => { | ||
{ | ||
const { id } = await keys.create({ name: '[email protected]' }) | ||
const key = await keys.update(id, { metadata: { email: undefined } }) | ||
t.is(key.metadata, undefined) | ||
} | ||
{ | ||
const { id } = await keys.create({ name: '[email protected]' }) | ||
const key = await keys.update(id, { metadata: { cc: '[email protected]', bcc: undefined } }) | ||
t.deepEqual(Object.keys(key.metadata), ['cc']) | ||
} | ||
}) | ||
|
||
test('.update # prevent to add random data', async t => { | ||
|
@@ -155,6 +198,13 @@ test('.update # prevent to add random data', async t => { | |
t.is(key.foo, undefined) | ||
}) | ||
|
||
test('.update # prevent to modify the key id', async t => { | ||
const { id } = await keys.create({ name: '[email protected]' }) | ||
const key = await keys.update(id, { id: 'foo' }) | ||
|
||
t.is(key.id, id) | ||
}) | ||
|
||
test.serial('.list', async t => { | ||
const { id: id1 } = await keys.create({ name: '[email protected]' }) | ||
const { id: id2 } = await keys.create({ name: '[email protected]' }) | ||
|
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