Skip to content

Commit

Permalink
BE #4 rebase develop and fix test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
tito433 committed May 17, 2020
1 parent 8d2590d commit bd51594
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 31 deletions.
23 changes: 4 additions & 19 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,14 @@
var debug = require('debug')('cms-backend-api:*');
var express = require('express');
var logger = require('morgan');
var mongoose=require('mongoose');

//custom modules
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var articleRouter = require('./routes/article');

//connect to database
var connect = mongoose.connect(process.env.DB_URL,{
autoIndex: false,
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false
});

connect.then((db) => {
debug('Connected to database ' + process.env.DB_URL);
}, (err) => { console.log(err); });

const bodyParser = require('body-parser');

const indexRouter = require('./routes/index')
const authRouter = require('./routes/auth.route');
const usersRouter = require('./routes/users.route');
const binaryRouter=require('./routes/binaries.route');
const articleRouter = require('./routes/article');

const app = express();

Expand All @@ -49,10 +33,11 @@ app.use(function (req, res, next) {
app.use(bodyParser.json());

//List all the routes
app.use('/', indexRouter);
app.use('/auth', authRouter);
app.use(`${process.env.API_ENPOINT_BASE}/users`, usersRouter);
app.use(`${process.env.API_ENPOINT_BASE}/binaries`, binaryRouter);
app.use('/article', articleRouter);
app.use(`${process.env.API_ENPOINT_BASE}/article`, articleRouter);

app.get(process.env.API_ENPOINT_BASE, (req, res) => {
res.send(`CMS API v1.`)
Expand Down
12 changes: 12 additions & 0 deletions routes/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

var express = require('express');
var router = express.Router();

/* GET home page. */
router.get('/', function(req, res, next) {
res.statusCode=200;
res.setHeader('Content-Type', 'text/html');
res.end('Hello CMS');
});

module.exports = router;
7 changes: 3 additions & 4 deletions services/db.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@ let count = 0;

const options = {
autoIndex: false, // Don't build indexes
reconnectTries: 30, // Retry up to 30 times
reconnectInterval: 500, // Reconnect every 500ms
poolSize: 10, // Maintain up to 10 socket connections
// If not connected, return errors immediately rather than waiting for reconnect
bufferMaxEntries: 0,
//get rid off the depreciation errors
useNewUrlParser: true,
useUnifiedTopology: true
useUnifiedTopology: true,
useFindAndModify: false

};
const connectWithRetry = () => {
console.log('MongoDB connection with retry')
//console.log('MongoDB connection with retry')
mongoose.connect(process.env.DB_URL, options).then(() => {
debug('MongoDB is connected')
}).catch(err => {
Expand Down
16 changes: 8 additions & 8 deletions test/routes/article.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const app = require("../../app");
describe("Test GET /article", () => {
test("It should response 200 on GET method", done => {
request(app)
.get("/article")
.get(`${process.env.API_ENPOINT_BASE}/article`)
.then(response => {
expect(response.statusCode).toBe(200);
done();
Expand All @@ -22,7 +22,7 @@ const article = {
describe("Test POST /article", () => {
test("It should response 200 on POST method", done => {
request(app)
.post("/article")
.post(`${process.env.API_ENPOINT_BASE}/article`)
.send(article)
.then(response => {
const respArticle = JSON.parse(response.text);
Expand All @@ -37,13 +37,13 @@ describe("Test POST /article", () => {
describe("Test POST /article/:articleId", () => {
test("It should response 200 on GET method", done => {
request(app)
.get("/article")
.get(`${process.env.API_ENPOINT_BASE}/article`)
.then(response => {
expect(response.statusCode).toBe(200);
const articles = JSON.parse(response.text);
if (articles && articles.length) {
request(app)
.get("/article/"+articles[0]._id)
.get(`${process.env.API_ENPOINT_BASE}/article/${articles[0]._id}`)
.then(response => {
expect(response.statusCode).toBe(200);
done();
Expand All @@ -60,7 +60,7 @@ describe("Test POST /article/:articleId", () => {
describe("Test PUT /article/:articleId", () => {
test("It should response 200 on PUT method", done => {
request(app)
.get("/article")
.get(`${process.env.API_ENPOINT_BASE}/article`)
.then(response => {
expect(response.statusCode).toBe(200);

Expand All @@ -72,7 +72,7 @@ describe("Test PUT /article/:articleId", () => {
art.title=newTitle;

request(app)
.put("/article/"+art._id)
.put(`${process.env.API_ENPOINT_BASE}/article/${art._id}`)
.send(art)
.then(resp => {
expect(resp.statusCode).toBe(200);
Expand All @@ -92,15 +92,15 @@ describe("Test PUT /article/:articleId", () => {
describe("Test DELETE /article/:articleId", () => {
test("It should response 200 on DELETE method", done => {
request(app)
.get("/article")
.get(`${process.env.API_ENPOINT_BASE}/article`)
.then(response => {
expect(response.statusCode).toBe(200);

const articles = JSON.parse(response.text);
if (articles && articles.length) {
let artId=articles[0]._id;
request(app)
.delete("/article/"+artId)
.delete(`${process.env.API_ENPOINT_BASE}/article/${artId}`)
.then(resp => {
expect(resp.statusCode).toBe(200);
done();
Expand Down

0 comments on commit bd51594

Please sign in to comment.