added tests for api routes
This commit is contained in:
parent
99a6cb5009
commit
e1db98ec95
|
@ -201,16 +201,12 @@ exports.signout = function(req, res) {
|
|||
|
||||
/* Generate API Key for User */
|
||||
exports.generateAPIKey = function(req, res) {
|
||||
if (!req.isAuthenticated()){
|
||||
return res.status(401).send({
|
||||
message: 'User is not Authorized'
|
||||
});
|
||||
}
|
||||
|
||||
User.findById(req.user.id)
|
||||
.exec( function(err, user) {
|
||||
if (err) {
|
||||
return res.status(400).send(err);
|
||||
return res.status(400).send({
|
||||
message: errorHandler.getErrorMessage(err)
|
||||
});
|
||||
}
|
||||
|
||||
if (!user) {
|
||||
|
@ -230,8 +226,7 @@ exports.generateAPIKey = function(req, res) {
|
|||
|
||||
var newUser = _user.toObject();
|
||||
|
||||
newUser = helpers.removeSensitiveModelData('private_user', newUser);
|
||||
return res.json(newUser);
|
||||
return res.json({ id: newUser._id, apiKey: newUser.apiKey });
|
||||
});
|
||||
|
||||
});
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
var _ = require('lodash'),
|
||||
mongoose = require('mongoose'),
|
||||
User = mongoose.model('User'),
|
||||
auth = require('../../config/passport_helpers');
|
||||
auth = require('../../../config/passport_helpers');
|
||||
|
||||
/**
|
||||
* User middleware
|
||||
|
|
|
@ -294,26 +294,6 @@ FormSchema.plugin(timeStampPlugin, {
|
|||
useVirtual: false
|
||||
});
|
||||
|
||||
FormSchema.pre('save', function (next) {
|
||||
switch(this.language){
|
||||
case 'spanish':
|
||||
this.language = 'es';
|
||||
break;
|
||||
case 'french':
|
||||
this.language = 'fr';
|
||||
break;
|
||||
case 'italian':
|
||||
this.language = 'it';
|
||||
break;
|
||||
case 'german':
|
||||
this.language = 'de';
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
next();
|
||||
});
|
||||
|
||||
function getDeletedIndexes(needle, haystack){
|
||||
var deletedIndexes = [];
|
||||
|
||||
|
|
|
@ -1,26 +0,0 @@
|
|||
'use strict';
|
||||
|
||||
const constants = require('../../libs/constants'),
|
||||
config = require('../../../config/config');
|
||||
|
||||
module.exports = exports = function lastModifiedPlugin (schema, options) {
|
||||
schema.add({
|
||||
language: {
|
||||
type: String,
|
||||
enum: constants.languageTypes,
|
||||
default: config.defaultLanguage,
|
||||
required: options.required || 'Must be a valid language'
|
||||
}
|
||||
});
|
||||
|
||||
schema.pre('save', function (next) {
|
||||
var currWord = this.language;
|
||||
|
||||
//English is the default backup language
|
||||
this.language = 'en';
|
||||
if(constants.wordToLangCode.has(currWord)){
|
||||
this.language = constants.wordToLangCode[currWord];
|
||||
}
|
||||
next();
|
||||
});
|
||||
};
|
|
@ -353,6 +353,100 @@ describe('User CRUD tests', function() {
|
|||
})
|
||||
});
|
||||
|
||||
describe(' > User API > ', function(){
|
||||
var apiKey;
|
||||
|
||||
this.timeout(10000);
|
||||
before(function(done){
|
||||
var UserObj = new User(_User);
|
||||
UserObj.save(function(err, user){
|
||||
done(err);
|
||||
})
|
||||
})
|
||||
|
||||
it('should be able to request API Key', function(done) {
|
||||
async.waterfall([
|
||||
function(callback) {
|
||||
userSession.post('/auth/signin')
|
||||
.send({
|
||||
username: _User.username,
|
||||
password: _User.password
|
||||
})
|
||||
.expect(200)
|
||||
.end(function(err, res) {
|
||||
callback(err);
|
||||
});
|
||||
},
|
||||
function(callback) {
|
||||
userSession.get('/auth/genkey')
|
||||
.expect(200)
|
||||
.end(function(err, res) {
|
||||
apiKey = res.body.apiKey;
|
||||
callback(err);
|
||||
});
|
||||
},
|
||||
function(callback) {
|
||||
userSession.get('/auth/signout')
|
||||
.expect(200)
|
||||
.end(function(err, res) {
|
||||
callback(err);
|
||||
});
|
||||
},
|
||||
function(callback) {
|
||||
userSession.get('/users/me?apikey=' + apiKey)
|
||||
.expect(200)
|
||||
.end(function(err, res) {
|
||||
console.log(res.body);
|
||||
var user = res.body;
|
||||
user.firstName.should.equal(_User.firstName);
|
||||
user.lastName.should.equal(_User.lastName);
|
||||
user.email.should.equal(_User.email);
|
||||
user.username.should.equal(_User.username);
|
||||
callback(err);
|
||||
});
|
||||
},
|
||||
], function (err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should be able to update user with API key', function(done) {
|
||||
var newUser = {};
|
||||
newUser.firstName = 'goodnight';
|
||||
newUser.lastName = 'everyone';
|
||||
|
||||
newUser.email = 'grcg@gcrc.com';
|
||||
newUser.username = 'grcg';
|
||||
|
||||
async.waterfall([
|
||||
function(callback) {
|
||||
userSession.put('/users?apikey=' + apiKey)
|
||||
.send(newUser)
|
||||
.expect(200)
|
||||
.end(function(err, res) {
|
||||
callback(err);
|
||||
});
|
||||
},
|
||||
function(callback) {
|
||||
User.findOne({ username: newUser.username })
|
||||
.exec(function(err, user){
|
||||
user.firstName.should.equal(newUser.firstName);
|
||||
user.lastName.should.equal(newUser.lastName);
|
||||
user.email.should.equal(newUser.email);
|
||||
user.username.should.equal(newUser.username);
|
||||
callback(err);
|
||||
});
|
||||
}
|
||||
], function (err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
afterEach(function(done){
|
||||
User.remove().exec(done);
|
||||
})
|
||||
});
|
||||
|
||||
after(function(done) {
|
||||
User.remove().exec(function () {
|
||||
tmpUser.remove().exec(function(){
|
||||
|
|
|
@ -6,6 +6,7 @@ module.exports.isAuthenticatedOrApiKey = function isAuthenticated(req, res, next
|
|||
if (req.isAuthenticated()) {
|
||||
return next();
|
||||
}
|
||||
|
||||
// Try authenticate with API KEY
|
||||
if (req.headers.apikey || req.query.apikey || req.body.apikey) {
|
||||
passport.authenticate('localapikey', function (err, user, info) {
|
||||
|
|
Loading…
Reference in a new issue