-
-
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.
routes/permission: added GET, POST, DELETE
- Loading branch information
Showing
10 changed files
with
333 additions
and
16 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
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,122 @@ | ||
import validate from '@nictool/validate' | ||
|
||
import Permission from '../lib/permission.js' | ||
import { meta } from '../lib/util.js' | ||
|
||
function PermissionRoutes(server) { | ||
server.route([ | ||
{ | ||
method: 'GET', | ||
path: '/permission/{id}', | ||
options: { | ||
validate: { | ||
// params: ??, | ||
query: validate.permission.GET, | ||
}, | ||
response: { | ||
schema: validate.permission.GET, | ||
}, | ||
tags: ['api'], | ||
}, | ||
handler: async (request, h) => { | ||
// console.log(request.params) | ||
|
||
const permission = await Permission.get({ | ||
deleted: request.query.deleted ?? 0, | ||
id: parseInt(request.params.id, 10), | ||
}) | ||
|
||
return h | ||
.response({ | ||
permission, | ||
meta: { | ||
api: meta.api, | ||
msg: `here's your permission`, | ||
}, | ||
}) | ||
.code(200) | ||
}, | ||
}, | ||
{ | ||
method: 'POST', | ||
path: '/permission', | ||
options: { | ||
validate: { | ||
payload: validate.permission.POST, | ||
}, | ||
response: { | ||
schema: validate.permission.GET, | ||
}, | ||
tags: ['api'], | ||
}, | ||
handler: async (request, h) => { | ||
const pid = await Permission.create(request.payload) | ||
if (!pid) { | ||
console.log(`POST /permission oops`) // TODO | ||
} | ||
|
||
const permission = await Permission.get({ id: pid }) | ||
|
||
return h | ||
.response({ | ||
permission, | ||
meta: { | ||
api: meta.api, | ||
msg: `the permission was created`, | ||
}, | ||
}) | ||
.code(201) | ||
}, | ||
}, | ||
{ | ||
method: 'DELETE', | ||
path: '/permission/{id}', | ||
options: { | ||
// response: { | ||
// schema: validate.permission.GET, | ||
// }, | ||
tags: ['api'], | ||
}, | ||
handler: async (request, h) => { | ||
|
||
const permission = await Permission.get({ | ||
deleted: parseInt(request.query.deleted ?? 0), | ||
id: parseInt(request.params.id, 10), | ||
}) | ||
|
||
if (!permission) { | ||
return h | ||
.response({ | ||
meta: { | ||
api: meta.api, | ||
msg: `I couldn't find that permission`, | ||
}, | ||
}) | ||
.code(404) | ||
} | ||
|
||
const action = request.query.destroy === 'true' ? 'destroy' : 'delete' | ||
// console.log(`action: ${action}`) | ||
await Permission[action]({ | ||
id: permission.id, | ||
deleted: permission.deleted, | ||
}) | ||
delete permission.gid | ||
|
||
return h | ||
.response({ | ||
permission, | ||
meta: { | ||
api: meta.api, | ||
msg: `I deleted that permission`, | ||
}, | ||
}) | ||
.code(200) | ||
}, | ||
}, | ||
]) | ||
} | ||
|
||
export default PermissionRoutes | ||
|
||
export { Permission, PermissionRoutes } |
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,154 @@ | ||
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 Permission from '../lib/permission.js' | ||
|
||
import groupCase from './test/group.json' with { type: 'json' } | ||
import userCase from './test/user.json' with { type: 'json' } | ||
import permCase from './test/permission.json' with { type: 'json' } | ||
|
||
let server | ||
|
||
before(async () => { | ||
server = await init() | ||
await Group.create(groupCase) | ||
await User.create(userCase) | ||
await Permission.create(permCase) | ||
}) | ||
|
||
after(async () => { | ||
await server.stop() | ||
}) | ||
|
||
describe('permission 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 /permission/${userCase.id}`, async () => { | ||
const res = await server.inject({ | ||
method: 'GET', | ||
url: `/permission/${userCase.id}`, | ||
headers: { | ||
Cookie: sessionCookie, | ||
}, | ||
}) | ||
// console.log(res.result) | ||
assert.equal(res.statusCode, 200) | ||
assert.equal(res.result.permission.zone.create, true) | ||
assert.equal(res.result.permission.nameserver.create, false) | ||
}) | ||
|
||
let case2Id = 4094 | ||
|
||
it('POST /permission', async () => { | ||
const testCase = JSON.parse(JSON.stringify(permCase)) | ||
testCase.id = case2Id // make it unique | ||
testCase.uid = case2Id | ||
testCase.gid = case2Id | ||
testCase.name = `Route Test Permission 2` | ||
delete testCase.deleted | ||
// console.log(testCase) | ||
|
||
const res = await server.inject({ | ||
method: 'POST', | ||
url: '/permission', | ||
headers: { | ||
Cookie: sessionCookie, | ||
}, | ||
payload: testCase, | ||
}) | ||
// console.log(res.result) | ||
assert.equal(res.statusCode, 201) | ||
assert.equal(res.result.permission.zone.create, true) | ||
assert.equal(res.result.permission.nameserver.create, false) | ||
}) | ||
|
||
it(`GET /permission/${case2Id}`, async () => { | ||
const res = await server.inject({ | ||
method: 'GET', | ||
url: `/permission/${case2Id}`, | ||
headers: { | ||
Cookie: sessionCookie, | ||
}, | ||
}) | ||
// console.log(res.result) | ||
assert.equal(res.statusCode, 200) | ||
assert.equal(res.result.permission.zone.create, true) | ||
assert.equal(res.result.permission.nameserver.create, false) | ||
}) | ||
|
||
it(`DELETE /permission/${case2Id}`, async () => { | ||
const res = await server.inject({ | ||
method: 'DELETE', | ||
url: `/permission/${case2Id}`, | ||
headers: { | ||
Cookie: sessionCookie, | ||
}, | ||
}) | ||
// console.log(res.result) | ||
assert.equal(res.statusCode, 200) | ||
}) | ||
|
||
it(`GET /permission/${case2Id}`, async () => { | ||
const res = await server.inject({ | ||
method: 'GET', | ||
url: `/permission/${case2Id}`, | ||
headers: { | ||
Cookie: sessionCookie, | ||
}, | ||
}) | ||
// console.log(res.result) | ||
// assert.equal(res.statusCode, 200) | ||
assert.equal(res.result.permission, undefined) | ||
}) | ||
|
||
it(`GET /permission/${case2Id} (deleted)`, async () => { | ||
const res = await server.inject({ | ||
method: 'GET', | ||
url: `/permission/${case2Id}?deleted=1`, | ||
headers: { | ||
Cookie: sessionCookie, | ||
}, | ||
}) | ||
// console.log(res.result) | ||
assert.equal(res.statusCode, 200) | ||
}) | ||
|
||
it(`DELETE /permission/${case2Id}`, async () => { | ||
const res = await server.inject({ | ||
method: 'DELETE', | ||
url: `/permission/${case2Id}?deleted=1&destroy=true`, | ||
headers: { | ||
Cookie: sessionCookie, | ||
}, | ||
}) | ||
// console.log(res.result) | ||
assert.equal(res.statusCode, 200) | ||
}) | ||
|
||
it('DELETE /session', async () => { | ||
const res = await server.inject({ | ||
method: 'DELETE', | ||
url: '/session', | ||
headers: { | ||
Cookie: sessionCookie, | ||
}, | ||
}) | ||
assert.equal(res.statusCode, 200) | ||
}) | ||
}) |
Oops, something went wrong.