e04d4e8bea
* add drop cap notification * add drop cap notification * add dismissible notification * fix(notification): correct remove icon positioning * track events * add modal * add back files * fix links and add missing analytics * fix rounded borders and hide sub info for subscribers * a/b test * fix comparison * Translated using Weblate (Spanish) Currently translated at 98.2% (55 of 56 strings) Translation: Habitica/Messages Translate-URL: https://translate.habitica.com/projects/habitica/messages/es/ Translated using Weblate (Spanish) Currently translated at 99.4% (179 of 180 strings) Translation: Habitica/Settings Translate-URL: https://translate.habitica.com/projects/habitica/settings/es/ Merge branch 'origin/develop' into Weblate. Translated using Weblate (Spanish) Currently translated at 99.4% (175 of 176 strings) Translation: Habitica/Subscriber Translate-URL: https://translate.habitica.com/projects/habitica/subscriber/es/ Translated using Weblate (Spanish (Latin America)) Currently translated at 98.6% (359 of 364 strings) Translation: Habitica/Groups Translate-URL: https://translate.habitica.com/projects/habitica/groups/es_419/ Translated using Weblate (Spanish) Currently translated at 85.7% (151 of 176 strings) Translation: Habitica/Subscriber Translate-URL: https://translate.habitica.com/projects/habitica/subscriber/es/ Translated using Weblate (Spanish) Currently translated at 95.3% (538 of 564 strings) Translation: Habitica/Backgrounds Translate-URL: https://translate.habitica.com/projects/habitica/backgrounds/es/ Translated using Weblate (Spanish (Latin America)) Currently translated at 98.6% (359 of 364 strings) Translation: Habitica/Groups Translate-URL: https://translate.habitica.com/projects/habitica/groups/es_419/ Translated using Weblate (French) Currently translated at 100.0% (56 of 56 strings) Translation: Habitica/Messages Translate-URL: https://translate.habitica.com/projects/habitica/messages/fr/ Translated using Weblate (German) Currently translated at 100.0% (56 of 56 strings) Translation: Habitica/Messages Translate-URL: https://translate.habitica.com/projects/habitica/messages/de/ Translated using Weblate (French) Currently translated at 100.0% (718 of 718 strings) Translation: Habitica/Questscontent Translate-URL: https://translate.habitica.com/projects/habitica/questscontent/fr/ Translated using Weblate (German) Currently translated at 100.0% (718 of 718 strings) Translation: Habitica/Questscontent Translate-URL: https://translate.habitica.com/projects/habitica/questscontent/de/ Translated using Weblate (Czech) Currently translated at 100.0% (56 of 56 strings) Translation: Habitica/Spells Translate-URL: https://translate.habitica.com/projects/habitica/spells/cs/ Translated using Weblate (Japanese) Currently translated at 100.0% (175 of 175 strings) Translation: Habitica/Subscriber Translate-URL: https://translate.habitica.com/projects/habitica/subscriber/ja/ Translated using Weblate (Italian) Currently translated at 100.0% (56 of 56 strings) Translation: Habitica/Messages Translate-URL: https://translate.habitica.com/projects/habitica/messages/it/ Translated using Weblate (Italian) Currently translated at 100.0% (718 of 718 strings) Translation: Habitica/Questscontent Translate-URL: https://translate.habitica.com/projects/habitica/questscontent/it/ Translated using Weblate (Czech) Currently translated at 100.0% (180 of 180 strings) Translation: Habitica/Settings Translate-URL: https://translate.habitica.com/projects/habitica/settings/cs/ Translated using Weblate (Basque) Currently translated at 100.0% (2 of 2 strings) Translation: Habitica/Noscript Translate-URL: https://translate.habitica.com/projects/habitica/noscript/eu/ Translated using Weblate (Basque) Currently translated at 6.5% (8 of 123 strings) Translation: Habitica/Communityguidelines Translate-URL: https://translate.habitica.com/projects/habitica/communityguidelines/eu/ Translated using Weblate (Chinese (Simplified)) Currently translated at 100.0% (56 of 56 strings) Translation: Habitica/Messages Translate-URL: https://translate.habitica.com/projects/habitica/messages/zh_Hans/ Translated using Weblate (Japanese) Currently translated at 100.0% (56 of 56 strings) Translation: Habitica/Messages Translate-URL: https://translate.habitica.com/projects/habitica/messages/ja/ Translated using Weblate (Chinese (Simplified)) Currently translated at 100.0% (718 of 718 strings) Translation: Habitica/Questscontent Translate-URL: https://translate.habitica.com/projects/habitica/questscontent/zh_Hans/ Translated using Weblate (Portuguese (Brazil)) Currently translated at 100.0% (718 of 718 strings) Translation: Habitica/Questscontent Translate-URL: https://translate.habitica.com/projects/habitica/questscontent/pt_BR/ Translated using Weblate (Portuguese (Brazil)) Currently translated at 99.8% (717 of 718 strings) Translation: Habitica/Questscontent Translate-URL: https://translate.habitica.com/projects/habitica/questscontent/pt_BR/ * clarify a/b test values * add tests * refactor user dropdown * fix hover state * fix user dropdown * fix user menu hierarchy * restore i18n files to release version Co-authored-by: Melior <admin@habitica.com>
400 lines
12 KiB
JavaScript
400 lines
12 KiB
JavaScript
import moment from 'moment';
|
|
import { v4 as generateUUID } from 'uuid';
|
|
import {
|
|
generateRes,
|
|
generateReq,
|
|
generateTodo,
|
|
generateDaily,
|
|
} from '../../../helpers/api-unit.helper';
|
|
import cronMiddleware from '../../../../website/server/middlewares/cron';
|
|
import { model as User } from '../../../../website/server/models/user';
|
|
import { model as Group } from '../../../../website/server/models/group';
|
|
import * as Tasks from '../../../../website/server/models/task';
|
|
import * as analyticsService from '../../../../website/server/libs/analyticsService';
|
|
import * as cronLib from '../../../../website/server/libs/cron';
|
|
|
|
const CRON_TIMEOUT_WAIT = new Date(60 * 60 * 1000).getTime();
|
|
const CRON_TIMEOUT_UNIT = new Date(60 * 1000).getTime();
|
|
|
|
describe('cron middleware', () => {
|
|
let res; let
|
|
req;
|
|
let user;
|
|
|
|
beforeEach(async () => {
|
|
res = generateRes();
|
|
req = generateReq();
|
|
user = await res.locals.user.save();
|
|
res.analytics = analyticsService;
|
|
});
|
|
|
|
afterEach(() => {
|
|
sandbox.restore();
|
|
});
|
|
|
|
it('calls next when user is not attached', done => {
|
|
res.locals.user = null;
|
|
cronMiddleware(req, res, done);
|
|
});
|
|
|
|
it('calls next when days have not been missed', done => {
|
|
cronMiddleware(req, res, done);
|
|
});
|
|
|
|
it('should clear todos older than 30 days for free users', async () => {
|
|
user.lastCron = moment(new Date()).subtract({ days: 2 });
|
|
const task = generateTodo(user);
|
|
task.dateCompleted = moment(new Date()).subtract({ days: 31 });
|
|
task.completed = true;
|
|
await task.save();
|
|
await user.save();
|
|
|
|
await new Promise((resolve, reject) => {
|
|
cronMiddleware(req, res, err => {
|
|
if (err) return reject(err);
|
|
|
|
Tasks.Task.findOne({ _id: task }, (secondErr, taskFound) => {
|
|
if (secondErr) return reject(err);
|
|
expect(secondErr).to.not.exist;
|
|
expect(taskFound).to.not.exist;
|
|
return resolve();
|
|
});
|
|
|
|
return null;
|
|
});
|
|
});
|
|
});
|
|
|
|
it('should not clear todos older than 30 days for subscribed users', async () => {
|
|
user.purchased.plan.customerId = 'subscribedId';
|
|
user.purchased.plan.dateUpdated = moment('012013', 'MMYYYY');
|
|
user.lastCron = moment(new Date()).subtract({ days: 2 });
|
|
const task = generateTodo(user);
|
|
task.dateCompleted = moment(new Date()).subtract({ days: 31 });
|
|
task.completed = true;
|
|
await task.save();
|
|
await user.save();
|
|
|
|
await new Promise((resolve, reject) => {
|
|
cronMiddleware(req, res, err => {
|
|
if (err) return reject(err);
|
|
Tasks.Task.findOne({ _id: task }, (secondErr, taskFound) => {
|
|
if (secondErr) return reject(secondErr);
|
|
expect(secondErr).to.not.exist;
|
|
expect(taskFound).to.exist;
|
|
return resolve();
|
|
});
|
|
return null;
|
|
});
|
|
});
|
|
});
|
|
|
|
it('should clear todos older than 90 days for subscribed users', async () => {
|
|
user.purchased.plan.customerId = 'subscribedId';
|
|
user.purchased.plan.dateUpdated = moment('012013', 'MMYYYY');
|
|
user.lastCron = moment(new Date()).subtract({ days: 2 });
|
|
|
|
const task = generateTodo(user);
|
|
task.dateCompleted = moment(new Date()).subtract({ days: 91 });
|
|
task.completed = true;
|
|
await task.save();
|
|
await user.save();
|
|
|
|
await new Promise((resolve, reject) => {
|
|
cronMiddleware(req, res, err => {
|
|
if (err) return reject(err);
|
|
Tasks.Task.findOne({ _id: task }, (secondErr, taskFound) => {
|
|
if (secondErr) return reject(secondErr);
|
|
expect(secondErr).to.not.exist;
|
|
expect(taskFound).to.not.exist;
|
|
return resolve();
|
|
});
|
|
return null;
|
|
});
|
|
});
|
|
});
|
|
|
|
it('should call next if user was not modified after cron', async () => {
|
|
const hpBefore = user.stats.hp;
|
|
user.lastCron = moment(new Date()).subtract({ days: 2 });
|
|
await user.save();
|
|
|
|
await new Promise((resolve, reject) => {
|
|
cronMiddleware(req, res, err => {
|
|
if (err) return reject(err);
|
|
expect(hpBefore).to.equal(user.stats.hp);
|
|
return resolve();
|
|
});
|
|
});
|
|
});
|
|
|
|
it('updates user.auth.timestamps.loggedin and lastCron', async () => {
|
|
user.lastCron = moment(new Date()).subtract({ days: 2 });
|
|
const now = new Date();
|
|
await user.save();
|
|
|
|
await new Promise((resolve, reject) => {
|
|
cronMiddleware(req, res, err => {
|
|
if (err) return reject(err);
|
|
expect(moment(now).isSame(user.lastCron, 'day'));
|
|
expect(moment(now).isSame(user.auth.timestamps.loggedin, 'day'));
|
|
return resolve();
|
|
});
|
|
});
|
|
});
|
|
|
|
it('does damage for missing dailies', async () => {
|
|
const hpBefore = user.stats.hp;
|
|
user.lastCron = moment(new Date()).subtract({ days: 2 });
|
|
const daily = generateDaily(user);
|
|
daily.startDate = moment(new Date()).subtract({ days: 2 });
|
|
await daily.save();
|
|
await user.save();
|
|
|
|
await new Promise((resolve, reject) => {
|
|
cronMiddleware(req, res, err => {
|
|
if (err) return reject(err);
|
|
return User.findOne({ _id: user._id }, (secondErr, updatedUser) => {
|
|
if (secondErr) return reject(secondErr);
|
|
expect(updatedUser.stats.hp).to.be.lessThan(hpBefore);
|
|
return resolve();
|
|
});
|
|
});
|
|
});
|
|
});
|
|
|
|
it('updates tasks', async () => {
|
|
user.lastCron = moment(new Date()).subtract({ days: 2 });
|
|
const todo = generateTodo(user);
|
|
const todoValueBefore = todo.value;
|
|
await Promise.all([todo.save(), user.save()]);
|
|
|
|
await new Promise((resolve, reject) => {
|
|
cronMiddleware(req, res, err => {
|
|
if (err) return reject(err);
|
|
return Tasks.Task.findOne({ _id: todo._id }, (secondErr, todoFound) => {
|
|
if (secondErr) return reject(secondErr);
|
|
expect(todoFound.value).to.be.lessThan(todoValueBefore);
|
|
return resolve();
|
|
});
|
|
});
|
|
});
|
|
});
|
|
|
|
it('applies quest progress', async () => {
|
|
const hpBefore = user.stats.hp;
|
|
user.lastCron = moment(new Date()).subtract({ days: 2 });
|
|
const daily = generateDaily(user);
|
|
daily.startDate = moment(new Date()).subtract({ days: 2 });
|
|
await daily.save();
|
|
|
|
const questKey = 'dilatory';
|
|
user.party.quest.key = questKey;
|
|
|
|
const party = new Group({
|
|
type: 'party',
|
|
name: generateUUID(),
|
|
leader: user._id,
|
|
});
|
|
party.quest.members[user._id] = true;
|
|
party.quest.key = questKey;
|
|
await party.save();
|
|
|
|
user.party._id = party._id;
|
|
await user.save();
|
|
|
|
party.startQuest(user);
|
|
|
|
await new Promise((resolve, reject) => {
|
|
cronMiddleware(req, res, err => {
|
|
if (err) return reject(err);
|
|
return User.findOne({ _id: user._id }, (secondErr, updatedUser) => {
|
|
if (secondErr) return reject(secondErr);
|
|
expect(updatedUser.stats.hp).to.be.lessThan(hpBefore);
|
|
return resolve();
|
|
});
|
|
});
|
|
});
|
|
});
|
|
|
|
it('recovers from failed cron and does not error when user is already cronning', async () => {
|
|
user.lastCron = moment(new Date()).subtract({ days: 2 });
|
|
await user.save();
|
|
|
|
const updatedUser = user.toObject();
|
|
updatedUser.nMatched = 0;
|
|
|
|
sandbox.spy(cronLib, 'recoverCron');
|
|
|
|
sandbox.stub(User, 'update')
|
|
.withArgs({
|
|
_id: user._id,
|
|
$or: [
|
|
{ _cronSignature: 'NOT_RUNNING' },
|
|
{ _cronSignature: { $lt: sinon.match.number } },
|
|
],
|
|
})
|
|
.returns({
|
|
exec () {
|
|
return Promise.resolve(updatedUser);
|
|
},
|
|
});
|
|
|
|
await new Promise((resolve, reject) => {
|
|
cronMiddleware(req, res, err => {
|
|
if (err) return reject(err);
|
|
expect(cronLib.recoverCron).to.be.calledOnce;
|
|
|
|
return resolve();
|
|
});
|
|
});
|
|
});
|
|
|
|
it('cronSignature less than an hour ago should error', async () => {
|
|
user.lastCron = moment(new Date()).subtract({ days: 2 });
|
|
const now = new Date();
|
|
await User.update({
|
|
_id: user._id,
|
|
}, {
|
|
$set: {
|
|
_cronSignature: now.getTime() - CRON_TIMEOUT_WAIT + CRON_TIMEOUT_UNIT,
|
|
},
|
|
}).exec();
|
|
await user.save();
|
|
const expectedErrMessage = `Impossible to recover from cron for user ${user._id}.`;
|
|
|
|
await new Promise((resolve, reject) => {
|
|
cronMiddleware(req, res, err => {
|
|
if (!err) return reject(new Error('Cron should have failed.'));
|
|
expect(err.message).to.be.equal(expectedErrMessage);
|
|
return resolve();
|
|
});
|
|
});
|
|
});
|
|
|
|
it('cronSignature longer than an hour ago should allow cron', async () => {
|
|
user.lastCron = moment(new Date()).subtract({ days: 2 });
|
|
const now = new Date();
|
|
await User.update({
|
|
_id: user._id,
|
|
}, {
|
|
$set: {
|
|
_cronSignature: now.getTime() - CRON_TIMEOUT_WAIT - CRON_TIMEOUT_UNIT,
|
|
},
|
|
}).exec();
|
|
await user.save();
|
|
|
|
await new Promise((resolve, reject) => {
|
|
cronMiddleware(req, res, err => {
|
|
if (err) return reject(err);
|
|
expect(moment(now).isSame(user.auth.timestamps.loggedin, 'day'));
|
|
expect(user._cronSignature).to.be.equal('NOT_RUNNING');
|
|
return resolve();
|
|
});
|
|
});
|
|
});
|
|
|
|
context('Drop Cap A/B Test', async () => {
|
|
it('enrolls web users', async () => {
|
|
user.lastCron = moment(new Date()).subtract({ days: 2 });
|
|
await user.save();
|
|
req.headers['x-client'] = 'habitica-web';
|
|
|
|
await new Promise((resolve, reject) => {
|
|
cronMiddleware(req, res, async err => {
|
|
if (err) return reject(err);
|
|
user = await User.findById(user._id).exec();
|
|
expect(user._ABtests.dropCapNotif).to.be.a.string;
|
|
|
|
return resolve();
|
|
});
|
|
});
|
|
});
|
|
|
|
it('does not enroll 80% of users', async () => {
|
|
sandbox.stub(Math, 'random').returns(0.5);
|
|
user.lastCron = moment(new Date()).subtract({ days: 2 });
|
|
await user.save();
|
|
req.headers['x-client'] = 'habitica-web';
|
|
|
|
await new Promise((resolve, reject) => {
|
|
cronMiddleware(req, res, async err => {
|
|
if (err) return reject(err);
|
|
user = await User.findById(user._id).exec();
|
|
expect(user._ABtests.dropCapNotif).to.be.equal('drop-cap-notif-not-enrolled');
|
|
|
|
return resolve();
|
|
});
|
|
});
|
|
});
|
|
|
|
it('enables the new notification for 10% of users', async () => {
|
|
sandbox.stub(Math, 'random').returns(0.1);
|
|
user.lastCron = moment(new Date()).subtract({ days: 2 });
|
|
await user.save();
|
|
req.headers['x-client'] = 'habitica-web';
|
|
|
|
await new Promise((resolve, reject) => {
|
|
cronMiddleware(req, res, async err => {
|
|
if (err) return reject(err);
|
|
user = await User.findById(user._id).exec();
|
|
expect(user._ABtests.dropCapNotif).to.be.equal('drop-cap-notif-enabled');
|
|
|
|
return resolve();
|
|
});
|
|
});
|
|
});
|
|
|
|
it('disables the new notification for 10% of users', async () => {
|
|
sandbox.stub(Math, 'random').returns(0.2);
|
|
user.lastCron = moment(new Date()).subtract({ days: 2 });
|
|
await user.save();
|
|
req.headers['x-client'] = 'habitica-web';
|
|
|
|
await new Promise((resolve, reject) => {
|
|
cronMiddleware(req, res, async err => {
|
|
if (err) return reject(err);
|
|
user = await User.findById(user._id).exec();
|
|
expect(user._ABtests.dropCapNotif).to.be.equal('drop-cap-notif-disabled');
|
|
|
|
return resolve();
|
|
});
|
|
});
|
|
});
|
|
|
|
it('does not affect subscribers', async () => {
|
|
sandbox.stub(Math, 'random').returns(0.2);
|
|
user.lastCron = moment(new Date()).subtract({ days: 2 });
|
|
await user.save();
|
|
req.headers['x-client'] = 'habitica-web';
|
|
sandbox.stub(User.prototype, 'isSubscribed').returns(true);
|
|
|
|
await new Promise((resolve, reject) => {
|
|
cronMiddleware(req, res, async err => {
|
|
if (err) return reject(err);
|
|
user = await User.findById(user._id).exec();
|
|
expect(user._ABtests.dropCapNotif).to.not.exist;
|
|
|
|
return resolve();
|
|
});
|
|
});
|
|
});
|
|
|
|
it('does not affect mobile users', async () => {
|
|
user.lastCron = moment(new Date()).subtract({ days: 2 });
|
|
await user.save();
|
|
req.headers['x-client'] = 'habitica-ios';
|
|
|
|
await new Promise((resolve, reject) => {
|
|
cronMiddleware(req, res, async err => {
|
|
if (err) return reject(err);
|
|
user = await User.findById(user._id).exec();
|
|
expect(user._ABtests.dropCapNotif).to.not.exist;
|
|
|
|
return resolve();
|
|
});
|
|
});
|
|
});
|
|
});
|
|
});
|