-
-
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.
- Loading branch information
Showing
7 changed files
with
288 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"id": 4095, | ||
"gid": 4095, | ||
"zone": "route.example.com", | ||
"mailaddr": "route-master.example.com.", | ||
"description": "route test", | ||
"serial": 20240306, | ||
"refresh": 2, | ||
"retry": 3, | ||
"expire": 4, | ||
"minimum": 5, | ||
"ttl": 3601, | ||
"location": "", | ||
"last_publish": null | ||
} |
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 Zone from '../lib/zone.js' | ||
import { meta } from '../lib/util.js' | ||
|
||
function ZoneRoutes(server) { | ||
server.route([ | ||
{ | ||
method: 'GET', | ||
path: '/zone/{id}', | ||
options: { | ||
validate: { | ||
query: validate.zone.GET_req, | ||
}, | ||
response: { | ||
schema: validate.zone.GET_res, | ||
}, | ||
tags: ['api'], | ||
}, | ||
handler: async (request, h) => { | ||
const getArgs = { | ||
deleted: request.query.deleted === true ? 1 : 0, | ||
id: parseInt(request.params.id, 10), | ||
} | ||
|
||
const zones = await Zone.get(getArgs) | ||
|
||
return h | ||
.response({ | ||
zone: zones[0], | ||
meta: { | ||
api: meta.api, | ||
msg: `here's your zone`, | ||
}, | ||
}) | ||
.code(200) | ||
}, | ||
}, | ||
{ | ||
method: 'POST', | ||
path: '/zone', | ||
options: { | ||
validate: { | ||
payload: validate.zone.POST, | ||
}, | ||
response: { | ||
schema: validate.zone.GET_res, | ||
}, | ||
tags: ['api'], | ||
}, | ||
handler: async (request, h) => { | ||
const id = await Zone.create(request.payload) | ||
|
||
const zones = await Zone.get({ id }) | ||
|
||
return h | ||
.response({ | ||
zone: zones[0], | ||
meta: { | ||
api: meta.api, | ||
msg: `the zone was created`, | ||
}, | ||
}) | ||
.code(201) | ||
}, | ||
}, | ||
{ | ||
method: 'DELETE', | ||
path: '/zone/{id}', | ||
options: { | ||
validate: { | ||
query: validate.zone.DELETE, | ||
}, | ||
response: { | ||
schema: validate.zone.GET_res, | ||
}, | ||
tags: ['api'], | ||
}, | ||
handler: async (request, h) => { | ||
const zones = await Zone.get({ | ||
deleted: request.query.deleted === true ? 1 : 0, | ||
id: parseInt(request.params.id, 10), | ||
}) | ||
|
||
if (zones.length === 0) { | ||
return h | ||
.response({ | ||
meta: { | ||
api: meta.api, | ||
msg: `I couldn't find that zone`, | ||
}, | ||
}) | ||
.code(404) | ||
} | ||
|
||
await Zone.delete({ | ||
id: zones[0].id, | ||
deleted: 1, | ||
}) | ||
|
||
return h | ||
.response({ | ||
zone: zones[0], | ||
meta: { | ||
api: meta.api, | ||
msg: `I deleted that zone`, | ||
}, | ||
}) | ||
.code(200) | ||
}, | ||
}, | ||
]) | ||
} | ||
|
||
export default ZoneRoutes | ||
|
||
export { Zone, ZoneRoutes } |
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,150 @@ | ||
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 Zone from '../lib/zone.js' | ||
|
||
import groupCase from './test/group.json' with { type: 'json' } | ||
import userCase from './test/user.json' with { type: 'json' } | ||
import nsCase from './test/zone.json' with { type: 'json' } | ||
|
||
let server | ||
let case2Id = 4094 | ||
|
||
before(async () => { | ||
await Zone.destroy({ id: case2Id }) | ||
await Group.create(groupCase) | ||
await User.create(userCase) | ||
await Zone.create(nsCase) | ||
server = await init() | ||
}) | ||
|
||
after(async () => { | ||
// await Zone.destroy({ id: case2Id }) | ||
server.stop() | ||
}) | ||
|
||
describe('zone 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 /zone/${nsCase.id}`, async () => { | ||
const res = await server.inject({ | ||
method: 'GET', | ||
url: `/zone/${nsCase.id}`, | ||
headers: { | ||
Cookie: sessionCookie, | ||
}, | ||
}) | ||
// console.log(res.result) | ||
assert.equal(res.statusCode, 200) | ||
assert.equal(res.result.zone.name, nsCase.name) | ||
}) | ||
|
||
it(`POST /zone (${case2Id})`, async () => { | ||
const testCase = JSON.parse(JSON.stringify(nsCase)) | ||
testCase.id = case2Id // make it unique | ||
testCase.gid = case2Id | ||
testCase.zone = 'route2.example.com.' | ||
|
||
const res = await server.inject({ | ||
method: 'POST', | ||
url: '/zone', | ||
headers: { | ||
Cookie: sessionCookie, | ||
}, | ||
payload: testCase, | ||
}) | ||
// console.log(res.result) | ||
assert.equal(res.statusCode, 201) | ||
assert.ok(res.result.zone.gid) | ||
}) | ||
|
||
it(`GET /zone/${case2Id}`, async () => { | ||
const res = await server.inject({ | ||
method: 'GET', | ||
url: `/zone/${case2Id}`, | ||
headers: { | ||
Cookie: sessionCookie, | ||
}, | ||
}) | ||
// console.log(res.result) | ||
assert.equal(res.statusCode, 200) | ||
assert.ok(res.result.zone.gid) | ||
}) | ||
|
||
it(`DELETE /zone/${case2Id}`, async () => { | ||
const res = await server.inject({ | ||
method: 'DELETE', | ||
url: `/zone/${case2Id}`, | ||
headers: { | ||
Cookie: sessionCookie, | ||
}, | ||
}) | ||
// console.log(res.result) | ||
assert.equal(res.statusCode, 200) | ||
}) | ||
|
||
it(`DELETE /zone/${case2Id}`, async () => { | ||
const res = await server.inject({ | ||
method: 'DELETE', | ||
url: `/zone/${case2Id}`, | ||
headers: { | ||
Cookie: sessionCookie, | ||
}, | ||
}) | ||
// console.log(res.result) | ||
assert.equal(res.statusCode, 404) | ||
}) | ||
|
||
it(`GET /zone/${case2Id}`, async () => { | ||
const res = await server.inject({ | ||
method: 'GET', | ||
url: `/zone/${case2Id}`, | ||
headers: { | ||
Cookie: sessionCookie, | ||
}, | ||
}) | ||
// console.log(res.result) | ||
// assert.equal(res.statusCode, 200) | ||
assert.equal(res.result.zone, undefined) | ||
}) | ||
|
||
it(`GET /zone/${case2Id} (deleted)`, async () => { | ||
const res = await server.inject({ | ||
method: 'GET', | ||
url: `/zone/${case2Id}?deleted=true`, | ||
headers: { | ||
Cookie: sessionCookie, | ||
}, | ||
}) | ||
// console.log(res.result) | ||
assert.equal(res.statusCode, 200) | ||
assert.equal(res?.result?.zone, undefined) | ||
}) | ||
|
||
it('DELETE /session', async () => { | ||
const res = await server.inject({ | ||
method: 'DELETE', | ||
url: '/session', | ||
headers: { | ||
Cookie: sessionCookie, | ||
}, | ||
}) | ||
assert.equal(res.statusCode, 200) | ||
}) | ||
}) |