mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-16 06:07:21 +01:00
remove old server_side tests
This commit is contained in:
@@ -175,32 +175,6 @@ gulp.task('test:content:safe', ['test:prepare:build'], (cb) => {
|
||||
pipe(runner);
|
||||
});
|
||||
|
||||
gulp.task('test:server_side', ['test:prepare:build'], (cb) => {
|
||||
let runner = exec(
|
||||
testBin(SERVER_SIDE_TEST_COMMAND),
|
||||
(err, stdout, stderr) => {
|
||||
cb(err);
|
||||
}
|
||||
);
|
||||
pipe(runner);
|
||||
});
|
||||
|
||||
gulp.task('test:server_side:safe', ['test:prepare:build'], (cb) => {
|
||||
let runner = exec(
|
||||
testBin(SERVER_SIDE_TEST_COMMAND),
|
||||
(err, stdout, stderr) => {
|
||||
testResults.push({
|
||||
suite: 'Server Side Specs',
|
||||
pass: testCount(stdout, /(\d+) passing/),
|
||||
fail: testCount(stdout, /(\d+) failing/),
|
||||
pend: testCount(stdout, /(\d+) pending/),
|
||||
});
|
||||
cb();
|
||||
}
|
||||
);
|
||||
pipe(runner);
|
||||
});
|
||||
|
||||
gulp.task('test:karma', ['test:prepare:build'], (cb) => {
|
||||
let runner = exec(
|
||||
testBin(KARMA_TEST_COMMAND),
|
||||
|
||||
@@ -1,467 +0,0 @@
|
||||
var sinon = require('sinon');
|
||||
var chai = require("chai")
|
||||
chai.use(require("sinon-chai"))
|
||||
var expect = chai.expect
|
||||
var rewire = require('rewire');
|
||||
|
||||
describe('analytics', function() {
|
||||
// Mocks
|
||||
var amplitudeMock = sinon.stub();
|
||||
var googleAnalyticsMock = sinon.stub();
|
||||
var amplitudeTrack = sinon.stub().returns({
|
||||
catch: function () { return true; }
|
||||
});
|
||||
var googleEvent = sinon.stub().returns({
|
||||
send: function() { }
|
||||
});
|
||||
var googleItem = sinon.stub().returns({
|
||||
send: function() { }
|
||||
});
|
||||
var googleTransaction = sinon.stub().returns({
|
||||
item: googleItem
|
||||
});
|
||||
|
||||
afterEach(function(){
|
||||
amplitudeMock.reset();
|
||||
amplitudeTrack.reset();
|
||||
googleEvent.reset();
|
||||
googleTransaction.reset();
|
||||
googleItem.reset();
|
||||
});
|
||||
|
||||
describe('init', function() {
|
||||
var analytics = rewire('../../website/server/libs/api-v2/analytics');
|
||||
|
||||
it('throws an error if no options are passed in', function() {
|
||||
expect(analytics).to.throw('No options provided');
|
||||
});
|
||||
|
||||
it('registers amplitude with token', function() {
|
||||
analytics.__set__('Amplitude', amplitudeMock);
|
||||
var options = {
|
||||
amplitudeToken: 'token'
|
||||
};
|
||||
analytics(options);
|
||||
|
||||
expect(amplitudeMock).to.be.calledOnce;
|
||||
expect(amplitudeMock).to.be.calledWith('token');
|
||||
});
|
||||
|
||||
it('registers google analytics with token', function() {
|
||||
analytics.__set__('googleAnalytics', googleAnalyticsMock);
|
||||
var options = {
|
||||
googleAnalytics: 'token'
|
||||
};
|
||||
analytics(options);
|
||||
|
||||
expect(googleAnalyticsMock).to.be.calledOnce;
|
||||
expect(googleAnalyticsMock).to.be.calledWith('token');
|
||||
});
|
||||
});
|
||||
|
||||
describe('track', function() {
|
||||
|
||||
var analyticsData, event_type;
|
||||
var analytics = rewire('../../website/server/libs/api-v2/analytics');
|
||||
var initializedAnalytics;
|
||||
|
||||
beforeEach(function() {
|
||||
analytics.__set__('Amplitude', amplitudeMock);
|
||||
initializedAnalytics = analytics({amplitudeToken: 'token'});
|
||||
analytics.__set__('amplitude.track', amplitudeTrack);
|
||||
analytics.__set__('ga.event', googleEvent);
|
||||
|
||||
event_type = 'Cron';
|
||||
analyticsData = {
|
||||
category: 'behavior',
|
||||
uuid: 'unique-user-id',
|
||||
resting: true,
|
||||
cronCount: 5
|
||||
}
|
||||
});
|
||||
|
||||
context('Amplitude', function() {
|
||||
it('tracks event in amplitude', function() {
|
||||
|
||||
initializedAnalytics.track(event_type, analyticsData);
|
||||
|
||||
expect(amplitudeTrack).to.be.calledOnce;
|
||||
expect(amplitudeTrack).to.be.calledWith({
|
||||
event_type: 'Cron',
|
||||
user_id: 'unique-user-id',
|
||||
platform: 'server',
|
||||
event_properties: {
|
||||
category: 'behavior',
|
||||
resting: true,
|
||||
cronCount: 5
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('uses a dummy user id if none is provided', function() {
|
||||
delete analyticsData.uuid;
|
||||
|
||||
initializedAnalytics.track(event_type, analyticsData);
|
||||
|
||||
expect(amplitudeTrack).to.be.calledOnce;
|
||||
expect(amplitudeTrack).to.be.calledWith({
|
||||
event_type: 'Cron',
|
||||
user_id: 'no-user-id-was-provided',
|
||||
platform: 'server',
|
||||
event_properties: {
|
||||
category: 'behavior',
|
||||
resting: true,
|
||||
cronCount: 5
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('sends english item name for gear if itemKey is provided', function() {
|
||||
analyticsData.itemKey = 'headAccessory_special_foxEars'
|
||||
|
||||
initializedAnalytics.track(event_type, analyticsData);
|
||||
|
||||
expect(amplitudeTrack).to.be.calledOnce;
|
||||
expect(amplitudeTrack).to.be.calledWith({
|
||||
event_type: 'Cron',
|
||||
user_id: 'unique-user-id',
|
||||
platform: 'server',
|
||||
event_properties: {
|
||||
itemKey: 'headAccessory_special_foxEars',
|
||||
itemName: 'Fox Ears',
|
||||
category: 'behavior',
|
||||
resting: true,
|
||||
cronCount: 5
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('sends english item name for egg if itemKey is provided', function() {
|
||||
analyticsData.itemKey = 'Wolf'
|
||||
|
||||
initializedAnalytics.track(event_type, analyticsData);
|
||||
|
||||
expect(amplitudeTrack).to.be.calledOnce;
|
||||
expect(amplitudeTrack).to.be.calledWith({
|
||||
event_type: 'Cron',
|
||||
user_id: 'unique-user-id',
|
||||
platform: 'server',
|
||||
event_properties: {
|
||||
itemKey: 'Wolf',
|
||||
itemName: 'Wolf Egg',
|
||||
category: 'behavior',
|
||||
resting: true,
|
||||
cronCount: 5
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('sends english item name for food if itemKey is provided', function() {
|
||||
analyticsData.itemKey = 'Cake_Skeleton'
|
||||
|
||||
initializedAnalytics.track(event_type, analyticsData);
|
||||
|
||||
expect(amplitudeTrack).to.be.calledOnce;
|
||||
expect(amplitudeTrack).to.be.calledWith({
|
||||
event_type: 'Cron',
|
||||
user_id: 'unique-user-id',
|
||||
platform: 'server',
|
||||
event_properties: {
|
||||
itemKey: 'Cake_Skeleton',
|
||||
itemName: 'Bare Bones Cake',
|
||||
category: 'behavior',
|
||||
resting: true,
|
||||
cronCount: 5
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('sends english item name for hatching potion if itemKey is provided', function() {
|
||||
analyticsData.itemKey = 'Golden'
|
||||
|
||||
initializedAnalytics.track(event_type, analyticsData);
|
||||
|
||||
expect(amplitudeTrack).to.be.calledOnce;
|
||||
expect(amplitudeTrack).to.be.calledWith({
|
||||
event_type: 'Cron',
|
||||
user_id: 'unique-user-id',
|
||||
platform: 'server',
|
||||
event_properties: {
|
||||
itemKey: 'Golden',
|
||||
itemName: 'Golden Hatching Potion',
|
||||
category: 'behavior',
|
||||
resting: true,
|
||||
cronCount: 5
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('sends english item name for quest if itemKey is provided', function() {
|
||||
analyticsData.itemKey = 'atom1'
|
||||
|
||||
initializedAnalytics.track(event_type, analyticsData);
|
||||
|
||||
expect(amplitudeTrack).to.be.calledOnce;
|
||||
expect(amplitudeTrack).to.be.calledWith({
|
||||
event_type: 'Cron',
|
||||
user_id: 'unique-user-id',
|
||||
platform: 'server',
|
||||
event_properties: {
|
||||
itemKey: 'atom1',
|
||||
itemName: 'Attack of the Mundane, Part 1: Dish Disaster!',
|
||||
category: 'behavior',
|
||||
resting: true,
|
||||
cronCount: 5
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('sends english item name for purchased spell if itemKey is provided', function() {
|
||||
analyticsData.itemKey = 'seafoam'
|
||||
|
||||
initializedAnalytics.track(event_type, analyticsData);
|
||||
|
||||
expect(amplitudeTrack).to.be.calledOnce;
|
||||
expect(amplitudeTrack).to.be.calledWith({
|
||||
event_type: 'Cron',
|
||||
user_id: 'unique-user-id',
|
||||
platform: 'server',
|
||||
event_properties: {
|
||||
itemKey: 'seafoam',
|
||||
itemName: 'Seafoam',
|
||||
category: 'behavior',
|
||||
resting: true,
|
||||
cronCount: 5
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('sends user data if provided', function() {
|
||||
var stats = { class: 'wizard', exp: 5, gp: 23, hp: 10, lvl: 4, mp: 30 };
|
||||
var user = {
|
||||
stats: stats,
|
||||
contributor: { level: 1 },
|
||||
purchased: { plan: { planId: 'foo-plan' } },
|
||||
flags: {tour: {intro: -2}},
|
||||
habits: [{_id: 'habit'}],
|
||||
dailys: [{_id: 'daily'}],
|
||||
todos: [{_id: 'todo'}],
|
||||
rewards: [{_id: 'reward'}]
|
||||
};
|
||||
|
||||
analyticsData.user = user;
|
||||
|
||||
initializedAnalytics.track(event_type, analyticsData);
|
||||
|
||||
expect(amplitudeTrack).to.be.calledOnce;
|
||||
expect(amplitudeTrack).to.be.calledWith({
|
||||
event_type: 'Cron',
|
||||
user_id: 'unique-user-id',
|
||||
platform: 'server',
|
||||
event_properties: {
|
||||
category: 'behavior',
|
||||
resting: true,
|
||||
cronCount: 5
|
||||
},
|
||||
user_properties: {
|
||||
Class: 'wizard',
|
||||
Experience: 5,
|
||||
Gold: 23,
|
||||
Health: 10,
|
||||
Level: 4,
|
||||
Mana: 30,
|
||||
contributorLevel: 1,
|
||||
subscription: 'foo-plan',
|
||||
tutorialComplete: true,
|
||||
"Number Of Tasks": {
|
||||
todos: 1,
|
||||
dailys: 1,
|
||||
habits: 1,
|
||||
rewards: 1
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
context('Google Analytics', function() {
|
||||
it('tracks event in google analytics', function() {
|
||||
initializedAnalytics.track(event_type, analyticsData);
|
||||
|
||||
expect(googleEvent).to.be.calledOnce;
|
||||
expect(googleEvent).to.be.calledWith({
|
||||
ec: 'behavior',
|
||||
ea: 'Cron'
|
||||
});
|
||||
});
|
||||
|
||||
it('if itemKey property is provided, use as label', function() {
|
||||
analyticsData.itemKey = 'some item';
|
||||
|
||||
initializedAnalytics.track(event_type, analyticsData);
|
||||
|
||||
expect(googleEvent).to.be.calledOnce;
|
||||
expect(googleEvent).to.be.calledWith({
|
||||
ec: 'behavior',
|
||||
ea: 'Cron',
|
||||
el: 'some item'
|
||||
});
|
||||
});
|
||||
|
||||
it('if gaLabel property is provided, use as label (overrides itemKey)', function() {
|
||||
analyticsData.value = 'some value';
|
||||
analyticsData.itemKey = 'some item';
|
||||
analyticsData.gaLabel = 'some label';
|
||||
|
||||
initializedAnalytics.track(event_type, analyticsData);
|
||||
|
||||
expect(googleEvent).to.be.calledOnce;
|
||||
expect(googleEvent).to.be.calledWith({
|
||||
ec: 'behavior',
|
||||
ea: 'Cron',
|
||||
el: 'some label'
|
||||
});
|
||||
});
|
||||
|
||||
it('if goldCost property is provided, use as value', function() {
|
||||
analyticsData.goldCost = 5;
|
||||
|
||||
initializedAnalytics.track(event_type, analyticsData);
|
||||
|
||||
expect(googleEvent).to.be.calledOnce;
|
||||
expect(googleEvent).to.be.calledWith({
|
||||
ec: 'behavior',
|
||||
ea: 'Cron',
|
||||
ev: 5
|
||||
});
|
||||
});
|
||||
|
||||
it('if gemCost property is provided, use as value (overrides goldCost)', function() {
|
||||
analyticsData.gemCost = 7;
|
||||
analyticsData.goldCost = 5;
|
||||
|
||||
initializedAnalytics.track(event_type, analyticsData);
|
||||
|
||||
expect(googleEvent).to.be.calledOnce;
|
||||
expect(googleEvent).to.be.calledWith({
|
||||
ec: 'behavior',
|
||||
ea: 'Cron',
|
||||
ev: 7
|
||||
});
|
||||
});
|
||||
|
||||
it('if gaValue property is provided, use as value (overrides gemCost)', function() {
|
||||
analyticsData.gemCost = 7;
|
||||
analyticsData.gaValue = 5;
|
||||
|
||||
initializedAnalytics.track(event_type, analyticsData);
|
||||
|
||||
expect(googleEvent).to.be.calledOnce;
|
||||
expect(googleEvent).to.be.calledWith({
|
||||
ec: 'behavior',
|
||||
ea: 'Cron',
|
||||
ev: 5
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('trackPurchase', function() {
|
||||
|
||||
var purchaseData;
|
||||
|
||||
var analytics = rewire('../../website/server/libs/api-v2/analytics');
|
||||
var initializedAnalytics;
|
||||
|
||||
beforeEach(function() {
|
||||
analytics.__set__('Amplitude', amplitudeMock);
|
||||
initializedAnalytics = analytics({amplitudeToken: 'token', googleAnalytics: 'token'});
|
||||
analytics.__set__('amplitude.track', amplitudeTrack);
|
||||
analytics.__set__('ga.event', googleEvent);
|
||||
analytics.__set__('ga.transaction', googleTransaction);
|
||||
|
||||
purchaseData = {
|
||||
uuid: 'user-id',
|
||||
sku: 'paypal-checkout',
|
||||
paymentMethod: 'PayPal',
|
||||
itemPurchased: 'Gems',
|
||||
purchaseValue: 8,
|
||||
purchaseType: 'checkout',
|
||||
gift: false,
|
||||
quantity: 1
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
context('Amplitude', function() {
|
||||
|
||||
it('calls amplitude.track', function() {
|
||||
initializedAnalytics.trackPurchase(purchaseData);
|
||||
|
||||
expect(amplitudeTrack).to.be.calledOnce;
|
||||
expect(amplitudeTrack).to.be.calledWith({
|
||||
event_type: 'purchase',
|
||||
user_id: 'user-id',
|
||||
platform: 'server',
|
||||
event_properties: {
|
||||
paymentMethod: 'PayPal',
|
||||
sku: 'paypal-checkout',
|
||||
gift: false,
|
||||
itemPurchased: 'Gems',
|
||||
purchaseType: 'checkout',
|
||||
quantity: 1
|
||||
},
|
||||
revenue: 8
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
context('Google Analytics', function() {
|
||||
|
||||
it('calls ga.event', function() {
|
||||
initializedAnalytics.trackPurchase(purchaseData);
|
||||
|
||||
expect(googleEvent).to.be.calledOnce;
|
||||
expect(googleEvent).to.be.calledWith({
|
||||
ec: 'commerce',
|
||||
ea: 'checkout',
|
||||
el: 'PayPal',
|
||||
ev: 8
|
||||
});
|
||||
});
|
||||
|
||||
it('calls ga.transaction', function() {
|
||||
initializedAnalytics.trackPurchase(purchaseData);
|
||||
|
||||
expect(googleTransaction).to.be.calledOnce;
|
||||
expect(googleTransaction).to.be.calledWith(
|
||||
'user-id',
|
||||
8
|
||||
);
|
||||
expect(googleItem).to.be.calledOnce;
|
||||
expect(googleItem).to.be.calledWith(
|
||||
8,
|
||||
1,
|
||||
'paypal-checkout',
|
||||
'Gems',
|
||||
'checkout'
|
||||
);
|
||||
});
|
||||
|
||||
it('appends gift to variation of ga.transaction.item if gift is true', function() {
|
||||
|
||||
purchaseData.gift = true;
|
||||
initializedAnalytics.trackPurchase(purchaseData);
|
||||
|
||||
expect(googleItem).to.be.calledOnce;
|
||||
expect(googleItem).to.be.calledWith(
|
||||
8,
|
||||
1,
|
||||
'paypal-checkout',
|
||||
'Gems',
|
||||
'checkout - Gift'
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,497 +0,0 @@
|
||||
var sinon = require('sinon');
|
||||
var chai = require("chai");
|
||||
chai.use(require("sinon-chai"));
|
||||
var expect = chai.expect;
|
||||
|
||||
var Bluebird = require('bluebird');
|
||||
var Group = require('../../../website/server/models/group').model;
|
||||
var groupsController = require('../../../website/server/controllers/api-v2/groups');
|
||||
|
||||
describe('Groups Controller', function() {
|
||||
var utils = require('../../../website/server/libs/api-v2/utils');
|
||||
|
||||
describe('#invite', function() {
|
||||
var res, req, user, group;
|
||||
|
||||
beforeEach(function() {
|
||||
group = {
|
||||
_id: 'group-id',
|
||||
name: 'group-name',
|
||||
type: 'party',
|
||||
members: [
|
||||
'user-id',
|
||||
'another-user'
|
||||
],
|
||||
save: sinon.stub().yields(),
|
||||
markModified: sinon.spy()
|
||||
};
|
||||
|
||||
user = {
|
||||
_id: 'user-id',
|
||||
name: 'inviter',
|
||||
email: 'inviter@example.com',
|
||||
save: sinon.stub().yields(),
|
||||
markModified: sinon.spy()
|
||||
};
|
||||
|
||||
res = {
|
||||
locals: {
|
||||
group: group,
|
||||
user: user
|
||||
},
|
||||
json: sinon.stub(),
|
||||
sendStatus: sinon.stub()
|
||||
};
|
||||
|
||||
req = {
|
||||
body: {}
|
||||
};
|
||||
});
|
||||
|
||||
context('uuids', function() {
|
||||
beforeEach(function() {
|
||||
req.body.uuids = ['invited-user'];
|
||||
});
|
||||
|
||||
it('returns 400 if user not found');
|
||||
|
||||
it('returns a 400 if user is already in the group');
|
||||
|
||||
it('retuns 400 if user was already invited to that group');
|
||||
|
||||
it('returns 400 if user is already pending an invitation');
|
||||
|
||||
it('returns 400 is user is already in another party');
|
||||
|
||||
it('emails invited user');
|
||||
|
||||
it('does not email invited user if email preference is set to false');
|
||||
});
|
||||
|
||||
context('emails', function() {
|
||||
var EmailUnsubscription = require('../../../website/server/models/emailUnsubscription').model;
|
||||
var execStub, selectStub;
|
||||
|
||||
beforeEach(function() {
|
||||
sinon.stub(utils, 'encrypt').returns('http://link.com');
|
||||
sinon.stub(utils, 'getUserInfo').returns({
|
||||
name: user.name,
|
||||
email: user.email
|
||||
});
|
||||
execStub = sinon.stub();
|
||||
selectStub = sinon.stub().returns({
|
||||
exec: execStub
|
||||
});
|
||||
sinon.stub(User, 'findOne').returns({
|
||||
select: selectStub
|
||||
});
|
||||
sinon.stub(EmailUnsubscription, 'findOne');
|
||||
sinon.stub(utils, 'txnEmail');
|
||||
|
||||
req.body.emails = [{email: 'user@example.com', name: 'user'}];
|
||||
});
|
||||
|
||||
afterEach(function() {
|
||||
User.findOne.restore();
|
||||
EmailUnsubscription.findOne.restore();
|
||||
utils.encrypt.restore();
|
||||
utils.getUserInfo.restore();
|
||||
utils.txnEmail.restore();
|
||||
});
|
||||
|
||||
it('emails user with invite', function() {
|
||||
execStub.yields(null, null);
|
||||
EmailUnsubscription.findOne.yields(null, null);
|
||||
|
||||
groupsController.invite(req, res);
|
||||
|
||||
expect(utils.txnEmail).to.be.calledOnce;
|
||||
expect(utils.txnEmail).to.be.calledWith(
|
||||
{ email: 'user@example.com', name: 'user' },
|
||||
'invite-friend',
|
||||
[
|
||||
{ name: 'LINK', content: '?partyInvite=http://link.com' },
|
||||
{ name: 'INVITER', content: 'inviter' }
|
||||
]
|
||||
);
|
||||
});
|
||||
|
||||
it('does not email user if user is on unsubscribe list', function() {
|
||||
EmailUnsubscription.findOne.yields(null, {_id: 'on-list'});
|
||||
|
||||
expect(utils.txnEmail).to.not.be.called;
|
||||
});
|
||||
|
||||
it('checks if a user with provided email already exists');
|
||||
});
|
||||
|
||||
context('others', function() {
|
||||
it ('returns a 400 error', function() {
|
||||
groupsController.invite(req, res);
|
||||
|
||||
expect(res.json).to.be.calledOnce;
|
||||
expect(res.json).to.be.calledWith(
|
||||
400,
|
||||
{ err: 'Can invite only by email or uuid' }
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('#leave', function() {
|
||||
var res, req, user, group;
|
||||
|
||||
beforeEach(function() {
|
||||
group = {
|
||||
_id: 'group-id',
|
||||
type: 'party',
|
||||
members: [
|
||||
'user-id',
|
||||
'another-user'
|
||||
],
|
||||
save: sinon.stub().yields(),
|
||||
leave: sinon.stub().yields(),
|
||||
markModified: sinon.spy()
|
||||
};
|
||||
|
||||
user = {
|
||||
_id: 'user-id',
|
||||
save: sinon.stub().yields(),
|
||||
markModified: sinon.spy()
|
||||
};
|
||||
|
||||
res = {
|
||||
locals: {
|
||||
group: group,
|
||||
user: user
|
||||
},
|
||||
json: sinon.stub(),
|
||||
sendStatus: sinon.stub()
|
||||
};
|
||||
|
||||
req = {
|
||||
query: { keep: 'keep' }
|
||||
};
|
||||
});
|
||||
|
||||
context('party', function() {
|
||||
beforeEach(function() {
|
||||
group.type = 'party';
|
||||
});
|
||||
|
||||
it('prevents user from leaving party if quest is active and part of the active members list', function() {
|
||||
group.quest = {
|
||||
active: true,
|
||||
members: {
|
||||
another_user: true,
|
||||
yet_another_user: null,
|
||||
'user-id': true
|
||||
}
|
||||
};
|
||||
|
||||
groupsController.leave(req, res);
|
||||
|
||||
expect(group.leave).to.not.be.called;
|
||||
expect(res.json).to.be.calledOnce;
|
||||
expect(res.json).to.be.calledWith(403, 'You cannot leave party during an active quest. Please leave the quest first.');
|
||||
});
|
||||
|
||||
it('prevents quest leader from leaving a party if they have started a quest', function() {
|
||||
group.quest = {
|
||||
active: false,
|
||||
leader: 'user-id'
|
||||
};
|
||||
|
||||
groupsController.leave(req, res);
|
||||
|
||||
expect(group.leave).to.not.be.called;
|
||||
expect(res.json).to.be.calledOnce;
|
||||
expect(res.json).to.be.calledWith(403, 'You cannot leave your party when you have started a quest. Abort the quest first.');
|
||||
});
|
||||
|
||||
it('leaves party if quest is not active', function() {
|
||||
group.quest = {
|
||||
active: false,
|
||||
members: {
|
||||
another_user: true,
|
||||
yet_another_user: null,
|
||||
'user-id': null
|
||||
}
|
||||
};
|
||||
|
||||
groupsController.leave(req, res);
|
||||
|
||||
expect(group.leave).to.be.calledOnce;
|
||||
expect(res.json).to.not.be.called;
|
||||
});
|
||||
|
||||
it('leaves party if quest is active, but user is not part of quest', function() {
|
||||
group.quest = {
|
||||
active: true,
|
||||
members: {
|
||||
another_user: true,
|
||||
yet_another_user: null,
|
||||
'user-id': null
|
||||
}
|
||||
};
|
||||
|
||||
groupsController.leave(req, res);
|
||||
|
||||
expect(group.leave).to.be.calledOnce;
|
||||
expect(res.json).to.not.be.called;
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('#questLeave', function() {
|
||||
var res, req, group, user, saveSpy;
|
||||
|
||||
beforeEach(function() {
|
||||
sinon.stub(Q, 'all').returns({
|
||||
done: sinon.stub().yields()
|
||||
});
|
||||
group = {
|
||||
_id: 'group-id',
|
||||
type: 'party',
|
||||
quest: {
|
||||
leader : 'another-user',
|
||||
active: true,
|
||||
members: {
|
||||
'user-id': true,
|
||||
'another-user': true
|
||||
},
|
||||
key : 'vice1',
|
||||
progress : {
|
||||
hp : 364,
|
||||
collect : {}
|
||||
}
|
||||
},
|
||||
save: sinon.stub().yields(),
|
||||
markModified: sinon.spy()
|
||||
};
|
||||
|
||||
user = {
|
||||
_id: 'user-id',
|
||||
party : {
|
||||
quest : {
|
||||
key : 'vice1',
|
||||
progress : {
|
||||
up : 50,
|
||||
down : 0,
|
||||
collectedItems : {}
|
||||
},
|
||||
completed : null,
|
||||
RSVPNeeded : false
|
||||
}
|
||||
},
|
||||
save: sinon.stub().yields(),
|
||||
markModified: sinon.spy()
|
||||
};
|
||||
|
||||
res = {
|
||||
locals: {
|
||||
group: group,
|
||||
user: user
|
||||
},
|
||||
json: sinon.stub(),
|
||||
sendStatus: sinon.stub()
|
||||
};
|
||||
|
||||
req = { };
|
||||
});
|
||||
|
||||
afterEach(function() {
|
||||
Promise.all.restore();
|
||||
});
|
||||
|
||||
context('error conditions', function() {
|
||||
it('errors if quest is not active', function() {
|
||||
group.quest.active = false;
|
||||
|
||||
groupsController.questLeave(req, res);
|
||||
|
||||
expect(res.json).to.be.calledOnce;
|
||||
expect(res.json).to.be.calledWith(
|
||||
404,
|
||||
{ err: 'No active quest to leave' }
|
||||
);
|
||||
});
|
||||
|
||||
it('errors if user is not part of quest', function() {
|
||||
delete group.quest.members[user._id];
|
||||
|
||||
groupsController.questLeave(req, res);
|
||||
|
||||
expect(res.json).to.be.calledOnce;
|
||||
expect(res.json).to.be.calledWith(
|
||||
403,
|
||||
{ err: 'You are not part of the quest' }
|
||||
);
|
||||
});
|
||||
|
||||
it('does not allow quest leader to leave quest', function() {
|
||||
group.quest.leader = 'user-id';
|
||||
|
||||
groupsController.questLeave(req, res);
|
||||
|
||||
expect(res.json).to.be.calledOnce;
|
||||
expect(res.json).to.be.calledWith(
|
||||
403,
|
||||
{ err: 'Quest leader cannot leave quest' }
|
||||
);
|
||||
});
|
||||
|
||||
it('sends 500 if group cannot save', function() {
|
||||
Promise.all.returns({
|
||||
done: sinon.stub().callsArgWith(1, {err: 'save error'})
|
||||
});
|
||||
var nextSpy = sinon.spy();
|
||||
|
||||
groupsController.questLeave(req, res, nextSpy);
|
||||
|
||||
expect(res.json).to.not.be.called;
|
||||
expect(nextSpy).to.be.calledOnce;
|
||||
expect(nextSpy).to.be.calledWith({err: 'save error'});
|
||||
});
|
||||
});
|
||||
|
||||
context('success', function() {
|
||||
it('removes user from quest', function() {
|
||||
expect(group.quest.members[user._id]).to.exist;
|
||||
|
||||
groupsController.questLeave(req, res);
|
||||
|
||||
expect(group.quest.members[user._id]).to.not.exist;
|
||||
});
|
||||
|
||||
it('scrubs quest data from user', function() {
|
||||
user.party.quest.progress = {
|
||||
up: 100,
|
||||
down: 32,
|
||||
collectedItems: 16,
|
||||
collect: {
|
||||
foo: 12,
|
||||
bar: 4
|
||||
}
|
||||
};
|
||||
|
||||
groupsController.questLeave(req, res);
|
||||
|
||||
expect(user.party.quest.key).to.not.exist;
|
||||
expect(user.party.quest.progress).to.eql({
|
||||
up: 0,
|
||||
down: 0,
|
||||
collectedItems: 0,
|
||||
});
|
||||
});
|
||||
|
||||
it('sends back 204 on success', function() {
|
||||
groupsController.questLeave(req, res);
|
||||
|
||||
expect(res.sendStatus).to.be.calledOnce;
|
||||
expect(res.sendStatus).to.be.calledWith(204);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('#removeMember', function() {
|
||||
var req, res, group, user;
|
||||
|
||||
beforeEach(function() {
|
||||
user = { _id: 'user-id' };
|
||||
group = {
|
||||
_id: 'group-id',
|
||||
leader: 'user-id',
|
||||
members: ['user-id', 'member-to-boot', 'another-user']
|
||||
}
|
||||
res = {
|
||||
locals: {
|
||||
user: user,
|
||||
group: group
|
||||
},
|
||||
sendStatus: sinon.stub()
|
||||
};
|
||||
req = {
|
||||
query: {
|
||||
uuid: 'member-to-boot'
|
||||
}
|
||||
};
|
||||
|
||||
sinon.stub(Group, 'update');
|
||||
sinon.stub(User, 'update');
|
||||
sinon.stub(User, 'findById');
|
||||
});
|
||||
|
||||
afterEach(function() {
|
||||
Group.update.restore();
|
||||
User.update.restore();
|
||||
User.findById.restore();
|
||||
});
|
||||
|
||||
context('quest behavior', function() {
|
||||
it('removes quest from party if booted member was quest leader', function() {
|
||||
group.quest = {
|
||||
leader: 'member-to-boot',
|
||||
active: true,
|
||||
members: {
|
||||
'user-id': true,
|
||||
'leader-id': true,
|
||||
'member-to-boot': true
|
||||
},
|
||||
key: 'whale'
|
||||
}
|
||||
|
||||
groupsController.removeMember(req, res);
|
||||
|
||||
expect(Group.update).to.be.calledOnce;
|
||||
expect(Group.update).to.be.calledWith(
|
||||
{ _id: 'group-id'},
|
||||
{
|
||||
'$inc': { memberCount: -1 },
|
||||
'$pull': { members: 'member-to-boot' },
|
||||
'$set': { quest: {key: null, leader: null} }
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
it('returns quest scroll to booted member if booted member was leader of quest', function() {
|
||||
Group.update.yields();
|
||||
var bootedMember = {
|
||||
_id: 'member-to-boot',
|
||||
apiToken: 'api',
|
||||
preferences: {
|
||||
emailNotifications: {
|
||||
kickedGroup: false
|
||||
}
|
||||
}
|
||||
};
|
||||
User.findById.yields(null, bootedMember);
|
||||
User.update.returns({
|
||||
exec: sinon.stub()
|
||||
});
|
||||
|
||||
group.quest = {
|
||||
leader: 'member-to-boot',
|
||||
active: true,
|
||||
members: {
|
||||
'user-id': true,
|
||||
'leader-id': true,
|
||||
'member-to-boot': true
|
||||
},
|
||||
key: 'whale'
|
||||
}
|
||||
|
||||
groupsController.removeMember(req, res);
|
||||
|
||||
expect(User.update).to.be.calledOnce;
|
||||
expect(User.update).to.be.calledWith(
|
||||
{ _id: 'member-to-boot', apiToken: 'api' },
|
||||
{
|
||||
'$unset': { 'newMessages.group-id': ''},
|
||||
'$inc': { 'items.quests.whale': 1 }
|
||||
}
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,617 +0,0 @@
|
||||
var sinon = require('sinon');
|
||||
var chai = require("chai")
|
||||
chai.use(require("sinon-chai"))
|
||||
var expect = chai.expect
|
||||
var rewire = require('rewire');
|
||||
|
||||
var userController = rewire('../../../website/server/controllers/api-v2/user');
|
||||
|
||||
describe('User Controller', function() {
|
||||
|
||||
describe('score', function() {
|
||||
var req, res, user;
|
||||
|
||||
beforeEach(function() {
|
||||
user = {
|
||||
_id: 'user-id',
|
||||
_tmp: {
|
||||
drop: true
|
||||
},
|
||||
_statsComputed: {
|
||||
maxMP: 100
|
||||
},
|
||||
ops: {
|
||||
score: sinon.stub(),
|
||||
addTask: sinon.stub()
|
||||
},
|
||||
stats: {
|
||||
lvl: 10,
|
||||
hp: 43,
|
||||
mp: 50
|
||||
},
|
||||
preferences: {
|
||||
webhooks: {
|
||||
'some-id': {
|
||||
sort: 0,
|
||||
id: 'some-id',
|
||||
enabled: true,
|
||||
url: 'http://example.org/endpoint'
|
||||
}
|
||||
}
|
||||
},
|
||||
save: sinon.stub(),
|
||||
tasks: {
|
||||
task_id: {
|
||||
id: 'task_id',
|
||||
type: 'todo'
|
||||
}
|
||||
}
|
||||
};
|
||||
req = {
|
||||
language: 'en',
|
||||
params: {
|
||||
id: 'task_id',
|
||||
direction: 'up'
|
||||
}
|
||||
};
|
||||
res = {
|
||||
locals: { user: user },
|
||||
json: sinon.spy()
|
||||
};
|
||||
});
|
||||
|
||||
context('early return conditions', function() {
|
||||
it('sends an error when no id is provided', function() {
|
||||
delete req.params.id;
|
||||
|
||||
userController.score(req, res);
|
||||
|
||||
expect(res.json).to.be.calledOnce;
|
||||
expect(res.json).to.be.calledWith(400, {err: ':id required'});
|
||||
});
|
||||
|
||||
it('sends an error when no direction is provided', function() {
|
||||
delete req.params.direction;
|
||||
|
||||
userController.score(req, res);
|
||||
|
||||
expect(res.json).to.be.calledOnce;
|
||||
expect(res.json).to.be.calledWith(400, {err: ":direction must be 'up' or 'down'"});
|
||||
});
|
||||
|
||||
it('calls next when direction is "unlink"', function() {
|
||||
req.params.direction = 'unlink';
|
||||
var nextSpy = sinon.spy();
|
||||
|
||||
userController.score(req, res, nextSpy);
|
||||
|
||||
expect(nextSpy).to.be.calledOnce;
|
||||
});
|
||||
|
||||
it('calls next when direction is "sort"', function() {
|
||||
req.params.direction = 'sort';
|
||||
var nextSpy = sinon.spy();
|
||||
|
||||
userController.score(req, res, nextSpy);
|
||||
|
||||
expect(nextSpy).to.be.calledOnce;
|
||||
});
|
||||
});
|
||||
|
||||
context('task exists', function() {
|
||||
it('sets todo to completed if direction is "up"', function() {
|
||||
req.params.direction = 'up';
|
||||
req.params.id = 'todo_id';
|
||||
user.tasks.todo_id = {
|
||||
_id: 'todo_id',
|
||||
type: 'todo',
|
||||
completed: false
|
||||
};
|
||||
|
||||
userController.score(req, res);
|
||||
|
||||
expect(user.tasks.todo_id.completed).to.eql(true);
|
||||
});
|
||||
|
||||
it('sets todo to not completed if direction is "down"', function() {
|
||||
req.params.direction = 'down';
|
||||
req.params.id = 'todo_id';
|
||||
user.tasks.todo_id = {
|
||||
_id: 'todo_id',
|
||||
type: 'todo',
|
||||
completed: true
|
||||
};
|
||||
|
||||
userController.score(req, res);
|
||||
|
||||
expect(user.tasks.todo_id.completed).to.eql(false);
|
||||
});
|
||||
|
||||
it('sets daily to completed if direction is "up"', function() {
|
||||
req.params.direction = 'up';
|
||||
req.params.id = 'daily_id';
|
||||
user.tasks.daily_id = {
|
||||
_id: 'daily_id',
|
||||
type: 'daily',
|
||||
completed: false
|
||||
};
|
||||
|
||||
userController.score(req, res);
|
||||
|
||||
expect(user.tasks.daily_id.completed).to.eql(true);
|
||||
});
|
||||
|
||||
it('sets daily to not completed if direction is "down"', function() {
|
||||
req.params.direction = 'down';
|
||||
req.params.id = 'daily_id';
|
||||
user.tasks.daily_id = {
|
||||
_id: 'daily_id',
|
||||
type: 'daily',
|
||||
completed: true
|
||||
};
|
||||
|
||||
userController.score(req, res);
|
||||
|
||||
expect(user.tasks.daily_id.completed).to.eql(false);
|
||||
});
|
||||
});
|
||||
|
||||
context('task does not exist', function() {
|
||||
it('creates the task', function() {
|
||||
user.ops.addTask.returns({id: 'an-id-that-does-not-exist'});
|
||||
|
||||
req.params.id = 'an-id-that-does-not-exist-yet';
|
||||
req.body = {
|
||||
type: 'todo',
|
||||
text: 'some todo',
|
||||
notes: 'some notes'
|
||||
}
|
||||
|
||||
userController.score(req, res);
|
||||
|
||||
expect(user.ops.addTask).to.be.calledOnce;
|
||||
expect(user.ops.addTask).to.be.calledWith({
|
||||
body: {
|
||||
id: 'an-id-that-does-not-exist-yet',
|
||||
completed: true,
|
||||
type: 'todo',
|
||||
text: 'some todo',
|
||||
notes: 'some notes'
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('provides a default note if no note is provided', function() {
|
||||
user.ops.addTask.returns({id: 'an-id-that-does-not-exist'});
|
||||
|
||||
req.params.id = 'an-id-that-does-not-exist-yet';
|
||||
req.body = {
|
||||
type: 'todo',
|
||||
text: 'some todo'
|
||||
}
|
||||
|
||||
userController.score(req, res);
|
||||
|
||||
expect(user.ops.addTask).to.be.calledOnce;
|
||||
expect(user.ops.addTask).to.be.calledWith({
|
||||
body: {
|
||||
id: 'an-id-that-does-not-exist-yet',
|
||||
completed: true,
|
||||
type: 'todo',
|
||||
text: 'some todo',
|
||||
notes: "This task was created by a third-party service. Feel free to edit, it won't harm the connection to that service. Additionally, multiple services may piggy-back off this task."
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('todo task is completed if direction is "up"', function() {
|
||||
user.ops.addTask.returns({id: 'an-id-that-does-not-exist'});
|
||||
|
||||
req.params.direction = 'up';
|
||||
req.params.id = 'an-id-that-does-not-exist-yet';
|
||||
req.body = {
|
||||
type: 'todo',
|
||||
text: 'some todo',
|
||||
notes: 'some notes'
|
||||
}
|
||||
|
||||
userController.score(req, res);
|
||||
|
||||
expect(user.ops.addTask).to.be.calledOnce;
|
||||
expect(user.ops.addTask).to.be.calledWith({
|
||||
body: {
|
||||
id: 'an-id-that-does-not-exist-yet',
|
||||
completed: true,
|
||||
type: 'todo',
|
||||
text: 'some todo',
|
||||
notes: 'some notes'
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('todo task is not completed if direction is "down"', function() {
|
||||
user.ops.addTask.returns({id: 'an-id-that-does-not-exist'});
|
||||
|
||||
req.params.direction = 'down';
|
||||
req.params.id = 'an-id-that-does-not-exist-yet';
|
||||
req.body = {
|
||||
type: 'todo',
|
||||
text: 'some todo',
|
||||
notes: 'some notes'
|
||||
}
|
||||
|
||||
userController.score(req, res);
|
||||
|
||||
expect(user.ops.addTask).to.be.calledOnce;
|
||||
expect(user.ops.addTask).to.be.calledWith({
|
||||
body: {
|
||||
id: 'an-id-that-does-not-exist-yet',
|
||||
completed: false,
|
||||
type: 'todo',
|
||||
text: 'some todo',
|
||||
notes: 'some notes'
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('daily task is completed if direction is "up"', function() {
|
||||
user.ops.addTask.returns({id: 'an-id-that-does-not-exist'});
|
||||
|
||||
req.params.direction = 'up';
|
||||
req.params.id = 'an-id-that-does-not-exist-yet';
|
||||
req.body = {
|
||||
type: 'daily',
|
||||
text: 'some daily',
|
||||
notes: 'some notes'
|
||||
}
|
||||
|
||||
userController.score(req, res);
|
||||
|
||||
expect(user.ops.addTask).to.be.calledOnce;
|
||||
expect(user.ops.addTask).to.be.calledWith({
|
||||
body: {
|
||||
id: 'an-id-that-does-not-exist-yet',
|
||||
completed: true,
|
||||
type: 'daily',
|
||||
text: 'some daily',
|
||||
notes: 'some notes'
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('daily task is not completed if direction is "down"', function() {
|
||||
user.ops.addTask.returns({id: 'an-id-that-does-not-exist'});
|
||||
|
||||
req.params.direction = 'down';
|
||||
req.params.id = 'an-id-that-does-not-exist-yet';
|
||||
req.body = {
|
||||
type: 'daily',
|
||||
text: 'some daily',
|
||||
notes: 'some notes'
|
||||
}
|
||||
|
||||
userController.score(req, res);
|
||||
|
||||
expect(user.ops.addTask).to.be.calledOnce;
|
||||
expect(user.ops.addTask).to.be.calledWith({
|
||||
body: {
|
||||
id: 'an-id-that-does-not-exist-yet',
|
||||
completed: false,
|
||||
type: 'daily',
|
||||
text: 'some daily',
|
||||
notes: 'some notes'
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
context('whether task exists or it does not exist', function() {
|
||||
it('calls user.ops.score', function() {
|
||||
userController.score(req, res);
|
||||
|
||||
expect(user.ops.score).to.be.calledOnce;
|
||||
expect(user.ops.score).to.be.calledWith({
|
||||
params: {id: 'task_id', direction: 'up'},
|
||||
language: 'en'
|
||||
});
|
||||
});
|
||||
|
||||
it('saves user', function() {
|
||||
userController.score(req, res);
|
||||
|
||||
expect(user.save).to.be.calledOnce;
|
||||
});
|
||||
});
|
||||
|
||||
context('user.save callback', function() {
|
||||
var savedUser;
|
||||
beforeEach(function() {
|
||||
savedUser = {
|
||||
stats: user.stats
|
||||
}
|
||||
|
||||
user.save.yields(null, savedUser);
|
||||
|
||||
user.ops.score.returns(1.5);
|
||||
});
|
||||
|
||||
it('calls next if saving yields an error', function() {
|
||||
var nextSpy = sinon.spy();
|
||||
user.save.yields('an error');
|
||||
|
||||
userController.score(req, res, nextSpy);
|
||||
|
||||
expect(nextSpy).to.be.calledOnce;
|
||||
expect(nextSpy).to.be.calledWith('an error');
|
||||
});
|
||||
|
||||
it('sends some user data with res.json', function() {
|
||||
userController.score(req, res);
|
||||
|
||||
expect(res.json).to.be.calledOnce;
|
||||
expect(res.json).to.be.calledWith(200, {
|
||||
delta: 1.5,
|
||||
_tmp: user._tmp,
|
||||
lvl: 10,
|
||||
hp: 43,
|
||||
mp: 50
|
||||
});
|
||||
});
|
||||
|
||||
it('sends webhooks', function() {
|
||||
var webhook = require('../../../website/server/libs/webhook');
|
||||
sinon.spy(webhook, 'sendTaskWebhook');
|
||||
|
||||
userController.score(req, res);
|
||||
|
||||
expect(webhook.sendTaskWebhook).to.be.calledOnce;
|
||||
expect(webhook.sendTaskWebhook).to.be.calledWith(
|
||||
user.preferences.webhooks,
|
||||
{
|
||||
task: {
|
||||
delta: 1.5,
|
||||
details: { completed: true, id: "task_id", type: "todo" },
|
||||
direction: "up"
|
||||
},
|
||||
user: {
|
||||
_id: "user-id",
|
||||
_tmp: { drop: true },
|
||||
stats: { hp: 43, lvl: 10, maxHealth: 50, maxMP: 100, mp: 50, toNextLevel: 260 }
|
||||
}
|
||||
}
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
context('save callback dealing with non challenge tasks', function() {
|
||||
var Challenge = require('../../../website/server/models/challenge').model;
|
||||
|
||||
beforeEach(function() {
|
||||
user.save.yields(null, user);
|
||||
sinon.stub(Challenge, 'findById');
|
||||
req.params.id = 'non_active_challenge_task';
|
||||
user.tasks.non_active_challenge_task = {
|
||||
id: 'non_active_challenge_task',
|
||||
challenge: { id: 'some-id' },
|
||||
type: 'todo'
|
||||
}
|
||||
});
|
||||
|
||||
afterEach(function() {
|
||||
Challenge.findById.restore();
|
||||
});
|
||||
|
||||
it('returns early if not a challenge', function() {
|
||||
delete user.tasks.non_active_challenge_task.challenge;
|
||||
|
||||
userController.score(req, res);
|
||||
|
||||
expect(Challenge.findById).to.not.be.called;
|
||||
});
|
||||
|
||||
it('returns early if no challenge id', function() {
|
||||
delete user.tasks.non_active_challenge_task.challenge.id;
|
||||
|
||||
userController.score(req, res);
|
||||
|
||||
expect(Challenge.findById).to.not.be.called;
|
||||
});
|
||||
|
||||
it('returns early if challenge is broken', function() {
|
||||
user.tasks.non_active_challenge_task.challenge.broken = true;
|
||||
|
||||
userController.score(req, res);
|
||||
|
||||
expect(Challenge.findById).to.not.be.called;
|
||||
});
|
||||
|
||||
it('returns early if task is a reward', function() {
|
||||
user.tasks.non_active_challenge_task.type = 'reward';
|
||||
|
||||
userController.score(req, res);
|
||||
|
||||
expect(Challenge.findById).to.not.be.called;
|
||||
});
|
||||
|
||||
it('calls next if there is an error looking up challenge', function() {
|
||||
Challenge.findById.yields('an error');
|
||||
var nextSpy = sinon.spy();
|
||||
|
||||
userController.score(req, res, nextSpy);
|
||||
|
||||
expect(Challenge.findById).to.be.calledOnce;
|
||||
expect(nextSpy).to.be.calledOnce;
|
||||
expect(nextSpy).to.be.calledWith('an error');
|
||||
});
|
||||
});
|
||||
|
||||
context('save callback dealing with challenge tasks', function() {
|
||||
var Challenge = require('../../../website/server/models/challenge').model;
|
||||
var chal;
|
||||
|
||||
beforeEach(function() {
|
||||
chal = {
|
||||
id: 'id',
|
||||
tasks: {
|
||||
active_challenge_task: { id: 'active_challenge_task', value: 1 }
|
||||
},
|
||||
syncToUser: sinon.spy(),
|
||||
save: sinon.spy()
|
||||
};
|
||||
user.save.yields(null, user);
|
||||
user.ops.score.returns(1.4);
|
||||
req.params.id = 'active_challenge_task';
|
||||
user.tasks.active_challenge_task = {
|
||||
id: 'active_challenge_task',
|
||||
challenge: { id: 'challenge_id' },
|
||||
type: 'todo'
|
||||
};
|
||||
|
||||
sinon.stub(Challenge, 'findById');
|
||||
});
|
||||
|
||||
afterEach(function() {
|
||||
Challenge.findById.restore();
|
||||
});
|
||||
|
||||
xit('sets challenge as broken if no challenge can be found', function() {
|
||||
Challenge.findById.yields(null, null);
|
||||
|
||||
userController.score(req, res);
|
||||
|
||||
expect(Challenge.findById).to.be.calledOnce;
|
||||
expect(user.tasks.active_challenge_task.challenge.broken).to.eql('CHALLENGE_DELETED');
|
||||
});
|
||||
|
||||
it('notifies user if task has been deleted from challenge', function() {
|
||||
delete chal.tasks.active_challenge_task;
|
||||
Challenge.findById.yields(null, chal);
|
||||
|
||||
userController.score(req, res);
|
||||
|
||||
expect(Challenge.findById).to.be.calledOnce;
|
||||
expect(chal.syncToUser).to.be.calledOnce;
|
||||
});
|
||||
|
||||
it('changes task value by delta', function() {
|
||||
Challenge.findById.yields(null, chal);
|
||||
|
||||
userController.score(req, res);
|
||||
|
||||
expect(Challenge.findById).to.be.calledOnce;
|
||||
expect(chal.tasks.active_challenge_task.value).to.be.eql(2.4);
|
||||
});
|
||||
|
||||
it('adds history if task is a habit', function() {
|
||||
chal.tasks.active_challenge_task = {
|
||||
id: 'active_challenge_task',
|
||||
type: 'habit',
|
||||
value: 1,
|
||||
history: [{value: 1, date: 1234}]
|
||||
};
|
||||
|
||||
Challenge.findById.yields(null, chal);
|
||||
|
||||
userController.score(req, res);
|
||||
|
||||
expect(Challenge.findById).to.be.calledOnce;
|
||||
|
||||
var historyEvent = chal.tasks.active_challenge_task.history[1];
|
||||
|
||||
expect(historyEvent.value).to.eql(2.4);
|
||||
expect(historyEvent.date).to.be.closeTo(+new Date, 10);
|
||||
});
|
||||
|
||||
it('adds history if task is a daily', function() {
|
||||
chal.tasks.active_challenge_task = {
|
||||
id: 'active_challenge_task',
|
||||
type: 'daily',
|
||||
value: 1,
|
||||
history: [{value: 1, date: 1234}]
|
||||
};
|
||||
|
||||
Challenge.findById.yields(null, chal);
|
||||
|
||||
userController.score(req, res);
|
||||
|
||||
expect(Challenge.findById).to.be.calledOnce;
|
||||
|
||||
var historyEvent = chal.tasks.active_challenge_task.history[1];
|
||||
|
||||
expect(historyEvent.value).to.eql(2.4);
|
||||
expect(historyEvent.date).to.be.closeTo(+new Date, 10);
|
||||
});
|
||||
|
||||
it('saves the challenge data', function() {
|
||||
Challenge.findById.yields(null, chal);
|
||||
|
||||
userController.score(req, res);
|
||||
|
||||
expect(Challenge.findById).to.be.calledOnce;
|
||||
expect(chal.save).to.be.calledOnce;
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('#addTenGems', function() {
|
||||
var req, res, user;
|
||||
|
||||
beforeEach(function() {
|
||||
user = {
|
||||
_id: 'user-id',
|
||||
balance: 5,
|
||||
save: sinon.stub().yields()
|
||||
};
|
||||
req = { };
|
||||
res = {
|
||||
locals: { user: user },
|
||||
send: sinon.spy()
|
||||
};
|
||||
});
|
||||
|
||||
it('adds 2.5 to user balance', function() {
|
||||
userController.addTenGems(req, res);
|
||||
|
||||
expect(user.balance).to.eql(7.5);
|
||||
expect(user.save).to.be.calledOnce;
|
||||
});
|
||||
|
||||
it('sends back 204', function() {
|
||||
userController.addTenGems(req, res);
|
||||
|
||||
expect(res.sendStatus).to.be.calledOnce;
|
||||
expect(res.sendStatus).to.be.calledWith(204);
|
||||
});
|
||||
});
|
||||
|
||||
describe('#addHourglass', function() {
|
||||
var req, res, user;
|
||||
|
||||
beforeEach(function() {
|
||||
user = {
|
||||
_id: 'user-id',
|
||||
purchased: { plan: { consecutive: { trinkets: 3 } } },
|
||||
save: sinon.stub().yields()
|
||||
};
|
||||
req = { };
|
||||
res = {
|
||||
locals: { user: user },
|
||||
send: sinon.spy()
|
||||
};
|
||||
});
|
||||
|
||||
it('adds an hourglass to user', function() {
|
||||
userController.addHourglass(req, res);
|
||||
|
||||
expect(user.purchased.plan.consecutive.trinkets).to.eql(4);
|
||||
expect(user.save).to.be.calledOnce;
|
||||
});
|
||||
|
||||
it('sends back 204', function() {
|
||||
userController.addHourglass(req, res);
|
||||
|
||||
expect(res.sendStatus).to.be.calledOnce;
|
||||
expect(res.sendStatus).to.be.calledWith(204);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,139 +0,0 @@
|
||||
var sinon = require('sinon');
|
||||
var chai = require("chai")
|
||||
chai.use(require("sinon-chai"))
|
||||
var expect = chai.expect
|
||||
var rewire = require('rewire');
|
||||
|
||||
var webhook = rewire('../../website/server/libs/api-v2/webhook');
|
||||
|
||||
describe('webhooks', function() {
|
||||
var postSpy;
|
||||
|
||||
beforeEach(function() {
|
||||
postSpy = sinon.stub();
|
||||
webhook.__set__('request.post', postSpy);
|
||||
});
|
||||
|
||||
describe('sendTaskWebhook', function() {
|
||||
var task = {
|
||||
details: { _id: 'task-id' },
|
||||
delta: 1.4,
|
||||
direction: 'up'
|
||||
};
|
||||
|
||||
var data = {
|
||||
task: task,
|
||||
user: { _id: 'user-id' }
|
||||
};
|
||||
|
||||
it('does not send if no webhook endpoints exist', function() {
|
||||
var webhooks = { };
|
||||
|
||||
webhook.sendTaskWebhook(webhooks, data);
|
||||
|
||||
expect(postSpy).to.not.be.called;
|
||||
});
|
||||
|
||||
it('does not send if no webhooks are enabled', function() {
|
||||
var webhooks = {
|
||||
'some-id': {
|
||||
sort: 0,
|
||||
id: 'some-id',
|
||||
enabled: false,
|
||||
url: 'http://example.org/endpoint'
|
||||
}
|
||||
};
|
||||
|
||||
webhook.sendTaskWebhook(webhooks, data);
|
||||
|
||||
expect(postSpy).to.not.be.called;
|
||||
});
|
||||
|
||||
it('does not send if webhook url is not valid', function() {
|
||||
var webhooks = {
|
||||
'some-id': {
|
||||
sort: 0,
|
||||
id: 'some-id',
|
||||
enabled: true,
|
||||
url: 'http://malformedurl/endpoint'
|
||||
}
|
||||
};
|
||||
|
||||
webhook.sendTaskWebhook(webhooks, data);
|
||||
|
||||
expect(postSpy).to.not.be.called;
|
||||
});
|
||||
|
||||
it('sends task direction, task, task delta, and abridged user data', function() {
|
||||
var webhooks = {
|
||||
'some-id': {
|
||||
sort: 0,
|
||||
id: 'some-id',
|
||||
enabled: true,
|
||||
url: 'http://example.org/endpoint'
|
||||
}
|
||||
};
|
||||
|
||||
webhook.sendTaskWebhook(webhooks, data);
|
||||
|
||||
expect(postSpy).to.be.calledOnce;
|
||||
expect(postSpy).to.be.calledWith({
|
||||
url: 'http://example.org/endpoint',
|
||||
body: {
|
||||
direction: 'up',
|
||||
task: { _id: 'task-id' },
|
||||
delta: 1.4,
|
||||
user: {
|
||||
_id: 'user-id'
|
||||
}
|
||||
},
|
||||
json: true
|
||||
});
|
||||
});
|
||||
|
||||
it('sends a post request for each webhook endpoint', function() {
|
||||
var webhooks = {
|
||||
'some-id': {
|
||||
sort: 0,
|
||||
id: 'some-id',
|
||||
enabled: true,
|
||||
url: 'http://example.org/endpoint'
|
||||
},
|
||||
'second-webhook': {
|
||||
sort: 1,
|
||||
id: 'second-webhook',
|
||||
enabled: true,
|
||||
url: 'http://example.com/2/endpoint'
|
||||
}
|
||||
};
|
||||
|
||||
webhook.sendTaskWebhook(webhooks, data);
|
||||
|
||||
expect(postSpy).to.be.calledTwice;
|
||||
expect(postSpy).to.be.calledWith({
|
||||
url: 'http://example.org/endpoint',
|
||||
body: {
|
||||
direction: 'up',
|
||||
task: { _id: 'task-id' },
|
||||
delta: 1.4,
|
||||
user: {
|
||||
_id: 'user-id'
|
||||
}
|
||||
},
|
||||
json: true
|
||||
});
|
||||
expect(postSpy).to.be.calledWith({
|
||||
url: 'http://example.com/2/endpoint',
|
||||
body: {
|
||||
direction: 'up',
|
||||
task: { _id: 'task-id' },
|
||||
delta: 1.4,
|
||||
user: {
|
||||
_id: 'user-id'
|
||||
}
|
||||
},
|
||||
json: true
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user