Skip to content

Commit 5fd3908

Browse files
authored
Merge pull request #248 from OpenPathfinder/ulises/v1-cecklist-details
2 parents 1a8cae3 + d6364e1 commit 5fd3908

File tree

4 files changed

+95
-4
lines changed

4 files changed

+95
-4
lines changed

__tests__/httpServer/apiV1.test.js

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ let getAllGithubOrganizationsByProjectsId
3939
let getAllChecks
4040
let getCheckById
4141
let getAllChecklists
42+
let getChecklistById
4243

4344
beforeAll(async () => {
4445
// Initialize server asynchronously
@@ -53,7 +54,8 @@ beforeAll(async () => {
5354
getAllGithubOrganizationsByProjectsId,
5455
getAllChecks,
5556
getCheckById,
56-
getAllChecklists
57+
getAllChecklists,
58+
getChecklistById
5759
} = initializeStore(knex))
5860
})
5961

@@ -460,4 +462,37 @@ describe('HTTP Server API V1', () => {
460462

461463
test.todo('should return 500 for internal server error')
462464
})
465+
466+
describe('GET /api/v1/compliance-checklist/{checklistId}', () => {
467+
test('should return 200 and a specific checklist', async () => {
468+
const response = await app.get('/api/v1/compliance-checklist/1')
469+
const storedChecklist = await getChecklistById(1)
470+
471+
expect(response.status).toBe(200)
472+
const expected = {
473+
...storedChecklist,
474+
created_at: storedChecklist.created_at.toISOString(),
475+
updated_at: storedChecklist.updated_at.toISOString()
476+
}
477+
expect(response.body).toStrictEqual(expected)
478+
})
479+
480+
test('should return 400 for invalid checklist ID', async () => {
481+
const response = await app.get('/api/v1/compliance-checklist/invalid')
482+
483+
expect(response.status).toBe(400)
484+
expect(response.body).toHaveProperty('errors')
485+
expect(response.body.errors[0]).toHaveProperty('message', 'must be integer')
486+
})
487+
488+
test('should return 404 for invalid checklist ID', async () => {
489+
const response = await app.get('/api/v1/compliance-checklist/9999999')
490+
491+
expect(response.status).toBe(404)
492+
expect(response.body).toHaveProperty('errors')
493+
expect(response.body.errors[0]).toHaveProperty('message', 'Compliance Checklist not found')
494+
})
495+
496+
test.todo('should return 500 for internal server error')
497+
})
463498
})

src/httpServer/routers/apiV1.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const runWorkflow = ({ workflowName, knex, data } = {}) => new Promise((resolve,
2727
})
2828

2929
function createApiRouter (knex, express) {
30-
const { addProject, getProjectByName, addGithubOrganization, getProjectById, getAllGithubOrganizationsByProjectsId, getAllChecks, getCheckById, getAllChecklists } = initializeStore(knex)
30+
const { addProject, getProjectByName, addGithubOrganization, getProjectById, getAllGithubOrganizationsByProjectsId, getAllChecks, getCheckById, getAllChecklists, getChecklistById } = initializeStore(knex)
3131

3232
const router = express.Router()
3333

@@ -146,6 +146,21 @@ function createApiRouter (knex, express) {
146146
}
147147
})
148148

149+
router.get('/compliance-checklist/:checklistId', async (req, res) => {
150+
try {
151+
// Params validation done in swagger
152+
const checklistId = parseInt(req.params.checklistId, 10)
153+
const checklist = await getChecklistById(checklistId)
154+
if (!checklist) {
155+
return res.status(404).json({ errors: [{ message: 'Compliance Checklist not found' }] })
156+
}
157+
res.json(checklist)
158+
} catch (error) {
159+
logger.error(error)
160+
res.status(500).json({ errors: [{ message: 'Failed to retrieve Compliance Checklist' }] })
161+
}
162+
})
163+
149164
router.get('/compliance-checklist', async (req, res) => {
150165
try {
151166
const checklists = await getAllChecklists()

src/httpServer/swagger/api-v1.yml

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,47 @@ paths:
346346
application/json:
347347
schema:
348348
$ref: '#/components/schemas/ErrorResponse'
349-
349+
/api/v1/compliance-checklist/{checklistId}:
350+
get:
351+
summary: Get a compliance checklist by ID
352+
description: Returns a compliance checklist by ID
353+
operationId: getComplianceChecklistById
354+
tags:
355+
- Compliance Checklists
356+
parameters:
357+
- name: checklistId
358+
in: path
359+
required: true
360+
description: The ID of the checklist
361+
schema:
362+
type: integer
363+
minimum: 1
364+
example: 1
365+
responses:
366+
'200':
367+
description: A compliance checklist
368+
content:
369+
application/json:
370+
schema:
371+
$ref: '#/components/schemas/ComplianceChecklist'
372+
'400':
373+
description: Bad request, invalid input data
374+
content:
375+
application/json:
376+
schema:
377+
$ref: '#/components/schemas/ErrorResponse'
378+
'404':
379+
description: Compliance checklist not found
380+
content:
381+
application/json:
382+
schema:
383+
$ref: '#/components/schemas/ErrorResponse'
384+
'500':
385+
description: Internal server error
386+
content:
387+
application/json:
388+
schema:
389+
$ref: '#/components/schemas/ErrorResponse'
350390
components:
351391
schemas:
352392
ComplianceChecklist:

src/store/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,8 @@ const initializeStore = (knex) => {
250250
getProjectById: (id) => getOne('projects', id),
251251
getProjectByName: getProjectByName(knex),
252252
getAllChecks: () => getAll('compliance_checks'),
253-
getCheckById: (id) => getOne('compliance_checks', id)
253+
getCheckById: (id) => getOne('compliance_checks', id),
254+
getChecklistById: (id) => getOne('compliance_checklists', id)
254255
}
255256
}
256257

0 commit comments

Comments
 (0)