Skip to content

Commit

Permalink
Merge pull request #68 from github/feelepxyz/simplify-fake-server-setup
Browse files Browse the repository at this point in the history
Refactor fake server setup
  • Loading branch information
Philip Harrison authored and GitHub committed Jul 28, 2021
2 parents e7f68ff + 03359dc commit c2aca79
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 36 deletions.
3 changes: 0 additions & 3 deletions __tests__/server/db.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,5 @@
}
}
],
"credentials": [{"id": 1}],
"dependencies": [],
"update_job_errors": [],
"pull_requests": []
}
59 changes: 27 additions & 32 deletions __tests__/server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@ const SERVER_PORT = process.argv.slice(2)[0] || 9000
// - POST /update_jobs {data: {...attrs}} would persist a new update job with id
// 2

// NOTE: Serialise the response like dependabot-api
router.render = (_, res) => {
const id = res.locals.data.id
const data = {
attributes: res.locals.data
}
if (id) {
data.id = id
server.use(middlewares)

server.get('/update_jobs/:id/details', (req, res) => {
const id = req.params.id
const updateJob = db.update_jobs.find(job => `${job.id}` === id)
if (!updateJob) {
return res.status(404).end()
}

res.jsonp({
data
data: {
attributes: updateJob
}
})
}

server.use(middlewares)
})

// Inject a legit GITHUB_TOKEN to increase rate limits fetching manifests from github
server.get('/update_jobs/:id/credentials', (_, res) => {
Expand All @@ -64,33 +64,28 @@ server.post(
db.pull_requests.push(data)
router.db.write()

res.jsonp({})
res.status(204).send()
}
)

// TEMP HACK: Always return 204 on post so the updater doesn't buil out
server.use(jsonServer.bodyParser, (req, res, next) => {
if (req.method === 'POST' && req.body.data) {
req.body = req.body.data
res.sendStatus(204)
return
}
next()
server.post('/update_jobs/:id/record_update_job_error', (_, res) => {
res.status(204).send()
})

// NOTE: These map to resources in db.json
server.use(
jsonServer.rewriter({
'/update_jobs/:id/details': '/update_jobs/:id',
'/update_jobs/:id/credentials': '/credentials/:id',
'/update_jobs/:id/record_update_job_error': '/update_job_errors/:id',
'/update_jobs/:id/mark_as_processed': '/update_jobs/:id',
'/update_jobs/:id/update_dependency_list': '/dependencies/:id',
'/update_jobs/:id/record_package_manager_version': '/update_jobs/:id'
})
)
server.patch('/update_jobs/:id/mark_as_processed', (_, res) => {
res.status(204).send()
})

server.post('/update_jobs/:id/update_dependency_list', (_, res) => {
res.status(204).send()
})

server.post('/update_jobs/:id/record_package_manager_version', (_, res) => {
res.status(204).send()
})

server.use(router)

server.listen(SERVER_PORT, () => {
console.log(`json-server is running on http://localhost:${SERVER_PORT}`)
})
2 changes: 1 addition & 1 deletion __tests__/updater-integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ describe('Updater', () => {
const res = await client.get('/pull_requests/1')

expect(res.status).toEqual(200)
expect(res.data.data.attributes['pr-title']).toEqual(
expect(res.data['pr-title']).toEqual(
'Bump fetch-factory from 0.0.1 to 0.2.1'
)
})
Expand Down

0 comments on commit c2aca79

Please sign in to comment.