This repository has been archived by the owner on Nov 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DEV-147 added basic routes and a paw file to test with
- Loading branch information
Showing
13 changed files
with
156 additions
and
32 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
const getPing = (req, res) => { | ||
res.json({ | ||
response: 'Okay', | ||
uptime: process.uptime() | ||
}) | ||
} | ||
|
||
module.exports = getPing |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const getPing = (req, res) => { | ||
res.json([ | ||
{ | ||
version: 1, | ||
path: '/api/v1' | ||
} | ||
]) | ||
} | ||
|
||
module.exports = getPing |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const path = require('path') | ||
|
||
const traversal = require('../utils/traversal') | ||
|
||
module.exports = traversal(__dirname, path.basename(module.filename))() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const path = require('path') | ||
|
||
const traversal = require('../../utils/traversal') | ||
|
||
module.exports = traversal(__dirname, path.basename(module.filename))() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const postLogin = (req, res) => { | ||
res.json({ | ||
test: 'test' | ||
}) | ||
} | ||
|
||
module.exports = postLogin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const postLogout = (req, res) => { | ||
res.json({ | ||
test: 'test' | ||
}) | ||
} | ||
|
||
module.exports = postLogout |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* eslint-disable camelcase */ | ||
const { getPing, getVersions, v1_postLogin, v1_postLogout } = require('./api') | ||
|
||
module.exports = { | ||
get: { | ||
'/': getVersions, | ||
'/ping': getPing | ||
}, | ||
post: { | ||
'/api/v1/login': v1_postLogin, | ||
'/api/v1/logout': v1_postLogout | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
const routes = require('../routes') | ||
|
||
const METHODS = ['get', 'post', 'patch', 'put', 'delete'] | ||
|
||
const rest = app => { | ||
const mapRoute = method => { | ||
if (routes[method]) { | ||
Object.keys(routes[method]).forEach(route => { | ||
app[method](route, routes[method][route]) | ||
}) | ||
} | ||
} | ||
|
||
METHODS.forEach(mapRoute) | ||
} | ||
|
||
module.exports = rest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
const fs = require('fs') | ||
const path = require('path') | ||
|
||
const traversal = (base, basename) => { | ||
const jsFiles = file => | ||
file.indexOf('.') !== 0 && file !== basename && file.slice(-3) === '.js' | ||
|
||
const folders = file => file.indexOf('.') === -1 | ||
|
||
const requireJs = (acc, elem) => { | ||
const controller = require(path.join(base, elem)) | ||
acc[elem.slice(0, -3)] = controller | ||
return acc | ||
} | ||
|
||
const requireFolderIndex = (acc, elem) => { | ||
const controllers = require(path.join(base, elem, basename)) | ||
Object.keys(controllers).forEach(controller => { | ||
acc[`${elem}_${controller}`] = controllers[controller] | ||
}) | ||
return acc | ||
} | ||
|
||
const getApis = () => { | ||
const contents = fs.readdirSync(base) | ||
const api = contents.filter(jsFiles).reduce(requireJs, {}) | ||
return contents.filter(folders).reduce(requireFolderIndex, api) | ||
} | ||
|
||
return getApis | ||
} | ||
|
||
module.exports = traversal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,21 @@ describe('Routes', () => { | |
db.close() | ||
}) | ||
|
||
describe('Ping', () => { | ||
describe('/', () => { | ||
it('returns a list of versions and status code 200', done => { | ||
request(server) | ||
.get('/') | ||
.end((err, res) => { | ||
expect(err).to.not.exist | ||
expect(res.statusCode).to.equal(200) | ||
expect(res.body[0].version).to.equal(1) | ||
expect(res.body[0].path).to.equal('/api/v1') | ||
done() | ||
}) | ||
}) | ||
}) | ||
|
||
describe('/ping', () => { | ||
it('returns an Okay result and status code 200', done => { | ||
request(server) | ||
.get('/ping') | ||
|
@@ -27,4 +41,33 @@ describe('Routes', () => { | |
}) | ||
}) | ||
}) | ||
|
||
describe('/api/v1/login', () => { | ||
it('returns a test result and status code 200', done => { | ||
request(server) | ||
.post('/api/v1/login', { | ||
username: '[email protected]', | ||
password: 'Password1' | ||
}) | ||
.end((err, res) => { | ||
expect(err).to.not.exist | ||
expect(res.statusCode).to.equal(200) | ||
expect(res.body.test).to.equal('test') | ||
done() | ||
}) | ||
}) | ||
}) | ||
|
||
describe('/api/v1/logout', () => { | ||
it('returns a test result and status code 200', done => { | ||
request(server) | ||
.post('/api/v1/logout') | ||
.end((err, res) => { | ||
expect(err).to.not.exist | ||
expect(res.statusCode).to.equal(200) | ||
expect(res.body.test).to.equal('test') | ||
done() | ||
}) | ||
}) | ||
}) | ||
}) |