mirror of
https://github.com/sudoxnym/habitica-self-host.git
synced 2026-07-30 02:30:29 +00:00
142 lines
4.4 KiB
JavaScript
142 lines
4.4 KiB
JavaScript
|
|
/* eslint-disable camelcase */
|
||
|
|
|
||
|
|
import sinon from 'sinon'; // eslint-disable-line no-shadow
|
||
|
|
import {
|
||
|
|
generateUser,
|
||
|
|
} from '../../../helpers/common.helper';
|
||
|
|
import {
|
||
|
|
BadRequest, NotAuthorized,
|
||
|
|
} from '../../../../website/common/script/libs/errors';
|
||
|
|
import i18n from '../../../../website/common/script/i18n';
|
||
|
|
import {BuyGemOperation} from '../../../../website/common/script/ops/buy/buyGem';
|
||
|
|
import planGemLimits from '../../../../website/common/script/libs/planGemLimits';
|
||
|
|
|
||
|
|
function buyGem (user, req, analytics) {
|
||
|
|
let buyOp = new BuyGemOperation(user, req, analytics);
|
||
|
|
|
||
|
|
return buyOp.purchase();
|
||
|
|
}
|
||
|
|
|
||
|
|
describe('shared.ops.buyGem', () => {
|
||
|
|
let user;
|
||
|
|
let analytics = {track () {}};
|
||
|
|
let goldPoints = 40;
|
||
|
|
let gemsBought = 40;
|
||
|
|
let userGemAmount = 10;
|
||
|
|
|
||
|
|
beforeEach(() => {
|
||
|
|
user = generateUser({
|
||
|
|
stats: { gp: goldPoints },
|
||
|
|
balance: userGemAmount,
|
||
|
|
purchased: {
|
||
|
|
plan: {
|
||
|
|
gemsBought: 0,
|
||
|
|
customerId: 'costumer-id',
|
||
|
|
},
|
||
|
|
},
|
||
|
|
});
|
||
|
|
|
||
|
|
sinon.stub(analytics, 'track');
|
||
|
|
});
|
||
|
|
|
||
|
|
afterEach(() => {
|
||
|
|
analytics.track.restore();
|
||
|
|
});
|
||
|
|
|
||
|
|
context('Gems', () => {
|
||
|
|
it('purchases gems', () => {
|
||
|
|
let [, message] = buyGem(user, {params: {type: 'gems', key: 'gem'}}, analytics);
|
||
|
|
|
||
|
|
expect(message).to.equal(i18n.t('plusGem', {count: 1}));
|
||
|
|
expect(user.balance).to.equal(userGemAmount + 0.25);
|
||
|
|
expect(user.purchased.plan.gemsBought).to.equal(1);
|
||
|
|
expect(user.stats.gp).to.equal(goldPoints - planGemLimits.convRate);
|
||
|
|
expect(analytics.track).to.be.calledOnce;
|
||
|
|
});
|
||
|
|
|
||
|
|
it('purchases gems with a different language than the default', () => {
|
||
|
|
let [, message] = buyGem(user, {params: {type: 'gems', key: 'gem'}, language: 'de'});
|
||
|
|
|
||
|
|
expect(message).to.equal(i18n.t('plusGem', {count: 1}, 'de'));
|
||
|
|
expect(user.balance).to.equal(userGemAmount + 0.25);
|
||
|
|
expect(user.purchased.plan.gemsBought).to.equal(1);
|
||
|
|
expect(user.stats.gp).to.equal(goldPoints - planGemLimits.convRate);
|
||
|
|
});
|
||
|
|
|
||
|
|
it('makes bulk purchases of gems', () => {
|
||
|
|
let [, message] = buyGem(user, {
|
||
|
|
params: {type: 'gems', key: 'gem'},
|
||
|
|
quantity: 2,
|
||
|
|
});
|
||
|
|
|
||
|
|
expect(message).to.equal(i18n.t('plusGem', {count: 2}));
|
||
|
|
expect(user.balance).to.equal(userGemAmount + 0.50);
|
||
|
|
expect(user.purchased.plan.gemsBought).to.equal(2);
|
||
|
|
expect(user.stats.gp).to.equal(goldPoints - planGemLimits.convRate * 2);
|
||
|
|
});
|
||
|
|
|
||
|
|
|
||
|
|
context('Failure conditions', () => {
|
||
|
|
it('returns an error when key is not provided', (done) => {
|
||
|
|
try {
|
||
|
|
buyGem(user, {params: {type: 'gems'}});
|
||
|
|
} catch (err) {
|
||
|
|
expect(err).to.be.an.instanceof(BadRequest);
|
||
|
|
expect(err.message).to.equal(i18n.t('missingKeyParam'));
|
||
|
|
done();
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
it('prevents unsubscribed user from buying gems', (done) => {
|
||
|
|
delete user.purchased.plan.customerId;
|
||
|
|
|
||
|
|
try {
|
||
|
|
buyGem(user, {params: {type: 'gems', key: 'gem'}});
|
||
|
|
} catch (err) {
|
||
|
|
expect(err).to.be.an.instanceof(NotAuthorized);
|
||
|
|
expect(err.message).to.equal(i18n.t('mustSubscribeToPurchaseGems'));
|
||
|
|
done();
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
it('prevents user with not enough gold from buying gems', (done) => {
|
||
|
|
user.stats.gp = 15;
|
||
|
|
|
||
|
|
try {
|
||
|
|
buyGem(user, {params: {type: 'gems', key: 'gem'}});
|
||
|
|
} catch (err) {
|
||
|
|
expect(err).to.be.an.instanceof(NotAuthorized);
|
||
|
|
expect(err.message).to.equal(i18n.t('messageNotEnoughGold'));
|
||
|
|
done();
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
it('prevents user that have reached the conversion cap from buying gems', (done) => {
|
||
|
|
user.stats.gp = goldPoints;
|
||
|
|
user.purchased.plan.gemsBought = gemsBought;
|
||
|
|
|
||
|
|
try {
|
||
|
|
buyGem(user, {params: {type: 'gems', key: 'gem'}});
|
||
|
|
} catch (err) {
|
||
|
|
expect(err).to.be.an.instanceof(NotAuthorized);
|
||
|
|
expect(err.message).to.equal(i18n.t('reachedGoldToGemCap', {convCap: planGemLimits.convCap}));
|
||
|
|
done();
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
it('prevents user from buying an invalid quantity', (done) => {
|
||
|
|
user.stats.gp = goldPoints;
|
||
|
|
user.purchased.plan.gemsBought = gemsBought;
|
||
|
|
|
||
|
|
try {
|
||
|
|
buyGem(user, {params: {type: 'gems', key: 'gem'}, quantity: 'a'});
|
||
|
|
} catch (err) {
|
||
|
|
expect(err).to.be.an.instanceof(BadRequest);
|
||
|
|
expect(err.message).to.equal(i18n.t('invalidQuantity'));
|
||
|
|
done();
|
||
|
|
}
|
||
|
|
});
|
||
|
|
});
|
||
|
|
});
|
||
|
|
});
|