Validate that everyX values in dailies are integers bounded by 0 and 9999 fixes #8782 (#9268)

* Validate that everyX values are integers bounded by 0 and 9999

* Added client side check

* Updated tests

* Added migration for bad dailies

Near idential to the other task migration.

* fix(typo): camelCase function call
This commit is contained in:
Tyler Nychka
2017-12-05 14:55:32 -05:00
committed by Sabe Jones
parent 8367de34bf
commit bfaa7c0fea
5 changed files with 135 additions and 3 deletions

View File

@@ -0,0 +1,88 @@
var migrationName = 'tasks-set-everyX';
var authorName = ''; // in case script author needs to know when their ...
var authorUuid = ''; //... own data is done
/*
* Iterates over all tasks and sets invalid everyX values (less than 0 or more than 9999 or not an int) field to 0
*/
var monk = require('monk');
var connectionString = 'mongodb://localhost:27017/habitrpg?auto_reconnect=true'; // FOR TEST DATABASE
var dbTasks = monk(connectionString).get('tasks', { castIds: false });
function processTasks(lastId) {
// specify a query to limit the affected tasks (empty for all tasks):
var query = {
type: "daily",
everyX: {
$not: {
$gte: 0,
$lte: 9999,
$type: "int",
}
},
};
if (lastId) {
query._id = {
$gt: lastId
}
}
dbTasks.find(query, {
sort: {_id: 1},
limit: 250,
fields: [],
})
.then(updateTasks)
.catch(function (err) {
console.log(err);
return exiting(1, 'ERROR! ' + err);
});
}
var progressCount = 1000;
var count = 0;
function updateTasks (tasks) {
if (!tasks || tasks.length === 0) {
console.warn('All appropriate tasks found and modified.');
displayData();
return;
}
var taskPromises = tasks.map(updatetask);
var lasttask = tasks[tasks.length - 1];
return Promise.all(taskPromises)
.then(function () {
processTasks(lasttask._id);
});
}
function updatetask (task) {
count++;
var set = {'everyX': 0};
dbTasks.update({_id: task._id}, {$set:set});
if (count % progressCount == 0) console.warn(count + ' ' + task._id);
if (task._id == authorUuid) console.warn(authorName + ' processed');
}
function displayData() {
console.warn('\n' + count + ' tasks processed\n');
return exiting(0);
}
function exiting(code, msg) {
code = code || 0; // 0 = success
if (code && !msg) { msg = 'ERROR!'; }
if (msg) {
if (code) { console.error(msg); }
else { console.log( msg); }
}
process.exit(code);
}
module.exports = processTasks;

View File

@@ -628,6 +628,43 @@ describe('POST /tasks/user', () => {
}); });
}); });
it('returns an error if everyX is a non int', async () => {
await expect(user.post('/tasks/user', {
text: 'test daily',
type: 'daily',
everyX: 2.5,
})).to.eventually.be.rejected.and.eql({
code: 400,
error: 'BadRequest',
message: 'daily validation failed',
});
});
it('returns an error if everyX is negative', async () => {
await expect(user.post('/tasks/user', {
text: 'test daily',
type: 'daily',
everyX: -1,
})).to.eventually.be.rejected.and.eql({
code: 400,
error: 'BadRequest',
message: 'daily validation failed',
});
});
it('returns an error if everyX is above 9999', async () => {
await expect(user.post('/tasks/user', {
text: 'test daily',
type: 'daily',
everyX: 10000,
})).to.eventually.be.rejected.and.eql({
code: 400,
error: 'BadRequest',
message: 'daily validation failed',
});
});
it('can create checklists', async () => { it('can create checklists', async () => {
let task = await user.post('/tasks/user', { let task = await user.post('/tasks/user', {
text: 'test daily', text: 'test daily',

View File

@@ -38,7 +38,7 @@ describe('shared.ops.addTask', () => {
expect(habit.counterDown).to.equal(0); expect(habit.counterDown).to.equal(0);
}); });
it('adds an habtit when type is invalid', () => { it('adds a habit when type is invalid', () => {
let habit = addTask(user, { let habit = addTask(user, {
body: { body: {
type: 'invalid', type: 'invalid',

View File

@@ -102,7 +102,7 @@
| {{ $t(frequency) }} | {{ $t(frequency) }}
.form-group .form-group
label(v-once) {{ $t('repeatEvery') }} label(v-once) {{ $t('repeatEvery') }}
input(type="number", v-model="task.everyX", min="0", required, :disabled='challengeAccessRequired') input(type="number", v-model="task.everyX", min="0", max="9999", required, :disabled='challengeAccessRequired')
| {{ repeatSuffix }} | {{ repeatSuffix }}
br br
template(v-if="task.frequency === 'weekly'") template(v-if="task.frequency === 'weekly'")

View File

@@ -239,7 +239,14 @@ export let habit = Task.discriminator('habit', HabitSchema);
export let DailySchema = new Schema(_.defaults({ export let DailySchema = new Schema(_.defaults({
frequency: {type: String, default: 'weekly', enum: ['daily', 'weekly', 'monthly', 'yearly']}, frequency: {type: String, default: 'weekly', enum: ['daily', 'weekly', 'monthly', 'yearly']},
everyX: {type: Number, default: 1}, // e.g. once every X weeks everyX: {
type: Number,
default: 1,
validate: [
(val) => val % 1 === 0 && val >= 0 && val <= 9999,
'Valid everyX values are integers from 0 to 9999',
],
},
startDate: { startDate: {
type: Date, type: Date,
default () { default () {