wip(shared): port sleep op

This commit is contained in:
Matteo Pagliazzi
2016-03-15 16:33:44 +01:00
parent f55df39fb7
commit 490c6a9ae1
7 changed files with 50 additions and 6 deletions

View File

@@ -99,9 +99,11 @@ api.count = count;
// TODO As ops and fns are ported, exported them through the api object // TODO As ops and fns are ported, exported them through the api object
import scoreTask from './ops/scoreTask'; import scoreTask from './ops/scoreTask';
import sleep from './ops/sleep';
api.ops = { api.ops = {
scoreTask, scoreTask,
sleep,
}; };
api.fns = {}; api.fns = {};

View File

@@ -1,4 +1,4 @@
module.exports = function(user, req, cb) { module.exports = function sleep (user) {
user.preferences.sleep = !user.preferences.sleep; user.preferences.sleep = !user.preferences.sleep;
return typeof cb === "function" ? cb(null, {}) : void 0; return user.preferences.sleep;
}; };

View File

@@ -106,8 +106,8 @@
"test:api-v3:integration": "gulp test:api-v3:integration", "test:api-v3:integration": "gulp test:api-v3:integration",
"test:api-v3:integration:separate-server": "gulp test:api-v3:integration:separate-server", "test:api-v3:integration:separate-server": "gulp test:api-v3:integration:separate-server",
"test:api-legacy": "istanbul cover -i \"website/src/**\" --dir coverage/api ./node_modules/mocha/bin/_mocha test/api-legacy", "test:api-legacy": "istanbul cover -i \"website/src/**\" --dir coverage/api ./node_modules/mocha/bin/_mocha test/api-legacy",
"test:common": "mocha test/common", "test:common": "mocha test/common --recursive",
"test:content": "mocha test/content", "test:content": "mocha test/content --recursive",
"test:karma": "karma start --single-run", "test:karma": "karma start --single-run",
"test:karma:watch": "karma start", "test:karma:watch": "karma start",
"test:prepare:webdriver": "webdriver-manager update", "test:prepare:webdriver": "webdriver-manager update",

View File

@@ -51,7 +51,6 @@ const COMMON_FILES = [
'!./common/script/ops/reset.js', '!./common/script/ops/reset.js',
'!./common/script/ops/revive.js', '!./common/script/ops/revive.js',
'!./common/script/ops/sell.js', '!./common/script/ops/sell.js',
'!./common/script/ops/sleep.js',
'!./common/script/ops/sortTag.js', '!./common/script/ops/sortTag.js',
'!./common/script/ops/sortTask.js', '!./common/script/ops/sortTask.js',
'!./common/script/ops/unlock.js', '!./common/script/ops/unlock.js',

View File

@@ -104,7 +104,7 @@ gulp.task('test:common:clean', (cb) => {
}); });
gulp.task('test:common:watch', ['test:common:clean'], () => { gulp.task('test:common:watch', ['test:common:clean'], () => {
gulp.watch(['common/script/**', 'test/common/**'], ['test:common:clean']); gulp.watch(['common/script/**/*', 'test/common/**/*'], ['test:common:clean']);
}); });
gulp.task('test:common:safe', ['test:prepare:build'], (cb) => { gulp.task('test:common:safe', ['test:prepare:build'], (cb) => {

18
test/common/ops/sleep.js Normal file
View File

@@ -0,0 +1,18 @@
import sleep from '../../../common/script/ops/sleep';
import {
generateUser,
} from '../../helpers/common.helper';
describe('shared.ops.sleep', () => {
it('changes user.preferences.sleep and returns the new value', () => {
let user = generateUser();
let res = sleep(user);
expect(res).to.equal(true);
expect(user.preferences.sleep).to.equal(true);
let res2 = sleep(user);
expect(res2).to.equal(false);
expect(user.preferences.sleep).to.equal(false);
});
});

View File

@@ -13,6 +13,8 @@ import Q from 'q';
import _ from 'lodash'; import _ from 'lodash';
import * as passwordUtils from '../../libs/api-v3/password'; import * as passwordUtils from '../../libs/api-v3/password';
const sleep = common.ops.sleep;
let api = {}; let api = {};
/** /**
@@ -277,4 +279,27 @@ api.castSpell = {
}, },
}; };
/**
* @api {post} /user/sleep Put the user in the inn.
* @apiVersion 3.0.0
* @apiName UserSleep
* @apiGroup User
*
* @apiSuccess {Object} Will return an object with the new `user.preferences.sleep` value. Example `{preferences: {sleep: true}}`
*/
api.sleep = {
method: 'POST',
middlewares: [authWithHeaders(), cron],
url: '/user/sleep',
async handler (req, res) {
let user = res.locals.user;
let sleepVal = sleep(user);
return res.respond(200, {
preferences: {
sleep: sleepVal,
},
});
},
};
module.exports = api; module.exports = api;