add sample tests for post commit hook

This commit is contained in:
pi-victor 2016-04-11 20:52:20 +02:00
parent 6263b25a02
commit 4166119035
6 changed files with 80 additions and 31 deletions

3
.gitignore vendored
View File

@ -1 +1,2 @@
/node_modules/ /node_modules/
npm-debug.log

View File

@ -112,7 +112,10 @@
"secret": "${GENERIC_WEBHOOK_SECRET}" "secret": "${GENERIC_WEBHOOK_SECRET}"
} }
} }
] ],
"postCommit": {
"script": "npm test"
},
} }
}, },
{ {

View File

@ -112,7 +112,10 @@
"secret": "${GENERIC_WEBHOOK_SECRET}" "secret": "${GENERIC_WEBHOOK_SECRET}"
} }
} }
] ],
"postCommit": {
"script": "npm test"
},
} }
}, },
{ {

View File

@ -4,16 +4,21 @@
"description": "Node.js sample app for OpenShift 3", "description": "Node.js sample app for OpenShift 3",
"main": "server.js", "main": "server.js",
"dependencies": { "dependencies": {
"express": "*", "chai": "^3.5.0",
"mongodb": "*", "chai-http": "^2.0.1",
"ejs": "*" "ejs": "^2.4.1",
"express": "^4.13.4",
"mocha": "^2.4.5",
"mongodb": "^2.1.16",
"morgan": "^1.7.0"
}, },
"engine": { "engine": {
"node": "*", "node": "*",
"npm": "*" "npm": "*"
}, },
"scripts": { "scripts": {
"start": "node server.js" "start": "node server.js",
"test": "IP=0.0.0.0 PORT=3030 mocha --timeout 5000 tests/*_test.js"
}, },
"repository": { "repository": {
"type": "git", "type": "git",

View File

@ -1,38 +1,44 @@
// OpenShift sample Node application // OpenShift sample Node application
var express = require('express'); var express = require('express'),
var fs = require('fs'); fs = require('fs'),
var app = express(); app = express(),
var eps = require('ejs'); eps = require('ejs'),
morgan = require('morgan');
app.engine('html', require('ejs').renderFile); app.engine('html', require('ejs').renderFile);
app.use(morgan('combined'))
var port = process.env.PORT || process.env.OPENSHIFT_NODEJS_PORT || 8080,
ip = process.env.IP || process.env.OPENSHIFT_NODEJS_IP || '0.0.0.0',
mongoURL = process.env.OPENSHIFT_MONGODB_DB_URL || process.env.MONGO_URL,
mongoURLLabel = "";
var port = process.env.PORT || process.env.OPENSHIFT_NODEJS_PORT || 8080;
var ip = process.env.IP || process.env.OPENSHIFT_NODEJS_IP || '0.0.0.0';
var mongoURL = process.env.OPENSHIFT_MONGODB_DB_URL || process.env.MONGO_URL;
var mongoURLLabel = "";
if (mongoURL == null && process.env.DATABASE_SERVICE_NAME) { if (mongoURL == null && process.env.DATABASE_SERVICE_NAME) {
var mongoServiceName = process.env.DATABASE_SERVICE_NAME.toUpperCase(); var mongoServiceName = process.env.DATABASE_SERVICE_NAME.toUpperCase(),
var mongoHost = process.env[mongoServiceName + "_SERVICE_HOST"]; mongoHost = process.env[mongoServiceName + '_SERVICE_HOST'],
var mongoPort = process.env[mongoServiceName + "_SERVICE_PORT"]; mongoPort = process.env[mongoServiceName + '_SERVICE_PORT'],
var mongoUser = process.env.MONGODB_USER mongoDatabase = process.env[mongoServiceName + '_DATABASE'],
if (mongoHost && mongoPort && process.env.MONGODB_DATABASE) { mongoPassword = process.env[mongoServiceName + '_PASSWORD']
mongoUser = process.env[mongoServiceName + '_USER'];
if (mongoHost && mongoPort && mongoDatabase) {
mongoURLLabel = mongoURL = 'mongodb://'; mongoURLLabel = mongoURL = 'mongodb://';
if (process.env.MONGODB_USER && process.env.MONGODB_PASSWORD) { if (mongoUser && mongoPassword) {
mongoURL += process.env.MONGODB_USER + ':' + process.env.MONGODB_PASSWORD + '@'; mongoURL += mongoUser + ':' + mongoPassword + '@';
} }
// Provide UI label that excludes user id and pw // Provide UI label that excludes user id and pw
mongoURLLabel += mongoHost + ':' + mongoPort + '/' + mongoDatabase;
mongoURL += mongoHost + ':' + mongoPort + '/' + mongoDatabase;
mongoURLLabel += mongoHost + ':' + mongoPort + '/' + process.env.MONGODB_DATABASE;
mongoURL += mongoHost + ':' + mongoPort + '/' + process.env.MONGODB_DATABASE;
} }
} }
var db = null; var db = null,
var dbDetails = new Object(); dbDetails = new Object();
var initDb = function(callback) { var initDb = function(callback) {
if (mongoURL == null) return; if (mongoURL == null) return;
var mongodb = require('mongodb'); var mongodb = require('mongodb');
if (mongodb == null) return; if (mongodb == null) return;
mongodb.connect(mongoURL, function(err, conn) { mongodb.connect(mongoURL, function(err, conn) {
@ -46,7 +52,7 @@ var initDb = function(callback) {
dbDetails.url = mongoURLLabel; dbDetails.url = mongoURLLabel;
dbDetails.type = 'MongoDB'; dbDetails.type = 'MongoDB';
console.log("Connected to MongoDB at: " + mongoURL); console.log('Connected to MongoDB at: %s', mongoURL);
}); });
}; };
@ -66,9 +72,9 @@ app.get('/', function (req, res) {
app.get('/pagecount', function (req, res) { app.get('/pagecount', function (req, res) {
if (db) { if (db) {
db.collection('counts').count(function(err, count ){ db.collection('counts').count(function(err, count ){
res.send('{ pageCount: ' + count +'}'); res.send('{ pageCount: ' + count + '}');
}); });
} else { } else {
res.send('{ pageCount: -1 }'); res.send('{ pageCount: -1 }');
} }
}); });
@ -84,4 +90,6 @@ initDb(function(err){
}); });
app.listen(port, ip); app.listen(port, ip);
console.log('Server running on ' + ip + ':' + port); console.log('Server running on http://%s:%s', ip, port);
module.exports = app ;

29
tests/app_test.js Normal file
View File

@ -0,0 +1,29 @@
var server = require('../server'),
chai = require('chai'),
chaiHttp = require('chai-http'),
should = chai.should();
chai.use(chaiHttp);
describe('Basic routes tests', function() {
it('GET to / should return 200', function(done){
chai.request(server)
.get('/')
.end(function(err, res) {
res.should.have.status(200);
done();
})
})
it('GET to /pagecount should return 200', function(done){
chai.request(server)
.get('/pagecount')
.end(function(err, res) {
res.should.have.status(200);
done();
})
})
})