Skip to content

Commit

Permalink
feat(lib/nameserver): added, with tests, fixes #22
Browse files Browse the repository at this point in the history
  • Loading branch information
msimerson committed Mar 7, 2024
1 parent ad0f021 commit 85a1e6f
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 2 deletions.
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
16 changes: 16 additions & 0 deletions lib/test/zone.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"id": 4096,
"gid": 4096,
"zone": "example.com",
"mailaddr": "hostmaster.example.com.",
"description": "unit test",
"serial": 20240306,
"refresh": 1,
"retry": 2,
"expire": 3,
"minimum": 4,
"ttl": 3600,
"location": "",
"last_publish": null,
"deleted": false
}
89 changes: 89 additions & 0 deletions lib/zone.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import Mysql from './mysql.js'
import { mapToDbColumn } from './util.js'

const zoneDbMap = { id: 'nt_zone_id', gid: 'nt_group_id' }
const boolFields = ['deleted']

class Zone {
constructor() {
this.mysql = Mysql
}

async create(args) {
if (args.id) {
const g = await this.get({ id: args.id })
if (g.length === 1) return g[0].id
}

return await Mysql.execute(
...Mysql.insert(
`nt_zone`,
mapToDbColumn(args, zoneDbMap),
),
)
}

async get(args) {
const rows = await Mysql.execute(
...Mysql.select(
`SELECT nt_zone_id AS id
, nt_group_id AS gid
, zone
, mailaddr
, description
, serial
, refresh
, retry
, expire
, minimum
, ttl
, location
, last_modified
, last_publish
, deleted
FROM nt_zone`,
mapToDbColumn(args, zoneDbMap),
),
)
for (const r of rows) {
for (const b of boolFields) {
r[b] = r[b] === 1
}
for (const f of ['description', 'location']) {
if ([null].includes(r[f])) r[f] = ''
}
}

return rows
}

async put(args) {
if (!args.id) return false
const id = args.id
delete args.id
const r = await Mysql.execute(
...Mysql.update(
`nt_zone`,
`nt_zone_id=${id}`,
mapToDbColumn(args, zoneDbMap),
),
)
return r.changedRows === 1
}

async delete(args) {
await Mysql.execute(
`UPDATE nt_zone SET deleted=? WHERE nt_zone_id=?`,
[args.deleted ?? 1, args.id],
)
return true
}

async destroy(args) {
return await Mysql.execute(
...Mysql.delete(`nt_zone`, { nt_zone_id: args.id }),
)
}
}

export default new Zone()
48 changes: 48 additions & 0 deletions lib/zone.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import assert from 'node:assert/strict'
import { describe, it, after, before } from 'node:test'

import Zone from './zone.js'

import testCase from './test/zone.json' with { type: 'json' }

before(async () => {
await Zone.destroy({ id: testCase.id })
await Zone.create(testCase)
})

after(async () => {
// await Zone.destroy({ id: testCase.id })
Zone.mysql.disconnect()
})

describe('zone', function () {
it('gets zone by id', async () => {
const g = await Zone.get({ id: testCase.id })
delete g[0].last_modified
assert.deepEqual(g[0], testCase)
})

it('gets zone by name', async () => {
const g = await Zone.get({ zone: testCase.zone })
delete g[0].last_modified
assert.deepEqual(g[0], testCase)
})

it('changes a zone', async () => {
assert.ok(
await Zone.put({ id: testCase.id, mailaddr: 'toastmaster.example.com.' }),
)
const ns = await Zone.get({ id: testCase.id })
assert.deepEqual(ns[0].mailaddr, 'toastmaster.example.com.')
assert.ok(await Zone.put({ id: testCase.id, mailaddr: testCase.mailaddr }))
})

it('deletes a zone', async () => {
assert.ok(await Zone.delete({ id: testCase.id }))
let g = await Zone.get({ id: testCase.id, deleted: 1 })
assert.equal(g[0]?.deleted, true)
await Zone.delete({ id: testCase.id, deleted: 0 }) // restore
g = await Zone.get({ id: testCase.id })
assert.equal(g[0].deleted, false)
})
})
1 change: 0 additions & 1 deletion routes/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ function UserRoutes(server) {
tags: ['api'],
},
handler: async (request, h) => {

const users = await User.get({
deleted: request.query.deleted ?? 0,
id: parseInt(request.params.id, 10),
Expand Down

0 comments on commit 85a1e6f

Please sign in to comment.