v3: misc fixes

This commit is contained in:
Matteo Pagliazzi
2016-05-12 18:06:26 +02:00
parent c9b7aa8342
commit 93336f3894
20 changed files with 74 additions and 33 deletions

View File

@@ -43,9 +43,10 @@ describe('GET /challenges/:challengeId', () => {
expect(chal.leader).to.eql({
_id: groupLeader._id,
id: groupLeader._id,
profile: {name: groupLeader.profile.name},
});
expect(chal.group).to.eql(_.pick(group, ['_id', 'name', 'type', 'privacy']));
expect(chal.group).to.eql(_.pick(group, ['_id', 'id', 'name', 'type', 'privacy']));
});
});
@@ -87,9 +88,10 @@ describe('GET /challenges/:challengeId', () => {
expect(chal.leader).to.eql({
_id: groupLeader._id,
id: groupLeader._id,
profile: {name: groupLeader.profile.name},
});
expect(chal.group).to.eql(_.pick(group, ['_id', 'name', 'type', 'privacy']));
expect(chal.group).to.eql(_.pick(group, ['_id', 'id', 'name', 'type', 'privacy']));
});
});
@@ -131,9 +133,10 @@ describe('GET /challenges/:challengeId', () => {
expect(chal.leader).to.eql({
_id: groupLeader._id,
id: groupLeader.id,
profile: {name: groupLeader.profile.name},
});
expect(chal.group).to.eql(_.pick(group, ['_id', 'name', 'type', 'privacy']));
expect(chal.group).to.eql(_.pick(group, ['_id', 'id', 'name', 'type', 'privacy']));
});
});
});

View File

@@ -48,9 +48,10 @@ describe('GET /challenges/:challengeId/members', () => {
let res = await user.get(`/challenges/${challenge._id}/members`);
expect(res[0]).to.eql({
_id: leader._id,
id: leader._id,
profile: {name: leader.profile.name},
});
expect(res[0]).to.have.all.keys(['_id', 'profile']);
expect(res[0]).to.have.all.keys(['_id', 'id', 'profile']);
expect(res[0].profile).to.have.all.keys(['name']);
});
@@ -61,9 +62,10 @@ describe('GET /challenges/:challengeId/members', () => {
let res = await user.get(`/challenges/${challenge._id}/members`);
expect(res[0]).to.eql({
_id: anotherUser._id,
id: anotherUser._id,
profile: {name: anotherUser.profile.name},
});
expect(res[0]).to.have.all.keys(['_id', 'profile']);
expect(res[0]).to.have.all.keys(['_id', 'id', 'profile']);
expect(res[0].profile).to.have.all.keys(['name']);
});
@@ -80,7 +82,7 @@ describe('GET /challenges/:challengeId/members', () => {
let res = await user.get(`/challenges/${challenge._id}/members`);
expect(res.length).to.equal(30);
res.forEach(member => {
expect(member).to.have.all.keys(['_id', 'profile']);
expect(member).to.have.all.keys(['_id', 'id', 'profile']);
expect(member.profile).to.have.all.keys(['name']);
});
});

View File

@@ -78,7 +78,7 @@ describe('GET /challenges/:challengeId/members/:memberId', () => {
await groupLeader.post(`/tasks/challenge/${challenge._id}`, [{type: 'habit', text: taskText}]);
let memberProgress = await user.get(`/challenges/${challenge._id}/members/${groupLeader._id}`);
expect(memberProgress).to.have.all.keys(['_id', 'profile', 'tasks']);
expect(memberProgress).to.have.all.keys(['_id', 'id', 'profile', 'tasks']);
expect(memberProgress.profile).to.have.all.keys(['name']);
expect(memberProgress.tasks.length).to.equal(1);
});

View File

@@ -34,12 +34,14 @@ describe('GET challenges/group/:groupId', () => {
expect(foundChallenge1).to.exist;
expect(foundChallenge1.leader).to.eql({
_id: publicGuild.leader._id,
id: publicGuild.leader._id,
profile: {name: user.profile.name},
});
let foundChallenge2 = _.find(challenges, { _id: challenge2._id });
expect(foundChallenge2).to.exist;
expect(foundChallenge2.leader).to.eql({
_id: publicGuild.leader._id,
id: publicGuild.leader._id,
profile: {name: user.profile.name},
});
});
@@ -51,12 +53,14 @@ describe('GET challenges/group/:groupId', () => {
expect(foundChallenge1).to.exist;
expect(foundChallenge1.leader).to.eql({
_id: publicGuild.leader._id,
id: publicGuild.leader._id,
profile: {name: user.profile.name},
});
let foundChallenge2 = _.find(challenges, { _id: challenge2._id });
expect(foundChallenge2).to.exist;
expect(foundChallenge2.leader).to.eql({
_id: publicGuild.leader._id,
id: publicGuild.leader._id,
profile: {name: user.profile.name},
});
});
@@ -99,12 +103,14 @@ describe('GET challenges/group/:groupId', () => {
expect(foundChallenge1).to.exist;
expect(foundChallenge1.leader).to.eql({
_id: privateGuild.leader._id,
id: privateGuild.leader._id,
profile: {name: user.profile.name},
});
let foundChallenge2 = _.find(challenges, { _id: challenge2._id });
expect(foundChallenge2).to.exist;
expect(foundChallenge2.leader).to.eql({
_id: privateGuild.leader._id,
id: privateGuild.leader._id,
profile: {name: user.profile.name},
});
});

