From f2ba05bdf0426962f3082c81cd273667440c918d Mon Sep 17 00:00:00 2001 From: Drasko DRASKOVIC Date: Sun, 1 Nov 2015 03:25:29 +0100 Subject: [PATCH] Add Mocha, Chai and Supertest combo for testing Signed-off-by: Drasko DRASKOVIC --- app/routes/status.js | 2 +- package.json | 5 +++-- server.js | 45 +++++++++++++++++++++++++++++--------------- test/apiTest.js | 35 ++++++++++++++++++++++++++++++++++ 4 files changed, 69 insertions(+), 18 deletions(-) create mode 100644 test/apiTest.js diff --git a/app/routes/status.js b/app/routes/status.js index d88bab4da5..40b8fda1fe 100644 --- a/app/routes/status.js +++ b/app/routes/status.js @@ -7,7 +7,7 @@ router.route('/') // get the status (accessed at GET http://localhost:8080/status) .get(function(req, res) { - var stat = {"status":"OK"} + var stat = {"status":"running"} res.send(stat); }); diff --git a/package.json b/package.json index 2907283dbb..71bef1fd03 100644 --- a/package.json +++ b/package.json @@ -7,15 +7,16 @@ "url": "https://github.com/Mainflux/mainflux" }, "license": "Apache-2.0", - "dependencies": { "express": "~4.0.0", "mongoose": "~3.6.13", "body-parser": "~1.0.1" }, "devDependencies": { + "chai": "^3.4.0", "gulp-jshint": "^1.11.2", "gulp-nodemon": "^2.0.3", - "jshint-stylish": "^2.0.1" + "jshint-stylish": "^2.0.1", + "supertest": "^1.1.0" } } diff --git a/server.js b/server.js index 8c60e3305a..ff70306399 100644 --- a/server.js +++ b/server.js @@ -1,4 +1,10 @@ -// server.js +/** + * Copyright (c) Mainflux + * + * Mainflux server is licensed under an Apache license, version 2.0 license. + * All rights not explicitly granted in the Apache license, version 2.0 are reserved. + * See the included LICENSE file for more details. + */ /** * Extrenal configs are kept in the config.js file on the same level @@ -7,36 +13,45 @@ var config = require('./config/config'); console.log(config.message); -// BASE SETUP -// ============================================================================= - -// call the packages we need +/** + * SETUP + */ var express = require('express'); // call express var app = express(); // define our app using express var bodyParser = require('body-parser'); -// MongoDB +/** MongoDB */ var mongoose = require('mongoose'); -// Docker MongoDB url -var docker_mongo_url = process.env.MAINFLUX_MONGODB_1_PORT_27017_TCP_ADDR -mongoose.connect(docker_mongo_url || config.db.path + ':' + config.db.port + '/' + config.db.name); // connect to our database +/** Docker MongoDB url */ +var docker_mongo_url = process.env.MAINFLUX_MONGODB_1_PORT_27017_TCP_ADDR +/** Connect to DB */ +mongoose.connect(docker_mongo_url || config.db.path + ':' + config.db.port + '/' + config.db.name); -// configure app to use bodyParser() -// this will let us get the data from a POST +/** Configure app to use bodyParser() */ app.use(bodyParser.urlencoded({ extended: true })); app.use(bodyParser.json()); var port = process.env.PORT || config.port; // set our port -// ROUTES FOR OUR API -// ============================================================================= + +/** + * ROUTES + */ app.use('/status', require('./app/routes/status')); app.use('/devices', require('./app/routes/devices')); -// START THE SERVER -// ============================================================================= + +/** + * SERVER START + */ app.listen(port); console.log('Magic happens on port ' + port); + + +/** + * Export app for testing + */ +module.exports = app; diff --git a/test/apiTest.js b/test/apiTest.js new file mode 100644 index 0000000000..e4103683d0 --- /dev/null +++ b/test/apiTest.js @@ -0,0 +1,35 @@ +/** Chai stuff */ +var should = require('chai').should; +var expect = require('chai').expect; + +/** Supertest for API */ +var supertest = require('supertest'); +var server = require('../server'); +var api = supertest(server); + +/** + * API test description + */ +describe('loading express', function () { + /** + * /status + */ + it('responds to /status', function testSlash(done) { + api + .get('/status') + .expect(200) + .end(function(err, res){ + expect(res.body.status).to.equal("running"); + done(); + }); + }); + + /** + * /foo/bar + */ + it('404 /foo/bar', function testPath(done) { + api + .get('/foo/bar') + .expect(404, done); + }); +});