-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
juan
committed
May 18, 2020
1 parent
fcc2376
commit c616f6e
Showing
14 changed files
with
1,063 additions
and
133 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,24 +1,21 @@ | ||
const express = require("express"); | ||
const config = require("./config"); | ||
const router = require("./routes") | ||
const {logger, middlewareLogger} = require('./logger'); | ||
const express = require('express') | ||
const config = require('./config') | ||
const router = require('./routes') | ||
const { logger, middlewareLogger } = require('./logger') | ||
|
||
const init = () => { | ||
const app = express() | ||
const port = config.common.port | ||
|
||
const app = express(); | ||
const port = config.common.port; | ||
|
||
module.exports = app; | ||
module.exports = app | ||
|
||
app.use(express.json()) | ||
|
||
|
||
app.use("/", router) | ||
app.use('/', router) | ||
app.use(middlewareLogger) | ||
|
||
logger.info(`Started app on http://localhost:${port}`) | ||
app.listen(port); | ||
|
||
}; | ||
app.listen(port) | ||
} | ||
|
||
init(); | ||
init() |
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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,18 +1,18 @@ | ||
require('dotenv').config() | ||
|
||
const config = { | ||
common: { | ||
database: { | ||
url: process.env.NODE_API_DB_URL, | ||
host: process.env.DB_HOST, | ||
port: process.env.DB_PORT, | ||
name: process.env.DB_NAME, | ||
username: process.env.DB_USERNAME, | ||
password: process.env.DB_PASSWORD | ||
}, | ||
environment: process.env.NODE_ENV || "development", | ||
port: process.env.PORT || 3030 | ||
} | ||
}; | ||
common: { | ||
database: { | ||
url: process.env.NODE_API_DB_URL, | ||
host: process.env.DB_HOST, | ||
port: process.env.DB_PORT, | ||
name: process.env.DB_NAME, | ||
username: process.env.DB_USERNAME, | ||
password: process.env.DB_PASSWORD | ||
}, | ||
environment: process.env.NODE_ENV || 'development', | ||
port: process.env.PORT || 3030 | ||
} | ||
} | ||
|
||
module.exports = config | ||
module.exports = config |
This file was deleted.
Oops, something went wrong.
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 @@ | ||
exports.auth = (req, res, next) => { | ||
const authorizedHeader = '123' | ||
if (req.headers.authorization === authorizedHeader) { | ||
next() | ||
} else { | ||
res.sendStatus(403) | ||
} | ||
} |
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 |
---|---|---|
@@ -1,33 +1,32 @@ | ||
'use strict'; | ||
'use strict' | ||
|
||
const fs = require('fs'); | ||
const path = require('path'); | ||
const Sequelize = require('sequelize'); | ||
const basename = path.basename(__filename); | ||
const fs = require('fs') | ||
const path = require('path') | ||
const Sequelize = require('sequelize') | ||
const basename = path.basename(__filename) | ||
|
||
const config = require('../config') | ||
const dbConfig = require('../config/db')[config.common.environment] | ||
const db = {}; | ||
const db = {} | ||
|
||
|
||
const sequelize = new Sequelize(dbConfig.database, dbConfig.username, dbConfig.password, dbConfig); | ||
const sequelize = new Sequelize(dbConfig.database, dbConfig.username, dbConfig.password, dbConfig) | ||
fs | ||
.readdirSync(__dirname) | ||
.filter(file => { | ||
return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js'); | ||
return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js') | ||
}) | ||
.forEach(file => { | ||
const model = sequelize['import'](path.join(__dirname, file)); | ||
db[model.name] = model; | ||
}); | ||
const model = sequelize.import(path.join(__dirname, file)) | ||
db[model.name] = model | ||
}) | ||
|
||
Object.keys(db).forEach(modelName => { | ||
if (db[modelName].associate) { | ||
db[modelName].associate(db); | ||
db[modelName].associate(db) | ||
} | ||
}); | ||
}) | ||
|
||
db.sequelize = sequelize; | ||
db.Sequelize = Sequelize; | ||
db.sequelize = sequelize | ||
db.Sequelize = Sequelize | ||
|
||
module.exports = db; | ||
module.exports = db |
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 |
---|---|---|
@@ -1,18 +1,17 @@ | ||
var express = require('express'); | ||
var router = express.Router(); | ||
var path = require('path') | ||
var express = require('express') | ||
var router = express.Router() | ||
var resource = require('./resource') | ||
|
||
router.get("/", function (req,res) { | ||
return res.send("Richard"); | ||
}); | ||
|
||
|
||
router.get("/health", function (req, res) { | ||
return res.send("OK"); | ||
}); | ||
|
||
router.use('/docs', express.static('docs')) | ||
router.get('/', function (req, res) { | ||
return res.send('Richard') | ||
}) | ||
|
||
router.get('/health', function (req, res) { | ||
return res.send('OK') | ||
}) | ||
|
||
router.use('/docs', express.static('docs')) | ||
|
||
router.use('/resource', resource) | ||
|
||
module.exports = router | ||
module.exports = router |
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 @@ | ||
var express = require('express') | ||
var router = express.Router() | ||
|
||
router.get('/:id', function (req, res) { | ||
return res.sendStatus(401) | ||
}) | ||
|
||
module.exports = router |
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,55 @@ | ||
const request = require('supertest') | ||
const app = require('../../app') | ||
const config = require('../../config') | ||
const expect = require('chai').expect | ||
// const dataCreator = require('../../dataCreator') | ||
|
||
describe('GET /resource', () => { | ||
beforeEach('Generate Test Data', async () => { | ||
// await dataCreator.seedAll() | ||
}) | ||
|
||
afterEach('clean data', async () => { | ||
// await dataCreator.cleanTables() | ||
}) | ||
|
||
context('With invalid token', () => { | ||
const baseRequest = request(app).get('/resource/23') | ||
|
||
it('No auth token should return unauthorized', (done) => { | ||
baseRequest | ||
.expect(response => { | ||
expect(response.statusCode).to.equal(401) | ||
}) | ||
.end(done) | ||
}) | ||
|
||
it('Invalid auth token should return unauthorized', (done) => { | ||
request(app).get('/resource/23') | ||
.set('authorization', 'invalid token') | ||
.expect(response => { | ||
expect(response.statusCode).to.equal(401) | ||
}) | ||
.end(done) | ||
}) | ||
}) | ||
|
||
// context('With valid auth token', () => { | ||
// it('respond with 404 if no resources found', (done) => { | ||
// request(app).get('/resource/notvalid').set('authorization', config.authToken) | ||
// .expect(response => { | ||
// expect(response.statusCode).to.equal(404) | ||
// }) | ||
// .end(done) | ||
// }) | ||
|
||
// it('respond with 200 if resources found', (done) => { | ||
// request(app).get('/resource/1').set('authorization', config.authToken) | ||
// .expect(response => { | ||
// expect(response.statusCode).to.equal(200) | ||
// // expect(response.body.message).to.contain(messageExpected) | ||
// }) | ||
// .end(done) | ||
// }) | ||
// }) | ||
}) |
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
const models = require('../models'); | ||
const models = require('../models') | ||
|
||
describe('Database tests', function() { | ||
it('should connect to database',async () => { | ||
await models.sequelize.authenticate() | ||
describe('Database tests', function () { | ||
it('should connect to database', async () => { | ||
await models.sequelize.authenticate() | ||
}) | ||
}) |
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 |
---|---|---|
@@ -1,33 +1,19 @@ | ||
const request = require("supertest"); | ||
const app = require("../app"); | ||
const config = require("../config"); | ||
const expect = require("chai").expect; | ||
const request = require('supertest') | ||
const app = require('../app') | ||
const expect = require('chai').expect | ||
|
||
describe("GET /docs", () => { | ||
it("respond with 200", (done) => { | ||
request(app).get("/").expect("Richard", done); | ||
describe('GET /docs', () => { | ||
it('respond with 200', (done) => { | ||
request(app).get('/').expect('Richard', done) | ||
}) | ||
}); | ||
}) | ||
|
||
describe("GET /health", () => { | ||
it("respond with 200", (done) => { | ||
response = request(app).get('/health') | ||
.expect( response => { | ||
expect(response.statusCode).to.equal(200) | ||
}) | ||
.end(done) | ||
describe('GET /health', () => { | ||
it('respond with 200', (done) => { | ||
request(app).get('/health') | ||
.expect(response => { | ||
expect(response.statusCode).to.equal(200) | ||
}) | ||
.end(done) | ||
}) | ||
}); | ||
|
||
describe('validPort', function(){ | ||
let port = config.common.port; | ||
it('Port should not be undefined', function(){ | ||
expect(port).to.not.be.undefined; | ||
}) | ||
it('Port should not be null', function(){ | ||
expect(port).to.not.be.null; | ||
}) | ||
it('Port should not be 0', function(){ | ||
expect(port).to.not.be.equal(0) | ||
}) | ||
}); | ||
}) |
Oops, something went wrong.