add routes for buy ops and integration tests

This commit is contained in:
Matteo Pagliazzi
2016-03-20 17:41:17 +01:00
parent fea2e0d8c0
commit 480194f53c
8 changed files with 286 additions and 10 deletions

View File

@@ -94,6 +94,9 @@ api.noTags = noTags;
import appliedTags from './libs/appliedTags';
api.appliedTags = appliedTags;
import pickDeep from './libs/pickDeep';
api.pickDeep = pickDeep;
import count from './count';
api.count = count;
@@ -101,11 +104,19 @@ api.count = count;
import scoreTask from './ops/scoreTask';
import sleep from './ops/sleep';
import allocate from './ops/allocate';
import buy from './ops/buy';
import buyMysterySet from './ops/buyMysterySet';
import buyQuest from './ops/buyQuest';
import buySpecialSpell from './ops/buySpecialSpell';
api.ops = {
scoreTask,
sleep,
allocate,
buy,
buyMysterySet,
buySpecialSpell,
buyQuest,
};
import handleTwoHanded from './fns/handleTwoHanded';

View File

@@ -106,15 +106,17 @@ module.exports = function buy (user, req = {}, analytics) {
}
user.items.gear.owned[item.key] = true;
if (!message) {
message = i18n.t('messageBought', {
itemText: item.text(req.language),
}, req.language);
}
if (item.last) ultimateGear(user);
}
user.stats.gp -= item.value;
if (!message) {
message = i18n.t('messageBought', {
itemText: item.text(req.language),
}, req.language);
}
if (analytics) {
analytics.track('acquire item', {
uuid: user._id,
@@ -125,11 +127,12 @@ module.exports = function buy (user, req = {}, analytics) {
});
}
let buyResp = _.pick(user, splitWhitespace('items achievements stats flags'));
if (armoireResp) buyResp.armoire = armoireResp;
return {
data: buyResp,
let res = {
data: _.pick(user, splitWhitespace('items achievements stats flags')),
message,
};
if (armoireResp) res.armoire = armoireResp;
return res;
};

View File

@@ -6,6 +6,8 @@ import {
NotFound,
} from '../libs/errors';
import _ from 'lodash';
// buy a quest with gold
module.exports = function buyQuest (user, req = {}, analytics) {
let key = _.get(req, 'params.key');
if (!key) throw new BadRequest(i18n.t('missingKeyParam', req.language));

View File

@@ -0,0 +1,42 @@
import {
generateUser,
translate as t,
} from '../../../../helpers/api-integration/v3';
import shared from '../../../../../common/script';
let content = shared.content;
describe('POST /user/buy/:key', () => {
let user;
beforeEach(async () => {
user = await generateUser({
'stats.gp': 400,
});
});
// More tests in common code unit tests
it('returns an error if the item is not found', async () => {
await expect(user.post(`/user/buy/notExisting`))
.to.eventually.be.rejected.and.eql({
code: 404,
error: 'NotFound',
message: t('itemNotFound', {key: 'notExisting'}),
});
});
it('buys an item', async () => {
let potion = content.potion;
let res = await user.post(`/user/buy/potion`);
await user.sync();
expect(res.data).to.eql({
items: JSON.parse(JSON.stringify(user.items)), // otherwise dates can't be compared
achievements: user.achievements,
stats: user.stats,
flags: JSON.parse(JSON.stringify(user.flags)), // otherwise dates can't be compared
});
expect(res.message).to.equal(t('messageBought', {itemText: potion.text()}));
});
});

View File

@@ -0,0 +1,42 @@
import {
generateUser,
translate as t,
} from '../../../../helpers/api-integration/v3';
describe('POST /user/buy-mystery-set/:key', () => {
let user;
beforeEach(async () => {
user = await generateUser({
'purchased.plan.consecutive.trinkets': 1,
});
});
// More tests in common code unit tests
it('returns an error if the mystery set is not found', async () => {
await expect(user.post(`/user/buy-mystery-set/notExisting`))
.to.eventually.be.rejected.and.eql({
code: 404,
error: 'NotFound',
message: t('mysterySetNotFound'),
});
});
it('buys a mystery set', async () => {
let key = 301404;
let res = await user.post(`/user/buy-mystery-set/${key}`);
await user.sync();
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,
},
},
});
expect(res.message).to.equal(t('hourglassPurchaseSet'));
});
});

View File

