From 1ee98743934536a430b2683f74ca13d3f5dba077 Mon Sep 17 00:00:00 2001 From: Sheogorath Date: Sun, 3 Mar 2019 14:55:14 +0100 Subject: [PATCH] Fix names with spaces in letter-avatars Seems like there is a possible problem when a name containing a space is passed to this function. using urlencode on the name should fix possible problems here. Signed-off-by: Sheogorath --- lib/letter-avatars.js | 1 + test/letter-avatars.js | 43 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/lib/letter-avatars.js b/lib/letter-avatars.js index 7d46395..a5dd820 100644 --- a/lib/letter-avatars.js +++ b/lib/letter-avatars.js @@ -30,6 +30,7 @@ exports.generateAvatarURL = function (name, email = '', big = true) { if (typeof email !== 'string') { email = '' + name + '@example.com' } + name=encodeURIComponent(name) let hash = crypto.createHash('md5') hash.update(email.toLowerCase()) diff --git a/test/letter-avatars.js b/test/letter-avatars.js index 16197f5..c0e967e 100644 --- a/test/letter-avatars.js +++ b/test/letter-avatars.js @@ -3,11 +3,50 @@ 'use strict' const assert = require('assert') -const avatars = require('../lib/letter-avatars') +const mock = require('mock-require') + +describe('generateAvatarURL() gravatar enabled', function () { + let avatars + beforeEach(function () { + // Reset config to make sure we don't influence other tests + let testconfig = { + allowGravatar: true, + serverURL: 'http://localhost:3000', + port: 3000 + } + mock('../lib/config', testconfig) + avatars = mock.reRequire('../lib/letter-avatars') + }) -describe('generateAvatarURL()', function () { it('should return correct urls', function () { assert.strictEqual(avatars.generateAvatarURL('Daan Sprenkels', 'hello@dsprenkels.com', true), 'https://www.gravatar.com/avatar/d41b5f3508cc3f31865566a47dd0336b?s=400') assert.strictEqual(avatars.generateAvatarURL('Daan Sprenkels', 'hello@dsprenkels.com', false), 'https://www.gravatar.com/avatar/d41b5f3508cc3f31865566a47dd0336b?s=96') }) + + it('should return correct urls for names with spaces', function () { + assert.strictEqual(avatars.generateAvatarURL('Daan Sprenkels'), 'http://localhost:3000/user/Daan%20Sprenkels/avatar.svg') + }) +}) + +describe('generateAvatarURL() gravatar disabled', function () { + let avatars + beforeEach(function () { + // Reset config to make sure we don't influence other tests + let testconfig = { + allowGravatar: false, + serverURL: 'http://localhost:3000', + port: 3000 + } + mock('../lib/config', testconfig) + avatars = mock.reRequire('../lib/letter-avatars') + }) + + it('should return correct urls', function () { + assert.strictEqual(avatars.generateAvatarURL('Daan Sprenkels', 'hello@dsprenkels.com', true), 'http://localhost:3000/user/Daan%20Sprenkels/avatar.svg') + assert.strictEqual(avatars.generateAvatarURL('Daan Sprenkels', 'hello@dsprenkels.com', false), 'http://localhost:3000/user/Daan%20Sprenkels/avatar.svg') + }) + + it('should return correct urls for names with spaces', function () { + assert.strictEqual(avatars.generateAvatarURL('Daan Sprenkels'), 'http://localhost:3000/user/Daan%20Sprenkels/avatar.svg') + }) })