Skip to content

Commit 8bda2c2

Browse files
authored
test: move test/setup & teardown into test/suite.js (#4)
- 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
1 parent 1cdf65d commit 8bda2c2

22 files changed

+659
-164
lines changed

lib/group.js

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
const Mysql = require('./mysql')
22
const Util = require('./util')
33

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

66
class Group {
77
constructor() {}
88

99
async create(args) {
10-
const { error } = validate.group.v2.validate(args)
11-
if (error) console.error(error)
12-
13-
const g = await this.get({ nt_group_id: args.nt_group_id })
14-
if (g.length) return g[0].id
10+
if (args.id) {
11+
const g = await this.get({ id: args.id })
12+
if (g.length) return g[0].id
13+
}
1514

16-
const groupId = await Mysql.insert(`INSERT INTO nt_group`, args)
17-
return groupId
15+
return await Mysql.insert(
16+
`INSERT INTO nt_group`,
17+
Util.mapToDbColumn(args, groupDbMap),
18+
)
1819
}
1920

2021
async get(args) {
21-
args = Util.mapToDbColumn(args, { id: 'nt_group_id' })
2222
return await Mysql.select(
2323
`SELECT nt_group_id AS id, name FROM nt_group WHERE`,
24-
args,
24+
Util.mapToDbColumn(args, groupDbMap),
2525
)
2626
}
2727

@@ -32,12 +32,22 @@ class Group {
3232
, parent_group_id AS parent_gid
3333
, deleted
3434
FROM nt_group WHERE`,
35-
Util.mapToDbColumn(args, { id: 'nt_group_id' }),
35+
Util.mapToDbColumn(args, groupDbMap),
3636
)
3737
}
3838

39+
async delete(args, val) {
40+
const g = await this.getAdmin(args)
41+
if (g.length !== 1) return false
42+
await Mysql.execute(`UPDATE nt_group SET deleted=? WHERE nt_group_id=?`, [
43+
val ?? 1,
44+
g[0].id,
45+
])
46+
return true
47+
}
48+
3949
async destroy(args) {
40-
const g = await this.getAdmin({ nt_group_id: args.nt_group_id })
50+
const g = await this.getAdmin(args)
4151
if (g.length === 1) {
4252
await Mysql.execute(`DELETE FROM nt_group WHERE nt_group_id=?`, [g[0].id])
4353
}

lib/group.test.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const assert = require('node:assert/strict')
2+
const { describe, it, after } = require('node:test')
3+
4+
const group = require('./group')
5+
6+
after(async () => {
7+
group._mysql.disconnect()
8+
})
9+
10+
describe('group', function () {
11+
it('gets group by id', async () => {
12+
const g = await group.get({ id: 4096 })
13+
assert.deepEqual(g[0], {
14+
id: 4096,
15+
name: 'example.com',
16+
})
17+
})
18+
19+
it('gets group by name', async () => {
20+
const u = await group.get({ name: 'example.com' })
21+
assert.deepEqual(u[0], {
22+
id: 4096,
23+
name: 'example.com',
24+
})
25+
})
26+
27+
it('delete a group', async () => {
28+
assert.ok(await group.delete({ id: 4096 }))
29+
let u = await group.getAdmin({ id: 4096 })
30+
assert.equal(u[0].deleted, 1)
31+
await group.delete({ id: 4096 }, 0) // restore
32+
u = await group.getAdmin({ id: 4096 })
33+
assert.equal(u[0].deleted, 0)
34+
})
35+
})

lib/session.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const assert = require('node:assert/strict')
22
const { describe, it, after } = require('node:test')
33

44
const session = require('./session')
5-
const userCase = require('../test/user.json')
5+
const userCase = require('../test/v3/user.json')
66

77
after(async () => {
88
session._mysql.disconnect()
@@ -15,7 +15,7 @@ describe('session', function () {
1515
describe('create', () => {
1616
it('creates a login session', async () => {
1717
sessionId = await session.create({
18-
nt_user_id: userCase.nt_user_id,
18+
nt_user_id: userCase.id,
1919
nt_user_session: '3.0.0',
2020
})
2121
assert.ok(sessionId)

lib/user.js

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
const crypto = require('node:crypto')
2-
const validate = require('@nictool/validate')
32

43
const Mysql = require('./mysql')
54
const Util = require('./util')
65
Util.setEnv()
76
const Config = require('./config')
87

8+
const userDbMap = { id: 'nt_user_id', gid: 'nt_group_id' }
9+
910
class User {
1011
constructor(args) {
1112
this.debug = args?.debug ?? false
@@ -61,24 +62,19 @@ class User {
6162
}
6263

6364
async create(args) {
64-
const { error } = validate.user.v2.validate(args)
65-
if (error) console.error(error)
66-
67-
const u = await this.get({
68-
nt_user_id: args.nt_user_id,
69-
nt_group_id: args.nt_group_id,
70-
})
71-
if (u.length) {
72-
// console.log(u)
73-
return u[0].nt_user_id
74-
}
65+
// console.log(args)
66+
const u = await this.get({ id: args.id, gid: args.gid })
67+
if (u.length === 1) return u[0].id
7568

7669
if (args.password) {
7770
if (!args.pass_salt) args.pass_salt = this.generateSalt()
7871
args.password = await this.hashAuthPbkdf2(args.password, args.pass_salt)
7972
}
8073

81-
const userId = await Mysql.insert(`INSERT INTO nt_user`, args)
74+
const userId = await Mysql.insert(
75+
`INSERT INTO nt_user`,
76+
Util.mapToDbColumn(args, userDbMap),
77+
)
8278
return userId
8379
}
8480

@@ -92,7 +88,7 @@ class User {
9288
, username
9389
, email
9490
FROM nt_user WHERE`,
95-
Util.mapToDbColumn(args, { id: 'nt_user_id', gid: 'nt_group_id' }),
91+
Util.mapToDbColumn(args, userDbMap),
9692
)
9793
}
9894

@@ -108,22 +104,22 @@ class User {
108104
, email
109105
, deleted
110106
FROM nt_user WHERE`,
111-
Util.mapToDbColumn(args, { id: 'nt_user_id', gid: 'nt_group_id' }),
107+
Util.mapToDbColumn(args, userDbMap),
112108
)
113109
}
114110

115111
async delete(args, val) {
116-
const u = await this.getAdmin({ nt_user_id: args.nt_user_id })
117-
if (u.length === 1) {
118-
await Mysql.execute(`UPDATE nt_user SET deleted=? WHERE nt_user_id=?`, [
119-
val ?? 1,
120-
u[0].id,
121-
])
122-
}
112+
const u = await this.getAdmin(args)
113+
if (u.length !== 1) return false
114+
await Mysql.execute(`UPDATE nt_user SET deleted=? WHERE nt_user_id=?`, [
115+
val ?? 1,
116+
u[0].id,
117+
])
118+
return true
123119
}
124120

125121
async destroy(args) {
126-
const u = await this.getAdmin({ nt_user_id: args.nt_user_id })
122+
const u = await this.getAdmin(args)
127123
if (u.length === 1) {
128124
await Mysql.execute(`DELETE FROM nt_user WHERE nt_user_id=?`, [u[0].id])
129125
}

lib/user.test.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ after(async () => {
99

1010
describe('user', function () {
1111
describe('get', function () {
12-
it('finds existing user by nt_user_id', async () => {
13-
const u = await user.get({ nt_user_id: 4096 })
12+
it('finds existing user by id', async () => {
13+
const u = await user.get({ id: 4096 })
1414
// console.log(u)
1515
assert.deepEqual(u[0], {
1616
gid: 4096,
@@ -38,11 +38,11 @@ describe('user', function () {
3838
})
3939

4040
it('deletes a user', async () => {
41-
await user.delete({ nt_user_id: 4096 })
42-
let u = await user.getAdmin({ nt_user_id: 4096 })
41+
assert.ok(await user.delete({ id: 4096 }))
42+
let u = await user.getAdmin({ id: 4096 })
4343
assert.equal(u[0].deleted, 1)
44-
await user.delete({ nt_user_id: 4096 }, 0) // restore
45-
u = await user.getAdmin({ nt_user_id: 4096 })
44+
await user.delete({ id: 4096 }, 0) // restore
45+
u = await user.getAdmin({ id: 4096 })
4646
assert.equal(u[0].deleted, 0)
4747
})
4848
})

lib/util.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,14 @@ exports.meta = {
2020
}
2121

2222
exports.mapToDbColumn = function (args, maps) {
23+
// create an instance, so we don't mangle the original
24+
const newArgs = JSON.parse(JSON.stringify(args))
25+
2326
for (const [key, val] of Object.entries(maps)) {
24-
if (args[key] !== undefined) {
25-
args[val] = args[key]
26-
delete args[key]
27+
if (newArgs[key] !== undefined) {
28+
newArgs[val] = newArgs[key]
29+
delete newArgs[key]
2730
}
2831
}
29-
return args
32+
return newArgs
3033
}

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"develop": "NODE_ENV=development node ./server",
1414
"test": "test/run.sh",
1515
"versions": "npx dependency-version-checker check",
16-
"watch": "node test/.setup.js && node --test --watch; node test/.teardown.js"
16+
"watch": "node test/suite setup && node --test --watch; node test/suite teardown"
1717
},
1818
"repository": {
1919
"type": "git",
@@ -32,15 +32,15 @@
3232
},
3333
"homepage": "https://github.com/NicTool/api#readme",
3434
"devDependencies": {
35-
"eslint": "^8.56.0"
35+
"eslint": "^8.57.0"
3636
},
3737
"dependencies": {
3838
"@hapi/cookie": "^12.0.1",
3939
"@hapi/hapi": "^21.3.3",
4040
"@hapi/hoek": "^11.0.4",
4141
"@hapi/inert": "^7.1.0",
4242
"@hapi/vision": "^7.0.3",
43-
"@nictool/validate": "^0.6.3",
43+
"@nictool/validate": "^0.7.1",
4444
"hapi-swagger": "^17.2.1",
4545
"mysql2": "^3.9.1",
4646
"qs": "^6.11.2",

routes/group.js

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
const validate = require('@nictool/validate')
2+
3+
const Group = require('../lib/group')
4+
const Util = require('../lib/util')
5+
6+
module.exports = (server) => {
7+
server.route([
8+
{
9+
method: 'GET',
10+
path: '/group/{id}',
11+
options: {
12+
response: {
13+
schema: validate.group.GET,
14+
},
15+
tags: ['api'],
16+
},
17+
handler: async (request, h) => {
18+
const groups = await Group.get({
19+
deleted: request.query.deleted ?? 0,
20+
id: parseInt(request.params.id, 10),
21+
})
22+
if (groups.length !== 1) {
23+
return h
24+
.response({
25+
meta: {
26+
api: Util.meta.api,
27+
msg: `No unique group match`,
28+
},
29+
})
30+
.code(204)
31+
}
32+
33+
return h
34+
.response({
35+
group: groups[0],
36+
meta: {
37+
api: Util.meta.api,
38+
msg: `here's your group`,
39+
},
40+
})
41+
.code(200)
42+
},
43+
},
44+
{
45+
method: 'POST',
46+
path: '/group',
47+
options: {
48+
validate: {
49+
payload: validate.group.POST,
50+
},
51+
response: {
52+
schema: validate.group.GET,
53+
},
54+
tags: ['api'],
55+
},
56+
handler: async (request, h) => {
57+
// console.log(request.payload)
58+
const gid = await Group.create(request.payload)
59+
if (!gid) {
60+
console.log(`POST /group oops`) // TODO
61+
}
62+
63+
const groups = await Group.get({ id: gid })
64+
65+
return h
66+
.response({
67+
group: groups[0],
68+
meta: {
69+
api: Util.meta.api,
70+
msg: `I created this group`,
71+
},
72+
})
73+
.code(201)
74+
},
75+
},
76+
{
77+
method: 'DELETE',
78+
path: '/group/{id}',
79+
options: {
80+
response: {
81+
schema: validate.group.GET,
82+
},
83+
tags: ['api'],
84+
},
85+
handler: async (request, h) => {
86+
const groups = await Group.get(request.params)
87+
if (groups.length !== 1) {
88+
return h
89+
.response({
90+
meta: {
91+
api: Util.meta.api,
92+
msg: `No unique group match`,
93+
},
94+
})
95+
.code(204)
96+
}
97+
98+
await Group.delete({ id: groups[0].id })
99+
delete groups[0].gid
100+
101+
return h
102+
.response({
103+
group: groups[0],
104+
meta: {
105+
api: Util.meta.api,
106+
msg: `I deleted that group`,
107+
},
108+
})
109+
.code(200)
110+
},
111+
},
112+
])
113+
}

0 commit comments

Comments
 (0)