From 9d473cc92eb5457b8fb95d858518b963182e09de Mon Sep 17 00:00:00 2001 From: Matteo Pagliazzi Date: Thu, 25 Apr 2019 22:41:43 +0200 Subject: [PATCH] wip: fix setting (some) items values in the hall of heroes (#11133) --- test/api/unit/libs/items/utils.test.js | 46 +++++++++++++++++++++++ website/server/controllers/api-v3/hall.js | 7 +++- website/server/libs/items/utils.js | 20 ++++++++++ 3 files changed, 71 insertions(+), 2 deletions(-) diff --git a/test/api/unit/libs/items/utils.test.js b/test/api/unit/libs/items/utils.test.js index 029d26a07c..274dc665fe 100644 --- a/test/api/unit/libs/items/utils.test.js +++ b/test/api/unit/libs/items/utils.test.js @@ -2,6 +2,7 @@ import { validateItemPath, getDefaultOwnedGear, + castItemVal, } from '../../../../../website/server/libs/items/utils'; describe('Items Utils', () => { @@ -64,4 +65,49 @@ describe('Items Utils', () => { expect(validateItemPath('items.quests.invalid')).to.equal(false); }); }); + + describe('castItemVal', () => { + it('returns the item val untouched if not an item path', () => { + expect(castItemVal('notitems.gear.owned.item', 'a string')).to.equal('a string'); + }); + + it('returns the item val untouched if an unsupported path', () => { + expect(validateItemPath('items.gear.equipped.weapon', 'a string')).to.equal('a string'); + expect(validateItemPath('items.currentPet', 'a string')).to.equal('a string'); + expect(validateItemPath('items.special.snowball', 'a string')).to.equal('a string'); + }); + + it('converts values for pets paths to numbers', () => { + expect(validateItemPath('items.pets.Wolf-CottonCandyPink', '5')).to.equal(5); + expect(validateItemPath('items.pets.Wolf-Invalid', '5')).to.equal(5); + }); + + it('converts values for eggs paths to numbers', () => { + expect(validateItemPath('items.eggs.LionCub', '5')).to.equal(5); + expect(validateItemPath('items.eggs.Armadillo', '5')).to.equal(5); + expect(validateItemPath('items.eggs.NotAnArmadillo', '5')).to.equal(5); + }); + + it('converts values for hatching potions paths to numbers', () => { + expect(validateItemPath('items.hatchingPotions.Base', '5')).to.equal(5); + expect(validateItemPath('items.hatchingPotions.StarryNight', '5')).to.equal(5); + expect(validateItemPath('items.hatchingPotions.Invalid', '5')).to.equal(5); + }); + + it('converts values for food paths to numbers', () => { + expect(validateItemPath('items.food.Cake_Base', '5')).to.equal(5); + expect(validateItemPath('items.food.Cake_Invalid', '5')).to.equal(5); + }); + + it('converts values for mounts paths to numbers', () => { + expect(validateItemPath('items.mounts.Cactus-Base', '5')).to.equal(5); + expect(validateItemPath('items.mounts.Aether-Invisible', '5')).to.equal(5); + expect(validateItemPath('items.mounts.Aether-Invalid', '5')).to.equal(5); + }); + + it('converts values for quests paths to numbers', () => { + expect(validateItemPath('items.quests.atom3', '5')).to.equal(5); + expect(validateItemPath('items.quests.invalid', '5')).to.equal(5); + }); + }); }); diff --git a/website/server/controllers/api-v3/hall.js b/website/server/controllers/api-v3/hall.js index 5309b56e4f..a8ac1456f0 100644 --- a/website/server/controllers/api-v3/hall.js +++ b/website/server/controllers/api-v3/hall.js @@ -7,7 +7,10 @@ import { import _ from 'lodash'; import apiError from '../../libs/apiError'; import validator from 'validator'; -import { validateItemPath } from '../../libs/items/utils'; +import { + validateItemPath, + castItemVal, +} from '../../libs/items/utils'; let api = {}; @@ -271,7 +274,7 @@ api.updateHero = { hero.markModified('items.pets'); } if (updateData.itemPath && updateData.itemVal && validateItemPath(updateData.itemPath)) { - _.set(hero, updateData.itemPath, updateData.itemVal); // Sanitization at 5c30944 (deemed unnecessary) + _.set(hero, updateData.itemPath, castItemVal(updateData.itemPath, updateData.itemVal)); // Sanitization at 5c30944 (deemed unnecessary) } if (updateData.auth && updateData.auth.blocked === true) { diff --git a/website/server/libs/items/utils.js b/website/server/libs/items/utils.js index 0c0d9adf2f..308bfcc8a7 100644 --- a/website/server/libs/items/utils.js +++ b/website/server/libs/items/utils.js @@ -54,4 +54,24 @@ export function validateItemPath (itemPath) { if (itemPath.indexOf('items.quests') === 0) { return Boolean(shared.content.quests[key]); } +} + +// When passed a value of an item in the user object it'll convert the +// value to the correct format. +// Example a numeric string like "5" applied to a food item (expecting an interger) +// will be converted to the number 5 +// TODO cast the correct value for `items.gear.owned` +export function castItemVal (itemPath, itemVal) { + if ( + itemPath.indexOf('items.pets') === 0 || + itemPath.indexOf('items.eggs') === 0 || + itemPath.indexOf('items.hatchingPotions') === 0 || + itemPath.indexOf('items.food') === 0 || + itemPath.indexOf('items.mounts') === 0 || + itemPath.indexOf('items.quests') === 0 + ) { + return Number(itemVal); + } + + return itemVal; } \ No newline at end of file