Skip to content

Commit fb479bd

Browse files
committed
feat: add GET /api/v1/check/{checkId} endpoint
1 parent 969f8a9 commit fb479bd

File tree

4 files changed

+58
-4
lines changed

4 files changed

+58
-4
lines changed

__tests__/httpServer/apiV1.test.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ let getAllProjects
3737
let addProject
3838
let getAllGithubOrganizationsByProjectsId
3939
let getAllChecks
40+
let getCheckById
4041

4142
beforeAll(async () => {
4243
// Initialize server asynchronously
@@ -49,7 +50,8 @@ beforeAll(async () => {
4950
getAllProjects,
5051
addProject,
5152
getAllGithubOrganizationsByProjectsId,
52-
getAllChecks
53+
getAllChecks,
54+
getCheckById
5355
} = initializeStore(knex))
5456
})
5557

@@ -402,4 +404,33 @@ describe('HTTP Server API V1', () => {
402404

403405
test.todo('should return 500 for internal server error')
404406
})
407+
408+
describe('GET /api/v1/check/:checkId', () => {
409+
test('should return 200 and a check by ID', async () => {
410+
const response = await app.get('/api/v1/check/1')
411+
const storedCheck = await getCheckById(1)
412+
413+
expect(response.status).toBe(200)
414+
// @TODO: find a more elegant way to solve the issue with the date format
415+
expect(response.body).toStrictEqual(JSON.parse(JSON.stringify(storedCheck)))
416+
})
417+
418+
test('should return 400 for invalid check ID', async () => {
419+
const response = await app.get('/api/v1/check/invalid')
420+
421+
expect(response.status).toBe(400)
422+
expect(response.body).toHaveProperty('errors')
423+
expect(response.body.errors[0]).toHaveProperty('message', 'must be integer')
424+
})
425+
426+
test('should return 404 for check not found', async () => {
427+
const response = await app.get('/api/v1/check/9999999')
428+
429+
expect(response.status).toBe(404)
430+
expect(response.body).toHaveProperty('errors')
431+
expect(response.body.errors[0]).toHaveProperty('message', 'Compliance Check not found')
432+
})
433+
434+
test.todo('should return 500 for internal server error')
435+
})
405436
})

src/httpServer/routers/apiV1.js

Lines changed: 17 additions & 2 deletions
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 } = initializeStore(knex)
30+
const { addProject, getProjectByName, addGithubOrganization, getProjectById, getAllGithubOrganizationsByProjectsId, getAllChecks, getCheckById } = initializeStore(knex)
3131

3232
const router = express.Router()
3333

@@ -121,13 +121,28 @@ function createApiRouter (knex, express) {
121121
}
122122
})
123123

124+
router.get('/check/:checkId', async (req, res) => {
125+
try {
126+
// Params validation done in swagger
127+
const checkId = parseInt(req.params.checkId, 10)
128+
const check = await getCheckById(checkId)
129+
if (!check) {
130+
return res.status(404).json({ errors: [{ message: 'Compliance Check not found' }] })
131+
}
132+
res.json(check)
133+
} catch (error) {
134+
logger.error(error)
135+
res.status(500).json({ errors: [{ message: 'Failed to retrieve Compliance Check' }] })
136+
}
137+
})
138+
124139
router.get('/check', async (req, res) => {
125140
try {
126141
const checks = await getAllChecks()
127142
res.json(checks)
128143
} catch (error) {
129144
logger.error(error)
130-
res.status(500).json({ errors: [{ message: 'Failed to retrieve checks' }] })
145+
res.status(500).json({ errors: [{ message: 'Failed to retrieve Compliance Checks' }] })
131146
}
132147
})
133148

src/httpServer/swagger/api-v1.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ paths:
225225
description: The ID of the check
226226
schema:
227227
type: integer
228+
minimum: 1
228229
example: 53
229230
responses:
230231
'200':
@@ -233,6 +234,12 @@ paths:
233234
application/json:
234235
schema:
235236
$ref: '#/components/schemas/Check'
237+
'400':
238+
description: Bad request, invalid input data
239+
content:
240+
application/json:
241+
schema:
242+
$ref: '#/components/schemas/ErrorResponse'
236243
'404':
237244
description: Check not found
238245
content:

src/store/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,8 @@ const initializeStore = (knex) => {
249249
getAllOSSFResults: () => getAll('ossf_scorecard_results'),
250250
getProjectById: (id) => getOne('projects', id),
251251
getProjectByName: getProjectByName(knex),
252-
getAllChecks: () => getAll('compliance_checks')
252+
getAllChecks: () => getAll('compliance_checks'),
253+
getCheckById: (id) => getOne('compliance_checks', id)
253254
}
254255
}
255256

0 commit comments

Comments
 (0)