Skip to content

Commit

Permalink
group: updated validate names (_res, _req)
Browse files Browse the repository at this point in the history
  • Loading branch information
msimerson committed Mar 6, 2024
1 parent ccbfe7d commit 899769f
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 70 deletions.
21 changes: 12 additions & 9 deletions routes/group.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ function GroupRoutes(server) {
method: 'GET',
path: '/group/{id}',
options: {
validate: {
query: validate.group.GET_req,
},
response: {
schema: validate.group.GET,
schema: validate.group.GET_res,
},
tags: ['api'],
},
Expand Down Expand Up @@ -50,15 +53,12 @@ function GroupRoutes(server) {
payload: validate.group.POST,
},
response: {
schema: validate.group.GET,
schema: validate.group.GET_res,
},
tags: ['api'],
},
handler: async (request, h) => {
const gid = await Group.create(request.payload)
if (!gid) {
console.log(`POST /group oops`) // TODO
}

const groups = await Group.get({ id: gid })

Expand All @@ -77,13 +77,17 @@ function GroupRoutes(server) {
method: 'DELETE',
path: '/group/{id}',
options: {
validate: {
query: validate.group.DELETE,
},
response: {
schema: validate.group.GET,
schema: validate.group.GET_res,
},
tags: ['api'],
},
handler: async (request, h) => {
const groups = await Group.get(request.params)

const groups = await Group.get({ id: parseInt(request.params.id, 10) })
/* c8 ignore next 10 */
if (groups.length !== 1) {
return h
Expand All @@ -96,8 +100,7 @@ function GroupRoutes(server) {
.code(204)
}

const action = request.query.destroy === 'true' ? 'destroy' : 'delete'
await Group[action]({ id: groups[0].id })
await Group.delete({ id: groups[0].id })
delete groups[0].gid

return h
Expand Down
24 changes: 6 additions & 18 deletions routes/group.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import groupCase from './test/group.json' with { type: 'json' }
import userCase from './test/user.json' with { type: 'json' }

let server
const case2Id = 4094

before(async () => {
server = await init()
Expand All @@ -17,7 +18,8 @@ before(async () => {
})

after(async () => {
await server.stop()
await Group.destroy({ id: case2Id })
server.stop()
})

describe('group routes', () => {
Expand Down Expand Up @@ -45,11 +47,9 @@ describe('group routes', () => {
},
})
// console.log(res.result)
assert.equal(res.statusCode, 200)
assert.ok([200, 204].includes(res.statusCode))
})

const case2Id = 4094

it('POST /group', async () => {
const testCase = JSON.parse(JSON.stringify(groupCase))
testCase.id = case2Id // make it unique
Expand Down Expand Up @@ -78,7 +78,7 @@ describe('group routes', () => {
},
})
// console.log(res.result)
assert.equal(res.statusCode, 200)
assert.ok([200, 204].includes(res.statusCode))
})

it(`DELETE /group/${case2Id}`, async () => {
Expand Down Expand Up @@ -108,19 +108,7 @@ describe('group routes', () => {
it(`GET /group/${case2Id} (deleted)`, async () => {
const res = await server.inject({
method: 'GET',
url: `/group/${case2Id}?deleted=1`,
headers: {
Cookie: sessionCookie,
},
})
// console.log(res.result)
assert.equal(res.statusCode, 200)
})

it(`DELETE /group/${case2Id}`, async () => {
const res = await server.inject({
method: 'DELETE',
url: `/group/${case2Id}?destroy=true`,
url: `/group/${case2Id}?deleted=true`,
headers: {
Cookie: sessionCookie,
},
Expand Down
8 changes: 4 additions & 4 deletions routes/nameserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ function NameserverRoutes(server) {
path: '/nameserver/{id}',
options: {
validate: {
query: validate.nameserver.v3,
query: validate.nameserver.GET_req,
},
response: {
schema: validate.nameserver.GET,
schema: validate.nameserver.GET_res,
},
tags: ['api'],
},
Expand Down Expand Up @@ -44,7 +44,7 @@ function NameserverRoutes(server) {
payload: validate.nameserver.POST,
},
response: {
schema: validate.nameserver.GET,
schema: validate.nameserver.GET_res,
},
tags: ['api'],
},
Expand Down Expand Up @@ -72,7 +72,7 @@ function NameserverRoutes(server) {
query: validate.nameserver.DELETE,
},
response: {
schema: validate.nameserver.GET,
schema: validate.nameserver.GET_res,
},
tags: ['api'],
},
Expand Down
3 changes: 2 additions & 1 deletion routes/nameserver.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ before(async () => {
})

after(async () => {
await server.stop()
await Nameserver.destroy({ id: case2Id })
server.stop()
})

describe('nameserver routes', () => {
Expand Down
50 changes: 15 additions & 35 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import Group from './lib/group.js'
import User from './lib/user.js'
import Session from './lib/session.js'
import Permission from './lib/permission.js'
import Nameserver from './lib/nameserver.js'

import groupCase from './lib/test/group.json' with { type: 'json' }
import userCase from './lib/test/user.json' with { type: 'json' }
import groupCaseR from './routes/test/group.json' with { type: 'json' }
import userCaseR from './routes/test/user.json' with { type: 'json' }
import nsCaseR from './routes/test/nameserver.json' with { type: 'json' }

switch (process.argv[2]) {
case 'setup':
Expand All @@ -26,22 +28,14 @@ switch (process.argv[2]) {
}

async function setup() {
await createTestGroup()
await createTestUser()
// await createTestSession()
await User.mysql.disconnect()
await Group.mysql.disconnect()
process.exit(0)
}

async function createTestGroup() {
await Group.create(groupCase)
await Group.create(groupCaseR)
}

async function createTestUser() {
await User.create(userCase)
await User.create(userCaseR)
// await createTestSession()
await User.mysql.disconnect()
await Group.mysql.disconnect()
process.exit(0)
}

// async function createTestSession() {
Expand All @@ -53,30 +47,16 @@ async function createTestUser() {
// }

async function teardown() {
await destroyTestSession()
await destroyTestUser()
await destroyTestGroup()
await destroyTestPermission()
await Nameserver.destroy({ id: nsCaseR.id })
await Nameserver.destroy({ id: nsCaseR.id -1 })
await Permission.destroy({ id: userCase.id })
await Permission.destroy({ id: userCase.id - 1 })
await Session.delete({ nt_user_id: userCase.id })
await User.destroy({ id: userCase.id })
await User.destroy({ id: userCaseR.id })
await Group.destroy({ id: groupCase.id })
await Group.destroy({ id: groupCaseR.id })
await User.mysql.disconnect()
await Group.mysql.disconnect()
process.exit(0)
}

async function destroyTestGroup() {
await Group.destroy({ id: groupCase.id })
await Group.destroy({ id: groupCaseR.id })
}

async function destroyTestUser() {
await User.destroy({ id: userCase.id })
await User.destroy({ id: userCaseR.id })
}

async function destroyTestSession() {
await Session.delete({ nt_user_id: userCase.id })
}

async function destroyTestPermission() {
await Permission.destroy({ id: userCase.id })
await Permission.destroy({ id: userCase.id - 1 })
}
13 changes: 10 additions & 3 deletions test.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
#!/bin/sh

set -euo pipefail

NODE="node --no-warnings=ExperimentalWarning"
$NODE test.js teardown
$NODE test.js setup

if [ "$1" = "watch" ]; then
function cleanup {
echo "cleaning DB objects"
$NODE test.js teardown
}

trap cleanup EXIT SIGINT SIGKILL

if [ $# -ge 1 ] && [ "$1" = "watch" ]; then
$NODE --test --watch
else
# if [ -n "$GITHUB_WORKFLOW" ]; then
Expand All @@ -16,5 +25,3 @@ else
fi

# npx mocha --exit --no-warnings=ExperimentalWarning lib/*.test.js routes/*.test.js

$NODE test.js teardown

0 comments on commit 899769f

Please sign in to comment.