@@ -0,0 +1,40 @@
import {
generateUser,
translate as t,
} from '../../../../helpers/api-integration/v3';
import shared from '../../../../../common/script';
let content = shared.content;
describe('POST /user/buy-quest/:key', () => {
let user;
beforeEach(async () => {
user = await generateUser();
});
// More tests in common code unit tests
it('returns an error if the quest is not found', async () => {
await expect(user.post(`/user/buy-quest/notExisting`))
.to.eventually.be.rejected.and.eql({
code: 404,
error: 'NotFound',
message: t('questNotFound', {key: 'notExisting'}),
});
});
it('buys a quest', async () => {
let key = 'dilatoryDistress1';
let item = content.quests[key];
await user.update({'stats.gp': 250});
let res = await user.post(`/user/buy-quest/${key}`);
await user.sync();
expect(res.data).to.eql(user.items.quests);
expect(res.message).to.equal(t('messageBought', {
itemText: item.text(),
}));
});
});

View File

@@ -0,0 +1,43 @@
import {
generateUser,
translate as t,
} from '../../../../helpers/api-integration/v3';
import shared from '../../../../../common/script';
let content = shared.content;
describe('POST /user/buy-special-spell/:key', () => {
let user;
beforeEach(async () => {
user = await generateUser();
});
// More tests in common code unit tests
it('returns an error if the special spell is not found', async () => {
await expect(user.post(`/user/buy-special-spell/notExisting`))
.to.eventually.be.rejected.and.eql({
code: 404,
error: 'NotFound',
message: t('spellNotFound', {spellId: 'notExisting'}),
});
});
it('buys a special spell', async () => {
let key = 'thankyou';
let item = content.special[key];
await user.update({'stats.gp': 250});
let res = await user.post(`/user/buy-special-spell/${key}`);
await user.sync();
expect(res.data).to.eql({
items: JSON.parse(JSON.stringify(user.items)), // otherwise dates can't be compared
stats: user.stats,
});
expect(res.message).to.equal(t('messageBought', {
itemText: item.text(),
}));
});
});

View File

@@ -196,4 +196,97 @@ api.allocate = {
},
};
/**
* @api {post} /user/buy/:key Buy a content item.
* @apiVersion 3.0.0
* @apiName UserBuy
* @apiGroup User
*
* @apiParam {string} key The item to buy.
*
* @apiSuccess {Object} data `items, achievements, stats, flags`
* @apiSuccess {object} armoireResp Optional extra item given by the armoire
* @apiSuccess {string} message
*/
api.buy = {
method: 'POST',
middlewares: [authWithHeaders(), cron],
url: '/user/buy/:key',
async handler (req, res) {
let user = res.locals.user;
let buyRes = common.ops.buy(user, req, res.analytics);
await user.save();
res.respond(200, buyRes);
},
};
/**
* @api {post} /user/buy-mystery-set/:key Buy a mystery set.
* @apiVersion 3.0.0
* @apiName UserBuyMysterySet
* @apiGroup User
*
* @apiParam {string} key The mystery set to buy.
*
* @apiSuccess {Object} data `items, purchased.plan.consecutive`
* @apiSuccess {string} message
*/
api.buyMysterySet = {
method: 'POST',
middlewares: [authWithHeaders(), cron],
url: '/user/buy-mystery-set/:key',
async handler (req, res) {
let user = res.locals.user;
let buyMysterySetRes = common.ops.buyMysterySet(user, req, res.analytics);
await user.save();
res.respond(200, buyMysterySetRes);
},
};
/**
* @api {post} /user/buy-quest/:key Buy a quest with gold.
* @apiVersion 3.0.0
* @apiName UserBuyQuest
* @apiGroup User
*
* @apiParam {string} key The quest spell to buy.
*
* @apiSuccess {Object} data `items.quests`
* @apiSuccess {string} message
*/
api.buyQuest = {
method: 'POST',
middlewares: [authWithHeaders(), cron],
url: '/user/buy-quest/:key',
async handler (req, res) {
let user = res.locals.user;
let buyQuestRes = common.ops.buyQuest(user, req, res.analytics);
await user.save();
res.respond(200, buyQuestRes);
},
};
/**
* @api {post} /user/buy-special-spell/:key Buy special spell.
* @apiVersion 3.0.0
* @apiName UserBuySpecialSpell
* @apiGroup User
*
* @apiParam {string} key The special spell to buy.
*
* @apiSuccess {Object} data `items, stats`
* @apiSuccess {string} message
*/
api.buySpecialSpell = {
method: 'POST',
middlewares: [authWithHeaders(), cron],
url: '/user/buy-special-spell/:key',
async handler (req, res) {
let user = res.locals.user;
let buySpecialSpellRes = common.ops.buySpecialSpell(user, req);
await user.save();
res.respond(200, buySpecialSpellRes);
},
};
module.exports = api;