mirror of
https://github.com/bitwarden/server
synced 2026-01-07 02:53:38 +00:00
Rewrite Icon fetching (#3023)
* Rewrite Icon fetching * Move validation to IconUri, Uri, or UriBuilder * `dotnet format` 🤖 * PR suggestions * Add not null compiler hint * Add twitter to test case * Move Uri manipulation to UriService * Implement MockedHttpClient Presents better, fluent handling of message matching and response building. * Add redirect handling tests * Add testing to models * More aggressively dispose content in icon link * Format 🤖 * Update icon lockfile * Convert to cloned stream for HttpResponseBuilder Content was being disposed when HttResponseMessage was being disposed. This avoids losing our reference to our content and allows multiple usages of the same `MockedHttpMessageResponse` * Move services to extension Extension is shared by testing and allows access to services from our service tests * Remove unused `using` * Prefer awaiting asyncs for better exception handling * `dotnet format` 🤖 * Await async * Update tests to use test TLD and ip ranges * Remove unused interfaces * Make assignments static when possible * Prefer invariant comparer to downcasing * Prefer injecting interface services to implementations * Prefer comparer set in HashSet initialization * Allow SVG icons * Filter out icons with unknown formats * Seek to beginning of MemoryStream after writing it * More appropriate to not return icon if it's invalid * Add svg icon test
This commit is contained in:
@@ -1,25 +1,25 @@
|
||||
using Bit.Icons.Services;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Xunit;
|
||||
|
||||
namespace Bit.Icons.Test.Services;
|
||||
|
||||
public class IconFetchingServiceTests
|
||||
public class IconFetchingServiceTests : ServiceTestBase<IconFetchingService>
|
||||
{
|
||||
[Theory]
|
||||
[InlineData("www.twitter.com")] // https site
|
||||
[InlineData("www.google.com")] // https site
|
||||
[InlineData("neverssl.com")] // http site
|
||||
[InlineData("ameritrade.com")]
|
||||
[InlineData("neopets.com")] // uses favicon.ico
|
||||
[InlineData("hopin.com")] // uses svg+xml format
|
||||
[InlineData("ameritrade.com")] // redirects to tdameritrace.com
|
||||
[InlineData("icloud.com")]
|
||||
[InlineData("bofa.com", Skip = "Broken in pipeline for .NET 6. Tracking link: https://bitwarden.atlassian.net/browse/PS-982")]
|
||||
public async Task GetIconAsync_Success(string domain)
|
||||
{
|
||||
var sut = new IconFetchingService(GetLogger());
|
||||
var sut = BuildSut();
|
||||
var result = await sut.GetIconAsync(domain);
|
||||
|
||||
Assert.NotNull(result);
|
||||
Assert.NotNull(result.Icon);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
@@ -28,23 +28,12 @@ public class IconFetchingServiceTests
|
||||
[InlineData("localhost")]
|
||||
public async Task GetIconAsync_ReturnsNull(string domain)
|
||||
{
|
||||
var sut = new IconFetchingService(GetLogger());
|
||||
var sut = BuildSut();
|
||||
var result = await sut.GetIconAsync(domain);
|
||||
|
||||
Assert.Null(result);
|
||||
}
|
||||
|
||||
private static ILogger<IconFetchingService> GetLogger()
|
||||
{
|
||||
var services = new ServiceCollection();
|
||||
services.AddLogging(b =>
|
||||
{
|
||||
b.ClearProviders();
|
||||
b.AddDebug();
|
||||
});
|
||||
|
||||
var provider = services.BuildServiceProvider();
|
||||
|
||||
return provider.GetRequiredService<ILogger<IconFetchingService>>();
|
||||
}
|
||||
private IconFetchingService BuildSut() =>
|
||||
GetService<IconFetchingService>();
|
||||
}
|
||||
|
||||
41
test/Icons.Test/Services/ServiceTestBase.cs
Normal file
41
test/Icons.Test/Services/ServiceTestBase.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
using Bit.Icons.Extensions;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Bit.Icons.Test.Services;
|
||||
|
||||
public class ServiceTestBase
|
||||
{
|
||||
internal ServiceCollection _services = new();
|
||||
internal ServiceProvider _provider;
|
||||
|
||||
public ServiceTestBase()
|
||||
{
|
||||
_services = new ServiceCollection();
|
||||
_services.AddLogging(b =>
|
||||
{
|
||||
b.ClearProviders();
|
||||
b.AddDebug();
|
||||
});
|
||||
|
||||
_services.ConfigureHttpClients();
|
||||
_services.AddHtmlParsing();
|
||||
_services.AddServices();
|
||||
|
||||
_provider = _services.BuildServiceProvider();
|
||||
}
|
||||
|
||||
public T GetService<T>() =>
|
||||
_provider.GetRequiredService<T>();
|
||||
}
|
||||
|
||||
public class ServiceTestBase<TSut> : ServiceTestBase where TSut : class
|
||||
{
|
||||
public ServiceTestBase() : base()
|
||||
{
|
||||
_services.AddTransient<TSut>();
|
||||
_provider = _services.BuildServiceProvider();
|
||||
}
|
||||
|
||||
public TSut Sut => GetService<TSut>();
|
||||
}
|
||||
Reference in New Issue
Block a user