mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-17 22:57:21 +01:00
change response for shared.ops.sleep, save user in POST /user/sleep and add integration tests for it
This commit is contained in:
@@ -1,4 +1,8 @@
|
||||
module.exports = function sleep (user) {
|
||||
user.preferences.sleep = !user.preferences.sleep;
|
||||
return user.preferences.sleep;
|
||||
return {
|
||||
preferences: {
|
||||
sleep: user.preferences.sleep,
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
27
test/api/v3/integration/user/sleep.test.js
Normal file
27
test/api/v3/integration/user/sleep.test.js
Normal file
@@ -0,0 +1,27 @@
|
||||
import {
|
||||
generateUser,
|
||||
} from '../../../../helpers/api-integration/v3';
|
||||
|
||||
describe('POST /user/sleep', () => {
|
||||
let user;
|
||||
|
||||
beforeEach(async () => {
|
||||
user = await generateUser();
|
||||
});
|
||||
|
||||
it('toggles sleep status', async () => {
|
||||
let res = await user.post(`/user/sleep`);
|
||||
expect(res).to.eql({
|
||||
preferences: {sleep: true},
|
||||
});
|
||||
await user.sync();
|
||||
expect(user.preferences.sleep).to.be.true;
|
||||
|
||||
let res2 = await user.post(`/user/sleep`);
|
||||
expect(res2).to.eql({
|
||||
preferences: {sleep: false},
|
||||
});
|
||||
await user.sync();
|
||||
expect(user.preferences.sleep).to.be.false;
|
||||
});
|
||||
});
|
||||
@@ -4,15 +4,15 @@ import {
|
||||
} from '../../helpers/common.helper';
|
||||
|
||||
describe('shared.ops.sleep', () => {
|
||||
it('changes user.preferences.sleep and returns the new value', () => {
|
||||
it('toggles user.preferences.sleep', () => {
|
||||
let user = generateUser();
|
||||
|
||||
let res = sleep(user);
|
||||
expect(res).to.equal(true);
|
||||
expect(res).to.eql({preferences: {sleep: true}});
|
||||
expect(user.preferences.sleep).to.equal(true);
|
||||
|
||||
let res2 = sleep(user);
|
||||
expect(res2).to.equal(false);
|
||||
expect(res2).to.eql({preferences: {sleep: false}});
|
||||
expect(user.preferences.sleep).to.equal(false);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -293,12 +293,9 @@ api.sleep = {
|
||||
url: '/user/sleep',
|
||||
async handler (req, res) {
|
||||
let user = res.locals.user;
|
||||
let sleepVal = sleep(user);
|
||||
return res.respond(200, {
|
||||
preferences: {
|
||||
sleep: sleepVal,
|
||||
},
|
||||
});
|
||||
let sleepRes = sleep(user);
|
||||
await user.save();
|
||||
res.respond(200, sleepRes);
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user