Ported reroll. Added unit tests. Added reroll route. Added integration tests

This commit is contained in:
Keith Holliday
2016-04-08 15:56:11 -05:00
parent 116839ca81
commit 1685b7285f
10 changed files with 184 additions and 28 deletions

View File

@@ -162,5 +162,6 @@
"alreadyUnlocked": "Item already unlocked", "alreadyUnlocked": "Item already unlocked",
"cannotRevive": "Cannot revive if not dead", "cannotRevive": "Cannot revive if not dead",
"rebirthComplete": "You have been reborn!", "rebirthComplete": "You have been reborn!",
"petNotOwned": "You do not own this pet." "petNotOwned": "You do not own this pet.",
"rerollComplete": "Reroll complete!"
} }

View File

@@ -128,6 +128,7 @@ import sell from './ops/sell';
import unlock from './ops/unlock'; import unlock from './ops/unlock';
import revive from './ops/revive'; import revive from './ops/revive';
import rebirth from './ops/rebirth'; import rebirth from './ops/rebirth';
import reroll from './ops/reroll';
api.ops = { api.ops = {
scoreTask, scoreTask,
@@ -157,6 +158,7 @@ api.ops = {
unlock, unlock,
revive, revive,
rebirth, rebirth,
reroll,
}; };
import handleTwoHanded from './fns/handleTwoHanded'; import handleTwoHanded from './fns/handleTwoHanded';

View File

@@ -4,6 +4,7 @@ import {
NotAuthorized, NotAuthorized,
} from '../libs/errors'; } from '../libs/errors';
import splitWhitespace from '../libs/splitWhitespace'; import splitWhitespace from '../libs/splitWhitespace';
import _ from 'lodash';
module.exports = function releaseBoth (user, req = {}, analytics) { module.exports = function releaseBoth (user, req = {}, analytics) {
let animal; let animal;
@@ -20,15 +21,15 @@ module.exports = function releaseBoth (user, req = {}, analytics) {
uuid: user._id, uuid: user._id,
acquireMethod: 'Gems', acquireMethod: 'Gems',
gemCost: 6, gemCost: 6,
category: 'behavior' category: 'behavior',
}); });
} }
user.balance -= 1.5; user.balance -= 1.5;
} }
user.items.currentMount = ""; user.items.currentMount = '';
user.items.currentPet = ""; user.items.currentPet = '';
for (animal in content.pets) { for (animal in content.pets) {
if (user.items.pets[animal] === -1) { if (user.items.pets[animal] === -1) {

View File

@@ -4,6 +4,7 @@ import {
NotAuthorized, NotAuthorized,
} from '../libs/errors'; } from '../libs/errors';
import splitWhitespace from '../libs/splitWhitespace'; import splitWhitespace from '../libs/splitWhitespace';
import _ from 'lodash';
module.exports = function releaseMounts (user, req = {}, analytics) { module.exports = function releaseMounts (user, req = {}, analytics) {
let mount; let mount;
@@ -29,7 +30,7 @@ module.exports = function releaseMounts (user, req = {}, analytics) {
uuid: user._id, uuid: user._id,
acquireMethod: 'Gems', acquireMethod: 'Gems',
gemCost: 4, gemCost: 4,
category: 'behavior' category: 'behavior',
}); });
} }

View File

@@ -4,6 +4,7 @@ import {
NotAuthorized, NotAuthorized,
} from '../libs/errors'; } from '../libs/errors';
import splitWhitespace from '../libs/splitWhitespace'; import splitWhitespace from '../libs/splitWhitespace';
import _ from 'lodash';
module.exports = function releasePets (user, req = {}, analytics) { module.exports = function releasePets (user, req = {}, analytics) {
if (user.balance < 1) { if (user.balance < 1) {
@@ -27,7 +28,7 @@ module.exports = function releasePets (user, req = {}, analytics) {
uuid: user._id, uuid: user._id,
acquireMethod: 'Gems', acquireMethod: 'Gems',
gemCost: 4, gemCost: 4,
category: 'behavior' category: 'behavior',
}); });
} }

View File

@@ -1,29 +1,36 @@
import i18n from '../i18n'; import i18n from '../i18n';
import _ from 'lodash'; import _ from 'lodash';
import {
NotAuthorized,
} from '../libs/errors';
module.exports = function(user, req, cb, analytics) { module.exports = function reroll (user, tasks = [], req = {}, analytics) {
var analyticsData;
if (user.balance < 1) { if (user.balance < 1) {
return typeof cb === "function" ? cb({ throw new NotAuthorized(i18n.t('notEnoughGems', req.language));
code: 401,
message: i18n.t('notEnoughGems', req.language)
}) : void 0;
} }
user.balance--; user.balance--;
_.each(user.tasks, function(task) { user.stats.hp = 50;
_.each(tasks, function resetTaskValues (task) {
if (task.type !== 'reward') { if (task.type !== 'reward') {
return task.value = 0; task.value = 0;
} }
}); });
user.stats.hp = 50;
analyticsData = { if (analytics) {
analytics.track('Fortify Potion', {
uuid: user._id, uuid: user._id,
acquireMethod: 'Gems', acquireMethod: 'Gems',
gemCost: 4, gemCost: 4,
category: 'behavior' category: 'behavior',
}; });
if (analytics != null) {
analytics.track('Fortify Potion', analyticsData);
} }
return typeof cb === "function" ? cb(null, user) : void 0;
let response = {
data: {user, tasks},
message: i18n.t('rerollComplete'),
};
return response;
}; };

