-
-
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: 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
- Loading branch information
Showing
22 changed files
with
659 additions
and
164 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
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) | ||
}) | ||
}) |
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
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 |
---|---|---|
@@ -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) | ||
}, | ||
}, | ||
]) | ||
} |
Oops, something went wrong.