mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-17 14:47:53 +01:00
Ported release both, added unit tests, add release both route with integration tests
This commit is contained in:
@@ -148,5 +148,6 @@
|
|||||||
"noSudoAccess": "You don't have sudo access.",
|
"noSudoAccess": "You don't have sudo access.",
|
||||||
"couponCodeRequired": "The coupon code is required.",
|
"couponCodeRequired": "The coupon code is required.",
|
||||||
"eventRequired": "\"req.params.event\" is required.",
|
"eventRequired": "\"req.params.event\" is required.",
|
||||||
"countRequired": "\"req.query.count\" is required."
|
"countRequired": "\"req.query.count\" is required.",
|
||||||
|
"mountsAndPetsReleased": "Mounts and pets released"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -118,6 +118,7 @@ import purchase from './ops/purchase';
|
|||||||
import purchaseHourglass from './ops/hourglassPurchase';
|
import purchaseHourglass from './ops/hourglassPurchase';
|
||||||
import readCard from './ops/readCard';
|
import readCard from './ops/readCard';
|
||||||
import openMysteryItem from './ops/openMysteryItem';
|
import openMysteryItem from './ops/openMysteryItem';
|
||||||
|
import releaseBoth from './ops/releaseBoth';
|
||||||
|
|
||||||
api.ops = {
|
api.ops = {
|
||||||
scoreTask,
|
scoreTask,
|
||||||
@@ -137,6 +138,7 @@ api.ops = {
|
|||||||
purchaseHourglass,
|
purchaseHourglass,
|
||||||
readCard,
|
readCard,
|
||||||
openMysteryItem,
|
openMysteryItem,
|
||||||
|
releaseBoth,
|
||||||
};
|
};
|
||||||
|
|
||||||
import handleTwoHanded from './fns/handleTwoHanded';
|
import handleTwoHanded from './fns/handleTwoHanded';
|
||||||
|
|||||||
@@ -1,50 +1,65 @@
|
|||||||
import content from '../content/index';
|
import content from '../content/index';
|
||||||
import i18n from '../i18n';
|
import i18n from '../i18n';
|
||||||
|
import {
|
||||||
|
NotAuthorized,
|
||||||
|
} from '../libs/errors';
|
||||||
|
import splitWhitespace from '../libs/splitWhitespace';
|
||||||
|
|
||||||
|
module.exports = function releaseBoth (user, req = {}, analytics) {
|
||||||
|
let animal;
|
||||||
|
|
||||||
module.exports = function(user, req, cb, analytics) {
|
|
||||||
var analyticsData, animal, giveTriadBingo;
|
|
||||||
if (user.balance < 1.5 && !user.achievements.triadBingo) {
|
if (user.balance < 1.5 && !user.achievements.triadBingo) {
|
||||||
return typeof cb === "function" ? cb({
|
throw new NotAuthorized(i18n.t('notEnoughGems', req.language));
|
||||||
code: 401,
|
}
|
||||||
message: i18n.t('notEnoughGems', req.language)
|
|
||||||
}) : void 0;
|
let giveTriadBingo = true;
|
||||||
} else {
|
|
||||||
giveTriadBingo = true;
|
if (!user.achievements.triadBingo) {
|
||||||
if (!user.achievements.triadBingo) {
|
if (analytics) {
|
||||||
analyticsData = {
|
analytics.track('release pets & mounts', {
|
||||||
uuid: user._id,
|
uuid: user._id,
|
||||||
acquireMethod: 'Gems',
|
acquireMethod: 'Gems',
|
||||||
gemCost: 6,
|
gemCost: 6,
|
||||||
category: 'behavior'
|
category: 'behavior'
|
||||||
};
|
});
|
||||||
if (typeof analytics !== "undefined" && analytics !== null) {
|
|
||||||
analytics.track('release pets & mounts', analyticsData);
|
|
||||||
}
|
|
||||||
user.balance -= 1.5;
|
|
||||||
}
|
|
||||||
user.items.currentMount = "";
|
|
||||||
user.items.currentPet = "";
|
|
||||||
for (animal in content.pets) {
|
|
||||||
if (user.items.pets[animal] === -1) {
|
|
||||||
giveTriadBingo = false;
|
|
||||||
}
|
|
||||||
user.items.pets[animal] = 0;
|
|
||||||
user.items.mounts[animal] = null;
|
|
||||||
}
|
|
||||||
if (!user.achievements.beastMasterCount) {
|
|
||||||
user.achievements.beastMasterCount = 0;
|
|
||||||
}
|
|
||||||
user.achievements.beastMasterCount++;
|
|
||||||
if (!user.achievements.mountMasterCount) {
|
|
||||||
user.achievements.mountMasterCount = 0;
|
|
||||||
}
|
|
||||||
user.achievements.mountMasterCount++;
|
|
||||||
if (giveTriadBingo) {
|
|
||||||
if (!user.achievements.triadBingoCount) {
|
|
||||||
user.achievements.triadBingoCount = 0;
|
|
||||||
}
|
|
||||||
user.achievements.triadBingoCount++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
user.balance -= 1.5;
|
||||||
}
|
}
|
||||||
return typeof cb === "function" ? cb(null, user) : void 0;
|
|
||||||
|
user.items.currentMount = "";
|
||||||
|
user.items.currentPet = "";
|
||||||
|
|
||||||
|
for (animal in content.pets) {
|
||||||
|
if (user.items.pets[animal] === -1) {
|
||||||
|
giveTriadBingo = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
user.items.pets[animal] = 0;
|
||||||
|
user.items.mounts[animal] = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!user.achievements.beastMasterCount) {
|
||||||
|
user.achievements.beastMasterCount = 0;
|
||||||
|
}
|
||||||
|
user.achievements.beastMasterCount++;
|
||||||
|
|
||||||
|
if (!user.achievements.mountMasterCount) {
|
||||||
|
user.achievements.mountMasterCount = 0;
|
||||||
|
}
|
||||||
|
user.achievements.mountMasterCount++;
|
||||||
|
|
||||||
|
if (giveTriadBingo) {
|
||||||
|
if (!user.achievements.triadBingoCount) {
|
||||||
|
user.achievements.triadBingoCount = 0;
|
||||||
|
}
|
||||||
|
user.achievements.triadBingoCount++;
|
||||||
|
}
|
||||||
|
|
||||||
|
let response = {
|
||||||
|
data: _.pick(user, splitWhitespace('achievements')),
|
||||||
|
message: i18n.t('mountsAndPetsReleased'),
|
||||||
|
};
|
||||||
|
|
||||||
|
return response;
|
||||||
};
|
};
|
||||||
|
|||||||
48
test/api/v3/integration/user/POST-user_release_both.test.js
Normal file
48
test/api/v3/integration/user/POST-user_release_both.test.js
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
import {
|
||||||
|
generateUser,
|
||||||
|
translate as t,
|
||||||
|
} from '../../../../helpers/api-integration/v3';
|
||||||
|
|
||||||
|
describe('POST /user/release-both', () => {
|
||||||
|
let user;
|
||||||
|
let animal = 'Wolf-Base';
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
user = await generateUser({
|
||||||
|
'items.currentMount': animal,
|
||||||
|
'items.currentPet': animal,
|
||||||
|
'items.pets': {animal: 5},
|
||||||
|
'items.mounts': {animal: true},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns an error when user balance is too low and user does not have triadBingo', async () => {
|
||||||
|
await expect(user.post('/user/release-both'))
|
||||||
|
.to.eventually.be.rejected.and.to.eql({
|
||||||
|
code: 401,
|
||||||
|
error: 'NotAuthorized',
|
||||||
|
message: t('notEnoughGems'),
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// More tests in common code unit tests
|
||||||
|
|
||||||
|
it('grants triad bingo with gems', async () => {
|
||||||
|
await user.update({
|
||||||
|
balance: 1.5,
|
||||||
|
});
|
||||||
|
|
||||||
|
let response = await user.post('/user/release-both');
|
||||||
|
await user.sync();
|
||||||
|
|
||||||
|
expect(response.message).to.equal(t('mountsAndPetsReleased'));
|
||||||
|
expect(user.balance).to.equal(0);
|
||||||
|
expect(user.items.currentMount).to.be.empty;
|
||||||
|
expect(user.items.currentPet).to.be.empty;
|
||||||
|
expect(user.items.pets[animal]).to.be.empty;
|
||||||
|
expect(user.items.mounts[animal]).to.equal(null);
|
||||||
|
expect(user.achievements.beastMasterCount).to.equal(1);
|
||||||
|
expect(user.achievements.mountMasterCount).to.equal(1);
|
||||||
|
expect(user.achievements.triadBingoCount).to.equal(1);
|
||||||
|
});
|
||||||
|
});
|
||||||
98
test/common/ops/releaseBoth.js
Normal file
98
test/common/ops/releaseBoth.js
Normal file
@@ -0,0 +1,98 @@
|
|||||||
|
import releaseBoth from '../../../common/script/ops/releaseBoth';
|
||||||
|
import i18n from '../../../common/script/i18n';
|
||||||
|
import {
|
||||||
|
generateUser,
|
||||||
|
} from '../../helpers/common.helper';
|
||||||
|
import {
|
||||||
|
NotAuthorized,
|
||||||
|
} from '../../../common/script/libs/errors';
|
||||||
|
|
||||||
|
describe('shared.ops.releaseBoth', () => {
|
||||||
|
let user;
|
||||||
|
let animal = 'Wolf-Base';
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
user = generateUser();
|
||||||
|
user.items.currentMount = animal;
|
||||||
|
user.items.currentPet = animal;
|
||||||
|
user.items.pets[animal] = 5;
|
||||||
|
user.items.mounts[animal] = true;
|
||||||
|
user.balance = 1.5;
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns an error when user balance is too low and user does not have triadBingo', (done) => {
|
||||||
|
user.balance = 0;
|
||||||
|
|
||||||
|
try {
|
||||||
|
releaseBoth(user);
|
||||||
|
} catch (err) {
|
||||||
|
expect(err).to.be.an.instanceof(NotAuthorized);
|
||||||
|
expect(err.message).to.equal(i18n.t('notEnoughGems'));
|
||||||
|
done();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it('grants triad bingo with gems', () => {
|
||||||
|
let response = releaseBoth(user);
|
||||||
|
|
||||||
|
expect(response.message).to.equal(i18n.t('mountsAndPetsReleased'));
|
||||||
|
expect(user.achievements.triadBingoCount).to.equal(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('grants triad bingo without gems', () => {
|
||||||
|
user.balance = 0;
|
||||||
|
user.achievements.triadBingo = 1;
|
||||||
|
user.achievements.triadBingoCount = 1;
|
||||||
|
|
||||||
|
let response = releaseBoth(user);
|
||||||
|
|
||||||
|
expect(response.message).to.equal(i18n.t('mountsAndPetsReleased'));
|
||||||
|
expect(user.achievements.triadBingoCount).to.equal(2);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('releases pets', () => {
|
||||||
|
let response = releaseBoth(user);
|
||||||
|
|
||||||
|
expect(response.message).to.equal(i18n.t('mountsAndPetsReleased'));
|
||||||
|
expect(user.items.pets[animal]).to.be.empty;
|
||||||
|
expect(user.items.mounts[animal]).to.equal(null);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('releases mounts', () => {
|
||||||
|
let response = releaseBoth(user);
|
||||||
|
|
||||||
|
expect(response.message).to.equal(i18n.t('mountsAndPetsReleased'));
|
||||||
|
expect(user.items.mounts[animal]).to.equal(null);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('removes currentPet', () => {
|
||||||
|
releaseBoth(user);
|
||||||
|
|
||||||
|
expect(user.items.currentMount).to.be.empty;
|
||||||
|
expect(user.items.currentPet).to.be.empty;
|
||||||
|
});
|
||||||
|
|
||||||
|
it('removes currentMount', () => {
|
||||||
|
releaseBoth(user);
|
||||||
|
|
||||||
|
expect(user.items.currentMount).to.be.empty;
|
||||||
|
});
|
||||||
|
|
||||||
|
it('decreases user\'s balance', () => {
|
||||||
|
releaseBoth(user);
|
||||||
|
|
||||||
|
expect(user.balance).to.equal(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('incremenets beastMasterCount', () => {
|
||||||
|
releaseBoth(user);
|
||||||
|
|
||||||
|
expect(user.achievements.beastMasterCount).to.equal(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('incremenets mountMasterCount', () => {
|
||||||
|
releaseBoth(user);
|
||||||
|
|
||||||
|
expect(user.achievements.mountMasterCount).to.equal(1);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -764,4 +764,24 @@ api.userOpenMysteryItem = {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @api {post} /user/release-both Releases Pets and Mounts and grants Triad Bingo.
|
||||||
|
* @apiVersion 3.0.0
|
||||||
|
* @apiName UserReleaseBoth
|
||||||
|
* @apiGroup User
|
||||||
|
*
|
||||||
|
* @apiSuccess {Object} data `user.items.gear.owned`
|
||||||
|
*/
|
||||||
|
api.userReleaseBoth = {
|
||||||
|
method: 'POST',
|
||||||
|
middlewares: [authWithHeaders(), cron],
|
||||||
|
url: '/user/release-both',
|
||||||
|
async handler (req, res) {
|
||||||
|
let user = res.locals.user;
|
||||||
|
let releaseBothResponse = common.ops.releaseBoth(user, req, res.analytics);
|
||||||
|
await user.save();
|
||||||
|
res.respond(200, releaseBothResponse);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
module.exports = api;
|
module.exports = api;
|
||||||
|
|||||||
Reference in New Issue
Block a user