Skip to content

Commit

Permalink
group: add GET,POST,DELETE routes
Browse files Browse the repository at this point in the history
  • Loading branch information
msimerson committed Feb 24, 2024
1 parent bb33a81 commit af74ff9
Show file tree
Hide file tree
Showing 5 changed files with 242 additions and 11 deletions.
113 changes: 113 additions & 0 deletions routes/group.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
const validate = require('@nictool/validate')

const Group = require('../lib/group')
const Util = require('../lib/util')

module.exports = (server) => {
server.route([
{
method: 'GET',
path: '/group/{id}',
options: {
response: {
schema: validate.group.GET,
},
tags: ['api'],
},
handler: async (request, h) => {
const groups = await Group.get({
deleted: request.query.deleted ?? 0,
id: parseInt(request.params.id, 10),
})
if (groups.length !== 1) {
return h
.response({
meta: {
api: Util.meta.api,
msg: `No unique group match`,
},
})
.code(204)
}

return h
.response({
group: groups[0],
meta: {
api: Util.meta.api,
msg: `here's your group`,
},
})
.code(200)

Check warning on line 41 in routes/group.js

View check run for this annotation

Codecov / codecov/patch

routes/group.js#L18-L41

Added lines #L18 - L41 were not covered by tests
},
},
{
method: 'POST',
path: '/group',
options: {
validate: {
payload: validate.group.POST,
},
response: {
schema: validate.group.GET,
},
tags: ['api'],
},
handler: async (request, h) => {
// console.log(request.payload)
const gid = await Group.create(request.payload)
if (!gid) {
console.log(`POST /group oops`) // TODO
}

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

return h
.response({
group: groups[0],
meta: {
api: Util.meta.api,
msg: `I created this group`,
},
})
.code(201)

Check warning on line 73 in routes/group.js

View check run for this annotation

Codecov / codecov/patch

routes/group.js#L57-L73

Added lines #L57 - L73 were not covered by tests
},
},
{
method: 'DELETE',
path: '/group/{id}',
options: {
response: {
schema: validate.group.GET,
},
tags: ['api'],
},
handler: async (request, h) => {
const groups = await Group.get(request.params)
if (groups.length !== 1) {
return h
.response({
meta: {
api: Util.meta.api,
msg: `No unique group match`,
},
})
.code(204)
}

await Group.delete({ id: groups[0].id })
delete groups[0].gid

return h
.response({
group: groups[0],
meta: {
api: Util.meta.api,
msg: `I deleted that group`,
},
})
.code(200)

Check warning on line 109 in routes/group.js

View check run for this annotation

Codecov / codecov/patch

routes/group.js#L86-L109

Added lines #L86 - L109 were not covered by tests
},
},
])
}
119 changes: 119 additions & 0 deletions routes/group.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
const assert = require('node:assert/strict')
const { describe, it, before, after } = require('node:test')

const { init } = require('./index')
const Group = require('../lib/Group')
const groupCase = require('../test/v3/group.json')

before(async () => {
const userCase = require('../test/v3/user.json')
this.server = await init()
const res = await this.server.inject({
method: 'POST',
url: '/session',
payload: {
username: `${userCase.username}@example.com`,
password: 'Wh@tA-Decent#P6ssw0rd',
},
})
assert.ok(res.headers['set-cookie'][0])
this.sessionCookie = res.headers['set-cookie'][0].split(';')[0]
})

after(async () => {
const res = await this.server.inject({
method: 'DELETE',
url: '/session',
headers: {
Cookie: this.sessionCookie,
},
})
// console.log(res.result)
assert.equal(res.statusCode, 200)

await this.server.stop()
})

describe('group', () => {
it('GET /group/4096', async () => {
const res = await this.server.inject({
method: 'GET',
url: '/group/4096',
headers: {
Cookie: this.sessionCookie,
},
})
// console.log(res.result)
assert.equal(res.statusCode, 200)
})

it('POST /group', async () => {
const testCase = JSON.parse(JSON.stringify(groupCase))
testCase.id = 4095 // make it unique
testCase.name = `example2.com`
delete testCase.deleted

const res = await this.server.inject({
method: 'POST',
url: '/group',
headers: {
Cookie: this.sessionCookie,
},
payload: testCase,
})
// console.log(res.result)
assert.equal(res.statusCode, 201)
})

it('GET /group', async () => {
const res = await this.server.inject({
method: 'GET',
url: '/group/4095',
headers: {
Cookie: this.sessionCookie,
},
})
// console.log(res.result)
assert.equal(res.statusCode, 200)
})

it('DELETE /group', async () => {
const res = await this.server.inject({
method: 'DELETE',
url: '/group/4095',
headers: {
Cookie: this.sessionCookie,
},
})
// console.log(res.result)
assert.equal(res.statusCode, 200)
})

it('GET /group/4095', async () => {
const res = await this.server.inject({
method: 'GET',
url: '/group/4095',
headers: {
Cookie: this.sessionCookie,
},
})
// console.log(res.result)
assert.equal(res.statusCode, 204)
})

it('GET /group/4095 (deleted)', async () => {
const res = await this.server.inject({
method: 'GET',
url: '/group/4095?deleted=1',
headers: {
Cookie: this.sessionCookie,
},
})
// console.log(res.result)
assert.equal(res.statusCode, 200)
})

it('nukes /group/4095', async () => {
assert.ok(Group.destroy({ id: 4095 }))
})
})
8 changes: 3 additions & 5 deletions routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ const Config = require('../lib/config')
const Session = require('../lib/session')
const User = require('../lib/user')

const SessionRoutes = require('./session')
const UserRoutes = require('./user')

let server

const setup = async () => {
Expand Down Expand Up @@ -78,8 +75,9 @@ const setup = async () => {
},
})

SessionRoutes(server)
UserRoutes(server)
require('./group')(server)
require('./user')(server)
require('./session')(server)

server.route({
method: '*',
Expand Down
10 changes: 5 additions & 5 deletions routes/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = (server) => {
path: '/user',
options: {
response: {
schema: validate.user.userGET,
schema: validate.user.GET,
},
// tags: ['api'],
},
Expand All @@ -36,7 +36,7 @@ module.exports = (server) => {
path: '/user/{id}',
options: {
response: {
schema: validate.user.userGET,
schema: validate.user.GET,
},
tags: ['api'],
},
Expand Down Expand Up @@ -79,7 +79,7 @@ module.exports = (server) => {
payload: validate.user.userPOST,
},
response: {
schema: validate.user.userGET,
schema: validate.user.GET,
},
tags: ['api'],
},
Expand All @@ -103,15 +103,15 @@ module.exports = (server) => {
msg: `I created this user`,
},
})
.code(200)
.code(201)
},
},
{
method: 'DELETE',
path: '/user/{id}',
options: {
response: {
schema: validate.user.userGET,
schema: validate.user.GET,
},
tags: ['api'],
},
Expand Down
3 changes: 2 additions & 1 deletion routes/user.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ describe('user', () => {
},
payload: testCase,
})
console.log(res.result)
// console.log(res.result)
assert.equal(res.statusCode, 201)
})

it('GET /user', async () => {
Expand Down

0 comments on commit af74ff9

Please sign in to comment.