Files
Govor/Govor.Server.Tests/TestDatabase.cs
Artemy 29cc51ee0c Add reactions, reliable push & session checks
Implements message reactions and reaction packs (domain models, services, admin HTTP API, media processing via ImageSharp and limits) and adds reliable push delivery (ChatPushNotification, ChatPushDispatcher, ChatPushWorker, improved ChatNotificationService). Enhances message reading (ReadChat API, unread counts, concurrency-safe receipts) and tightens session security (access-token session validation in JWT, hub filter, SessionAuthorization). Improves SignalR behavior (session groups, reaction/channel events, hub/error mappings). Adds EF migrations, many controller fixes (Forbid handling, password API), tests (Govor.Server.Tests), docs, CI and tooling updates (Dockerfile → .NET 10, GH Actions matrix for sqlite/postgres, Govor.Server.slnx, global.json), and small infra changes (AutoMapper bump, NullPushProvider, storage/token handling).
2026-10-03 17:42:24 +07:00

107 lines
3.7 KiB
C#

using Govor.Domain;
using Govor.Domain.Models;
using Govor.Domain.Models.Messages;
using Govor.Domain.Models.Users;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using System.Data.Common;
using Npgsql;
using Microsoft.EntityFrameworkCore.Diagnostics;
namespace Govor.Server.Tests;
public sealed class TestDatabase : IDisposable
{
public DbConnection Connection { get; }
private readonly string? _schema;
public GovorDbContext Context { get; }
public Guid Alice { get; } = Guid.NewGuid();
public Guid Bob { get; } = Guid.NewGuid();
public Guid Outsider { get; } = Guid.NewGuid();
public Guid ChatId { get; } = Guid.NewGuid();
public TestDatabase()
{
var postgres = Environment.GetEnvironmentVariable("TEST_POSTGRES_CONNECTION");
if (string.IsNullOrWhiteSpace(postgres))
Connection = new SqliteConnection("Data Source=:memory:");
else
{
var settings = new NpgsqlConnectionStringBuilder(postgres);
if (settings.Database?.EndsWith("_tests", StringComparison.Ordinal) != true)
throw new InvalidOperationException("PostgreSQL tests require a dedicated database ending in _tests.");
_schema = "test_" + Guid.NewGuid().ToString("N");
settings.SearchPath = _schema;
Connection = new NpgsqlConnection(settings.ConnectionString);
}
Connection.Open();
if (_schema is not null)
{
using var command = Connection.CreateCommand();
command.CommandText = $"CREATE SCHEMA {_schema}";
command.ExecuteNonQuery();
}
Context = CreateContext();
if (_schema is null)
Context.Database.EnsureCreated();
else
Context.Database.Migrate();
var invite = new Invitation
{
Id = Guid.NewGuid(), Code = "test", Description = "test",
EndDate = DateTime.UtcNow.AddDays(1), MaxParticipants = 100
};
Context.Invitations.Add(invite);
foreach (var id in new[] { Alice, Bob, Outsider })
Context.Users.Add(new User
{
Id = id, Username = id.ToString(), PasswordHash = "test", Description = "",
InviteId = invite.Id, WasOnline = DateTime.UtcNow
});
Context.PrivateChats.Add(new PrivateChat { Id = ChatId, UserAId = Alice, UserBId = Bob });
Context.SaveChanges();
}
public void ConfigureOptions(DbContextOptionsBuilder options)
{
if (_schema is null)
options.UseSqlite(Connection);
else
options.UseNpgsql(Connection);
}
public GovorDbContext CreateContext(params IInterceptor[] interceptors)
{
var options = new DbContextOptionsBuilder<GovorDbContext>();
ConfigureOptions(options);
options.AddInterceptors(interceptors);
return new GovorDbContext(options.Options);
}
public Message AddMessage(Guid? sender = null, Guid? chat = null,
RecipientType type = RecipientType.User, DateTime? sentAt = null, Guid? id = null)
{
var message = new Message
{
Id = id ?? Guid.NewGuid(), SenderId = sender ?? Alice,
RecipientId = chat ?? ChatId, RecipientType = type,
SentAt = sentAt ?? DateTime.UtcNow, EncryptedContent = "hello"
};
Context.Messages.Add(message);
Context.SaveChanges();
return message;
}
public void Dispose()
{
Context.Dispose();
if (_schema is not null)
{
using var command = Connection.CreateCommand();
command.CommandText = $"DROP SCHEMA {_schema} CASCADE";
command.ExecuteNonQuery();
}
Connection.Dispose();
}
}