Skip to content
This repository has been archived by the owner on Jun 20, 2022. It is now read-only.

Commit

Permalink
feat: Add enumeration API to trail-hapi-plugin.
Browse files Browse the repository at this point in the history
  • Loading branch information
Shogun committed May 16, 2018
1 parent 2cb1b31 commit c1fb32c
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 1 deletion.
31 changes: 31 additions & 0 deletions packages/trail-hapi-plugin/lib/routes/trails.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,37 @@ module.exports = {
}
})

addApiRoute(server, 'trails', {
method: 'GET',
path: '/trails/enumerate',
async handler (request, h) {
const { from, to, type, page, pageSize, desc } = request.query

const results = await request.trailCore.enumerate({ from, to, type, page, pageSize, desc })

return results.length ? results : h.response().code(204)
},
config: {
description: 'Enumerate audit trails ids.',
tags: ['api', 'trails'],
validate: {
query: trailSchema.enumerate,
failAction,
options: validationOptions
},
response: {
status: {
200: Joi.array()
.description('The enumeration results.')
.items(Joi.string().description('A trail who, what or subject id')),
204: Joi.empty().description('No ids found.'),
422: errorsSchemas['422'],
500: errorsSchemas['500']
}
}
}
})

addApiRoute(server, 'trails', {
method: 'POST',
path: '/trails',
Expand Down
24 changes: 24 additions & 0 deletions packages/trail-hapi-plugin/lib/schemas/trails.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,30 @@ const trailSchema = {
.valid(['when', 'id', 'who', 'what', 'subject', '-when', '-id', '-who', '-what', '-subject'])
.example('-when')
},
enumerate: {
from: dateTime
.description('The minimum timestamp (inclusive)')
.required(),
to: dateTime
.description('The maximum timestamp (inclusive)')
.required(),
type: Joi.string()
.description(`The type of id to search`)
.required()
.valid(['who', 'what', 'subject'])
.example('who'),
page: Joi.number()
.description('The page of results to return')
.min(1)
.example(5),
pageSize: Joi.number()
.description('The number of results per page (default is 25)')
.min(1)
.example(25),
desc: Joi.boolean()
.description(`If to sort alphabetically by descending order`)
.example(true)
},
request: Joi.object()
.description('A audit trail')
.meta({id: 'models/trail.request'})
Expand Down
66 changes: 65 additions & 1 deletion packages/trail-hapi-plugin/test/routes/trails.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('Trails REST operations', () => {
}
})

describe('POST /trails', async () => {
describe('GET /trails', async () => {
test('it should search trails and return it with 200', async () => {
await server.trailCore.performDatabaseOperations(client => client.query('TRUNCATE trails'))

Expand Down Expand Up @@ -119,6 +119,70 @@ describe('Trails REST operations', () => {
})
})

describe('GET /trails/enumerate', async () => {
test('it should enumerate trails and return it with 200', async () => {
await server.trailCore.performDatabaseOperations(client => client.query('TRUNCATE trails'))

const id = await server.trailCore.insert({
when: '2016-01-02T18:04:05.123+03:00',
who: '1',
what: '2',
subject: '3'
})

const response = await server.inject({
method: 'GET',
url: `/trails/enumerate?from=${encodeURIComponent('2014-01-02T18:04:05.123+03:00')}&to=${encodeURIComponent('2018-01-02T18:04:05.123+03:00')}&type=who`
})

expect(response.statusCode).toEqual(200)
const trails = JSON.parse(response.payload)

expect(trails).toEqual(['1'])

await server.trailCore.delete(id)
})

test('it should enumerate trails and return no body with 204 when no records are found', async () => {
await server.trailCore.performDatabaseOperations(client => client.query('TRUNCATE trails'))

const id = await server.trailCore.insert({
when: '2016-01-02T18:04:05.123+03:00',
who: '1',
what: '2',
subject: '3'
})

const response = await server.inject({
method: 'GET',
url: `/trails/enumerate?from=${encodeURIComponent('2014-01-02T18:04:05.123+03:00')}&to=${encodeURIComponent('2015-01-02T18:04:05.123+03:00')}&type=who`
})

expect(response.statusCode).toEqual(204)
expect(response.payload).toEqual('')

await server.trailCore.delete(id)
})

test('it should return 422 in case of validation errors', async () => {
const response = await server.inject({
method: 'GET',
url: `/trails/enumerate?from=bar`
})

expect(response.statusCode).toEqual(422)
expect(JSON.parse(response.payload)).toMatchObject({
statusCode: 422,
error: 'Unprocessable Entity',
message: 'Invalid input data.',
reasons: {
from: 'must be a valid UTC timestamp in the format YYYY-MM-DDTHH:MM:SS.sss (example: 2018-07-06T12:34:56.123)',
to: 'must be present and non empty'
}
})
})
})

describe('POST /trails', async () => {
test('it should create a new trail and return it with 201', async () => {
const response = await server.inject({
Expand Down

0 comments on commit c1fb32c

Please sign in to comment.