diff --git a/src/core/Chain/chain.controller.js b/src/core/Chain/chain.controller.js new file mode 100644 index 0000000..9ffdb37 --- /dev/null +++ b/src/core/Chain/chain.controller.js @@ -0,0 +1,81 @@ +var Chain = require('../../models/Chain.js'); +var mongoose = require('mongoose'); + +module.exports = (app)=>{ + + 'use strict'; + + var getAll = function(req, res, next){ + Chain.find().exec().then(function(chain){ + res.status(201).send({'chains' : chain}); + return next(); + }).catch(function(err){ + res.status(500).send('Internal Error Server'); + return next(); + }); + } + + var addChain = function(req, res, next){ + if (!req.body.chain) return res.status(404).send('Missing property chain!'); + if (!req.body.chain.name) return res.status(404).send('Missing property name!'); + let newChain = new Chain(req.body.chain); + newChain.userId = req.user._id; + newChain.save().then(function(entity){ + res.status(201).send(entity); + return next(); + }).catch( function(err){ + return res.status(500).send('Missing parameters'); + }); + } + + var remove = function (req, res, next){ + if(!req.body.chain) return res.status(404).send('Missing property chain!'); + if(!req.body.chain._id) return res.status(404).send('Missing property _id'); + Chain.findByIdAndRemove({'_id':req.body.chain._id}).exec().then(function (){ + res.status(200).send("Chain remove"); + return next(); + }).catch( function(err){ + res.status(500).send("Fail delete chain") + return next(); + }) + } + + var update = function(req, res, next){ + if(!req.body.chain) return res.status(404).send('Missing property chain!'); + if(!req.body.chain._id) return res.status(404).send('Missing property _id'); + Chain.findByIdAndUpdate({'_id':req.body.chain._id}, req.body.chain, {upsert: true}).exec().then(function(chain){ + res.status(201).send('Chain updated'); + return next(); + }).catch(function(err){ + res.status(500).send('Fail Update'); + return next(); + }) + } + + var getSingleChain = function(req, res, next){ + if(!req.body.chain) return res.status(404).send('Missing property chain!'); + if(!req.body.chain._id) return res.status(404).send('Missing property _id'); + if(!req.body.chain._id) return res.status(404).send('Missing property _id'); + Chain.findById(req.body.chain._id).exec().then(function(chain){ + res.status(201).send({'chain' : chain}); + return next(); + }).catch(function(err){ + res.status(500).send('Fail Get'); + return next(); + }) + } + + return{ + getAll : getAll, + addChain : addChain, + remove : remove, + update : update, + getSingleChain : getSingleChain + } +} + +//curl -X GET -H 'Authorization: Bearer 50684f53063111e71779fc1ee4987bc76c770c2dabfeac0a15dd63f63a9bc92b' http://localhost:3000/v1/chain -v +//curl -X POST -H "Content-Type: application/json" -d '{"chain" : {"name":"Brian"}}' -H 'Authorization: Bearer 50684f53063111e71779fc1ee4987bc76c770c2dabfeac0a15dd63f63a9bc92b' http://localhost:3000/v1/chain -v +//curl -X GET -H "Content-Type: application/json" -d '{"chain" : {"_id":"578a9027fe5e7e10148b0a74"}}' -H 'Authorization: Bearer 50684f53063111e71779fc1ee4987bc76c770c2dabfeac0a15dd63f63a9bc92b' http://localhost:3000/v1/chain/577690c42a1afed8366c4905 -v +//curl -X PUT -H "Content-Type: application/json" -d '{"chain" : {"_id":"578a9027fe5e7e10148b0a74", "name":"miaolo"}}' -H 'Authorization: Bearer 50684f53063111e71779fc1ee4987bc76c770c2dabfeac0a15dd63f63a9bc92b' http://localhost:3000/v1/chain/577690c42a1afed8366c4905 -v +//curl -X DELETE -H "Content-Type: application/json" -d '{"chain" : {"_id":"578a9027fe5e7e10148b0a74"}}' -H 'Authorization: Bearer 50684f53063111e71779fc1ee4987bc76c770c2dabfeac0a15dd63f63a9bc92b' http://localhost:3000/v1/chain/577690c42a1afed8366c4905 -v diff --git a/src/models/Block.js b/src/models/Chain.js similarity index 77% rename from src/models/Block.js rename to src/models/Chain.js index 158917e..dd93539 100644 --- a/src/models/Block.js +++ b/src/models/Chain.js @@ -3,7 +3,7 @@ var mongoose = require('mongoose'); var Schema = mongoose.Schema; -var Block = new Schema({ +var Chain = new Schema({ userId: { type: Schema.ObjectId, required: true, @@ -18,4 +18,4 @@ var Block = new Schema({ } }); -module.exports = mongoose.model('Block', Block); +module.exports = mongoose.model('Chain', Chain); diff --git a/src/routing/block.js b/src/routing/block.js deleted file mode 100644 index a973956..0000000 --- a/src/routing/block.js +++ /dev/null @@ -1,32 +0,0 @@ -'use strict' - -var passport = require('passport'); -var auth = require('../auth/auth'); - -module.exports = (app) => { - - // BLOCK CRUD - app.route('/v1/block') - .all( auth.bearer() ) - - .get( (req, res, next) => { - res.send({'uno': 'due'}); - return next(); - }) - - .post( (req, res, next) => { - res.send({'uno': 'due'}); - return next(); - }) - - .put( (req, res, next) => { - res.send({'uno': 'due'}); - return next(); - }) - - .delete( (req, res, next) => { - res.send({'uno': 'due'}); - return next(); - }) - -} diff --git a/src/routing/chain.js b/src/routing/chain.js new file mode 100644 index 0000000..113e7d9 --- /dev/null +++ b/src/routing/chain.js @@ -0,0 +1,24 @@ +'use strict' + +var passport = require('passport'); +var auth = require('../auth/auth'); +var Chain = require('../core/Chain/chain.controller.js')(); +module.exports = (app) => { + // CHAIN CRUD ALL + app.route('/v1/chain') + .all( auth.bearer() ) + + .get( Chain.getAll ) + + .post( Chain.addChain ); + + // CHAIN CRUD SINGLE + app.route('/v1/chain/:id') + .all( auth.bearer() ) + + .get( Chain.getSingleChain ) + + .put( Chain.update) + + .delete( Chain.remove ) +} diff --git a/src/routing/index.js b/src/routing/index.js index 28db578..8c45e0c 100644 --- a/src/routing/index.js +++ b/src/routing/index.js @@ -4,7 +4,7 @@ var passport = require('passport'); var pkg = require('../../package.json'); var auth = require('../auth/auth'); -let block = require('./block'); +let chain = require('./chain'); module.exports = (app) => { app.get('/', (req, res) => { @@ -24,5 +24,5 @@ module.exports = (app) => { }); }); - block(app); + chain(app); } diff --git a/src/server.js b/src/server.js index 731299c..3720f1c 100644 --- a/src/server.js +++ b/src/server.js @@ -30,7 +30,7 @@ var Server = { app.use(compress()); app.use(bodyParser.urlencoded({ extended: false })); - app.use(bodyParser.json({type: 'application/*+json'})); + app.use(bodyParser.json()); app.use(passport.initialize()) diff --git a/test/10-crudCatenae/00-crud.js b/test/10-crudCatenae/00-crud.js new file mode 100644 index 0000000..53b0209 --- /dev/null +++ b/test/10-crudCatenae/00-crud.js @@ -0,0 +1,263 @@ +'use strict' + +var chai = require('chai'); +chai.should(); +var request = require('superagent'); +var Promise = require('bluebird'); + +var User = require('../../src/models/User') +var AccessToken = require('../../src/models/AccessToken') +var Chain = require('../../src/models/Chain') + +var user, token, invalidToken, chainId, userId; +// var chainId, userId; +// var token = 'QWERTYUIOPASDFGHJKLZXCVBNM'; + +describe('User shoud be authenticated', () => { + before( done => { + var testuser = new User({ + username: 'test' + }) + testuser.save().then( u => { + user = u; + var testtoken = new AccessToken({ + userId: user._id, + token: 'QWERTYUIOPASDFGHJKLZXCVBNM' + }); + + testtoken.save().then( t => { + token = t; + var invalidUserToken = new AccessToken({ + userId: t._id, //just a mongoId + token: 'AAAAAAAAAAAAAAAAAAAAAAA' + }); + invalidUserToken.save( function(err, t2){ + invalidToken = t2; + }); + return done(); + }) + }) + }); + + it('should create a chain', (done) => { + request + .post('localhost:3000/v1/chain') + .set('Authorization', 'Bearer ' + token.token) + .send({"chain" : {"name":"Brian"}}) + .end(function(err, res){ + res.statusCode.should.equal(201); + res.body.name.should.equal('Brian'); + res.body._id.should.be.not.empty; + chainId = res.body._id; + return done(); + }); + }); + + it('should reject a call create chain | post', (done) => { + request + .post('localhost:3000/v1/chain') + .set('Authorization', 'Bearer INVALIDTOKEN') + .end(function(err, res){ + res.statusCode.should.equal(401); + res.body.should.be.empty; + res.text.should.equal('Unauthorized'); + return done(); + }); + }); + + it('should reject a call with unvalid object | post', (done) => { + request + .post('localhost:3000/v1/chain') + .set('Authorization', 'Bearer ' + token.token) + .end(function(err, res){ + res.statusCode.should.equal(404); + res.body.should.be.empty; + res.text.should.equal('Missing property chain!'); + return done(); + }); + }); + + it('should reject a call with unvalid object | post', (done) => { + request + .post('localhost:3000/v1/chain') + .set('Authorization', 'Bearer ' + token.token) + .send({"chain" : {}}) + .end(function(err, res){ + res.statusCode.should.equal(404); + res.body.should.be.empty; + res.text.should.equal('Missing property name!'); + return done(); + }); + }); + + it('should accept a call with valid auth | get', (done) => { + request + .get('localhost:3000/v1/chain') + .set('Authorization', 'Bearer ' + token.token) + .send({"chain" : {"id" : chainId }}) + .end(function(err, res){ + res.statusCode.should.equal(201); + res.should.be.json; + res.body.should.be.a('object'); + res.body.should.have.property('chains'); + res.body.chains.should.be.instanceof(Array); + return done(); + }); + }); + + it('should reject a call with unvalid auth | get', (done) => { + request + .get('localhost:3000/v1/chain') + .set('Authorization', 'Bearer INVALIDTOKEN') + .end(function(err, res){ + res.statusCode.should.equal(401); + res.body.should.be.empty; + res.text.should.equal('Unauthorized'); + return done(); + }); + }); + + it('should accept a call with valid auth | getSingle', (done) => { + request + .get('localhost:3000/v1/chain/'+ chainId) + .set('Authorization', 'Bearer ' + token.token) + .send({"chain" : {'_id' : chainId}}) + .end(function(err, res){ + res.statusCode.should.equal(201); + res.should.be.json; + res.body.chain.should.have.property('_id'); + res.body.chain.should.have.property('userId'); + res.body.chain.should.have.property('name'); + res.body.chain.should.have.property('created'); + res.body.chain._id.should.equal(chainId); + res.body.chain._id.should.be.not.empty; + return done(); + }); + }); + + it('should reject a call with unvalid _id | getSingle', (done) => { + request + .get('localhost:3000/v1/chain/' + chainId) + .set('Authorization', 'Bearer ' + token.token) + .send({"chain" : {}}) + .end(function(err, res){ + res.statusCode.should.equal(404); + res.body.should.be.empty; + res.text.should.equal('Missing property _id'); + return done(); + }); + }); + + it('should reject a call with unvalid auth | getSingle', (done) => { + request + .get('localhost:3000/v1/chain') + .set('Authorization', 'Bearer INVALIDTOKEN') + .send({"chain" : {'_id' : 'null'}}) + .end(function(err, res){ + res.statusCode.should.equal(401); + res.body.should.be.empty; + res.text.should.equal('Unauthorized'); + return done(); + }); + }); + + it('should accept a call with valid auth | put', (done) => { + request + .put('localhost:3000/v1/chain/' +chainId) + .set('Authorization', 'Bearer ' + token.token) + .send({"chain" : {"_id": chainId, "name" : "Brian" }}) + .end(function(err, res){ + res.statusCode.should.equal(201); + res.body.should.be.empty; + res.text.should.equal('Chain updated'); + return done(); + }); + }); + + it('should reject a call with unvalid _id | put', (done) => { + request + .put('localhost:3000/v1/chain/'+chainId) + .set('Authorization', 'Bearer ' + token.token) + .send({"chain" : {}}) + .end(function(err, res){ + res.statusCode.should.equal(404); + res.body.should.be.empty; + res.text.should.equal('Missing property _id'); + return done(); + }); + }); + + it('should reject a call with unvalid auth | put', (done) => { + request + .put('localhost:3000/v1/chain/'+chainId) + .set('Authorization', 'Bearer INVALIDTOKEN') + .send({"chain" : {"_id": chainId}}) + .end(function(err, res){ + res.statusCode.should.equal(401); + res.body.should.be.empty; + res.text.should.equal('Unauthorized'); + return done(); + }); + }); + + it('should deleted a call with valid auth | delete', (done) => { + request + .delete('localhost:3000/v1/chain/'+chainId) + .set('Authorization', 'Bearer ' + token.token) + .send({"chain" : {'_id' : chainId}}) + .end(function(err, res){ + res.statusCode.should.equal(200); + res.text.should.equal('Chain remove'); + return done(); + }); + }); + + it('should reject a call with unvalid _id | delete', (done) => { + request + .delete('localhost:3000/v1/chain/' + chainId) + .set('Authorization', 'Bearer ' + token.token) + .send() + .end(function(err, res){ + res.statusCode.should.equal(404); + res.body.should.be.empty; + res.text.should.equal('Missing property chain!'); + return done(); + }); + }); + + it('should reject a call with unvalid _id | delete', (done) => { + request + .delete('localhost:3000/v1/chain/' + chainId) + .set('Authorization', 'Bearer ' + token.token) + .send({"chain" : {}}) + .end(function(err, res){ + res.statusCode.should.equal(404); + res.body.should.be.empty; + res.text.should.equal('Missing property _id'); + return done(); + }); + }); + + it('should reject a call with unvalid auth | delete', (done) => { + request + .delete('localhost:3000/v1/chain/'+chainId) + .set('Authorization', 'Bearer INVALIDTOKEN') + .send({"chain" : {'_id' : 'null'}}) + .end(function(err, res){ + res.statusCode.should.equal(401); + res.body.should.be.empty; + res.text.should.equal('Unauthorized'); + return done(); + }); + }); + + after( done => { + Promise.all([ + User.remove({_id: user._id}), + AccessToken.remove({userId: user._id}), + AccessToken.remove({token: invalidToken.token}) + ]).then( result => { + return done(); + }); + }); +});