v2 port: enable integration tests, port register route

This commit is contained in:
Matteo Pagliazzi
2016-04-03 16:40:01 +02:00
parent fc73fc7f8c
commit 58c20c2a64
36 changed files with 114 additions and 50 deletions
+10 -14
View File
@@ -138,15 +138,13 @@ api.registerUser = function(req, res, next) {
}]
}, function(err, data) {
if (err) return err.code ? res.status(err.code).json(err) : next(err);
res.status(200).json(data.register[0]);
data.register[0].getTransformedData(function(err, userTransformed){
if(err) return next(err);
res.status(200).json(userTransformed);
});
});
};
/*
Register new user with uname / password
*/
api.loginLocal = function(req, res, next) {
var username = req.body.username;
var password = req.body.password;
@@ -348,7 +346,8 @@ api.changePassword = function(req, res, next) {
})
};
var firebaseTokenGeneratorInstance = new FirebaseTokenGenerator(nconf.get('FIREBASE:SECRET'));
// DISABLED FOR API v2
/*var firebaseTokenGeneratorInstance = new FirebaseTokenGenerator(nconf.get('FIREBASE:SECRET'));
api.getFirebaseToken = function(req, res, next) {
var user = res.locals.user;
// Expires 24 hours after now (60*60*24*1000) (in milliseconds)
@@ -367,13 +366,10 @@ api.getFirebaseToken = function(req, res, next) {
token: token,
expires: expires
});
};
};*/
/*
Registers a new user. Only accepting username/password registrations, no Facebook
*/
api.setupPassport = function(router) {
// DISABLED FOR API v2
/*api.setupPassport = function(router) {
router.get('/logout', i18n.getUserLanguage, function(req, res) {
req.logout();
@@ -381,4 +377,4 @@ api.setupPassport = function(router) {
res.redirect('/');
})
};
};*/
+1 -1
View File
@@ -502,7 +502,7 @@ api.logout = {
url: '/user/auth/logout', // TODO this is under /api/v3 route, should be accessible through habitica.com/logout
middlewares: [authWithSession, cron],
async handler (req, res) {
req.logout();
req.logout(); // passportjs method
req.session = null;
res.redirect('/');
},
+57
View File
@@ -725,6 +725,63 @@ schema.methods.sendMessage = async function sendMessage (userToReceiveMessage, m
await Q.all(promises);
};
// Methods to adapt the new schema to API v2 responses (mostly tasks inside the user model)
// These will be removed once API v2 is discontinued
// Get all the tasks belonging to an user,
schema.methods.getTasks = function getUserTasks (cb) {
Tasks.Task.find({
userId: this._id,
}, cb);
};
// Given user and an array of tasks, return an API compatible user + tasks obj
schema.methods.addTasksToUser = function addTasksToUser (tasks) {
let obj = this.toJSON();
let tasksOrder = obj.tasksOrder; // Saving a reference because we won't return it
obj.habits = [];
obj.dailys = [];
obj.todos = [];
obj.rewards = [];
obj.tasksOrder = undefined;
let unordered = [];
tasks.forEach((task) => {
// We want to push the task at the same position where it's stored in tasksOrder
let pos = tasksOrder[`${task.type}s`].indexOf(task._id);
if (pos === -1) { // Should never happen, it means the lists got out of sync
unordered.push(task.toJSON());
} else {
obj[`${task.type}s`][pos] = task.toJSON();
}
});
// Reconcile unordered items
unordered.forEach((task) => {
obj[`${task.type}s`].push(task);
});
// Remove null values that can be created when inserting tasks at an index > length
['habits', 'dailys', 'rewards', 'todos'].forEach((type) => {
obj[type] = _.compact(obj[type]);
});
return obj;
};
// Return the data maintaining backward compatibility
schema.methods.getTransformedData = function getTransformedData (cb) {
let self = this;
this.getTasks((err, tasks) => {
if (err) return cb(err);
cb(null, self.addTasksToUser(tasks));
});
};
// END of API v2 methods
export let model = mongoose.model('User', schema);
// Initially export an empty object so external requires will get
+2 -2
View File
@@ -4,7 +4,7 @@ var i18n = require('../../libs/api-v2/i18n');
var router = express.Router();
/* auth.auth*/
auth.setupPassport(router); //FIXME make this consistent with the others
// auth.setupPassport(router); //FIXME make this consistent with the others
router.post('/register', i18n.getUserLanguage, auth.registerUser);
router.post('/user/auth/local', i18n.getUserLanguage, auth.loginLocal);
router.post('/user/auth/social', i18n.getUserLanguage, auth.loginSocial);
@@ -13,6 +13,6 @@ router.post('/user/reset-password', i18n.getUserLanguage, auth.resetPassword);
router.post('/user/change-password', i18n.getUserLanguage, auth.auth, auth.changePassword);
router.post('/user/change-username', i18n.getUserLanguage, auth.auth, auth.changeUsername);
router.post('/user/change-email', i18n.getUserLanguage, auth.auth, auth.changeEmail);
router.post('/user/auth/firebase', i18n.getUserLanguage, auth.auth, auth.getFirebaseToken);
// router.post('/user/auth/firebase', i18n.getUserLanguage, auth.auth, auth.getFirebaseToken);
module.exports = router;