Skip to content

Commit

Permalink
test: move test/setup & teardown into test/suite.js (#4)
Browse files Browse the repository at this point in the history
- so that running 'node --test' doesn't do teardown during test suite runs
- user: GET, POST, DELETE mostly working
- group: add GET,POST,DELETE routes
* user: GET, POST, DELETE mostly working
* group: add GET,POST,DELETE routes
  • Loading branch information
msimerson authored Feb 25, 2024
1 parent 1cdf65d commit 8bda2c2
Show file tree
Hide file tree
Showing 22 changed files with 659 additions and 164 deletions.
34 changes: 22 additions & 12 deletions lib/group.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
const Mysql = require('./mysql')
const Util = require('./util')

const validate = require('@nictool/validate')
const groupDbMap = { id: 'nt_group_id', parent_gid: 'parent_group_id' }

class Group {
constructor() {}

async create(args) {
const { error } = validate.group.v2.validate(args)
if (error) console.error(error)

const g = await this.get({ nt_group_id: args.nt_group_id })
if (g.length) return g[0].id
if (args.id) {
const g = await this.get({ id: args.id })
if (g.length) return g[0].id
}

const groupId = await Mysql.insert(`INSERT INTO nt_group`, args)
return groupId
return await Mysql.insert(
`INSERT INTO nt_group`,
Util.mapToDbColumn(args, groupDbMap),
)
}

async get(args) {
args = Util.mapToDbColumn(args, { id: 'nt_group_id' })
return await Mysql.select(
`SELECT nt_group_id AS id, name FROM nt_group WHERE`,
args,
Util.mapToDbColumn(args, groupDbMap),
)
}

Expand All @@ -32,12 +32,22 @@ class Group {
, parent_group_id AS parent_gid
, deleted
FROM nt_group WHERE`,
Util.mapToDbColumn(args, { id: 'nt_group_id' }),
Util.mapToDbColumn(args, groupDbMap),
)
}

async delete(args, val) {
const g = await this.getAdmin(args)
if (g.length !== 1) return false
await Mysql.execute(`UPDATE nt_group SET deleted=? WHERE nt_group_id=?`, [
val ?? 1,
g[0].id,
])
return true
}

async destroy(args) {
const g = await this.getAdmin({ nt_group_id: args.nt_group_id })
const g = await this.getAdmin(args)
if (g.length === 1) {
await Mysql.execute(`DELETE FROM nt_group WHERE nt_group_id=?`, [g[0].id])
}
Expand Down
35 changes: 35 additions & 0 deletions lib/group.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const assert = require('node:assert/strict')
const { describe, it, after } = require('node:test')

const group = require('./group')

after(async () => {
group._mysql.disconnect()
})

describe('group', function () {
it('gets group by id', async () => {
const g = await group.get({ id: 4096 })
assert.deepEqual(g[0], {
id: 4096,
name: 'example.com',
})
})

it('gets group by name', async () => {
const u = await group.get({ name: 'example.com' })
assert.deepEqual(u[0], {
id: 4096,
name: 'example.com',
})
})

it('delete a group', async () => {
assert.ok(await group.delete({ id: 4096 }))
let u = await group.getAdmin({ id: 4096 })
assert.equal(u[0].deleted, 1)
await group.delete({ id: 4096 }, 0) // restore
u = await group.getAdmin({ id: 4096 })
assert.equal(u[0].deleted, 0)
})
})
4 changes: 2 additions & 2 deletions lib/session.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const assert = require('node:assert/strict')
const { describe, it, after } = require('node:test')

const session = require('./session')
const userCase = require('../test/user.json')
const userCase = require('../test/v3/user.json')

after(async () => {
session._mysql.disconnect()
Expand All @@ -15,7 +15,7 @@ describe('session', function () {
describe('create', () => {
it('creates a login session', async () => {
sessionId = await session.create({
nt_user_id: userCase.nt_user_id,
nt_user_id: userCase.id,
nt_user_session: '3.0.0',
})
assert.ok(sessionId)
Expand Down
42 changes: 19 additions & 23 deletions lib/user.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
const crypto = require('node:crypto')
const validate = require('@nictool/validate')

const Mysql = require('./mysql')
const Util = require('./util')
Util.setEnv()
const Config = require('./config')

const userDbMap = { id: 'nt_user_id', gid: 'nt_group_id' }

class User {
constructor(args) {
this.debug = args?.debug ?? false
Expand Down Expand Up @@ -61,24 +62,19 @@ class User {
}

async create(args) {
const { error } = validate.user.v2.validate(args)
if (error) console.error(error)

const u = await this.get({
nt_user_id: args.nt_user_id,
nt_group_id: args.nt_group_id,
})
if (u.length) {
// console.log(u)
return u[0].nt_user_id
}
// console.log(args)
const u = await this.get({ id: args.id, gid: args.gid })
if (u.length === 1) return u[0].id

if (args.password) {
if (!args.pass_salt) args.pass_salt = this.generateSalt()
args.password = await this.hashAuthPbkdf2(args.password, args.pass_salt)
}

const userId = await Mysql.insert(`INSERT INTO nt_user`, args)
const userId = await Mysql.insert(
`INSERT INTO nt_user`,
Util.mapToDbColumn(args, userDbMap),
)
return userId
}

Expand All @@ -92,7 +88,7 @@ class User {
, username
, email
FROM nt_user WHERE`,
Util.mapToDbColumn(args, { id: 'nt_user_id', gid: 'nt_group_id' }),
Util.mapToDbColumn(args, userDbMap),
)
}

Expand All @@ -108,22 +104,22 @@ class User {
, email
, deleted
FROM nt_user WHERE`,
Util.mapToDbColumn(args, { id: 'nt_user_id', gid: 'nt_group_id' }),
Util.mapToDbColumn(args, userDbMap),
)
}

async delete(args, val) {
const u = await this.getAdmin({ nt_user_id: args.nt_user_id })
if (u.length === 1) {
await Mysql.execute(`UPDATE nt_user SET deleted=? WHERE nt_user_id=?`, [
val ?? 1,
u[0].id,
])
}
const u = await this.getAdmin(args)
if (u.length !== 1) return false
await Mysql.execute(`UPDATE nt_user SET deleted=? WHERE nt_user_id=?`, [
val ?? 1,
u[0].id,
])
return true
}

async destroy(args) {
const u = await this.getAdmin({ nt_user_id: args.nt_user_id })
const u = await this.getAdmin(args)
if (u.length === 1) {
await Mysql.execute(`DELETE FROM nt_user WHERE nt_user_id=?`, [u[0].id])
}
Expand Down
12 changes: 6 additions & 6 deletions lib/user.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ after(async () => {

describe('user', function () {
describe('get', function () {
it('finds existing user by nt_user_id', async () => {
const u = await user.get({ nt_user_id: 4096 })
it('finds existing user by id', async () => {
const u = await user.get({ id: 4096 })
// console.log(u)
assert.deepEqual(u[0], {
gid: 4096,
Expand Down Expand Up @@ -38,11 +38,11 @@ describe('user', function () {
})

it('deletes a user', async () => {
await user.delete({ nt_user_id: 4096 })
let u = await user.getAdmin({ nt_user_id: 4096 })
assert.ok(await user.delete({ id: 4096 }))
let u = await user.getAdmin({ id: 4096 })
assert.equal(u[0].deleted, 1)
await user.delete({ nt_user_id: 4096 }, 0) // restore
u = await user.getAdmin({ nt_user_id: 4096 })
await user.delete({ id: 4096 }, 0) // restore
u = await user.getAdmin({ id: 4096 })
assert.equal(u[0].deleted, 0)
})
})
Expand Down
11 changes: 7 additions & 4 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ exports.meta = {
}

exports.mapToDbColumn = function (args, maps) {
// create an instance, so we don't mangle the original
const newArgs = JSON.parse(JSON.stringify(args))

for (const [key, val] of Object.entries(maps)) {
if (args[key] !== undefined) {
args[val] = args[key]
delete args[key]
if (newArgs[key] !== undefined) {
newArgs[val] = newArgs[key]
delete newArgs[key]
}
}
return args
return newArgs
}
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"develop": "NODE_ENV=development node ./server",
"test": "test/run.sh",
"versions": "npx dependency-version-checker check",
"watch": "node test/.setup.js && node --test --watch; node test/.teardown.js"
"watch": "node test/suite setup && node --test --watch; node test/suite teardown"
},
"repository": {
"type": "git",
Expand All @@ -32,15 +32,15 @@
},
"homepage": "https://github.com/NicTool/api#readme",
"devDependencies": {
"eslint": "^8.56.0"
"eslint": "^8.57.0"
},
"dependencies": {
"@hapi/cookie": "^12.0.1",
"@hapi/hapi": "^21.3.3",
"@hapi/hoek": "^11.0.4",
"@hapi/inert": "^7.1.0",
"@hapi/vision": "^7.0.3",
"@nictool/validate": "^0.6.3",
"@nictool/validate": "^0.7.1",
"hapi-swagger": "^17.2.1",
"mysql2": "^3.9.1",
"qs": "^6.11.2",
Expand Down
113 changes: 113 additions & 0 deletions routes/group.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
const validate = require('@nictool/validate')

const Group = require('../lib/group')
const Util = require('../lib/util')

module.exports = (server) => {
server.route([
{
method: 'GET',
path: '/group/{id}',
options: {
response: {
schema: validate.group.GET,
},
tags: ['api'],
},
handler: async (request, h) => {
const groups = await Group.get({
deleted: request.query.deleted ?? 0,
id: parseInt(request.params.id, 10),
})
if (groups.length !== 1) {
return h
.response({
meta: {
api: Util.meta.api,
msg: `No unique group match`,
},
})
.code(204)
}

return h
.response({
group: groups[0],
meta: {
api: Util.meta.api,
msg: `here's your group`,
},
})
.code(200)
},
},
{
method: 'POST',
path: '/group',
options: {
validate: {
payload: validate.group.POST,
},
response: {
schema: validate.group.GET,
},
tags: ['api'],
},
handler: async (request, h) => {
// console.log(request.payload)
const gid = await Group.create(request.payload)
if (!gid) {
console.log(`POST /group oops`) // TODO
}

const groups = await Group.get({ id: gid })

return h
.response({
group: groups[0],
meta: {
api: Util.meta.api,
msg: `I created this group`,
},
})
.code(201)
},
},
{
method: 'DELETE',
path: '/group/{id}',
options: {
response: {
schema: validate.group.GET,
},
tags: ['api'],
},
handler: async (request, h) => {
const groups = await Group.get(request.params)
if (groups.length !== 1) {
return h
.response({
meta: {
api: Util.meta.api,
msg: `No unique group match`,
},
})
.code(204)
}

await Group.delete({ id: groups[0].id })
delete groups[0].gid

return h
.response({
group: groups[0],
meta: {
api: Util.meta.api,
msg: `I deleted that group`,
},
})
.code(200)
},
},
])
}
Loading

0 comments on commit 8bda2c2

Please sign in to comment.