Files
habitica/test/common/ops/buy/hourglassPurchase.js
T
negue 54153ec299 Refactor Purchase API - Part 1 (#9714)
* move to shops/purchase

* move files to /buy/ instead of /purchase/

* refactor buy.js - add more itemtypes

* revert moving special purchases to buy

* only use buyOp from api-routes

* fix buying potion client-side

* undo import buy instead of purchase

* enable potion bulk purchase - use buyGear as fallback (as before)

* move quantity purchase inside buyHealthPotion

* move quantity purchase inside buyQuest

* move quantity purchase inside buySpecialSpell + add analytics

* remove unused quantity variable - set req.type on specialKeys

* fix `buyKnownKeys` on buy api

* test buy-special-spell if not enough gold

* more buy ops coverage

* fix lint

* buyMysterySet: test for window.confirm, buyQuest: check for Masterclassers unlock

* fix test & lint

* re-create package-lock.json to travis build ?

* use global.window instead of method argument

* add back canOwn checks

* remove buyMysterySet confirm request
2018-02-10 11:14:40 +01:00

153 lines
4.9 KiB
JavaScript

import hourglassPurchase from '../../../../website/common/script/ops/buy/hourglassPurchase';
import {
BadRequest,
NotAuthorized,
} from '../../../../website/common/script/libs/errors';
import i18n from '../../../../website/common/script/i18n';
import content from '../../../../website/common/script/content/index';
import {
generateUser,
} from '../../../helpers/common.helper';
describe('common.ops.hourglassPurchase', () => {
let user;
let analytics = {track () {}};
beforeEach(() => {
user = generateUser();
sinon.stub(analytics, 'track');
});
afterEach(() => {
analytics.track.restore();
});
context('failure conditions', () => {
it('return error when key is not provided', (done) => {
try {
hourglassPurchase(user, {params: {}});
} catch (err) {
expect(err).to.be.an.instanceof(BadRequest);
expect(err.message).to.eql(i18n.t('missingKeyParam'));
done();
}
});
it('returns error when type is not provided', (done) => {
try {
hourglassPurchase(user, {params: {key: 'Base'}});
} catch (err) {
expect(err).to.be.an.instanceof(BadRequest);
expect(err.message).to.eql(i18n.t('missingTypeParam'));
done();
}
});
it('returns error when inccorect type is provided', (done) => {
try {
hourglassPurchase(user, {params: {type: 'notAType', key: 'MantisShrimp-Base'}});
} catch (err) {
expect(err).to.be.an.instanceof(NotAuthorized);
expect(err.message).to.eql(i18n.t('typeNotAllowedHourglass', {allowedTypes: _.keys(content.timeTravelStable).toString()}));
done();
}
});
it('does not grant to pets without Mystic Hourglasses', (done) => {
try {
hourglassPurchase(user, {params: {type: 'pets', key: 'MantisShrimp-Base'}});
} catch (err) {
expect(err).to.be.an.instanceof(NotAuthorized);
expect(err.message).to.eql(i18n.t('notEnoughHourglasses'));
done();
}
});
it('does not grant to mounts without Mystic Hourglasses', (done) => {
try {
hourglassPurchase(user, {params: {type: 'mounts', key: 'MantisShrimp-Base'}});
} catch (err) {
expect(err).to.be.an.instanceof(NotAuthorized);
expect(err.message).to.eql(i18n.t('notEnoughHourglasses'));
done();
}
});
it('does not grant pet that is not part of the Time Travel Stable', (done) => {
user.purchased.plan.consecutive.trinkets = 1;
try {
hourglassPurchase(user, {params: {type: 'pets', key: 'Wolf-Veteran'}});
} catch (err) {
expect(err).to.be.an.instanceof(NotAuthorized);
expect(err.message).to.eql(i18n.t('notAllowedHourglass'));
done();
}
});
it('does not grant mount that is not part of the Time Travel Stable', (done) => {
user.purchased.plan.consecutive.trinkets = 1;
try {
hourglassPurchase(user, {params: {type: 'mounts', key: 'Orca-Base'}});
} catch (err) {
expect(err).to.be.an.instanceof(NotAuthorized);
expect(err.message).to.eql(i18n.t('notAllowedHourglass'));
done();
}
});
it('does not grant pet that has already been purchased', (done) => {
user.purchased.plan.consecutive.trinkets = 1;
user.items.pets = {
'MantisShrimp-Base': true,
};
try {
hourglassPurchase(user, {params: {type: 'pets', key: 'MantisShrimp-Base'}});
} catch (err) {
expect(err).to.be.an.instanceof(NotAuthorized);
expect(err.message).to.eql(i18n.t('petsAlreadyOwned'));
done();
}
});
it('does not grant mount that has already been purchased', (done) => {
user.purchased.plan.consecutive.trinkets = 1;
user.items.mounts = {
'MantisShrimp-Base': true,
};
try {
hourglassPurchase(user, {params: {type: 'mounts', key: 'MantisShrimp-Base'}});
} catch (err) {
expect(err).to.be.an.instanceof(NotAuthorized);
expect(err.message).to.eql(i18n.t('mountsAlreadyOwned'));
done();
}
});
});
context('successful purchases', () => {
it('buys a pet', () => {
user.purchased.plan.consecutive.trinkets = 2;
let [, message] = hourglassPurchase(user, {params: {type: 'pets', key: 'MantisShrimp-Base'}}, analytics);
expect(message).to.eql(i18n.t('hourglassPurchase'));
expect(user.purchased.plan.consecutive.trinkets).to.eql(1);
expect(user.items.pets).to.eql({'MantisShrimp-Base': 5});
expect(analytics.track).to.be.calledOnce;
});
it('buys a mount', () => {
user.purchased.plan.consecutive.trinkets = 2;
let [, message] = hourglassPurchase(user, {params: {type: 'mounts', key: 'MantisShrimp-Base'}});
expect(message).to.eql(i18n.t('hourglassPurchase'));
expect(user.purchased.plan.consecutive.trinkets).to.eql(1);
expect(user.items.mounts).to.eql({'MantisShrimp-Base': true});
});
});
});