-
-
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(routes/nameserver): added, with tests
- Loading branch information
Showing
7 changed files
with
297 additions
and
11 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
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
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
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
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,117 @@ | ||
import validate from '@nictool/validate' | ||
|
||
import Nameserver from '../lib/nameserver.js' | ||
import { meta } from '../lib/util.js' | ||
|
||
function NameserverRoutes(server) { | ||
server.route([ | ||
{ | ||
method: 'GET', | ||
path: '/nameserver/{id}', | ||
options: { | ||
validate: { | ||
query: validate.nameserver.v3, | ||
}, | ||
response: { | ||
schema: validate.nameserver.GET, | ||
}, | ||
tags: ['api'], | ||
}, | ||
handler: async (request, h) => { | ||
const getArgs = { | ||
deleted: request.query.deleted === true ? 1 : 0, | ||
id: parseInt(request.params.id, 10), | ||
} | ||
|
||
const nameservers = await Nameserver.get(getArgs) | ||
|
||
return h | ||
.response({ | ||
nameserver: nameservers[0], | ||
meta: { | ||
api: meta.api, | ||
msg: `here's your nameserver`, | ||
}, | ||
}) | ||
.code(200) | ||
}, | ||
}, | ||
{ | ||
method: 'POST', | ||
path: '/nameserver', | ||
options: { | ||
validate: { | ||
payload: validate.nameserver.POST, | ||
}, | ||
response: { | ||
schema: validate.nameserver.GET, | ||
}, | ||
tags: ['api'], | ||
}, | ||
handler: async (request, h) => { | ||
const id = await Nameserver.create(request.payload) | ||
|
||
const nameservers = await Nameserver.get({ id }) | ||
|
||
return h | ||
.response({ | ||
nameserver: nameservers[0], | ||
meta: { | ||
api: meta.api, | ||
msg: `the nameserver was created`, | ||
}, | ||
}) | ||
.code(201) | ||
}, | ||
}, | ||
{ | ||
method: 'DELETE', | ||
path: '/nameserver/{id}', | ||
options: { | ||
validate: { | ||
query: validate.nameserver.DELETE, | ||
}, | ||
response: { | ||
schema: validate.nameserver.GET, | ||
}, | ||
tags: ['api'], | ||
}, | ||
handler: async (request, h) => { | ||
const nameservers = await Nameserver.get({ | ||
deleted: request.query.deleted === true ? 1 : 0, | ||
id: parseInt(request.params.id, 10), | ||
}) | ||
|
||
if (!nameservers) { | ||
return h | ||
.response({ | ||
meta: { | ||
api: meta.api, | ||
msg: `I couldn't find that nameserver`, | ||
}, | ||
}) | ||
.code(404) | ||
} | ||
|
||
await Nameserver.delete({ | ||
id: nameservers[0].id, | ||
deleted: 1, | ||
}) | ||
|
||
return h | ||
.response({ | ||
nameserver: nameservers[0], | ||
meta: { | ||
api: meta.api, | ||
msg: `I deleted that nameserver`, | ||
}, | ||
}) | ||
.code(200) | ||
}, | ||
}, | ||
]) | ||
} | ||
|
||
export default NameserverRoutes | ||
|
||
export { Nameserver, NameserverRoutes } |
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,138 @@ | ||
import assert from 'node:assert/strict' | ||
import { describe, it, before, after } from 'node:test' | ||
|
||
import { init } from './index.js' | ||
import Group from '../lib/group.js' | ||
import User from '../lib/user.js' | ||
import Nameserver from '../lib/nameserver.js' | ||
|
||
import groupCase from './test/group.json' with { type: 'json' } | ||
import userCase from './test/user.json' with { type: 'json' } | ||
import nsCase from './test/nameserver.json' with { type: 'json' } | ||
|
||
let server | ||
let case2Id = 4094 | ||
|
||
before(async () => { | ||
await Nameserver.destroy({ id: case2Id }) | ||
await Group.create(groupCase) | ||
await User.create(userCase) | ||
await Nameserver.create(nsCase) | ||
server = await init() | ||
}) | ||
|
||
|
||
after(async () => { | ||
await server.stop() | ||
}) | ||
|
||
describe('nameserver routes', () => { | ||
let sessionCookie | ||
|
||
it('POST /session establishes a session', async () => { | ||
const res = await server.inject({ | ||
method: 'POST', | ||
url: '/session', | ||
payload: { | ||
username: `${userCase.username}@${groupCase.name}`, | ||
password: userCase.password, | ||
}, | ||
}) | ||
assert.ok(res.headers['set-cookie'][0]) | ||
sessionCookie = res.headers['set-cookie'][0].split(';')[0] | ||
}) | ||
|
||
it(`GET /nameserver/${nsCase.id}`, async () => { | ||
const res = await server.inject({ | ||
method: 'GET', | ||
url: `/nameserver/${nsCase.id}`, | ||
headers: { | ||
Cookie: sessionCookie, | ||
}, | ||
}) | ||
// console.log(res.result) | ||
assert.equal(res.statusCode, 200) | ||
assert.equal(res.result.nameserver.name, nsCase.name) | ||
}) | ||
|
||
it(`POST /nameserver (${case2Id})`, async () => { | ||
const testCase = JSON.parse(JSON.stringify(nsCase)) | ||
testCase.id = case2Id // make it unique | ||
testCase.gid = case2Id | ||
testCase.name = 'c.ns.example.com.' | ||
|
||
const res = await server.inject({ | ||
method: 'POST', | ||
url: '/nameserver', | ||
headers: { | ||
Cookie: sessionCookie, | ||
}, | ||
payload: testCase, | ||
}) | ||
// console.log(res.result) | ||
assert.equal(res.statusCode, 201) | ||
assert.ok(res.result.nameserver.gid) | ||
}) | ||
|
||
it(`GET /nameserver/${case2Id}`, async () => { | ||
const res = await server.inject({ | ||
method: 'GET', | ||
url: `/nameserver/${case2Id}`, | ||
headers: { | ||
Cookie: sessionCookie, | ||
}, | ||
}) | ||
// console.log(res.result) | ||
assert.equal(res.statusCode, 200) | ||
assert.ok(res.result.nameserver.gid) | ||
}) | ||
|
||
it(`DELETE /nameserver/${case2Id}`, async () => { | ||
const res = await server.inject({ | ||
method: 'DELETE', | ||
url: `/nameserver/${case2Id}`, | ||
headers: { | ||
Cookie: sessionCookie, | ||
}, | ||
}) | ||
// console.log(res.result) | ||
assert.equal(res.statusCode, 200) | ||
}) | ||
|
||
it(`GET /nameserver/${case2Id}`, async () => { | ||
const res = await server.inject({ | ||
method: 'GET', | ||
url: `/nameserver/${case2Id}`, | ||
headers: { | ||
Cookie: sessionCookie, | ||
}, | ||
}) | ||
// console.log(res.result) | ||
// assert.equal(res.statusCode, 200) | ||
assert.equal(res.result.nameserver, undefined) | ||
}) | ||
|
||
it(`GET /nameserver/${case2Id} (deleted)`, async () => { | ||
const res = await server.inject({ | ||
method: 'GET', | ||
url: `/nameserver/${case2Id}?deleted=true`, | ||
headers: { | ||
Cookie: sessionCookie, | ||
}, | ||
}) | ||
// console.log(res.result) | ||
assert.equal(res.statusCode, 200) | ||
assert.ok(res.result.nameserver) | ||
}) | ||
|
||
it('DELETE /session', async () => { | ||
const res = await server.inject({ | ||
method: 'DELETE', | ||
url: '/session', | ||
headers: { | ||
Cookie: sessionCookie, | ||
}, | ||
}) | ||
assert.equal(res.statusCode, 200) | ||
}) | ||
}) |
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,19 @@ | ||
{ | ||
"id": 4095, | ||
"gid": 4095, | ||
"name": "b.ns.example.com.", | ||
"description": "", | ||
"address": "2.3.4.5", | ||
"address6": "2001:DB8::2", | ||
"remote_login": "nsd", | ||
"logdir": "/foo", | ||
"datadir": "/bar", | ||
"export": { | ||
"interval": 0, | ||
"serials": true, | ||
"status": "last run:03-05 15:25<br>last cp :09-20 12:59", | ||
"type": "NSD" | ||
}, | ||
"ttl": 3600, | ||
"deleted": false | ||
} |