diff --git a/unleash-server/lib/eventDb.js b/unleash-server/lib/eventDb.js index e89f9d5587..ce348cdd83 100644 --- a/unleash-server/lib/eventDb.js +++ b/unleash-server/lib/eventDb.js @@ -2,8 +2,8 @@ var Promise = require('bluebird'), dbPool = require('./dbPool'); function storeEvent(event) { - var sql = 'INSERT INTO events(type, user, data) VALUES ($1, $2, $3)'; - var params = [event.type,event.user,event.data]; + var sql = 'INSERT INTO events(type, created_by, data) VALUES ($1, $2, $3)'; + var params = [event.type, event.createdBy, event.data]; return new Promise(function (resolve, reject) { dbPool.query(sql, params, function (err) { @@ -15,5 +15,5 @@ function storeEvent(event) { module.exports = { - store: storeEvent() + store: storeEvent }; \ No newline at end of file diff --git a/unleash-server/lib/eventStore.js b/unleash-server/lib/eventStore.js index 846fe293f0..0c4207c08f 100644 --- a/unleash-server/lib/eventStore.js +++ b/unleash-server/lib/eventStore.js @@ -1,5 +1,5 @@ -var Promise = require('bluebird'), - util = require('util'), +var util = require('util'), + eventDb = require('./eventDb'), EventEmitter = require('events').EventEmitter; function EventStore() { @@ -8,10 +8,10 @@ function EventStore() { util.inherits(EventStore, EventEmitter); EventStore.prototype.create = function (event) { - return new Promise(function (resolve) { - this.emit(event.type, event); - resolve(); - }.bind(this)); + var that = this; + return eventDb.store(event).then(function() { + that.emit(event.type, event); + }); }; module.exports = new EventStore(); \ No newline at end of file diff --git a/unleash-server/lib/featureApi.js b/unleash-server/lib/featureApi.js index 884810a64b..ec25836441 100644 --- a/unleash-server/lib/featureApi.js +++ b/unleash-server/lib/featureApi.js @@ -23,7 +23,7 @@ module.exports = function (app) { app.post('/features', function (req, res) { var newFeature = req.body, - user = req.connection.remoteAddress; + createdBy = req.connection.remoteAddress; db.getFeature(newFeature.name).then(function (feature) { if (feature) { @@ -32,7 +32,7 @@ module.exports = function (app) { } else { eventStore.create({ type: eventType.featureCreated, - user: user, + createdBy: createdBy, data: newFeature }).then(function() { res.status(201).end(); diff --git a/unleash-server/lib/featureDb.js b/unleash-server/lib/featureDb.js index 08d960da46..282ec66ec7 100644 --- a/unleash-server/lib/featureDb.js +++ b/unleash-server/lib/featureDb.js @@ -1,10 +1,11 @@ var eventStore = require('./eventStore'), eventType = require('./eventType'), featuresMock = require('./featuresMock'), + logger = require('./logger'), Promise = require('bluebird'); eventStore.on(eventType.featureCreated, function (event) { - console.log('feature created event recieved', event); + logger.info('feature created event recieved', event); } ); diff --git a/unleash-server/package.json b/unleash-server/package.json index 68bffe570f..f2b3732e89 100644 --- a/unleash-server/package.json +++ b/unleash-server/package.json @@ -1,54 +1,55 @@ { - "name": "unleash-server", - "description": "unleash your features", - "version": "0.0.1", - "keywords": [ - "unleash", - "feature toggle", - "feature", - "toggle" - ], - "repository": { - "type": "git", - "url": "ssh://git@github.com:finn-no/unleash.git" - }, - "bugs": { - "url": "https://github.com/finn-no/unleash/issues" - }, - "private": true, - "scripts": { - "start": "node server.js", - "start-dev": "NODE_ENV=local supervisor --ignore ./node_modules/,./public/js server.js", - "test": "jshint server.js lib public/scripts test && mocha test test/*", - "tdd": "mocha --watch test test/*", - "test-bamboo-ci": "mocha test test/*", - "db-create": "createdb unleash_${NODE_ENV:-dev}", - "db-drop": "dropdb unleash_${NODE_ENV:-dev}", - "db-migrate-up": "db-migrate --config config/database.json up", - "db-migrate-down": "db-migrate --config config/database.json down", - "db-setup": "npm run db-create; npm run db-migrate-up" - }, - "dependencies": { - "bluebird": "2.2.2", - "body-parser": "1.4.3", - "db-migrate": "^0.7.1", - "errorhandler": "1.1.1", - "express": "4.9.8", - "express-validator": "2.6.0", - "ini": "1.3.0", - "log4js": "0.6.21", - "nconf": "0.6.9", - "pg": "^3.6.1", - "any-db": "2.1.0", - "any-db-pool": "2.1.0", - "any-db-postgres": "2.1.3" - }, - "devDependencies": { - "chai": "1.9.1", - "jshint": "2.5.2", - "mocha": "1.20.1", - "supertest": "0.13.0", - "supervisor": "~0.6.0", - "xmlbuilder": "^2.4.4" - } + "name": "unleash-server", + "description": "unleash your features", + "version": "0.0.1", + "keywords": [ + "unleash", + "feature toggle", + "feature", + "toggle" + ], + "repository": { + "type": "git", + "url": "ssh://git@github.com:finn-no/unleash.git" + }, + "bugs": { + "url": "https://github.com/finn-no/unleash/issues" + }, + "private": true, + "scripts": { + "start": "node server.js", + "start-dev": "NODE_ENV=local supervisor --ignore ./node_modules/,./public/js server.js", + "test": "jshint server.js lib public/scripts test && mocha test test/*", + "tdd": "mocha --watch test test/*", + "test-bamboo-ci": "mocha test test/*", + "db-create": "createdb unleash_${NODE_ENV:-dev}", + "db-drop": "dropdb unleash_${NODE_ENV:-dev}", + "db-migrate-up": "db-migrate --config config/database.json up", + "db-migrate-down": "db-migrate --config config/database.json down", + "db-setup": "npm run db-create; npm run db-migrate-up" + }, + "dependencies": { + "bluebird": "2.2.2", + "body-parser": "1.4.3", + "db-migrate": "^0.7.1", + "errorhandler": "1.1.1", + "express": "4.9.8", + "express-validator": "2.6.0", + "ini": "1.3.0", + "log4js": "0.6.21", + "nconf": "0.6.9", + "pg": "^3.6.1", + "any-db": "2.1.0", + "any-db-pool": "2.1.0", + "any-db-postgres": "2.1.3" + }, + "devDependencies": { + "chai": "1.9.1", + "jshint": "2.5.2", + "mocha": "1.20.1", + "supertest": "0.13.0", + "supervisor": "~0.6.0", + "xmlbuilder": "^2.4.4", + "mockery": "~1.4.0" + } } diff --git a/unleash-server/test/eventDbMock.js b/unleash-server/test/eventDbMock.js new file mode 100644 index 0000000000..a610f775b2 --- /dev/null +++ b/unleash-server/test/eventDbMock.js @@ -0,0 +1,13 @@ +var Promise = require('bluebird'); + +function storeEvent(event) { + console.log('using eventDbMock to store: ', event); + return new Promise(function (resolve) { + resolve(); + }); +} + + +module.exports = { + store: storeEvent +}; \ No newline at end of file diff --git a/unleash-server/test/eventStoreSpec.js b/unleash-server/test/eventStoreSpec.js index aaf5b04b3c..9f711747a1 100644 --- a/unleash-server/test/eventStoreSpec.js +++ b/unleash-server/test/eventStoreSpec.js @@ -1,8 +1,27 @@ -var assert = require('assert'); -var eventType = require('../lib/eventType'); -var eventStore = require('../lib/eventStore'); +var assert = require('assert'), + mockery = require('mockery'), + eventType = require('../lib/eventType'), + eventStore; + describe('EventStore', function () { + before(function () { + mockery.enable({ + warnOnReplace: false, + warnOnUnregistered: false, + useCleanCache: true + }); + + mockery.registerSubstitute('./eventDb', '../test/eventDbMock'); + + eventStore = require('../lib/eventStore'); + }); + + after(function () { + mockery.disable(); + mockery.deregisterAll(); + }); + describe('#create()', function () { it('should emit event', function (done) { eventStore.once(eventType.featureCreated, function (x) { @@ -20,7 +39,7 @@ describe('EventStore', function () { eventStore.create({ type: eventType.featureCreated, - user: 'ole', + createdBy: 'ole', data: eventData }); }); diff --git a/unleash-server/test/featureApiSpec.js b/unleash-server/test/featureApiSpec.js index 8072d5a88c..fa4b572278 100644 --- a/unleash-server/test/featureApiSpec.js +++ b/unleash-server/test/featureApiSpec.js @@ -1,4 +1,5 @@ -var request = require('supertest'); +var request = require('supertest'), + mockery = require('mockery'), request = request('http://localhost:4242'); @@ -6,10 +7,21 @@ describe('The api', function () { var server; before(function () { + mockery.enable({ + warnOnReplace: false, + warnOnUnregistered: false, + useCleanCache: true + }); + + mockery.registerSubstitute('./eventDb', '../test/eventDbMock'); + + server = require('../server'); }); after(function () { + mockery.disable(); + mockery.deregisterAll(); server.server.close(); });