View File

@@ -35,6 +35,7 @@ describe('GET challenges/user', () => {
expect(foundChallenge).to.exist;
expect(foundChallenge.leader).to.eql({
_id: publicGuild.leader._id,
id: publicGuild.leader._id,
profile: {name: user.profile.name},
});
expect(foundChallenge.group).to.eql({
@@ -52,6 +53,7 @@ describe('GET challenges/user', () => {
expect(foundChallenge1).to.exist;
expect(foundChallenge1.leader).to.eql({
_id: publicGuild.leader._id,
id: publicGuild.leader._id,
profile: {name: user.profile.name},
});
expect(foundChallenge1.group).to.eql({
@@ -64,6 +66,7 @@ describe('GET challenges/user', () => {
expect(foundChallenge2).to.exist;
expect(foundChallenge2.leader).to.eql({
_id: publicGuild.leader._id,
id: publicGuild.leader._id,
profile: {name: user.profile.name},
});
expect(foundChallenge2.group).to.eql({
@@ -81,6 +84,7 @@ describe('GET challenges/user', () => {
expect(foundChallenge1).to.exist;
expect(foundChallenge1.leader).to.eql({
_id: publicGuild.leader._id,
id: publicGuild.leader._id,
profile: {name: user.profile.name},
});
expect(foundChallenge1.group).to.eql({
@@ -93,6 +97,7 @@ describe('GET challenges/user', () => {
expect(foundChallenge2).to.exist;
expect(foundChallenge2.leader).to.eql({
_id: publicGuild.leader._id,
id: publicGuild.leader._id,
profile: {name: user.profile.name},
});
expect(foundChallenge2.group).to.eql({

View File

@@ -66,6 +66,7 @@ describe('POST /challenges/:challengeId/join', () => {
});
expect(res.leader).to.eql({
_id: groupLeader._id,
id: groupLeader._id,
profile: {name: groupLeader.profile.name},
});
expect(res.name).to.equal(challenge.name);

View File

@@ -76,6 +76,7 @@ describe('PUT /challenges/:challengeId', () => {
expect(res.leader).to.eql({
_id: member._id,
id: member._id,
profile: {name: member.profile.name},
});
expect(res.name).to.equal('New Challenge Name');

View File

@@ -48,6 +48,7 @@ describe('GET /groups/:groupId/invites', () => {
expect(res.length).to.equal(1);
expect(res[0]).to.eql({
_id: invited._id,
id: invited._id,
profile: {name: invited.profile.name},
});
});
@@ -57,7 +58,7 @@ describe('GET /groups/:groupId/invites', () => {
let invited = await generateUser();
await user.post(`/groups/${group._id}/invite`, {uuids: [invited._id]});
let res = await user.get('/groups/party/invites');
expect(res[0]).to.have.all.keys(['_id', 'profile']);
expect(res[0]).to.have.all.keys(['_id', 'id', 'profile']);
expect(res[0].profile).to.have.all.keys(['name']);
});
@@ -73,7 +74,7 @@ describe('GET /groups/:groupId/invites', () => {
let res = await user.get('/groups/party/invites');
expect(res.length).to.equal(30);
res.forEach(member => {
expect(member).to.have.all.keys(['_id', 'profile']);
expect(member).to.have.all.keys(['_id', 'id', 'profile']);
expect(member.profile).to.have.all.keys(['name']);
});
});

View File

@@ -45,6 +45,7 @@ describe('GET /groups/:groupId/members', () => {
expect(res.length).to.equal(1);
expect(res[0]).to.eql({
_id: user._id,
id: user._id,
profile: {name: user.profile.name},
});
});
@@ -52,7 +53,7 @@ describe('GET /groups/:groupId/members', () => {
it('populates only some fields', async () => {
await generateGroup(user, {type: 'party', name: generateUUID()});
let res = await user.get('/groups/party/members');
expect(res[0]).to.have.all.keys(['_id', 'profile']);
expect(res[0]).to.have.all.keys(['_id', 'id', 'profile']);
expect(res[0].profile).to.have.all.keys(['name']);
});
@@ -68,7 +69,7 @@ describe('GET /groups/:groupId/members', () => {
let res = await user.get('/groups/party/members');
expect(res.length).to.equal(30);
res.forEach(member => {
expect(member).to.have.all.keys(['_id', 'profile']);
expect(member).to.have.all.keys(['_id', 'id', 'profile']);
expect(member.profile).to.have.all.keys(['name']);
});
});

View File

@@ -47,7 +47,7 @@ describe('GET /heroes/:heroId', () => {
let heroRes = await user.get(`/hall/heroes/${hero._id}`);
expect(heroRes).to.have.all.keys([ // works as: object has all and only these keys
'_id', 'balance', 'profile', 'purchased',
'_id', 'id', 'balance', 'profile', 'purchased',
'contributor', 'auth', 'items',
]);
expect(heroRes.auth.local).not.to.have.keys(['salt', 'hashed_password']);

View File

@@ -30,7 +30,7 @@ describe('GET /members/:memberId', () => {
});
let memberRes = await user.get(`/members/${member._id}`);
expect(memberRes).to.have.all.keys([ // works as: object has all and only these keys
'_id', 'preferences', 'profile', 'stats', 'achievements', 'party',
'_id', 'id', 'preferences', 'profile', 'stats', 'achievements', 'party',
'backer', 'contributor', 'auth', 'items',
]);
expect(Object.keys(memberRes.auth)).to.eql(['timestamps']);

View File

@@ -156,12 +156,14 @@ api.registerLocal = {
uuid: savedUser._id,
});
}
return null;
},
};
function _loginRes (user, req, res) {
if (user.auth.blocked) throw new NotAuthorized(res.t('accountSuspended', {userId: user._id}));
res.respond(200, {id: user._id, apiToken: user.apiToken});
return res.respond(200, {id: user._id, apiToken: user.apiToken});
}
/**
@@ -210,7 +212,7 @@ api.loginLocal = {
let user = await User.findOne(login, {auth: 1, apiToken: 1}).exec();
let isValidPassword = user && user.auth.local.hashed_password === passwordUtils.encrypt(req.body.password, user.auth.local.salt);
if (!isValidPassword) throw new NotAuthorized(res.t('invalidLoginCredentialsLong'));
_loginRes(user, ...arguments);
return _loginRes(user, ...arguments);
},
};
@@ -275,6 +277,8 @@ api.loginSocial = {
gaLabel: network,
uuid: savedUser._id,
});
return null;
}
},
};

View File

@@ -108,6 +108,8 @@ api.createChallengeTasks = {
// If adding tasks to a challenge -> sync users
if (challenge) challenge.addTasks(tasks);
return null;
},
};
@@ -185,7 +187,7 @@ api.getUserTasks = {
let validationErrors = req.validationErrors();
if (validationErrors) throw validationErrors;
await _getTasks(req, res, res.locals.user);
return await _getTasks(req, res, res.locals.user);
},
};
@@ -220,7 +222,7 @@ api.getChallengeTasks = {
let group = await Group.getGroup({user, groupId: challenge.group, fields: '_id type privacy', optionalMembership: true});
if (!group || !challenge.canView(user, group)) throw new NotFound(res.t('challengeNotFound'));
await _getTasks(req, res, res.locals.user, challenge);
return await _getTasks(req, res, res.locals.user, challenge);
},
};
@@ -312,6 +314,8 @@ api.updateTask = {
let savedTask = await task.save();
res.respond(200, savedTask);
if (challenge) challenge.updateTask(savedTask);
return null;
},
};
@@ -418,6 +422,8 @@ api.scoreTask = {
logger.error(e);
}
}
return null;
},
};
@@ -522,6 +528,8 @@ api.addChecklistItem = {
res.respond(200, savedTask);
if (challenge) challenge.updateTask(savedTask);
return null;
},
};
@@ -615,6 +623,8 @@ api.updateChecklistItem = {
res.respond(200, savedTask);
if (challenge) challenge.updateTask(savedTask);
return null;
},
};
@@ -664,6 +674,8 @@ api.removeChecklistItem = {
let savedTask = await task.save();
res.respond(200, savedTask);
if (challenge) challenge.updateTask(savedTask);
return null;
},
};
@@ -877,6 +889,8 @@ api.deleteTask = {
res.respond(200, {});
if (challenge) challenge.removeTask(task);
return null;
},
};

View File

@@ -80,6 +80,7 @@ let updatablePaths = [
'flags.welcomed',
'flags.cardReceived',
'flags.warnedLowHealth',
'flags.newStuff',
'achievements',

View File

@@ -20,7 +20,7 @@ api.getFrontPage = {
return res.redirect('/static/front');
}
res.render('index.jade', {
return res.render('index.jade', {
title: 'Habitica | Your Life The Role Playing Game',
env: res.locals.habitrpg,
});
@@ -40,7 +40,7 @@ _.each(staticPages, (name) => {
middlewares: [locals],
runCron: false,
async handler (req, res) {
res.render(`static/${name}.jade`, {
return res.render(`static/${name}.jade`, {
env: res.locals.habitrpg,
md,
userCount: TOTAL_USER_COUNT,
@@ -58,7 +58,7 @@ _.each(shareables, (name) => {
middlewares: [locals],
runCron: false,
async handler (req, res) {
res.render(`social/${name}`, {
return res.render(`social/${name}`, {
env: res.locals.habitrpg,
md,
userCount: TOTAL_USER_COUNT,
@@ -72,7 +72,7 @@ api.redirectExtensionsPage = {
url: '/static/extensions',
runCron: false,
async handler (req, res) {
res.redirect('http://habitica.wikia.com/wiki/App_and_Extension_Integrations');
return res.redirect('http://habitica.wikia.com/wiki/App_and_Extension_Integrations');
},
};

View File

@@ -12,7 +12,7 @@ const logger = new winston.Logger();
if (IS_PROD) {
// TODO production logging, use loggly and new relic too
if (ENABLE_CONSOLE_LOGS_IN_PROD) {
if (ENABLE_CONSOLE_LOGS_IN_PROD === 'true') {
logger.add(winston.transports.Console, {
colorize: true,
prettyPrint: true,

View File

@@ -19,7 +19,7 @@ export function authWithHeaders (optional = false) {
return next(new NotAuthorized(res.t('missingAuthHeaders')));
}
User.findOne({
return User.findOne({
_id: userId,
apiToken,
})
@@ -31,7 +31,7 @@ export function authWithHeaders (optional = false) {
res.locals.user = user;
// TODO use either session/cookie or headers, not both
req.session.userId = user._id;
next();
return next();
})
.catch(next);
};
@@ -43,7 +43,7 @@ export function authWithSession (req, res, next) {
if (!userId) return next(new NotAuthorized(res.t('invalidCredentials')));
User.findOne({
return User.findOne({
_id: userId,
})
.exec()
@@ -51,7 +51,7 @@ export function authWithSession (req, res, next) {
if (!user) throw new NotAuthorized(res.t('invalidCredentials'));
res.locals.user = user;
next();
return next();
})
.catch(next);
}
@@ -64,12 +64,12 @@ export function authWithUrl (req, res, next) {
throw new NotAuthorized(res.t('missingAuthParams'));
}
User.findOne({ _id: userId, apiToken }).exec()
return User.findOne({ _id: userId, apiToken }).exec()
.then((user) => {
if (!user) throw new NotAuthorized(res.t('invalidCredentials'));
res.locals.user = user;
next();
return next();
})
.catch(next);
}

View File

@@ -139,7 +139,7 @@ module.exports = function cronMiddleware (req, res, next) {
toSave.push(task.save());
});
Bluebird.all(toSave)
return Bluebird.all(toSave)
.then(saved => {
user = res.locals.user = saved[0];
if (!quest) return;
@@ -150,6 +150,8 @@ module.exports = function cronMiddleware (req, res, next) {
.then(() => User.findById(user._id).exec()) // fetch the updated user...
.then(updatedUser => {
res.locals.user = updatedUser;
return null;
});
})
.then(() => next())

View File

@@ -49,8 +49,8 @@ module.exports = function attachMiddlewares (app, server) {
app.use(favicon(`${PUBLIC_DIR}/favicon.ico`));
app.use(cors);
//app.use(forceSSL);
//app.use(forceHabitica);
app.use(forceSSL);
app.use(forceHabitica);
app.use(bodyParser.urlencoded({
extended: true, // Uses 'qs' library as old connect middleware
@@ -70,9 +70,9 @@ module.exports = function attachMiddlewares (app, server) {
app.use(passport.initialize());
app.use(passport.session());
app.use(v3); // the main app, also setup top-level routes
app.use('/api/v2', v2);
app.use('/api/v1', v1);
app.use(v3); // the main app, also setup top-level routes
staticMiddleware(app);
app.use(notFoundHandler);

View File

@@ -74,7 +74,7 @@ export function getUserLanguage (req, res, next) {
req.language = _getFromUser(req.locals.user, req);
return next();
} else if (req.session && req.session.userId) { // Same thing if the user has a valid session
User.findOne({
return User.findOne({
_id: req.session.userId,
}, 'preferences.language')
.lean()