Skip to content

Commit

Permalink
user, session, group updates (#6)
Browse files Browse the repository at this point in the history
- session: split to separate file
- user: sessionPost -> session.POST
- test: switch file names to \*.test.js
- group: added GET & POST
- shared: added meta
  • Loading branch information
msimerson authored Feb 25, 2024
1 parent fd1edbf commit 9eb88e1
Show file tree
Hide file tree
Showing 15 changed files with 122 additions and 95 deletions.
2 changes: 2 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ codecov.yml
.release
.travis.yml
.eslintrc.yaml
.eslintrc.yml
.eslintrc.json
.prettier.yml
8 changes: 4 additions & 4 deletions test/index.js → index.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const assert = require('node:assert/strict')
const { describe, it } = require('node:test')

const schema = require('../index')
const schema = require('./index')

describe('index', function () {
const testGroup = require('./fixtures/v2/group.json')
const testUser = require('./fixtures/v2/user.json')
const testNs = require('./fixtures/v2/nameserver.json')
const testGroup = require('./test/fixtures/v2/group.json')
const testUser = require('./test/fixtures/v2/user.json')
const testNs = require('./test/fixtures/v2/nameserver.json')

it('exports user', () => {
const testCase = JSON.parse(JSON.stringify(testUser))
Expand Down
14 changes: 14 additions & 0 deletions lib/group.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,17 @@ exports.v2 = Joi.object({
has_children: Joi.boolean(),
permission: shared.permission,
})

exports.GET = Joi.object({
group: Joi.object({
id: exports.id,
name: exports.name,
}),
meta: shared.meta,
})

exports.POST = Joi.object({
id: exports.id,
name: exports.name,
parent_gid: exports.id,
})
4 changes: 2 additions & 2 deletions test/group.js → lib/group.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const assert = require('node:assert/strict')
const { describe, it } = require('node:test')

const schema = require('../lib/group').v2
const testGroup = require('./fixtures/v2/group.json')
const schema = require('./group').v2
const testGroup = require('../test/fixtures/v2/group.json')

describe('group', function () {
describe('name', function () {
Expand Down
4 changes: 2 additions & 2 deletions test/nameserver.js → lib/nameserver.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const assert = require('node:assert/strict')
const { describe, it } = require('node:test')

const schema = require('../lib/nameserver').nameserver
const testNS = require('./fixtures/v2/nameserver.json')
const schema = require('./nameserver').nameserver
const testNS = require('../test/fixtures/v2/nameserver.json')

describe('nameserver', function () {
describe('name', function () {
Expand Down
23 changes: 23 additions & 0 deletions lib/session.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const Joi = require('joi')

const shared = require('./shared')
const group = require('./group')
const user = require('./user')

exports.POST = Joi.object({
username: user.username.required(),
password: user.password.required(),
})

exports.GET = Joi.object({
user: user.v3,
group: Joi.object({
id: group.id,
name: group.name,
}),
session: Joi.object({
id: Joi.number().integer().min(1).max(4294967295),
last_access: Joi.number().integer().min(1).max(4294967295),
}),
meta: shared.meta,
})
30 changes: 30 additions & 0 deletions lib/session.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const assert = require('node:assert/strict')
const { describe, it } = require('node:test')

const schema = require('./session')

describe('session', () => {
describe('POST', function () {
it('accepts valid', () => {
const { error } = schema.POST.validate({
username: 'valid',
password: 'ab12CD#%alph',
})
assert.ifError(error)
})

it('rejects missing username', () => {
const { error } = schema.POST.validate({
password: 'ab12CD#%alph',
})
assert.strictEqual(error.message, '"username" is required')
})

it('rejects missing password', () => {
const { error } = schema.POST.validate({
username: 'valid',
})
assert.strictEqual(error.message, '"password" is required')
})
})
})
8 changes: 7 additions & 1 deletion lib/shared.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
const Joi = require('joi')

exports.nt_group_id = Joi.number().integer().positive()
exports.meta = Joi.object({
msg: Joi.string(),
err: Joi.string(),
api: Joi.object({
version: Joi.string(),
}).unknown(),
}).unknown()

exports.permission = Joi.object({
group: Joi.object({
Expand Down
2 changes: 1 addition & 1 deletion test/shared.js → lib/shared.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const assert = require('node:assert/strict')
const { describe, it } = require('node:test')

const shared = require('../lib/shared')
const shared = require('./shared')

describe('shared', function () {
describe('ttl', function () {
Expand Down
40 changes: 19 additions & 21 deletions lib/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const Joi = require('joi')
const { joiPasswordExtendCore } = require('joi-password')
const JoiPassword = Joi.extend(joiPasswordExtendCore)

// const shared = require('./shared')
const shared = require('./shared')
const group = require('./group')

exports.id = Joi.number().integer().min(1).max(4294967295)
Expand All @@ -29,46 +29,44 @@ exports.v3 = Joi.object({
first_name: Joi.string().min(1),
last_name: Joi.string().min(1),
username: exports.username,
password: exports.password,
email: exports.email,
password: exports.password,
is_admin: Joi.boolean(),
deleted: Joi.boolean(),
})

exports.sessionIn = Joi.object({
username: exports.username.required(),
password: exports.password.required(),
})

exports.sessionOut = Joi.object({
exports.GET = Joi.object({
user: exports.v3,
group: Joi.object({
id: group.id,
name: group.name,
}),
session: Joi.object({
id: Joi.number().integer().min(1).max(4294967295),
last_access: Joi.number().integer().min(1).max(4294967295),
}),
meta: Joi.object(),
meta: shared.meta,
})

exports.POST = Joi.object({
id: exports.id,
gid: group.id,
first_name: Joi.string().min(1),
last_name: Joi.string().min(1),
username: exports.username,
email: exports.email,
password: exports.password,
is_admin: Joi.boolean(),
})

// v2 API response
exports.v2 = Joi.object({
nt_user_id: exports.id,
nt_group_id: group.id,

first_name: Joi.string().min(1),
last_name: Joi.string().min(1),

username: exports.username.required(),
email: exports.email,
username: exports.username,
password: exports.password,

email: exports.email.required(),

is_admin: Joi.boolean(),

deleted: Joi.boolean(),
is_admin: Joi.number().min(0).max(1),
deleted: Joi.number().min(0).max(1),

// nt_user_session: Joi.string(),
// last_access: Joi.string(),
Expand Down
66 changes: 11 additions & 55 deletions test/user.js → lib/user.test.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,36 @@
const assert = require('node:assert/strict')
const { describe, it } = require('node:test')

const schema = require('../lib/user')
const schema = require('./user')

const userV2 = require('./fixtures/v2/user.json')
const userV2 = require('../test/fixtures/v2/user.json')

describe('user', function () {
describe('v2', function () {
it('accepts valid', () => {
const testCase = JSON.parse(JSON.stringify(userV2))

const { error, value } = schema.v2.validate(testCase)

assert.ifError(error)
assert.deepEqual(value, testCase)
})

it('rejects missing username', () => {
const testCase = JSON.parse(JSON.stringify(userV2))
delete testCase.username

const { error, value } = schema.v2.validate(testCase)

assert.strictEqual(error.message, '"username" is required')
assert.deepEqual(value, testCase)
})
})

describe('v3', function () {
const userV3 = require('./fixtures/v3/user.json')
const userV3 = require('../test/fixtures/v3/user.json')
it('accepts valid', () => {
const testCase = JSON.parse(JSON.stringify(userV3))
const { error, value } = schema.v3.validate(testCase)
assert.ifError(error)
assert.deepEqual(value, testCase)
})

it('rejects missing username', () => {
const testCase = JSON.parse(JSON.stringify(userV3))
delete testCase.username

const { error, value } = schema.v2.validate(testCase)

assert.strictEqual(error.message, '"username" is required')
assert.deepEqual(value, testCase)
})
})

describe('username', () => {
it('rejects empty', () => {
const { error } = schema.username.validate('')
assert.ok(/not allowed to be empty/.test(error.message))
})

it('rejects too short', () => {
const { error, value } = schema.username.validate('u')
assert.ok(/length must be at least 3 characters long/.test(error.message))
Expand Down Expand Up @@ -86,12 +69,9 @@ describe('user', function () {
assert.equal(value, email)
})

it('rejects missing', () => {
const testCase = JSON.parse(JSON.stringify(userV2))
delete testCase.email
const { error, value } = schema.v2.validate(testCase)
assert.strictEqual(error.message, '"email" is required')
assert.deepEqual(value, testCase)
it('rejects empty', () => {
const { error } = schema.email.validate('')
assert.ok(error)
})

it('rejects invalid format', () => {
Expand Down Expand Up @@ -124,28 +104,4 @@ describe('user', function () {
assert.deepEqual(value, password)
})
})

describe('sessionIn', function () {
it('accepts valid', () => {
const { error } = schema.sessionIn.validate({
username: 'valid',
password: 'ab12CD#%alph',
})
assert.ifError(error)
})

it('rejects missing username', () => {
const { error } = schema.sessionIn.validate({
password: 'ab12CD#%alph',
})
assert.strictEqual(error.message, '"username" is required')
})

it('rejects missing password', () => {
const { error } = schema.sessionIn.validate({
username: 'valid',
})
assert.strictEqual(error.message, '"password" is required')
})
})
})
4 changes: 2 additions & 2 deletions test/zone.js → lib/zone.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const assert = require('node:assert/strict')
const { describe, it } = require('node:test')

const schema = require('../lib/zone').zone
const testZone = require('./fixtures/v2/zone.json')
const schema = require('./zone').zone
const testZone = require('../test/fixtures/v2/zone.json')

describe('zone', function () {
describe('zone', function () {
Expand Down
4 changes: 2 additions & 2 deletions test/zone_record.js → lib/zone_record.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const assert = require('node:assert/strict')
const { describe, it } = require('node:test')

const schema = require('../lib/zone_record').zone_record
const testZR = require('./fixtures/v2/zone_record.json')
const schema = require('./zone_record').zone_record
const testZR = require('../test/fixtures/v2/zone_record.json')

describe('zone_record', function () {
describe('nt_zone_id', function () {
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nictool/validate",
"version": "0.6.3",
"version": "0.7.0",
"description": "NicTool Object Validation",
"main": "index.js",
"directories": {
Expand All @@ -13,9 +13,9 @@
"lint:fix": "npm run lint --fix",
"prettier": "npx prettier --ignore-path .gitignore --check .",
"prettier:fix": "npx prettier --ignore-path .gitignore --write .",
"test": "test/run.sh",
"test": "node --test",
"versions": "npx dependency-version-checker check",
"watch": "npm run test -- -w"
"watch": "node --test --watch"
},
"repository": {
"type": "git",
Expand Down
2 changes: 0 additions & 2 deletions test/run.sh

This file was deleted.

0 comments on commit 9eb88e1

Please sign in to comment.