From c8896e8be7e646833fa6a199123660c4595afea5 Mon Sep 17 00:00:00 2001 From: Sebastian Stehle Date: Fri, 26 May 2017 20:52:03 +0200 Subject: [PATCH] GravatarHelper fixed --- src/Squidex.Infrastructure/GravatarHelper.cs | 29 +++++++++++++-- .../GravatarHelperTests.cs | 37 +++++++++++++++++++ 2 files changed, 63 insertions(+), 3 deletions(-) create mode 100644 tests/Squidex.Infrastructure.Tests/GravatarHelperTests.cs diff --git a/src/Squidex.Infrastructure/GravatarHelper.cs b/src/Squidex.Infrastructure/GravatarHelper.cs index 83e3f37ef..6ec09d256 100644 --- a/src/Squidex.Infrastructure/GravatarHelper.cs +++ b/src/Squidex.Infrastructure/GravatarHelper.cs @@ -9,18 +9,41 @@ using System.Security.Cryptography; using System.Text; +// ReSharper disable ForCanBeConvertedToForeach + namespace Squidex.Infrastructure { public static class GravatarHelper { public static string CreatePictureUrl(string email) + { + var gravatarUrl = $"https://www.gravatar.com/avatar/{Hash(email)}"; + + return gravatarUrl; + } + + public static string CreateProfileUrl(string email) + { + var gravatarUrl = $"https://www.gravatar.com/{Hash(email)}"; + + return gravatarUrl; + } + + private static string Hash(string email) { using (var md5 = MD5.Create()) { - var gravatarHash = md5.ComputeHash(Encoding.UTF8.GetBytes(email.ToLowerInvariant().Trim())); - var gravatarUrl = $"https://www.gravatar.com/avatar/{gravatarHash}"; + var normalizedEmail = email.ToLowerInvariant().Trim(); + + var hashBytes = md5.ComputeHash(Encoding.UTF8.GetBytes(normalizedEmail)); + var hashBuilder = new StringBuilder(); + + for (var i = 0; i < hashBytes.Length; i++) + { + hashBuilder.Append(hashBytes[i].ToString("x2")); + } - return gravatarUrl; + return hashBuilder.ToString(); } } } diff --git a/tests/Squidex.Infrastructure.Tests/GravatarHelperTests.cs b/tests/Squidex.Infrastructure.Tests/GravatarHelperTests.cs new file mode 100644 index 000000000..09865302a --- /dev/null +++ b/tests/Squidex.Infrastructure.Tests/GravatarHelperTests.cs @@ -0,0 +1,37 @@ +// ========================================================================== +// GravatarHelperTests.cs +// Squidex Headless CMS +// ========================================================================== +// Copyright (c) Squidex Group +// All rights reserved. +// ========================================================================== + +using Xunit; + +namespace Squidex.Infrastructure +{ + public class GravatarHelperTests + { + [Theory] + [InlineData("MyEmailAddress@example.com ")] + [InlineData("MyEmailAddress@example.com")] + [InlineData("myemailaddress@example.com")] + public void Should_generate_picture_url(string email) + { + var url = GravatarHelper.CreatePictureUrl(email); + + Assert.Equal(url, "https://www.gravatar.com/avatar/0bc83cb571cd1c50ba6f3e8a78ef1346"); + } + + [Theory] + [InlineData("MyEmailAddress@example.com ")] + [InlineData("MyEmailAddress@example.com")] + [InlineData("myemailaddress@example.com")] + public void Should_generate_profile_url(string email) + { + var url = GravatarHelper.CreateProfileUrl(email); + + Assert.Equal(url, "https://www.gravatar.com/0bc83cb571cd1c50ba6f3e8a78ef1346"); + } + } +} \ No newline at end of file