Skip to content

Commit

Permalink
more
Browse files Browse the repository at this point in the history
  • Loading branch information
msimerson committed Mar 4, 2024
1 parent 318a56f commit dbdfe14
Show file tree
Hide file tree
Showing 12 changed files with 124 additions and 128 deletions.
9 changes: 8 additions & 1 deletion lib/group.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Mysql from './mysql.js'
import { mapToDbColumn } from './util.js'

const groupDbMap = { id: 'nt_group_id', parent_gid: 'parent_group_id' }
const boolFields = ['deleted']

class Group {
constructor() {
Expand All @@ -20,7 +21,7 @@ class Group {
}

async get(args) {
return await Mysql.execute(
const rows = await Mysql.execute(
...Mysql.select(
`SELECT nt_group_id AS id
, parent_group_id AS parent_gid
Expand All @@ -30,6 +31,12 @@ class Group {
mapToDbColumn(args, groupDbMap),
),
)
for (const r of rows) {
for (const b of boolFields) {
r[b] = r[b] === 1
}
}
return rows
}

async put(args) {
Expand Down
10 changes: 5 additions & 5 deletions lib/group.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('group', function () {
id: testCase.id,
name: testCase.name,
parent_gid: 0,
deleted: 0,
deleted: false,
})
})

Expand All @@ -30,7 +30,7 @@ describe('group', function () {
id: testCase.id,
name: testCase.name,
parent_gid: 0,
deleted: 0,
deleted: false,
})
})

Expand All @@ -41,7 +41,7 @@ describe('group', function () {
id: testCase.id,
name: 'example.net',
parent_gid: 0,
deleted: 0,
deleted: false,
},
])
assert.ok(await Group.put({ id: testCase.id, name: testCase.name }))
Expand All @@ -50,9 +50,9 @@ describe('group', function () {
it('deletes a group', async () => {
assert.ok(await Group.delete({ id: testCase.id }))
let g = await Group.get({ id: testCase.id, deleted: 1 })
assert.equal(g[0]?.deleted, 1)
assert.equal(g[0]?.deleted, true)
await Group.delete({ id: testCase.id, deleted: 0 }) // restore
g = await Group.get({ id: testCase.id })
assert.equal(g[0].deleted, 0)
assert.equal(g[0].deleted, false)
})
})
27 changes: 16 additions & 11 deletions lib/permission.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class Permission {
}

return await Mysql.execute(
...Mysql.insert(`nt_perm`, mapToDbColumn(objectToDb(args), permDbMap)
))
...Mysql.insert(`nt_perm`, mapToDbColumn(objectToDb(args), permDbMap)),
)
}

async get(args) {
Expand All @@ -36,7 +36,10 @@ class Permission {
FROM nt_perm p`
// Mysql.debug(1)
if (args.deleted === undefined) args.deleted = false
const rows = await Mysql.execute(...Mysql.select(query, mapToDbColumn(args, permDbMap)))

const rows = await Mysql.execute(
...Mysql.select(query, mapToDbColumn(args, permDbMap)),
)
if (rows.length === 0) return
if (rows.length > 1) {
throw new Error(
Expand Down Expand Up @@ -68,11 +71,13 @@ class Permission {
const id = args.id
delete args.id
// Mysql.debug(1)
const r = await Mysql.execute(...Mysql.update(
`nt_perm`,
`nt_perm_id=${id}`,
mapToDbColumn(args, permDbMap),
))
const r = await Mysql.execute(
...Mysql.update(
`nt_perm`,
`nt_perm_id=${id}`,
mapToDbColumn(args, permDbMap),
),
)
return r.changedRows === 1
}

Expand All @@ -85,9 +90,9 @@ class Permission {
}

async destroy(args) {
const p = await this.get(args)
if (!p) return false
return await Mysql.execute(...Mysql.delete(`nt_perm`, mapToDbColumn(args, permDbMap)))
return await Mysql.execute(
...Mysql.delete(`nt_perm`, mapToDbColumn(args, permDbMap)),
)
}
}

Expand Down
26 changes: 13 additions & 13 deletions lib/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ class Session {
const r = await this.get(args)
if (r) return r.id

const id = await Mysql.execute(...Mysql.insert(
`nt_user_session`,
mapToDbColumn(args, sessionDbMap),
))
const id = await Mysql.execute(
...Mysql.insert(`nt_user_session`, mapToDbColumn(args, sessionDbMap)),
)
return id
}

Expand Down Expand Up @@ -64,20 +63,21 @@ class Session {

const id = args.id
delete args.id
const r = await Mysql.execute(...Mysql.update(
`nt_user_session`,
`nt_user_session_id=${id}`,
mapToDbColumn(args, sessionDbMap),
))
const r = await Mysql.execute(
...Mysql.update(
`nt_user_session`,
`nt_user_session_id=${id}`,
mapToDbColumn(args, sessionDbMap),
),
)
// console.log(r)
return r.changedRows === 1
}

async delete(args) {
const r = await Mysql.execute(...Mysql.delete(
`nt_user_session`,
mapToDbColumn(args, sessionDbMap),
))
const r = await Mysql.execute(
...Mysql.delete(`nt_user_session`, mapToDbColumn(args, sessionDbMap)),
)
return r.affectedRows === 1
}
}
Expand Down
32 changes: 20 additions & 12 deletions lib/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Config from './config.js'
import { mapToDbColumn } from './util.js'

const userDbMap = { id: 'nt_user_id', gid: 'nt_group_id' }
const boolFields = [ 'is_admin', 'deleted' ]
const boolFields = ['is_admin', 'deleted']

class User {
constructor(args = {}) {
Expand Down Expand Up @@ -71,14 +71,17 @@ class User {
args.password = await this.hashAuthPbkdf2(args.password, args.pass_salt)
}

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

async get(args) {
if (args.deleted === undefined) args.deleted = false
const rows = await Mysql.execute(...Mysql.select(
`SELECT email
const rows = await Mysql.execute(
...Mysql.select(
`SELECT email
, first_name
, last_name
, nt_group_id AS gid
Expand All @@ -87,8 +90,9 @@ class User {
, email
, deleted
FROM nt_user`,
mapToDbColumn(args, userDbMap),
))
mapToDbColumn(args, userDbMap),
),
)
for (const r of rows) {
for (const b of boolFields) {
r[b] = r[b] === 1
Expand All @@ -101,11 +105,13 @@ class User {
if (!args.id) return false
const id = args.id
delete args.id
const r = await Mysql.execute(...Mysql.update(
`nt_user`,
`nt_user_id=${id}`,
mapToDbColumn(args, userDbMap),
))
const r = await Mysql.execute(
...Mysql.update(
`nt_user`,
`nt_user_id=${id}`,
mapToDbColumn(args, userDbMap),
),
)
return r.changedRows === 1
}

Expand All @@ -118,7 +124,9 @@ class User {
}

async destroy(args) {
await Mysql.execute(...Mysql.delete(`nt_user`, mapToDbColumn({ id: u[0].id }, userDbMap)))
await Mysql.execute(
...Mysql.delete(`nt_user`, mapToDbColumn({ id: args.id }, userDbMap)),
)
}

generateSalt(length = 16) {
Expand Down
31 changes: 15 additions & 16 deletions routes/permission.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,20 @@ function PermissionRoutes(server) {
path: '/permission/{id}',
options: {
validate: {
// params: ??,
query: validate.permission.GET,
query: validate.permission.v3,
},
response: {
schema: validate.permission.GET,
},
tags: ['api'],
},
handler: async (request, h) => {
// console.log(request.params)

const permission = await Permission.get({
deleted: request.query.deleted ?? 0,
const getArgs = {
deleted: request.query.deleted === true ? 1 : 0,
id: parseInt(request.params.id, 10),
})
}

const permission = await Permission.get(getArgs)

return h
.response({
Expand Down Expand Up @@ -72,14 +71,17 @@ function PermissionRoutes(server) {
method: 'DELETE',
path: '/permission/{id}',
options: {
// response: {
// schema: validate.permission.GET,
// },
validate: {
query: validate.permission.DELETE,
},
response: {
schema: validate.permission.GET,
},
tags: ['api'],
},
handler: async (request, h) => {
const permission = await Permission.get({
deleted: parseInt(request.query.deleted ?? 0),
deleted: request.query.deleted === true ? 1 : 0,
id: parseInt(request.params.id, 10),
})

Expand All @@ -94,13 +96,10 @@ function PermissionRoutes(server) {
.code(404)
}

Check warning on line 97 in routes/permission.js

View check run for this annotation

Codecov / codecov/patch

routes/permission.js#L89-L97

Added lines #L89 - L97 were not covered by tests

const action = request.query.destroy === 'true' ? 'destroy' : 'delete'
// console.log(`action: ${action}`)
await Permission[action]({
await Permission.delete({
id: permission.id,
deleted: permission.deleted,
deleted: 1,
})
delete permission.gid

return h
.response({
Expand Down
26 changes: 8 additions & 18 deletions routes/permission.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ before(async () => {
await Permission.create(permCase)
})

let case2Id = 4094

after(async () => {
Permission.destroy({ id: case2Id })
await server.stop()
})

Expand Down Expand Up @@ -53,13 +56,11 @@ describe('permission routes', () => {
assert.equal(res.result.permission.nameserver.create, false)
})

let case2Id = 4094

it('POST /permission', async () => {
it(`POST /permission (${case2Id})`, async () => {
const testCase = JSON.parse(JSON.stringify(permCase))
testCase.id = case2Id // make it unique
testCase.uid = case2Id
testCase.gid = case2Id
testCase.user.id = case2Id
testCase.group.id = case2Id
testCase.name = `Route Test Permission 2`
delete testCase.deleted
// console.log(testCase)
Expand Down Expand Up @@ -120,25 +121,14 @@ describe('permission routes', () => {
it(`GET /permission/${case2Id} (deleted)`, async () => {
const res = await server.inject({
method: 'GET',
url: `/permission/${case2Id}?deleted=1`,
headers: {
Cookie: sessionCookie,
},
})
// console.log(res.result)
assert.equal(res.statusCode, 200)
})

it(`DELETE /permission/${case2Id}`, async () => {
const res = await server.inject({
method: 'DELETE',
url: `/permission/${case2Id}?deleted=1&destroy=true`,
url: `/permission/${case2Id}?deleted=true`,
headers: {
Cookie: sessionCookie,
},
})
// console.log(res.result)
assert.equal(res.statusCode, 200)
assert.ok(res.result.permission)
})

it('DELETE /session', async () => {
Expand Down
Loading

0 comments on commit dbdfe14

Please sign in to comment.