Skip to content

Commit 1ee9874

Browse files
committed
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 <[email protected]>
1 parent 1128274 commit 1ee9874

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

Diff for: lib/letter-avatars.js

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ exports.generateAvatarURL = function (name, email = '', big = true) {
3030
if (typeof email !== 'string') {
3131
email = '' + name + '@example.com'
3232
}
33+
name=encodeURIComponent(name)
3334

3435
let hash = crypto.createHash('md5')
3536
hash.update(email.toLowerCase())

Diff for: test/letter-avatars.js

+41-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,50 @@
33
'use strict'
44

55
const assert = require('assert')
6-
const avatars = require('../lib/letter-avatars')
6+
const mock = require('mock-require')
7+
8+
describe('generateAvatarURL() gravatar enabled', function () {
9+
let avatars
10+
beforeEach(function () {
11+
// Reset config to make sure we don't influence other tests
12+
let testconfig = {
13+
allowGravatar: true,
14+
serverURL: 'http://localhost:3000',
15+
port: 3000
16+
}
17+
mock('../lib/config', testconfig)
18+
avatars = mock.reRequire('../lib/letter-avatars')
19+
})
720

8-
describe('generateAvatarURL()', function () {
921
it('should return correct urls', function () {
1022
assert.strictEqual(avatars.generateAvatarURL('Daan Sprenkels', '[email protected]', true), 'https://www.gravatar.com/avatar/d41b5f3508cc3f31865566a47dd0336b?s=400')
1123
assert.strictEqual(avatars.generateAvatarURL('Daan Sprenkels', '[email protected]', false), 'https://www.gravatar.com/avatar/d41b5f3508cc3f31865566a47dd0336b?s=96')
1224
})
25+
26+
it('should return correct urls for names with spaces', function () {
27+
assert.strictEqual(avatars.generateAvatarURL('Daan Sprenkels'), 'http://localhost:3000/user/Daan%20Sprenkels/avatar.svg')
28+
})
29+
})
30+
31+
describe('generateAvatarURL() gravatar disabled', function () {
32+
let avatars
33+
beforeEach(function () {
34+
// Reset config to make sure we don't influence other tests
35+
let testconfig = {
36+
allowGravatar: false,
37+
serverURL: 'http://localhost:3000',
38+
port: 3000
39+
}
40+
mock('../lib/config', testconfig)
41+
avatars = mock.reRequire('../lib/letter-avatars')
42+
})
43+
44+
it('should return correct urls', function () {
45+
assert.strictEqual(avatars.generateAvatarURL('Daan Sprenkels', '[email protected]', true), 'http://localhost:3000/user/Daan%20Sprenkels/avatar.svg')
46+
assert.strictEqual(avatars.generateAvatarURL('Daan Sprenkels', '[email protected]', false), 'http://localhost:3000/user/Daan%20Sprenkels/avatar.svg')
47+
})
48+
49+
it('should return correct urls for names with spaces', function () {
50+
assert.strictEqual(avatars.generateAvatarURL('Daan Sprenkels'), 'http://localhost:3000/user/Daan%20Sprenkels/avatar.svg')
51+
})
1352
})

0 commit comments

Comments
 (0)