-
-
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.
feat(lib/nameserver): added, with tests, fixes #22
- Loading branch information
Showing
6 changed files
with
156 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,8 @@ name: CI | |
|
||
on: | ||
push: | ||
paths-ignore: | ||
- '*.md' | ||
pull_request: | ||
|
||
env: | ||
|
Submodule .release
updated
5 files
+7 −5 | CHANGELOG.md | |
+1 −1 | LICENSE | |
+11 −9 | README.md | |
+7 −6 | npm/prepend-scope.cjs | |
+13 −4 | submit.sh |
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,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 | ||
} |
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,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() |
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,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) | ||
}) | ||
}) |
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