From bf7a2467079ac2f062eecd92e2a01e302f73d841 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivar=20Conradi=20=C3=98sthus?= Date: Tue, 28 Apr 2020 08:17:04 +0200 Subject: [PATCH] fix: user should not crash if email is missing --- lib/db/context-field-store.js | 2 +- lib/user.js | 10 ++++++++-- lib/user.test.js | 9 +++++++++ 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/lib/db/context-field-store.js b/lib/db/context-field-store.js index 8214d2e023..3e6f68d102 100644 --- a/lib/db/context-field-store.js +++ b/lib/db/context-field-store.js @@ -41,7 +41,7 @@ class ContextFieldStore { } async _createFromConfig(customContextFields) { - if (customContextFields) { + if (customContextFields && customContextFields.length > 0) { this.logger.info( 'Create custom context fields', customContextFields, diff --git a/lib/user.js b/lib/user.js index b130a7c58b..f62c0c8769 100644 --- a/lib/user.js +++ b/lib/user.js @@ -24,7 +24,13 @@ module.exports = class User { this.email = email; this.systemId = systemId; this.permissions = permissions; - this.imageUrl = - imageUrl || gravatarUrl(email, { size: '42', default: 'retro' }); + this.imageUrl = imageUrl || this.generateImageUrl(); + } + + generateImageUrl() { + return gravatarUrl(this.email || this.username, { + size: '42', + default: 'retro', + }); } }; diff --git a/lib/user.test.js b/lib/user.test.js index 0e45d30eb7..ee3c957b3e 100644 --- a/lib/user.test.js +++ b/lib/user.test.js @@ -57,3 +57,12 @@ test('Should require valid email', t => { t.is(error.message, 'Email "value" must be a valid email'); }); + +test('Should create user with only username defined', t => { + const user = new User({ username: 'some-user' }); + t.is(user.username, 'some-user'); + t.is( + user.imageUrl, + 'https://gravatar.com/avatar/140fd5a002fb8d728a9848f8c9fcea2a?size=42&default=retro', + ); +});