mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 16:46:12 +01:00
test(user): added additional tests for icon background colour
This commit is contained in:
@@ -137,4 +137,10 @@ get:
|
|||||||
hideCategoryLastPost:
|
hideCategoryLastPost:
|
||||||
type: boolean
|
type: boolean
|
||||||
enableQuickReply:
|
enableQuickReply:
|
||||||
type: boolean
|
type: boolean
|
||||||
|
iconBackgrounds:
|
||||||
|
type: array
|
||||||
|
items:
|
||||||
|
type: string
|
||||||
|
description: A valid CSS colour code
|
||||||
|
example: '#fff'
|
||||||
@@ -199,8 +199,13 @@ module.exports = function (User) {
|
|||||||
// User Icons
|
// User Icons
|
||||||
if (requestedFields.includes('picture') && user.username && parseInt(user.uid, 10) && !meta.config.defaultAvatar) {
|
if (requestedFields.includes('picture') && user.username && parseInt(user.uid, 10) && !meta.config.defaultAvatar) {
|
||||||
const iconBackgrounds = await User.getIconBackgrounds(user.uid);
|
const iconBackgrounds = await User.getIconBackgrounds(user.uid);
|
||||||
|
let bgColor = await User.getUserField(user.uid, 'icon:bgColor');
|
||||||
|
if (!iconBackgrounds.includes(bgColor)) {
|
||||||
|
bgColor = Array.prototype.reduce.call(user.username, (cur, next) => cur + next.charCodeAt(), 0);
|
||||||
|
bgColor = iconBackgrounds[bgColor % iconBackgrounds.length];
|
||||||
|
}
|
||||||
user['icon:text'] = (user.username[0] || '').toUpperCase();
|
user['icon:text'] = (user.username[0] || '').toUpperCase();
|
||||||
user['icon:bgColor'] = await User.getUserField(user.uid, 'icon:bgColor') || iconBackgrounds[Array.prototype.reduce.call(user.username, (cur, next) => cur + next.charCodeAt(), 0) % iconBackgrounds.length];
|
user['icon:bgColor'] = bgColor;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (user.hasOwnProperty('joindate')) {
|
if (user.hasOwnProperty('joindate')) {
|
||||||
|
|||||||
17
test/user.js
17
test/user.js
@@ -740,6 +740,23 @@ describe('User', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should return an icon text and valid background if username and picture is explicitly requested', async () => {
|
||||||
|
const payload = await User.getUserFields(testUid, ['username', 'picture']);
|
||||||
|
const validBackgrounds = await User.getIconBackgrounds(testUid);
|
||||||
|
assert.strictEqual(payload['icon:text'], userData.username.slice(0, 1).toUpperCase());
|
||||||
|
assert(payload['icon:bgColor']);
|
||||||
|
assert(validBackgrounds.includes(payload['icon:bgColor']));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return a valid background, even if an invalid background colour is set', async () => {
|
||||||
|
await User.setUserField(testUid, 'icon:bgColor', 'teal');
|
||||||
|
const payload = await User.getUserFields(testUid, ['username', 'picture']);
|
||||||
|
const validBackgrounds = await User.getIconBackgrounds(testUid);
|
||||||
|
|
||||||
|
assert(payload['icon:bgColor']);
|
||||||
|
assert(validBackgrounds.includes(payload['icon:bgColor']));
|
||||||
|
});
|
||||||
|
|
||||||
it('should return private data if field is whitelisted', (done) => {
|
it('should return private data if field is whitelisted', (done) => {
|
||||||
function filterMethod(data, callback) {
|
function filterMethod(data, callback) {
|
||||||
data.whitelist.push('another_secret');
|
data.whitelist.push('another_secret');
|
||||||
|
|||||||
Reference in New Issue
Block a user