Skip to content

Commit

Permalink
Release v3.0.0-alpha.5 (#31)
Browse files Browse the repository at this point in the history
- feat(lib/zone): added, with tests, fixes #22
- feat(lib/zone_record): added, with tests, fixes #23
- feat: default GET sets deleted=false
  - group, nameserver, permission, user, zone
- sql: return indicative boolean for delete
- test(zr): added maps from NT SQL 2 to dns-rr std formats
* feat(zr): added dbToObject and objectToDb
* user: avoid logging auth data
* feat(zrs): test every type of zone record
  • Loading branch information
msimerson authored Mar 11, 2024
1 parent ad0f021 commit b38c5bf
Show file tree
Hide file tree
Showing 56 changed files with 1,044 additions and 108 deletions.
3 changes: 3 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# These are supported funding model platforms

github: msimerson
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ name: CI

on:
push:
paths-ignore:
- '*.md'
pull_request:

env:
Expand Down
2 changes: 1 addition & 1 deletion .release
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@
### Unreleased


### [3.0.0-alpha.5] - 2024-03-06

- feat(lib/zone): added, with tests, fixes #22
- feat(lib/zone_record): added, with tests, fixes #23
- feat: default GET sets deleted=false
- group, nameserver, permission, user, zone
- sql: return indicative boolean for delete
- test(zr): added maps from NT SQL 2 to dns-rr std formats

### [3.0.0-alpha.4] - 2024-03-05

- feat(lib/nameserver): added, with tests
Expand All @@ -21,3 +30,4 @@

[3.0.0-alpha.3]: https://github.com/NicTool/api/releases/tag/3.0.0-alpha.3
[3.0.0-alpha.4]: https://github.com/NicTool/api/releases/tag/3.0.0-alpha.4
[3.0.0-alpha.5]: https://github.com/NicTool/api/releases/tag/3.0.0-alpha.5
24 changes: 14 additions & 10 deletions lib/group.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ class Group {
}

async get(args) {
args = JSON.parse(JSON.stringify(args))
if (args.deleted === undefined) args.deleted = false

const rows = await Mysql.execute(
...Mysql.select(
`SELECT nt_group_id AS id
Expand All @@ -31,10 +34,11 @@ class Group {
mapToDbColumn(args, groupDbMap),
),
)
for (const r of rows) {
for (const row of rows) {
for (const b of boolFields) {
r[b] = r[b] === 1
row[b] = row[b] === 1
}
if (args.deleted === false) delete row.deleted
}
return rows
}
Expand All @@ -43,30 +47,30 @@ class Group {
if (!args.id) return false
const id = args.id
delete args.id
// Mysql.debug(1)
const r = await Mysql.execute(
...Mysql.update(
`nt_group`,
`nt_group_id=${id}`,
mapToDbColumn(args, groupDbMap),
),
)
// console.log(r)
return r.changedRows === 1
}

async delete(args) {
await Mysql.execute(`UPDATE nt_group SET deleted=? WHERE nt_group_id=?`, [
args.deleted ?? 1,
args.id,
])
return true
const r = await Mysql.execute(
...Mysql.update(`nt_group`, `nt_group_id=${args.id}`, {
deleted: args.deleted ?? 1,
}),
)
return r.changedRows === 1
}

async destroy(args) {
return await Mysql.execute(
const r = await Mysql.execute(
...Mysql.delete(`nt_group`, { nt_group_id: args.id }),
)
return r.affectedRows === 1
}
}

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

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

Expand All @@ -41,7 +39,6 @@ describe('group', function () {
id: testCase.id,
name: 'example.net',
parent_gid: 0,
deleted: false,
},
])
assert.ok(await Group.put({ id: testCase.id, name: testCase.name }))
Expand All @@ -53,6 +50,6 @@ describe('group', function () {
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, false)
assert.equal(g[0].deleted, undefined)
})
})
21 changes: 13 additions & 8 deletions lib/nameserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ class Nameserver {
}

async get(args) {
args = JSON.parse(JSON.stringify(args))
if (args.deleted === undefined) args.deleted = false

if (args.name !== undefined) {
args['ns.name'] = args.name
delete args.name
Expand All @@ -61,10 +64,11 @@ class Nameserver {
mapToDbColumn(args, nsDbMap),
),
)
for (const r of rows) {
for (const row of rows) {
for (const b of boolFields) {
r[b] = r[b] === 1
row[b] = row[b] === 1
}
if (args.deleted === false) delete row.deleted
}
return dbToObject(rows)
}
Expand All @@ -81,22 +85,23 @@ class Nameserver {
mapToDbColumn(args, nsDbMap),
),
)
// console.log(r)
return r.changedRows === 1
}

async delete(args) {
await Mysql.execute(
`UPDATE nt_nameserver SET deleted=? WHERE nt_nameserver_id=?`,
[args.deleted ?? 1, args.id],
const r = await Mysql.execute(
...Mysql.update(`nt_nameserver`, `nt_nameserver_id=${args.id}`, {
deleted: args.deleted ?? 1,
}),
)
return true
return r.changedRows === 1
}

async destroy(args) {
return await Mysql.execute(
const r = await Mysql.execute(
...Mysql.delete(`nt_nameserver`, { nt_nameserver_id: args.id }),
)
return r.affectedRows === 1
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/nameserver.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ describe('nameserver', function () {
assert.equal(g[0]?.deleted, true)
await Nameserver.delete({ id: testCase.id, deleted: 0 }) // restore
g = await Nameserver.get({ id: testCase.id })
assert.equal(g[0].deleted, false)
assert.equal(g[0].deleted, undefined)
})
})
100 changes: 54 additions & 46 deletions lib/permission.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ class Permission {
}

async get(args) {
args = JSON.parse(JSON.stringify(args))
if (args.deleted === undefined) args.deleted = false

const query = `SELECT p.nt_perm_id AS id
, p.nt_user_id AS uid
, p.nt_group_id AS gid
Expand All @@ -34,8 +37,6 @@ class Permission {
${getPermFields()}
, p.deleted
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)),
Expand All @@ -46,7 +47,9 @@ class Permission {
`permissions.get found ${rows.length} rows for uid ${args.uid}`,
)
}
return dbToObject(rows[0])
const row = dbToObject(rows[0])
if (args.deleted === false) delete row.deleted
return row
}

async getGroup(args) {
Expand All @@ -59,18 +62,19 @@ class Permission {
, p.deleted
FROM nt_perm p
INNER JOIN nt_user u ON p.nt_group_id = u.nt_group_id
WHERE p.deleted=0
WHERE p.deleted=${args.deleted === true ? 1 : 0}
AND u.deleted=0
AND u.nt_user_id=?`
const rows = await Mysql.execute(...Mysql.select(query, [args.uid]))
return dbToObject(rows[0])
const row = dbToObject(rows[0])
if ([false, undefined].includes(args.deleted)) delete row.deleted
return row
}

async put(args) {
if (!args.id) return false
const id = args.id
delete args.id
// Mysql.debug(1)
const r = await Mysql.execute(
...Mysql.update(
`nt_perm`,
Expand All @@ -82,17 +86,20 @@ class Permission {
}

async delete(args) {
await Mysql.execute(`UPDATE nt_perm SET deleted=? WHERE nt_perm_id=?`, [
args.deleted ?? 1,
args.id,
])
return true
if (!args.id) return false
const r = await Mysql.execute(
...Mysql.update(`nt_perm`, `nt_perm_id=${args.id}`, {
deleted: args.deleted ?? 1,
}),
)
return r.changedRows === 1
}

async destroy(args) {
return await Mysql.execute(
const r = await Mysql.execute(
...Mysql.delete(`nt_perm`, mapToDbColumn(args, permDbMap)),
)
return r.affectedRows === 1
}
}

Expand Down Expand Up @@ -185,60 +192,61 @@ JSON object format:
const boolFields = ['self_write', 'inherit', 'deleted']

function dbToObject(row) {
const newRow = JSON.parse(JSON.stringify(row))
row = JSON.parse(JSON.stringify(row))
for (const f of ['group', 'nameserver', 'zone', 'zonerecord', 'user']) {
for (const p of ['create', 'write', 'delete', 'delegate']) {
if (newRow[`${f}_${p}`] !== undefined) {
if (newRow[f] === undefined) newRow[f] = {}
newRow[f][p] = newRow[`${f}_${p}`] === 1
delete newRow[`${f}_${p}`]
if (row[`${f}_${p}`] !== undefined) {
if (row[f] === undefined) row[f] = {}
row[f][p] = row[`${f}_${p}`] === 1
delete row[`${f}_${p}`]
}
}
}
for (const b of boolFields) {
newRow[b] = newRow[b] === 1
row[b] = row[b] === 1
}
if (newRow.uid !== undefined) {
newRow.user.id = newRow.uid
delete newRow.uid

if (row.uid !== undefined) {
row.user.id = row.uid
delete row.uid
}
if (newRow.gid !== undefined) {
newRow.group.id = newRow.gid
delete newRow.gid
if (row.gid !== undefined) {
row.group.id = row.gid
delete row.gid
}
newRow.nameserver.usable = []
if (![undefined, ''].includes(newRow.usable_ns)) {
newRow.nameserver.usable = newRow.usable_ns.split(',')
row.nameserver.usable = []
if (![undefined, ''].includes(row.usable_ns)) {
row.nameserver.usable = row.usable_ns.split(',')
}
delete newRow.usable_ns
return newRow
delete row.usable_ns
return row
}

function objectToDb(row) {
const newRow = JSON.parse(JSON.stringify(row))
if (newRow?.user?.id !== undefined) {
newRow.uid = newRow.user.id
delete newRow.user.id
row = JSON.parse(JSON.stringify(row))
if (row?.user?.id !== undefined) {
row.uid = row.user.id
delete row.user.id
}
if (newRow?.group?.id !== undefined) {
newRow.gid = newRow.group.id
delete newRow.group.id
if (row?.group?.id !== undefined) {
row.gid = row.group.id
delete row.group.id
}
if (newRow?.nameserver?.usable !== undefined) {
newRow.usable_ns = newRow.nameserver.usable.join(',')
delete newRow.nameserver.usable
if (row?.nameserver?.usable !== undefined) {
row.usable_ns = row.nameserver.usable.join(',')
delete row.nameserver.usable
}
for (const f of ['group', 'nameserver', 'zone', 'zonerecord', 'user']) {
for (const p of ['create', 'write', 'delete', 'delegate']) {
if (newRow[f] === undefined) continue
if (newRow[f][p] === undefined) continue
newRow[`${f}_${p}`] = newRow[f][p] === true ? 1 : 0
delete newRow[f][p]
if (row[f] === undefined) continue
if (row[f][p] === undefined) continue
row[`${f}_${p}`] = row[f][p] === true ? 1 : 0
delete row[f][p]
}
delete newRow[f]
delete row[f]
}
for (const b of boolFields) {
newRow[b] = newRow[b] === true ? 1 : 0
row[b] = row[b] === true ? 1 : 0
}
return newRow
return row
}
5 changes: 2 additions & 3 deletions lib/permission.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,11 @@ describe('permission', function () {
assert.equal(p?.deleted, true)
await Permission.delete({ id: permTestCase.id, deleted: 0 }) // restore
p = await Permission.get({ id: permTestCase.id })
assert.equal(p.deleted, false)
assert.equal(p.deleted, undefined)
})

it('destroys a permission', async () => {
const r = await Permission.destroy({ id: permTestCase.id })
assert.equal(r.affectedRows, 1)
assert.ok(await Permission.destroy({ id: permTestCase.id }))
const p = await Permission.get({ id: permTestCase.id })
assert.equal(p, undefined)
})
Expand Down
3 changes: 1 addition & 2 deletions lib/test/group.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"id": 4096,
"parent_gid": 0,
"name": "example.com",
"deleted": false
"name": "example.com"
}
Loading

0 comments on commit b38c5bf

Please sign in to comment.