mirror of
https://github.com/Govor-team/Govor.git
synced 2026-11-17 12:59:25 +00:00
Expand server tests and enforce coverage
Consolidate server tests under tests/, upgrade them to .NET 10, and add CI coverage thresholds and reports. Expand API and application boundary coverage while fixing related issues in media handling, friend blocking, session crypto, presence, username validation, and local storage.
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
using Govor.API.Common.Extensions;
|
||||
using Govor.Application.PushNotifications;
|
||||
using Govor.Application.PushNotifications.Providers;
|
||||
using Govor.Application.Storage;
|
||||
using Govor.Domain;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Moq;
|
||||
|
||||
namespace Govor.API.Tests.Unit.Common;
|
||||
|
||||
[TestFixture]
|
||||
public class ConfigurationTests
|
||||
{
|
||||
[TestCase(false)] [TestCase(true)]
|
||||
public void DatabaseConfigurationUsesPostgresAndHonorsDetailedErrors(bool detailed)
|
||||
{
|
||||
var config=new ConfigurationBuilder().AddInMemoryCollection(new Dictionary<string,string?>{{"ConnectionStrings:GovorDbContext","Host=localhost;Database=configuration_tests;Username=tests"},{"Database:EnableDetailedErrors",detailed.ToString()}}).Build();
|
||||
var services=new ServiceCollection();services.AddGovorDbContext(config);
|
||||
using var provider=services.BuildServiceProvider();using var scope=provider.CreateScope();var db=scope.ServiceProvider.GetRequiredService<GovorDbContext>();
|
||||
Assert.That(db.Database.ProviderName,Is.EqualTo("Npgsql.EntityFrameworkCore.PostgreSQL"));
|
||||
Assert.That(db.Database.CreateExecutionStrategy().RetriesOnFailure,Is.True);
|
||||
var options=scope.ServiceProvider.GetRequiredService<DbContextOptions<GovorDbContext>>();
|
||||
Assert.That(options.FindExtension<Microsoft.EntityFrameworkCore.Infrastructure.CoreOptionsExtension>()!.DetailedErrorsEnabled,Is.EqualTo(detailed));
|
||||
}
|
||||
[Test]
|
||||
public void DisabledFirebaseAndLocalStorageResolveThroughProductionRegistration()
|
||||
{
|
||||
var services=new ServiceCollection();services.AddLogging();
|
||||
services.AddSingleton<IConfiguration>(new ConfigurationBuilder().AddInMemoryCollection(new Dictionary<string,string?>{{"Firebase:Enabled","false"}}).Build());
|
||||
var environment=new Mock<IWebHostEnvironment>();environment.SetupGet(x=>x.ContentRootPath).Returns(Path.GetTempPath());services.AddSingleton(environment.Object);
|
||||
services.AddServices();using var provider=services.BuildServiceProvider();using var scope=provider.CreateScope();
|
||||
Assert.That(scope.ServiceProvider.GetRequiredService<IStorageService>(),Is.TypeOf<LocalStorageService>());
|
||||
Assert.That(scope.ServiceProvider.GetRequiredService<IPushNotificationProvider>(),Is.TypeOf<NullPushProvider>());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
using Govor.API.Common.Extensions;
|
||||
using Govor.API.Common.Mapping;
|
||||
using Govor.API.Filters;
|
||||
using Govor.Application.Profiles;
|
||||
using Govor.Application.Users.UserOnlineStatus;
|
||||
using Govor.Contracts.DTOs;
|
||||
using Govor.Contracts.Responses.SignalR;
|
||||
using Govor.Domain.Common;
|
||||
using Govor.Domain.Models.Users;
|
||||
using Govor.Tests.Shared;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using Moq;
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace Govor.API.Tests.Unit.Common;
|
||||
|
||||
[TestFixture]
|
||||
public class ErrorAndMappingTests
|
||||
{
|
||||
[TestCase(ErrorType.NotFound, 404, "Not Found")]
|
||||
[TestCase(ErrorType.Validation, 400, "Validation Error")]
|
||||
[TestCase(ErrorType.Conflict, 409, "Conflict")]
|
||||
[TestCase(ErrorType.Unauthorized, 401, "Unauthorized")]
|
||||
[TestCase(ErrorType.Forbidden, 403, "Forbidden")]
|
||||
[TestCase(ErrorType.ServerError, 500, "Internal Server Error")]
|
||||
[TestCase(ErrorType.Failure, 400, "Bad Request")]
|
||||
public void ResultErrorsPreserveCodeDetailsAndFieldErrors(ErrorType type, int status, string title)
|
||||
{
|
||||
var fields = new Dictionary<string, string[]> { { "field", ["required"] } };
|
||||
var error = new Error("code", "detail", type, fields);
|
||||
var response = (ObjectResult)Result.Failure<int>(error).ToActionResult();
|
||||
var problem = (ProblemDetails)response.Value!;
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(response.StatusCode, Is.EqualTo(status));
|
||||
Assert.That(problem.Title, Is.EqualTo(title));
|
||||
Assert.That(problem.Detail, Is.EqualTo("detail"));
|
||||
Assert.That(problem.Extensions["errorCode"], Is.EqualTo("code"));
|
||||
Assert.That(problem.Extensions["errors"], Is.SameAs(fields));
|
||||
});
|
||||
var hub = error.ToHubResult<int>();
|
||||
Assert.That((int)hub.Status, Is.EqualTo(status));
|
||||
Assert.That(hub.ErrorMessage, Is.EqualTo(type == ErrorType.ServerError ? "Internal server error." : "detail"));
|
||||
}
|
||||
|
||||
[TestCase(false)]
|
||||
[TestCase(true)]
|
||||
public void MappingActionsUseCurrentOnlineState(bool online)
|
||||
{
|
||||
var id = Guid.NewGuid();
|
||||
var store = new Mock<IOnlineUserStore>();
|
||||
store.Setup(x => x.IsOnline(id)).Returns(online);
|
||||
var user = new UserDto();
|
||||
new UserToUserDtoMappingAction(store.Object).Process(new User { Id = id }, user, null!);
|
||||
var profile = new UserProfileDto();
|
||||
new UserProfileToUserProfileDtoMappingAction(store.Object).Process(new UserProfile { Id = id }, profile, null!);
|
||||
Assert.That(user.IsOnline, Is.EqualTo(online));
|
||||
Assert.That(profile.IsOnline, Is.EqualTo(online));
|
||||
store.Verify(x => x.IsOnline(id), Times.Exactly(2));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user