diff --git a/lib/group.js b/lib/group.js index 3431546..d29c823 100644 --- a/lib/group.js +++ b/lib/group.js @@ -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), ) } @@ -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]) } diff --git a/lib/group.test.js b/lib/group.test.js new file mode 100644 index 0000000..14abab2 --- /dev/null +++ b/lib/group.test.js @@ -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) + }) +}) diff --git a/lib/session.test.js b/lib/session.test.js index c27062d..53729d0 100644 --- a/lib/session.test.js +++ b/lib/session.test.js @@ -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() @@ -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) diff --git a/lib/user.js b/lib/user.js index df73150..d0c7b3f 100644 --- a/lib/user.js +++ b/lib/user.js @@ -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 @@ -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 } @@ -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), ) } @@ -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]) } diff --git a/lib/user.test.js b/lib/user.test.js index ec645d9..9e927e2 100644 --- a/lib/user.test.js +++ b/lib/user.test.js @@ -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, @@ -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) }) }) diff --git a/lib/util.js b/lib/util.js index 851d6da..e78b43f 100644 --- a/lib/util.js +++ b/lib/util.js @@ -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 } diff --git a/package.json b/package.json index 9446f37..54b3d28 100644 --- a/package.json +++ b/package.json @@ -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", @@ -32,7 +32,7 @@ }, "homepage": "https://github.com/NicTool/api#readme", "devDependencies": { - "eslint": "^8.56.0" + "eslint": "^8.57.0" }, "dependencies": { "@hapi/cookie": "^12.0.1", @@ -40,7 +40,7 @@ "@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", diff --git a/routes/group.js b/routes/group.js new file mode 100644 index 0000000..966a9e0 --- /dev/null +++ b/routes/group.js @@ -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) + }, + }, + ]) +} diff --git a/routes/group.test.js b/routes/group.test.js new file mode 100644 index 0000000..70693c7 --- /dev/null +++ b/routes/group.test.js @@ -0,0 +1,120 @@ +const assert = require('node:assert/strict') +const { describe, it, before, after } = require('node:test') + +const { init } = require('./index') +const Group = require('../lib/group') +const groupCase = require('../test/v3/group.json') + +before(async () => { + const userCase = require('../test/v3/user.json') + this.server = await init() + const res = await this.server.inject({ + method: 'POST', + url: '/session', + payload: { + username: `${userCase.username}@example.com`, + password: 'Wh@tA-Decent#P6ssw0rd', + }, + }) + assert.ok(res.headers['set-cookie'][0]) + this.sessionCookie = res.headers['set-cookie'][0].split(';')[0] +}) + +after(async () => { + const res = await this.server.inject({ + method: 'DELETE', + url: '/session', + headers: { + Cookie: this.sessionCookie, + }, + }) + // console.log(res.result) + assert.equal(res.statusCode, 200) + + await this.server.stop() +}) + +describe('group', () => { + it('GET /group/4096', async () => { + const res = await this.server.inject({ + method: 'GET', + url: '/group/4096', + headers: { + Cookie: this.sessionCookie, + }, + }) + // console.log(res.result) + assert.equal(res.statusCode, 200) + }) + + it('POST /group', async () => { + const testCase = JSON.parse(JSON.stringify(groupCase)) + testCase.id = 4095 // make it unique + testCase.name = `example2.com` + delete testCase.deleted + // console.log(testCase) + + const res = await this.server.inject({ + method: 'POST', + url: '/group', + headers: { + Cookie: this.sessionCookie, + }, + payload: testCase, + }) + // console.log(res.result) + assert.equal(res.statusCode, 201) + }) + + it('GET /group', async () => { + const res = await this.server.inject({ + method: 'GET', + url: '/group/4095', + headers: { + Cookie: this.sessionCookie, + }, + }) + // console.log(res.result) + assert.equal(res.statusCode, 200) + }) + + it('DELETE /group', async () => { + const res = await this.server.inject({ + method: 'DELETE', + url: '/group/4095', + headers: { + Cookie: this.sessionCookie, + }, + }) + // console.log(res.result) + assert.equal(res.statusCode, 200) + }) + + it('GET /group/4095', async () => { + const res = await this.server.inject({ + method: 'GET', + url: '/group/4095', + headers: { + Cookie: this.sessionCookie, + }, + }) + // console.log(res.result) + assert.equal(res.statusCode, 204) + }) + + it('GET /group/4095 (deleted)', async () => { + const res = await this.server.inject({ + method: 'GET', + url: '/group/4095?deleted=1', + headers: { + Cookie: this.sessionCookie, + }, + }) + // console.log(res.result) + assert.equal(res.statusCode, 200) + }) + + it('nukes /group/4095', async () => { + assert.ok(Group.destroy({ id: 4095 })) + }) +}) diff --git a/routes/index.js b/routes/index.js index 236e121..382d371 100644 --- a/routes/index.js +++ b/routes/index.js @@ -17,9 +17,6 @@ const Config = require('../lib/config') const Session = require('../lib/session') const User = require('../lib/user') -const SessionRoutes = require('./session') -const UserRoutes = require('./user') - let server const setup = async () => { @@ -78,8 +75,9 @@ const setup = async () => { }, }) - SessionRoutes(server) - UserRoutes(server) + require('./group')(server) + require('./user')(server) + require('./session')(server) server.route({ method: '*', @@ -111,3 +109,33 @@ process.on('unhandledRejection', (err) => { console.error(err) process.exit(1) }) + +/* + server.route({ + method: 'POST', // GET PUT POST DELETE + path: '/', + handler: (request, h) => { + // request.query + // request.params + // request.payload + // console.log(request.payload) + return 'Hello Login World!' + }, + options: { + auth: { mode: 'try' }, + // plugins: { + // cookie: { + // redirectTo: false, + // } + // }, + // response: {}, + validate: { + // headers: true, + // query: true, + params: validate.login, + // payload: true, + // state: true, + }, + }, + }), +*/ diff --git a/routes/session.js b/routes/session.js index 80924a7..c6d179b 100644 --- a/routes/session.js +++ b/routes/session.js @@ -12,7 +12,7 @@ module.exports = (server) => { path: '/session', options: { response: { - schema: validate.user.sessionOut, + schema: validate.session.GET, }, tags: ['api'], }, @@ -40,10 +40,10 @@ module.exports = (server) => { options: { auth: { mode: 'try' }, validate: { - payload: validate.user.sessionPost, + payload: validate.session.POST, }, response: { - schema: validate.user.sessionOut, + schema: validate.session.GET, }, tags: ['api'], }, @@ -93,7 +93,7 @@ module.exports = (server) => { }, options: { response: { - schema: validate.user.sessionOut, + schema: validate.session.GET, }, tags: ['api'], }, diff --git a/routes/session.test.js b/routes/session.test.js index 358fdc0..b0749ad 100644 --- a/routes/session.test.js +++ b/routes/session.test.js @@ -2,7 +2,7 @@ const assert = require('node:assert/strict') const { describe, it, before, after } = require('node:test') const { init } = require('./index') -const userCase = require('../test/user.json') +const userCase = require('../test/v3/user.json') before(async () => { this.server = await init() diff --git a/routes/user.js b/routes/user.js index 962964f..1784bd1 100644 --- a/routes/user.js +++ b/routes/user.js @@ -10,17 +10,59 @@ module.exports = (server) => { path: '/user', options: { response: { - schema: validate.user.userGET, + schema: validate.user.GET, }, - tags: ['api'], + // tags: ['api'], }, handler: async (request, h) => { + // get myself const { nt_user_id } = request.state['sid-nictool'] const users = await User.get({ id: nt_user_id }) + + delete users[0].gid + return h + .response({ + user: users[0], + meta: { + api: Util.meta.api, + msg: `this is you`, + }, + }) + .code(200) + }, + }, + { + method: 'GET', + path: '/user/{id}', + options: { + response: { + schema: validate.user.GET, + }, + tags: ['api'], + }, + handler: async (request, h) => { + const users = await User.get({ + deleted: request.query.deleted ?? 0, + id: parseInt(request.params.id, 10), + }) + if (users.length !== 1) { + return h + .response({ + meta: { + api: Util.meta.api, + msg: `No unique user match`, + }, + }) + .code(204) + } + + const gid = parseInt(users[0].gid, 10) delete users[0].gid + return h .response({ user: users[0], + group: { id: gid }, meta: { api: Util.meta.api, msg: `here's your user`, @@ -29,35 +71,76 @@ module.exports = (server) => { .code(200) }, }, - ]) -} + { + method: 'POST', + path: '/user', + options: { + validate: { + payload: validate.user.userPOST, + }, + response: { + schema: validate.user.GET, + }, + tags: ['api'], + }, + handler: async (request, h) => { + // console.log(request.payload) + const uid = await User.create(request.payload) + if (!uid) { + console.log(`POST /user oops`) // TODO + } + + const users = await User.get({ id: uid }) + const group = { id: users[0].gid } + delete users[0].gid -/* - server.route({ - method: 'POST', // GET PUT POST DELETE - path: '/', - handler: (request, h) => { - // request.query - // request.params - // request.payload - // console.log(request.payload) - return 'Hello Login World!' + return h + .response({ + user: users[0], + group, + meta: { + api: Util.meta.api, + msg: `I created this user`, + }, + }) + .code(201) + }, }, - options: { - auth: { mode: 'try' }, - // plugins: { - // cookie: { - // redirectTo: false, - // } - // }, - // response: {}, - validate: { - // headers: true, - // query: true, - params: validate.login, - // payload: true, - // state: true, + { + method: 'DELETE', + path: '/user/{id}', + options: { + response: { + schema: validate.user.GET, + }, + tags: ['api'], + }, + handler: async (request, h) => { + const users = await User.get(request.params) + if (users.length !== 1) { + return h + .response({ + meta: { + api: Util.meta.api, + msg: `No unique user match`, + }, + }) + .code(204) + } + + await User.delete({ id: users[0].id }) + delete users[0].gid + + return h + .response({ + user: users[0], + meta: { + api: Util.meta.api, + msg: `I deleted that user`, + }, + }) + .code(200) }, }, - }), -*/ + ]) +} diff --git a/routes/user.test.js b/routes/user.test.js index a509fa1..90380d5 100644 --- a/routes/user.test.js +++ b/routes/user.test.js @@ -2,7 +2,8 @@ const assert = require('node:assert/strict') const { describe, it, before, after } = require('node:test') const { init } = require('./index') -const userCase = require('../test/user.json') +const User = require('../lib/user') +const userCase = require('../test/v3/user.json') before(async () => { this.server = await init() @@ -48,4 +49,86 @@ describe('user', () => { // console.log(res.result) assert.equal(res.statusCode, 200) }) + + it('GET /user/4096', async () => { + const res = await this.server.inject({ + method: 'GET', + url: '/user/4096', + headers: { + Cookie: this.sessionCookie, + }, + }) + // console.log(res.result) + assert.equal(res.statusCode, 200) + }) + + it('POST /user', async () => { + const testCase = JSON.parse(JSON.stringify(userCase)) + testCase.id = 4095 // make it unique + testCase.username = `${testCase.username}2` + delete testCase.deleted + + const res = await this.server.inject({ + method: 'POST', + url: '/user', + headers: { + Cookie: this.sessionCookie, + }, + payload: testCase, + }) + // console.log(res.result) + assert.equal(res.statusCode, 201) + }) + + it('GET /user', async () => { + const res = await this.server.inject({ + method: 'GET', + url: '/user/4095', + headers: { + Cookie: this.sessionCookie, + }, + }) + // console.log(res.result) + assert.equal(res.statusCode, 200) + }) + + it('DELETE /user', async () => { + const res = await this.server.inject({ + method: 'DELETE', + url: '/user/4095', + headers: { + Cookie: this.sessionCookie, + }, + }) + // console.log(res.result) + assert.equal(res.statusCode, 200) + }) + + it('GET /user/4095', async () => { + const res = await this.server.inject({ + method: 'GET', + url: '/user/4095', + headers: { + Cookie: this.sessionCookie, + }, + }) + // console.log(res.result) + assert.equal(res.statusCode, 204) + }) + + it('GET /user/4095 (deleted)', async () => { + const res = await this.server.inject({ + method: 'GET', + url: '/user/4095?deleted=1', + headers: { + Cookie: this.sessionCookie, + }, + }) + // console.log(res.result) + assert.equal(res.statusCode, 200) + }) + + it('nukes /user/4095', async () => { + assert.ok(User.destroy({ id: 4095 })) + }) }) diff --git a/test/.setup.js b/test/.setup.js deleted file mode 100644 index e36ed74..0000000 --- a/test/.setup.js +++ /dev/null @@ -1,41 +0,0 @@ -const group = require('../lib/group') -const user = require('../lib/user') -// const session = require('../lib/session') - -const userCase = require('./user.json') -const groupCase = require('./group.json') - -const setup = async () => { - await createTestGroup() - await createTestUser() - // await createTestSession() - await user._mysql.disconnect() - await group._mysql.disconnect() - process.exit() -} - -setup() - -async function createTestGroup() { - let g = group.get({ nt_group_id: groupCase.nt_group_id }) - if (g.length === 1) return - - await group.create(groupCase) -} - -async function createTestUser() { - let u = await user.get({ nt_user_id: userCase.nt_user_id }) - if (u.length === 1) return - - const instance = JSON.parse(JSON.stringify(userCase)) - instance.password = 'Wh@tA-Decent#P6ssw0rd' - - await user.create(instance) -} - -async function createTestSession() { - this.sessionId = await session.create({ - nt_user_id: userCase.nt_user_id, - nt_user_session: '3.0.0', - }) -} diff --git a/test/.teardown.js b/test/.teardown.js deleted file mode 100644 index fea981b..0000000 --- a/test/.teardown.js +++ /dev/null @@ -1,28 +0,0 @@ -const group = require('../lib/group') -const user = require('../lib/user') -// const session = require('../lib/session') -const userCase = require('./user.json') -const groupCase = require('./group.json') - -const teardown = async () => { - // await destroyTestSession() - await destroyTestUser() - await destroyTestGroup() - // await user._mysql.disconnect() - // await group._mysql.disconnect() - process.exit() -} - -teardown() - -async function destroyTestGroup() { - await group.destroy({ nt_group_id: groupCase.nt_group_id }) -} - -async function destroyTestUser() { - await user.destroy({ nt_user_id: userCase.nt_user_id }) -} - -async function destroyTestSession() { - // await session.destroy({ nt_user_id: ... }) -} diff --git a/test/run.sh b/test/run.sh index 5b81082..69abdad 100755 --- a/test/run.sh +++ b/test/run.sh @@ -1,5 +1,5 @@ #!/bin/sh -node test/.setup.js +node test/suite.js setup node --test -node test/.teardown.js \ No newline at end of file +node test/suite.js teardown diff --git a/test/suite.js b/test/suite.js new file mode 100644 index 0000000..45e2ffd --- /dev/null +++ b/test/suite.js @@ -0,0 +1,76 @@ +'use strict' + +const path = require('node:path') + +const group = require('../lib/group') +const user = require('../lib/user') +// const session = require('../lib/session') +const userCase = require('./v3/user.json') +const groupCase = require('./v3/group.json') + +switch (process.argv[2]) { + case 'setup': + setup() + break + case 'teardown': + teardown() + break + default: + console.log( + `\nusage:\tnode ${path.basename(process.argv[1])} [ setup | teardown ]\n`, + ) +} + +async function setup() { + await createTestGroup() + await createTestUser() + // await createTestSession() + await user._mysql.disconnect() + await group._mysql.disconnect() + process.exit(0) +} + +async function createTestGroup() { + let g = group.get({ id: groupCase.id }) + if (g.length === 1) return + + await group.create(groupCase) +} + +async function createTestUser() { + let u = await user.get({ id: userCase.id }) + if (u.length === 1) return + + const instance = JSON.parse(JSON.stringify(userCase)) + instance.password = 'Wh@tA-Decent#P6ssw0rd' + + await user.create(instance) +} + +// async function createTestSession() { +// this.sessionId = await session.create({ +// nt_user_id: userCase.nt_user_id, +// nt_user_session: '3.0.0', +// }) +// } + +async function teardown() { + // await destroyTestSession() + await destroyTestUser() + await destroyTestGroup() + // await user._mysql.disconnect() + // await group._mysql.disconnect() + process.exit(0) +} + +async function destroyTestGroup() { + await group.destroy({ id: groupCase.id }) +} + +async function destroyTestUser() { + await user.destroy({ id: userCase.id }) +} + +// async function destroyTestSession() { +// await session.destroy({ nt_user_id: ... }) +// } diff --git a/test/group.json b/test/v2/group.json similarity index 80% rename from test/group.json rename to test/v2/group.json index 1b684d5..41c8429 100644 --- a/test/group.json +++ b/test/v2/group.json @@ -2,5 +2,5 @@ "nt_group_id": 4096, "parent_group_id": 0, "name": "example.com", - "deleted": false + "deleted": 0 } diff --git a/test/user.json b/test/v2/user.json similarity index 75% rename from test/user.json rename to test/v2/user.json index 13a6f92..860b489 100644 --- a/test/user.json +++ b/test/v2/user.json @@ -3,7 +3,8 @@ "nt_group_id": 4096, "username": "unit-test", "email": "unit-test@example.com", + "password": "What@%Fun34Security", "first_name": "Unit", "last_name": "Test", - "deleted": false + "deleted": 0 } diff --git a/test/v3/group.json b/test/v3/group.json new file mode 100644 index 0000000..1c31eaa --- /dev/null +++ b/test/v3/group.json @@ -0,0 +1,6 @@ +{ + "id": 4096, + "parent_gid": 0, + "name": "example.com", + "deleted": false +} diff --git a/test/v3/user.json b/test/v3/user.json new file mode 100644 index 0000000..32b9a59 --- /dev/null +++ b/test/v3/user.json @@ -0,0 +1,10 @@ +{ + "id": 4096, + "gid": 4096, + "username": "unit-test", + "email": "unit-test@example.com", + "password": "What@%Fun34Security", + "first_name": "Unit", + "last_name": "Test", + "deleted": false +}