mirror of
https://github.com/bitwarden/server.git
synced 2025-06-30 07:36:14 -05:00
[SM-153] Add scaffolded API integration test project (#2209)
This commit is contained in:
@ -35,13 +35,18 @@ namespace Bit.IntegrationTestCommon.Factories
|
||||
{
|
||||
builder.ConfigureAppConfiguration(c =>
|
||||
{
|
||||
c.SetBasePath(AppContext.BaseDirectory)
|
||||
.AddJsonFile("appsettings.json")
|
||||
.AddJsonFile("appsettings.Development.json");
|
||||
|
||||
c.AddUserSecrets(typeof(Identity.Startup).Assembly, optional: true);
|
||||
c.AddInMemoryCollection(new Dictionary<string, string>
|
||||
{
|
||||
// Manually insert a EF provider so that ConfigureServices will add EF repositories but we will override
|
||||
// DbContextOptions to use an in memory database
|
||||
{ "globalSettings:databaseProvider", "postgres" },
|
||||
{ "globalSettings:postgreSql:connectionString", "Host=localhost;Username=test;Password=test;Database=test" },
|
||||
|
||||
|
||||
// Clear the redis connection string for distributed caching, forcing an in-memory implementation
|
||||
{ "globalSettings:redis:connectionString", ""}
|
||||
});
|
||||
@ -82,7 +87,7 @@ namespace Bit.IntegrationTestCommon.Factories
|
||||
services.AddSingleton<IEventRepository, EventRepository>();
|
||||
|
||||
// Our Rate limiter works so well that it begins to fail tests unless we carve out
|
||||
// one whitelisted ip. We should still test the rate limiter though and they should change the Ip
|
||||
// one whitelisted ip. We should still test the rate limiter though and they should change the Ip
|
||||
// to something that is NOT whitelisted
|
||||
services.Configure<IpRateLimitOptions>(options =>
|
||||
{
|
||||
@ -91,6 +96,9 @@ namespace Bit.IntegrationTestCommon.Factories
|
||||
FactoryConstants.WhitelistedIp,
|
||||
};
|
||||
});
|
||||
|
||||
// Fix IP Rate Limiting
|
||||
services.AddSingleton<IStartupFilter, CustomStartupFilter>();
|
||||
});
|
||||
}
|
||||
|
||||
|
36
test/IntegrationTestCommon/FakeRemoteIpAddressMiddleware.cs
Normal file
36
test/IntegrationTestCommon/FakeRemoteIpAddressMiddleware.cs
Normal file
@ -0,0 +1,36 @@
|
||||
using System.Net;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
|
||||
namespace Bit.IntegrationTestCommon;
|
||||
|
||||
public class FakeRemoteIpAddressMiddleware
|
||||
{
|
||||
private readonly RequestDelegate _next;
|
||||
private readonly IPAddress _fakeIpAddress;
|
||||
|
||||
public FakeRemoteIpAddressMiddleware(RequestDelegate next, IPAddress fakeIpAddress = null)
|
||||
{
|
||||
_next = next;
|
||||
_fakeIpAddress = fakeIpAddress ?? IPAddress.Parse("127.0.0.1");
|
||||
}
|
||||
|
||||
public async Task Invoke(HttpContext httpContext)
|
||||
{
|
||||
httpContext.Connection.RemoteIpAddress ??= _fakeIpAddress;
|
||||
await _next(httpContext);
|
||||
}
|
||||
}
|
||||
|
||||
public class CustomStartupFilter : IStartupFilter
|
||||
{
|
||||
public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next)
|
||||
{
|
||||
return app =>
|
||||
{
|
||||
app.UseMiddleware<FakeRemoteIpAddressMiddleware>();
|
||||
next(app);
|
||||
};
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user