Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into fix-rest-pin-message
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigok committed Dec 21, 2018
2 parents 5a969b1 + 5bdd50a commit 94dce6d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 10 deletions.
19 changes: 14 additions & 5 deletions packages/rocketchat-api/server/v1/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,6 @@ RocketChat.API.v1.addRoute('users.setPreferences', { authRequired: true }, {
muteFocusedConversations: Match.Optional(Boolean),
}),
});

const userId = this.bodyParams.userId ? this.bodyParams.userId : this.userId;
const userData = {
_id: userId,
Expand All @@ -438,13 +437,23 @@ RocketChat.API.v1.addRoute('users.setPreferences', { authRequired: true }, {
}

Meteor.runAsUser(this.userId, () => RocketChat.saveUser(this.userId, userData));
const user = RocketChat.models.Users.findOneById(userId, {
fields: {
'settings.preferences': 1,
language: 1,
},
});

return RocketChat.API.v1.success({
user: RocketChat.models.Users.findOneById(userId, {
fields: {
'settings.preferences': 1,
user: {
_id: user._id,
settings: {
preferences: {
...user.settings.preferences,
language: user.language,
},
},
}),
},
});
},
});
Expand Down
5 changes: 4 additions & 1 deletion packages/rocketchat-lib/server/functions/saveUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,11 +242,14 @@ RocketChat.saveUser = function(userId, userData) {
if (userData.roles) {
updateUser.$set.roles = userData.roles;
}

if (userData.settings) {
updateUser.$set.settings = { preferences: userData.settings.preferences };
}

if (userData.language) {
updateUser.$set.language = userData.language;
}

if (typeof userData.requirePasswordChange !== 'undefined') {
updateUser.$set.requirePasswordChange = userData.requirePasswordChange;
}
Expand Down
36 changes: 32 additions & 4 deletions tests/end-to-end/api/01-users.js
Original file line number Diff line number Diff line change
Expand Up @@ -981,28 +981,56 @@ describe('[Users]', function() {

describe('[/users.setPreferences]', () => {
it('should set some preferences by user when execute successfully', (done) => {
preferences.userId = credentials['X-User-Id'];
const userPreferences = {
userId: credentials['X-User-Id'],
data: {
...preferences.data,
},
};
request.post(api('users.setPreferences'))
.set(credentials)
.send(preferences)
.send(userPreferences)
.expect(200)
.expect('Content-Type', 'application/json')
.expect((res) => {
expect(res.body.user.settings.preferences).to.be.eql(preferences.data);
expect(Object.keys(res.body.user.settings.preferences)).to.include.members(Object.keys(userPreferences.data));
expect(res.body).to.have.property('success', true);
})
.end(done);
});
it('should set some preferences and language preference by user when execute successfully', (done) => {
const userPreferences = {
userId: credentials['X-User-Id'],
data: {
...preferences.data,
language: 'en',
},
};
request.post(api('users.setPreferences'))
.set(credentials)
.send(userPreferences)
.expect(200)
.expect('Content-Type', 'application/json')
.expect((res) => {
expect(res.body).to.have.property('success', true);
expect(res.body.user.settings.preferences).to.have.property('language', 'en');
})
.end(done);
});
});

describe('[/users.getPreferences]', () => {
it('should return all preferences when execute successfully', (done) => {
const userPreferences = {
...preferences.data,
language: 'en',
};
request.get(api('users.getPreferences'))
.set(credentials)
.expect(200)
.expect('Content-Type', 'application/json')
.expect((res) => {
expect(res.body.preferences).to.be.eql(preferences.data);
expect(res.body.preferences).to.be.eql(userPreferences);
expect(res.body).to.have.property('success', true);
})
.end(done);
Expand Down

0 comments on commit 94dce6d

Please sign in to comment.