res.respond: allow for thrid parameter (message), refactor shared ops responses and adapt tests

This commit is contained in:
Matteo Pagliazzi
2016-04-21 00:52:03 +02:00
parent 9e3d8ba4ac
commit 6568fcfd5e
81 changed files with 419 additions and 465 deletions
@@ -36,6 +36,6 @@ describe('POST /user/allocate', () => {
await user.sync();
expect(user.stats.con).to.equal(1);
expect(user.stats.points).to.equal(0);
expect(res.stats.con).to.equal(1);
expect(res.con).to.equal(1);
});
});
@@ -18,11 +18,7 @@ describe('POST /user/allocate-now', () => {
let res = await user.post('/user/allocate-now');
await user.sync();
expect(res).to.eql({
data: {
stats: user.stats,
},
});
expect(res).to.eql(user.stats);
expect(user.stats.points).to.equal(0);
expect(user.stats.con).to.equal(9);
expect(user.stats.int).to.equal(8);
@@ -36,9 +36,7 @@ describe('POST /user/buy/:key', () => {
await user.sync();
expect(user.stats.hp).to.equal(50);
expect(res.data).to.eql({
stats: user.stats,
});
expect(res.data).to.eql(user.stats);
expect(res.message).to.equal(t('messageBought', {itemText: potion.text()}));
});
@@ -31,11 +31,7 @@ describe('POST /user/buy-mystery-set/:key', () => {
expect(res.data).to.eql({
items: JSON.parse(JSON.stringify(user.items)), // otherwise dates can't be compared
purchased: {
plan: {
consecutive: user.purchased.plan.consecutive,
},
},
purchasedPlanConsecutive: user.purchased.plan.consecutive,
});
expect(res.message).to.equal(t('hourglassPurchaseSet'));
});
@@ -36,9 +36,7 @@ describe('POST /user/buy-potion', () => {
await user.sync();
expect(user.stats.hp).to.equal(50);
expect(res.data).to.eql({
stats: user.stats,
});
expect(res.data).to.eql(user.stats);
expect(res.message).to.equal(t('messageBought', {itemText: potion.text()}));
});
});
@@ -18,13 +18,13 @@ describe('POST /user/change-class', () => {
let res = await user.post('/user/change-class?class=rogue');
await user.sync();
expect(res).to.eql({
data: JSON.parse(JSON.stringify({
expect(res).to.eql(JSON.parse(
JSON.stringify({
preferences: user.preferences,
stats: user.stats,
flags: user.flags,
items: user.items,
})),
});
})
));
});
});
@@ -15,12 +15,12 @@ describe('POST /user/disable-classes', () => {
let res = await user.post('/user/disable-classes');
await user.sync();
expect(res).to.eql({
data: JSON.parse(JSON.stringify({
expect(res).to.eql(JSON.parse(
JSON.stringify({
preferences: user.preferences,
stats: user.stats,
flags: user.flags,
})),
});
})
));
});
});
@@ -35,8 +35,6 @@ describe('POST /user/equip/:type/:key', () => {
let res = await user.post('/user/equip/equipped/weapon_warrior_2');
await user.sync();
expect(res).to.eql({
data: JSON.parse(JSON.stringify(user.items)),
});
expect(res).to.eql(JSON.parse(JSON.stringify(user.items)));
});
});
@@ -13,16 +13,12 @@ describe('POST /user/sleep', () => {
it('toggles sleep status', async () => {
let res = await user.post('/user/sleep');
expect(res).to.eql({
preferences: {sleep: true},
});
expect(res).to.eql(true);
await user.sync();
expect(user.preferences.sleep).to.be.true;
let res2 = await user.post('/user/sleep');
expect(res2).to.eql({
preferences: {sleep: false},
});
expect(res2).to.eql(false);
await user.sync();
expect(user.preferences.sleep).to.be.false;
});
@@ -16,7 +16,7 @@ describe('POST /user/reset-password', async () => {
let response = await user.post(endpoint, {
email: user.auth.local.email,
});
expect(response).to.eql({ message: t('passwordReset') });
expect(response).to.eql({ data: {}, message: t('passwordReset') });
await user.sync();
expect(user.auth.local.hashed_password).to.not.eql(previousPassword);
});
@@ -25,7 +25,7 @@ describe('POST /user/reset-password', async () => {
let response = await user.post(endpoint, {
email: 'nonExistent@email.com',
});
expect(response).to.eql({ message: t('passwordReset') });
expect(response).to.eql({ data: {}, message: t('passwordReset') });
});
it('errors if email is not provided', async () => {
@@ -36,4 +36,3 @@ describe('POST /user/reset-password', async () => {
});
});
});
+15
View File
@@ -35,6 +35,21 @@ describe('response middleware', () => {
});
});
it('can be passed a third parameter to be used as optional message', () => {
responseMiddleware(req, res, next);
res.respond(200, {field: 1}, 'hello');
expect(res.status).to.be.calledOnce;
expect(res.json).to.be.calledOnce;
expect(res.status).to.be.calledWith(200);
expect(res.json).to.be.calledWith({
success: true,
data: {field: 1},
message: 'hello',
});
});
it('treats status >= 400 as failures', () => {
responseMiddleware(req, res, next);
res.respond(403, {field: 1});
+1 -1
View File
@@ -110,7 +110,7 @@ describe('Challenge Model', () => {
};
Tasks.Task.sanitize(req.body);
_.assign(task, common.ops.updateTask(task.toObject(), req));
_.assign(task, common.ops.updateTask(task.toObject(), req)[0]);
await challenge.updateTask(task);