Premiere commit - Omika
This commit is contained in:
@@ -0,0 +1,200 @@
|
||||
using System.Net;
|
||||
using System.Net.Http.Json;
|
||||
using Microsoft.AspNetCore.Mvc.Testing;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using Omika.Core.Abstractions;
|
||||
using Omika.Core.Compatibility;
|
||||
using Omika.Core.Contracts.Mobile.Sync;
|
||||
using Omika.Core.Interfaces.Mobile;
|
||||
using Omika.Core.Runtime;
|
||||
using Omika.Data.Configuration;
|
||||
using Omika.Data.Connection;
|
||||
using Omika.Data.Runtime;
|
||||
|
||||
namespace Omika.Tests.Api;
|
||||
|
||||
/// <summary>
|
||||
/// Tests HTTP des endpoints de synchronisation mobile.
|
||||
/// Vérifie l'authentification (sans token) et la réception d'une opération.
|
||||
/// </summary>
|
||||
[Collection("ApplicationRuntimeContext")]
|
||||
public class SyncEndpointsTests : IDisposable
|
||||
{
|
||||
private const string SigningKey = "test-signing-key-for-integration-tests-only";
|
||||
private readonly FakeSyncOperationService _fakeService = new();
|
||||
private readonly WebApplicationFactory<Program> _factory;
|
||||
|
||||
public SyncEndpointsTests()
|
||||
{
|
||||
ApplicationRuntimeContext.Reset();
|
||||
|
||||
_factory = new WebApplicationFactory<Program>()
|
||||
.WithWebHostBuilder(builder =>
|
||||
{
|
||||
builder.ConfigureServices(services =>
|
||||
{
|
||||
var config = new ConfigurationBuilder()
|
||||
.AddInMemoryCollection(new Dictionary<string, string?>
|
||||
{
|
||||
["Hfsql:Server"] = "test-server",
|
||||
["Hfsql:DatabaseName"] = "test-db",
|
||||
["MobileAuth:SigningKey"] = SigningKey
|
||||
})
|
||||
.Build();
|
||||
services.AddSingleton<IConfiguration>(config);
|
||||
|
||||
services.RemoveAll<IDatabaseRuntimeInitializer>();
|
||||
services.AddScoped<IDatabaseRuntimeInitializer, FakeRuntimeInitializer>();
|
||||
|
||||
services.RemoveAll<IHfsqlConnection>();
|
||||
services.AddScoped<IHfsqlConnection, FakeHfsqlConnection>();
|
||||
|
||||
services.RemoveAll<ISyncOperationService>();
|
||||
services.AddScoped<ISyncOperationService>(_ => _fakeService);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_factory.Dispose();
|
||||
ApplicationRuntimeContext.Reset();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Receive_WithoutToken_Returns401()
|
||||
{
|
||||
var client = _factory.CreateClient();
|
||||
|
||||
var response = await client.PostAsJsonAsync("/api/sync/operations", ValidOperation());
|
||||
|
||||
Assert.Equal(401, (int)response.StatusCode);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Receive_ValidOperation_Returns200Received()
|
||||
{
|
||||
_fakeService.Receipt = new SyncOperationReceipt
|
||||
{
|
||||
OperationId = Guid.NewGuid(),
|
||||
Status = "RECEIVED",
|
||||
ReceivedAtUtc = DateTimeOffset.UtcNow
|
||||
};
|
||||
var client = _factory.CreateClient();
|
||||
client.DefaultRequestHeaders.Authorization =
|
||||
new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", CreateValidToken());
|
||||
|
||||
var response = await client.PostAsJsonAsync("/api/sync/operations", ValidOperation());
|
||||
|
||||
Assert.Equal(200, (int)response.StatusCode);
|
||||
var body = await response.Content.ReadFromJsonAsync<SyncOperationReceipt>();
|
||||
Assert.NotNull(body);
|
||||
Assert.Equal("RECEIVED", body.Status);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Receive_ValidOperation_PassesIdentityFromToken()
|
||||
{
|
||||
_fakeService.Receipt = new SyncOperationReceipt
|
||||
{
|
||||
OperationId = Guid.NewGuid(),
|
||||
Status = "RECEIVED",
|
||||
ReceivedAtUtc = DateTimeOffset.UtcNow
|
||||
};
|
||||
var client = _factory.CreateClient();
|
||||
client.DefaultRequestHeaders.Authorization =
|
||||
new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", CreateValidToken());
|
||||
|
||||
var response = await client.PostAsJsonAsync("/api/sync/operations", ValidOperation());
|
||||
|
||||
Assert.Equal(200, (int)response.StatusCode);
|
||||
Assert.NotNull(_fakeService.LastIdentity);
|
||||
Assert.Equal("BATIK.01", _fakeService.LastIdentity.CodeClient);
|
||||
Assert.Equal("device-abc-123", _fakeService.LastIdentity.DeviceId);
|
||||
Assert.Equal("user-1", _fakeService.LastIdentity.UserId);
|
||||
}
|
||||
|
||||
private static SyncOperation ValidOperation()
|
||||
{
|
||||
return new SyncOperation
|
||||
{
|
||||
OperationId = Guid.NewGuid(),
|
||||
DeviceId = "device-abc-123",
|
||||
EntityType = "Client",
|
||||
OperationType = SyncOperationType.Create,
|
||||
EntityGuid = Guid.NewGuid(),
|
||||
Payload = "{}"
|
||||
};
|
||||
}
|
||||
|
||||
private string CreateValidToken()
|
||||
{
|
||||
using var scope = _factory.Services.CreateScope();
|
||||
var tokenService = scope.ServiceProvider.GetRequiredService<ITokenService>();
|
||||
return tokenService.GenerateToken(new MobileTokenIdentity
|
||||
{
|
||||
CodeClient = "BATIK.01",
|
||||
DeviceId = "device-abc-123",
|
||||
UserId = "user-1",
|
||||
CollaborateurId = "7",
|
||||
NumeroLicence = "1ab053eb5e274e1f92455bbb7de9f2d3"
|
||||
});
|
||||
}
|
||||
|
||||
// ===== Fakes =====
|
||||
|
||||
private sealed class FakeSyncOperationService : ISyncOperationService
|
||||
{
|
||||
public SyncOperationReceipt Receipt { get; set; } = new();
|
||||
public MobileTokenIdentity? LastIdentity { get; private set; }
|
||||
|
||||
public Task<SyncOperationReceipt> ReceiveAsync(
|
||||
SyncOperation operation,
|
||||
MobileTokenIdentity identity,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
LastIdentity = identity;
|
||||
return Task.FromResult(Receipt);
|
||||
}
|
||||
|
||||
public Task<SyncStatusResponse> GetStatusAsync(
|
||||
Guid operationId,
|
||||
string authenticatedDeviceId,
|
||||
CancellationToken cancellationToken = default)
|
||||
=> Task.FromResult(new SyncStatusResponse
|
||||
{
|
||||
OperationId = operationId,
|
||||
Status = SyncStatus.StatusNotAvailable,
|
||||
DeviceId = authenticatedDeviceId
|
||||
});
|
||||
}
|
||||
|
||||
private sealed class FakeRuntimeInitializer : IDatabaseRuntimeInitializer
|
||||
{
|
||||
public Task<Result> InitializeAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
ApplicationRuntimeContext.SetDetectedVersion(DatabaseSchemaVersion.Batik);
|
||||
return Task.FromResult(Result.Success());
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class FakeHfsqlConnection : IHfsqlConnection, IDisposable
|
||||
{
|
||||
public HfsqlOptions Options => new();
|
||||
public HfsqlConnectionState State => HfsqlConnectionState.Closed;
|
||||
public void Dispose() { }
|
||||
public Task OpenAsync(CancellationToken ct = default) => Task.CompletedTask;
|
||||
public Task CloseAsync() => Task.CompletedTask;
|
||||
public Task<object?> ExecuteScalarAsync(string sql, IReadOnlyList<object?>? parameters = null, CancellationToken ct = default)
|
||||
=> Task.FromResult<object?>(null);
|
||||
public Task<int> ExecuteNonQueryAsync(string sql, IReadOnlyList<object?>? parameters = null, CancellationToken ct = default)
|
||||
=> Task.FromResult(0);
|
||||
public Task<IReadOnlyList<IReadOnlyDictionary<string, object?>>> QueryAsync(string sql, IReadOnlyList<object?>? parameters = null, CancellationToken ct = default)
|
||||
=> Task.FromResult<IReadOnlyList<IReadOnlyDictionary<string, object?>>>([]);
|
||||
public Task<IReadOnlyList<HfsqlColumnMetadata>> GetColumnsAsync(string tableName, CancellationToken ct = default)
|
||||
=> Task.FromResult<IReadOnlyList<HfsqlColumnMetadata>>([]);
|
||||
public ValueTask DisposeAsync() => ValueTask.CompletedTask;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user