feat: sync guard - Blockieren alter Clients beim sync
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
using LehrerApp.Sync;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Xunit;
|
||||
|
||||
namespace LehrerApp.Api.Tests;
|
||||
|
||||
public sealed class SyncProtocolVersionMiddlewareTests
|
||||
{
|
||||
[Theory]
|
||||
[InlineData("/api/sync/push")]
|
||||
[InlineData("/api/sync/plain/push")]
|
||||
[InlineData("/api/sync/attachments/datei")]
|
||||
[InlineData("/api/snapshot/upload")]
|
||||
public async Task FehlendeVersion_BlockiertAlleSyncKanaeleVorDemEndpoint(string path)
|
||||
{
|
||||
var endpointReached = false;
|
||||
var middleware = Middleware(() => endpointReached = true);
|
||||
var context = Context(path);
|
||||
|
||||
await middleware.InvokeAsync(context);
|
||||
|
||||
Assert.False(endpointReached);
|
||||
Assert.Equal(StatusCodes.Status426UpgradeRequired, context.Response.StatusCode);
|
||||
Assert.Equal(SyncProtocol.CurrentVersion,
|
||||
context.Response.Headers[SyncProtocol.VersionHeaderName].ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task AbweichendeVersion_BlockiertRequest()
|
||||
{
|
||||
var endpointReached = false;
|
||||
var middleware = Middleware(() => endpointReached = true);
|
||||
var context = Context("/api/sync/push");
|
||||
context.Request.Headers[SyncProtocol.VersionHeaderName] = "0";
|
||||
|
||||
await middleware.InvokeAsync(context);
|
||||
|
||||
Assert.False(endpointReached);
|
||||
Assert.Equal(StatusCodes.Status426UpgradeRequired, context.Response.StatusCode);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task AktuelleVersion_LaesstRequestZumEndpointDurch()
|
||||
{
|
||||
var endpointReached = false;
|
||||
var middleware = Middleware(() => endpointReached = true);
|
||||
var context = Context("/api/sync/push");
|
||||
context.Request.Headers[SyncProtocol.VersionHeaderName] = SyncProtocol.CurrentVersion;
|
||||
|
||||
await middleware.InvokeAsync(context);
|
||||
|
||||
Assert.True(endpointReached);
|
||||
Assert.NotEqual(StatusCodes.Status426UpgradeRequired, context.Response.StatusCode);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task NichtSyncEndpoint_BrauchtKeineVersion()
|
||||
{
|
||||
var endpointReached = false;
|
||||
var middleware = Middleware(() => endpointReached = true);
|
||||
var context = Context("/api/auth/login");
|
||||
|
||||
await middleware.InvokeAsync(context);
|
||||
|
||||
Assert.True(endpointReached);
|
||||
}
|
||||
|
||||
private static SyncProtocolVersionMiddleware Middleware(Action reached) => new(_ =>
|
||||
{
|
||||
reached();
|
||||
return Task.CompletedTask;
|
||||
});
|
||||
|
||||
private static DefaultHttpContext Context(string path)
|
||||
{
|
||||
var context = new DefaultHttpContext();
|
||||
context.Request.Path = path;
|
||||
context.Response.Body = new MemoryStream();
|
||||
return context;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user