View File

@@ -21,10 +21,6 @@ const COMMON_FILES = [
'!./common/script/ops/deleteTask.js', '!./common/script/ops/deleteTask.js',
'!./common/script/ops/getTag.js', '!./common/script/ops/getTag.js',
'!./common/script/ops/getTags.js', '!./common/script/ops/getTags.js',
'!./common/script/ops/releaseBoth.js',
'!./common/script/ops/releaseMounts.js',
'!./common/script/ops/releasePets.js',
'!./common/script/ops/reroll.js',
'!./common/script/ops/reset.js', '!./common/script/ops/reset.js',
'!./common/script/ops/sortTag.js', '!./common/script/ops/sortTag.js',
'!./common/script/ops/sortTask.js', '!./common/script/ops/sortTask.js',

View File

@@ -0,0 +1,54 @@
import {
generateUser,
generateDaily,
generateReward,
translate as t,
} from '../../../../helpers/api-integration/v3';
describe('POST /user/reroll', () => {
let user;
beforeEach(async () => {
user = await generateUser();
});
it('returns an error when user balance is too low', async () => {
await expect(user.post('/user/reroll'))
.to.eventually.be.rejected.and.to.eql({
code: 401,
error: 'NotAuthorized',
message: t('notEnoughGems'),
});
});
// More tests in common code unit tests
it('resets user\'s tasks', async () => {
await user.update({
balance: 2,
});
let daily = await generateDaily({
text: 'test habit',
type: 'daily',
userId: user._id,
});
let reward = await generateReward({
text: 'test reward',
type: 'reward',
value: 1,
userId: user._id,
});
let response = await user.post('/user/reroll');
await user.sync();
let updatedDaily = await user.get(`/tasks/${daily._id}`);
let updatedReward = await user.get(`/tasks/${reward._id}`);
expect(response.message).to.equal(t('rerollComplete'));
expect(updatedDaily.value).to.equal(0);
expect(updatedReward.value).to.equal(1);
});
});

63
test/common/ops/reroll.js Normal file
View File

@@ -0,0 +1,63 @@
import reroll from '../../../common/script/ops/reroll';
import i18n from '../../../common/script/i18n';
import {
generateUser,
generateDaily,
generateReward,
} from '../../helpers/common.helper';
import {
NotAuthorized,
} from '../../../common/script/libs/errors';
describe('shared.ops.reroll', () => {
let user;
let tasks = [];
beforeEach(() => {
user = generateUser();
user.balance = 1;
tasks = [generateDaily(), generateReward()];
});
it('returns an error when user balance is too low', (done) => {
user.balance = 0;
try {
reroll(user);
} catch (err) {
expect(err).to.be.an.instanceof(NotAuthorized);
expect(err.message).to.equal(i18n.t('notEnoughGems'));
done();
}
});
it('rerolls a user with enough gems', () => {
let response = reroll(user);
expect(response.message).to.equal(i18n.t('rerollComplete'));
});
it('reduces a user\'s balance', () => {
reroll(user);
expect(user.balance).to.equal(0);
});
it('resets a user\'s health points', () => {
user.stats.hp = 40;
reroll(user);
expect(user.stats.hp).to.equal(50);
});
it('resets user\'s taks values except for rewards to 0', () => {
tasks[0].value = 1;
tasks[1].value = 1;
reroll(user, tasks);
expect(tasks[0].value).to.equal(0);
expect(tasks[1].value).to.equal(1);
});
});

View File

@@ -974,4 +974,34 @@ api.userRebirth = {
}, },
}; };
/*
* @api {post} /user/reroll Rerolls a user.
* @apiVersion 3.0.0
* @apiName UserReroll
* @apiGroup User
*
* @apiSuccess {Object} data `user`
*/
api.userReroll = {
method: 'POST',
middlewares: [authWithHeaders(), cron],
url: '/user/reroll',
async handler (req, res) {
let user = res.locals.user;
let query = {
userId: user._id,
type: {$in: ['daily', 'habit', 'todo']},
};
let tasks = await Tasks.Task.find(query).exec();
let rerollResponse = common.ops.reroll(user, tasks, req, res.analytics);
let promises = tasks.map(task => task.save());
promises.push(user.save());
await Q.all(promises);
res.respond(200, rerollResponse);
},
};
module.exports = api; module.exports = api;