Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion __tests__/httpServer/apiV1.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ let getAllProjects
let addProject
let getAllGithubOrganizationsByProjectsId
let getAllChecks
let getCheckById

beforeAll(async () => {
// Initialize server asynchronously
Expand All @@ -49,7 +50,8 @@ beforeAll(async () => {
getAllProjects,
addProject,
getAllGithubOrganizationsByProjectsId,
getAllChecks
getAllChecks,
getCheckById
} = initializeStore(knex))
})

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

test.todo('should return 500 for internal server error')
})

describe('GET /api/v1/check/:checkId', () => {
test('should return 200 and a check by ID', async () => {
const response = await app.get('/api/v1/check/1')
const storedCheck = await getCheckById(1)

expect(response.status).toBe(200)
// @TODO: find a more elegant way to solve the issue with the date format
expect(response.body).toStrictEqual(JSON.parse(JSON.stringify(storedCheck)))
})

test('should return 400 for invalid check ID', async () => {
const response = await app.get('/api/v1/check/invalid')

expect(response.status).toBe(400)
expect(response.body).toHaveProperty('errors')
expect(response.body.errors[0]).toHaveProperty('message', 'must be integer')
})

test('should return 404 for check not found', async () => {
const response = await app.get('/api/v1/check/9999999')

expect(response.status).toBe(404)
expect(response.body).toHaveProperty('errors')
expect(response.body.errors[0]).toHaveProperty('message', 'Compliance Check not found')
})

test.todo('should return 500 for internal server error')
})
})
19 changes: 17 additions & 2 deletions src/httpServer/routers/apiV1.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const runWorkflow = ({ workflowName, knex, data } = {}) => new Promise((resolve,
})

function createApiRouter (knex, express) {
const { addProject, getProjectByName, addGithubOrganization, getProjectById, getAllGithubOrganizationsByProjectsId, getAllChecks } = initializeStore(knex)
const { addProject, getProjectByName, addGithubOrganization, getProjectById, getAllGithubOrganizationsByProjectsId, getAllChecks, getCheckById } = initializeStore(knex)

const router = express.Router()

Expand Down Expand Up @@ -121,13 +121,28 @@ function createApiRouter (knex, express) {
}
})

router.get('/check/:checkId', async (req, res) => {
try {
// Params validation done in swagger
const checkId = parseInt(req.params.checkId, 10)
const check = await getCheckById(checkId)
if (!check) {
return res.status(404).json({ errors: [{ message: 'Compliance Check not found' }] })
}
res.json(check)
} catch (error) {
logger.error(error)
res.status(500).json({ errors: [{ message: 'Failed to retrieve Compliance Check' }] })
}
})

router.get('/check', async (req, res) => {
try {
const checks = await getAllChecks()
res.json(checks)
} catch (error) {
logger.error(error)
res.status(500).json({ errors: [{ message: 'Failed to retrieve checks' }] })
res.status(500).json({ errors: [{ message: 'Failed to retrieve Compliance Checks' }] })
}
})

Expand Down
41 changes: 41 additions & 0 deletions src/httpServer/swagger/api-v1.yml
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,47 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/ErrorResponse'
/api/v1/check/{checkId}:
get:
summary: Get a check by ID
description: Returns a check by ID
operationId: getCheckById
tags:
- Checks
parameters:
- name: checkId
in: path
required: true
description: The ID of the check
schema:
type: integer
minimum: 1
example: 53
responses:
'200':
description: A check
content:
application/json:
schema:
$ref: '#/components/schemas/Check'
'400':
description: Bad request, invalid input data
content:
application/json:
schema:
$ref: '#/components/schemas/ErrorResponse'
'404':
description: Check not found
content:
application/json:
schema:
$ref: '#/components/schemas/ErrorResponse'
'500':
description: Internal server error
content:
application/json:
schema:
$ref: '#/components/schemas/ErrorResponse'
/api/v1/check:
get:
summary: List all checks
Expand Down
3 changes: 2 additions & 1 deletion src/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,8 @@ const initializeStore = (knex) => {
getAllOSSFResults: () => getAll('ossf_scorecard_results'),
getProjectById: (id) => getOne('projects', id),
getProjectByName: getProjectByName(knex),
getAllChecks: () => getAll('compliance_checks')
getAllChecks: () => getAll('compliance_checks'),
getCheckById: (id) => getOne('compliance_checks', id)
}
}

Expand Down
Loading