Browse Source

GravatarHelper fixed

pull/65/head
Sebastian Stehle 9 years ago
parent
commit
c8896e8be7
  1. 29
      src/Squidex.Infrastructure/GravatarHelper.cs
  2. 37
      tests/Squidex.Infrastructure.Tests/GravatarHelperTests.cs

29
src/Squidex.Infrastructure/GravatarHelper.cs

@ -9,18 +9,41 @@
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Text; using System.Text;
// ReSharper disable ForCanBeConvertedToForeach
namespace Squidex.Infrastructure namespace Squidex.Infrastructure
{ {
public static class GravatarHelper public static class GravatarHelper
{ {
public static string CreatePictureUrl(string email) 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()) using (var md5 = MD5.Create())
{ {
var gravatarHash = md5.ComputeHash(Encoding.UTF8.GetBytes(email.ToLowerInvariant().Trim())); var normalizedEmail = email.ToLowerInvariant().Trim();
var gravatarUrl = $"https://www.gravatar.com/avatar/{gravatarHash}";
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();
} }
} }
} }

37
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");
}
}
}
Loading…
Cancel
Save