From 213332f83f343dd921a0ab194e71e5d809941fcf Mon Sep 17 00:00:00 2001 From: "Eric J. Smith" Date: Tue, 17 Dec 2024 14:00:51 -0600 Subject: [PATCH] Fix tests --- .../Exceptionless.Insulation.csproj | 6 +- .../Exceptionless.Job.csproj | 8 +- .../Exceptionless.Web.csproj | 12 +- src/Exceptionless.Web/Startup.cs | 3 +- .../AspireWebHostFactory.cs | 23 ++-- .../Controllers/AuthControllerTests.cs | 108 +++++++++--------- .../Exceptionless.Tests.csproj | 2 +- .../IntegrationTestsBase.cs | 46 +++----- tests/Exceptionless.Tests/TestWithServices.cs | 9 +- 9 files changed, 101 insertions(+), 116 deletions(-) diff --git a/src/Exceptionless.Insulation/Exceptionless.Insulation.csproj b/src/Exceptionless.Insulation/Exceptionless.Insulation.csproj index 94aca8e88..953d5aede 100644 --- a/src/Exceptionless.Insulation/Exceptionless.Insulation.csproj +++ b/src/Exceptionless.Insulation/Exceptionless.Insulation.csproj @@ -14,10 +14,10 @@ - - + + - + diff --git a/src/Exceptionless.Job/Exceptionless.Job.csproj b/src/Exceptionless.Job/Exceptionless.Job.csproj index 10b6c6b6b..a7161b3e2 100644 --- a/src/Exceptionless.Job/Exceptionless.Job.csproj +++ b/src/Exceptionless.Job/Exceptionless.Job.csproj @@ -7,7 +7,7 @@ - + @@ -15,12 +15,12 @@ - + - + - + diff --git a/src/Exceptionless.Web/Exceptionless.Web.csproj b/src/Exceptionless.Web/Exceptionless.Web.csproj index 13d3b903c..7ca6504b2 100644 --- a/src/Exceptionless.Web/Exceptionless.Web.csproj +++ b/src/Exceptionless.Web/Exceptionless.Web.csproj @@ -16,8 +16,8 @@ - - + + @@ -25,13 +25,13 @@ - + - + - - + + diff --git a/src/Exceptionless.Web/Startup.cs b/src/Exceptionless.Web/Startup.cs index 8b7bfdfcf..c20655cd0 100644 --- a/src/Exceptionless.Web/Startup.cs +++ b/src/Exceptionless.Web/Startup.cs @@ -311,7 +311,8 @@ ApplicationException applicationException when applicationException.Message.Cont { o.EnrichDiagnosticContext = (context, httpContext) => { - context.Set("ActivityId", Activity.Current?.Id); + if (Activity.Current?.Id is not null) + context.Set("ActivityId", Activity.Current.Id); }; o.MessageTemplate = "{ActivityId} HTTP {RequestMethod} {RequestPath} responded {StatusCode} in {Elapsed:0.0000} ms"; o.GetLevel = (context, duration, ex) => diff --git a/tests/Exceptionless.Tests/AspireWebHostFactory.cs b/tests/Exceptionless.Tests/AspireWebHostFactory.cs index 1d389b727..f7778df10 100644 --- a/tests/Exceptionless.Tests/AspireWebHostFactory.cs +++ b/tests/Exceptionless.Tests/AspireWebHostFactory.cs @@ -1,6 +1,5 @@ using Aspire.Hosting; using Aspire.Hosting.ApplicationModel; -using Aspire.Hosting.Testing; using Exceptionless.Insulation.Configuration; using Exceptionless.Web; using Microsoft.AspNetCore.Mvc.Testing; @@ -15,30 +14,22 @@ public class AspireWebHostFactory : WebApplicationFactory, IAsyncLifeti public DistributedApplication App => _app ?? throw new InvalidOperationException("The application is not initialized"); - public string? ElasticsearchConnectionString { get; private set; } - public string? RedisConnectionString { get; private set; } - - public async Task InitializeAsync() + public Task InitializeAsync() { var options = new DistributedApplicationOptions { AssemblyName = typeof(ElasticsearchResource).Assembly.FullName, DisableDashboard = true }; var builder = DistributedApplication.CreateBuilder(options); - builder.AddElasticsearch("Elasticsearch") - .WithContainerName("Exceptionless-Elasticsearch-Test") + // don't use random ports for tests + builder.Configuration["DcpPublisher:RandomizePorts"] = "false"; + + builder.AddElasticsearch("Elasticsearch", port: 9200) + .WithContainerName("Exceptionless-Elasticsearch") .WithImageTag("8.17.0") .WithLifetime(ContainerLifetime.Persistent); - builder.AddRedis("Redis") - .WithContainerName("Exceptionless-Redis-Test") - .WithImageTag("7.4") - .WithLifetime(ContainerLifetime.Persistent);; - _app = builder.Build(); - await _app.StartAsync(); - - ElasticsearchConnectionString = await _app.GetConnectionStringAsync("Elasticsearch"); - RedisConnectionString = await _app.GetConnectionStringAsync("Redis"); + return _app.StartAsync(); } protected override void ConfigureWebHost(IWebHostBuilder builder) diff --git a/tests/Exceptionless.Tests/Controllers/AuthControllerTests.cs b/tests/Exceptionless.Tests/Controllers/AuthControllerTests.cs index 54b5a0414..92336f058 100644 --- a/tests/Exceptionless.Tests/Controllers/AuthControllerTests.cs +++ b/tests/Exceptionless.Tests/Controllers/AuthControllerTests.cs @@ -22,17 +22,21 @@ namespace Exceptionless.Tests.Controllers; public class AuthControllerTests : IntegrationTestsBase { - private readonly AuthOptions _authOptions; - private readonly IUserRepository _userRepository; - private readonly IOrganizationRepository _organizationRepository; - private readonly ITokenRepository _tokenRepository; + private AuthOptions? _authOptions; + private IUserRepository? _userRepository; + private IOrganizationRepository? _organizationRepository; + private ITokenRepository? _tokenRepository; public AuthControllerTests(ITestOutputHelper output, AspireWebHostFactory factory) : base(output, factory) { + } + + public override async Task InitializeAsync() + { + await base.InitializeAsync(); _authOptions = GetService(); _authOptions.EnableAccountCreation = true; _authOptions.EnableActiveDirectoryAuth = false; - _organizationRepository = GetService(); _userRepository = GetService(); _tokenRepository = GetService(); @@ -71,7 +75,7 @@ public async Task CannotSignupWithoutPassword() [InlineData(false, "test1@exceptionless.io", "Password1$")] public Task CannotSignupWhenAccountCreationDisabledWithNoTokenAsync(bool enableAdAuth, string email, string password) { - _authOptions.EnableAccountCreation = false; + _authOptions!.EnableAccountCreation = false; _authOptions.EnableActiveDirectoryAuth = enableAdAuth; if (enableAdAuth && email == TestDomainLoginProvider.ValidUsername) @@ -100,7 +104,7 @@ public Task CannotSignupWhenAccountCreationDisabledWithNoTokenAsync(bool enableA [InlineData(false, "test2@exceptionless.io", "Password1$")] public Task CannotSignupWhenAccountCreationDisabledWithInvalidTokenAsync(bool enableAdAuth, string email, string password) { - _authOptions.EnableAccountCreation = false; + _authOptions!.EnableAccountCreation = false; _authOptions.EnableActiveDirectoryAuth = enableAdAuth; if (enableAdAuth && email == TestDomainLoginProvider.ValidUsername) @@ -128,7 +132,7 @@ public Task CannotSignupWhenAccountCreationDisabledWithInvalidTokenAsync(bool en [InlineData(false, "test3@exceptionless.io", "Password1$")] public async Task CanSignupWhenAccountCreationDisabledWithValidTokenAsync(bool enableAdAuth, string email, string password) { - _authOptions.EnableAccountCreation = false; + _authOptions!.EnableAccountCreation = false; _authOptions.EnableActiveDirectoryAuth = enableAdAuth; if (enableAdAuth && email == TestDomainLoginProvider.ValidUsername) @@ -137,7 +141,7 @@ public async Task CanSignupWhenAccountCreationDisabledWithValidTokenAsync(bool e email = provider.GetEmailAddressFromUsername(email); } - var results = await _organizationRepository.GetAllAsync(); + var results = await _organizationRepository!.GetAllAsync(); var organization = results.Documents.First(); var invite = new Invite @@ -166,7 +170,7 @@ public async Task CanSignupWhenAccountCreationDisabledWithValidTokenAsync(bool e Assert.NotNull(result); Assert.False(String.IsNullOrEmpty(result.Token)); - var user = await _userRepository.GetByEmailAddressAsync(email); + var user = await _userRepository!.GetByEmailAddressAsync(email); Assert.NotNull(user); Assert.Equal("Test", user.FullName); Assert.Equal(email, user.EmailAddress); @@ -182,13 +186,13 @@ public async Task CanSignupWhenAccountCreationDisabledWithValidTokenAsync(bool e [Fact] public async Task CanSignupWhenAccountCreationDisabledWithValidTokenAndInvalidAdAccountAsync() { - _authOptions.EnableAccountCreation = false; + _authOptions!.EnableAccountCreation = false; _authOptions.EnableActiveDirectoryAuth = true; const string email = "test-user1@exceptionless.io"; const string password = "invalidAccount1"; - var organizations = await _organizationRepository.GetAllAsync(); + var organizations = await _organizationRepository!.GetAllAsync(); var organization = organizations.Documents.First(); var invite = new Invite { @@ -218,7 +222,7 @@ await SendRequestAsync(r => r [Fact] public async Task CanSignupWhenAccountCreationEnabledWithNoTokenAsync() { - _authOptions.EnableAccountCreation = true; + _authOptions!.EnableAccountCreation = true; const string email = "test4@exceptionless.io"; const string password = "Password1$"; @@ -239,7 +243,7 @@ public async Task CanSignupWhenAccountCreationEnabledWithNoTokenAsync() Assert.NotNull(result); Assert.False(String.IsNullOrEmpty(result.Token)); - var user = await _userRepository.GetByEmailAddressAsync(email); + var user = await _userRepository!.GetByEmailAddressAsync(email); Assert.NotNull(user); Assert.Equal("Test", user.FullName); Assert.Equal(email, user.EmailAddress); @@ -254,7 +258,7 @@ public async Task CanSignupWhenAccountCreationEnabledWithNoTokenAsync() [Fact] public async Task CanSignupWhenAccountCreationEnabledWithNoTokenAndValidAdAccountAsync() { - _authOptions.EnableAccountCreation = true; + _authOptions!.EnableAccountCreation = true; _authOptions.EnableActiveDirectoryAuth = true; var provider = new TestDomainLoginProvider(); @@ -280,7 +284,7 @@ public async Task CanSignupWhenAccountCreationEnabledWithNoTokenAndValidAdAccoun [Fact] public Task CanSignupWhenAccountCreationEnabledWithNoTokenAndInvalidAdAccountAsync() { - _authOptions.EnableAccountCreation = true; + _authOptions!.EnableAccountCreation = true; _authOptions.EnableActiveDirectoryAuth = true; return SendRequestAsync(r => r @@ -300,9 +304,9 @@ public Task CanSignupWhenAccountCreationEnabledWithNoTokenAndInvalidAdAccountAsy [Fact] public async Task CanSignupWhenAccountCreationEnabledWithValidTokenAsync() { - _authOptions.EnableAccountCreation = true; + _authOptions!.EnableAccountCreation = true; - var organizations = await _organizationRepository.GetAllAsync(); + var organizations = await _organizationRepository!.GetAllAsync(); var organization = organizations.Documents.First(); const string email = "test5@exceptionless.io"; const string name = "Test"; @@ -338,7 +342,7 @@ public async Task CanSignupWhenAccountCreationEnabledWithValidTokenAsync() await RefreshDataAsync(); - var user = await _userRepository.GetByEmailAddressAsync(email); + var user = await _userRepository!.GetByEmailAddressAsync(email); Assert.NotNull(user); Assert.Equal("Test", user.FullName); Assert.NotEmpty(user.OrganizationIds); @@ -350,7 +354,7 @@ public async Task CanSignupWhenAccountCreationEnabledWithValidTokenAsync() organization = await _organizationRepository.GetByIdAsync(organization.Id); Assert.Empty(organization.Invites); - var token = await _tokenRepository.GetByIdAsync(result.Token); + var token = await _tokenRepository!.GetByIdAsync(result.Token); Assert.NotNull(token); Assert.Equal(user.Id, token.UserId); Assert.Equal(TokenType.Authentication, token.Type); @@ -363,13 +367,13 @@ public async Task CanSignupWhenAccountCreationEnabledWithValidTokenAsync() [Fact] public async Task CanSignupWhenAccountCreationEnabledWithValidTokenAndValidAdAccountAsync() { - _authOptions.EnableAccountCreation = true; + _authOptions!.EnableAccountCreation = true; _authOptions.EnableActiveDirectoryAuth = true; var provider = new TestDomainLoginProvider(); string email = provider.GetEmailAddressFromUsername(TestDomainLoginProvider.ValidUsername); - var results = await _organizationRepository.GetAllAsync(); + var results = await _organizationRepository!.GetAllAsync(); var organization = results.Documents.First(); var invite = new Invite { @@ -401,11 +405,11 @@ public async Task CanSignupWhenAccountCreationEnabledWithValidTokenAndValidAdAcc [Fact] public async Task CanSignupWhenAccountCreationEnabledWithValidTokenAndInvalidAdAccountAsync() { - _authOptions.EnableAccountCreation = true; + _authOptions!.EnableAccountCreation = true; _authOptions.EnableActiveDirectoryAuth = true; string email = "test-user4@exceptionless.io"; - var results = await _organizationRepository.GetAllAsync(); + var results = await _organizationRepository!.GetAllAsync(); var organization = results.Documents.First(); var invite = new Invite { @@ -448,7 +452,7 @@ public async Task SignupShouldFailWhenUsingExistingAccountWithNoPasswordOrInvali }; user.MarkEmailAddressVerified(); - await _userRepository.AddAsync(user); + await _userRepository!.AddAsync(user); var problemDetails = await SendRequestAsAsync(r => r .Post() @@ -482,7 +486,7 @@ await SendRequestAsync(r => r [Fact] public async Task LoginValidAsync() { - _authOptions.EnableActiveDirectoryAuth = false; + _authOptions!.EnableActiveDirectoryAuth = false; const string email = "test6@exceptionless.io"; const string password = "Test6 password"; @@ -497,7 +501,7 @@ public async Task LoginValidAsync() }; user.MarkEmailAddressVerified(); - await _userRepository.AddAsync(user); + await _userRepository!.AddAsync(user); var result = await SendRequestAsAsync(r => r .Post() @@ -517,7 +521,7 @@ public async Task LoginValidAsync() [Fact] public async Task LoginInvalidPasswordAsync() { - _authOptions.EnableActiveDirectoryAuth = false; + _authOptions!.EnableActiveDirectoryAuth = false; const string email = "test7@exceptionless.io"; const string password = "Test7 password"; @@ -533,7 +537,7 @@ public async Task LoginInvalidPasswordAsync() }; user.MarkEmailAddressVerified(); - await _userRepository.AddAsync(user); + await _userRepository!.AddAsync(user); await SendRequestAsync(r => r .Post() @@ -550,7 +554,7 @@ await SendRequestAsync(r => r [Fact] public async Task LoginNoSuchUserAsync() { - _authOptions.EnableActiveDirectoryAuth = false; + _authOptions!.EnableActiveDirectoryAuth = false; const string email = "test8@exceptionless.io"; const string password = "Test8 password"; @@ -565,7 +569,7 @@ public async Task LoginNoSuchUserAsync() }; user.MarkEmailAddressVerified(); - await _userRepository.AddAsync(user); + await _userRepository!.AddAsync(user); await SendRequestAsync(r => r .Post() @@ -582,7 +586,7 @@ await SendRequestAsync(r => r [Fact] public async Task LoginValidExistingActiveDirectoryAsync() { - _authOptions.EnableActiveDirectoryAuth = true; + _authOptions!.EnableActiveDirectoryAuth = true; var provider = new TestDomainLoginProvider(); string email = provider.GetEmailAddressFromUsername(TestDomainLoginProvider.ValidUsername); @@ -593,7 +597,7 @@ public async Task LoginValidExistingActiveDirectoryAsync() }; user.MarkEmailAddressVerified(); - await _userRepository.AddAsync(user); + await _userRepository!.AddAsync(user); var result = await SendRequestAsAsync(r => r .Post() @@ -613,7 +617,7 @@ public async Task LoginValidExistingActiveDirectoryAsync() [Fact] public Task LoginValidNonExistentActiveDirectoryAsync() { - _authOptions.EnableActiveDirectoryAuth = true; + _authOptions!.EnableActiveDirectoryAuth = true; var provider = new TestDomainLoginProvider(); string email = provider.GetEmailAddressFromUsername(TestDomainLoginProvider.ValidUsername); @@ -633,7 +637,7 @@ public Task LoginValidNonExistentActiveDirectoryAsync() [Fact] public async Task LoginInvalidNonExistentActiveDirectoryAsync() { - _authOptions.EnableActiveDirectoryAuth = true; + _authOptions!.EnableActiveDirectoryAuth = true; var provider = new TestDomainLoginProvider(); string email = provider.GetEmailAddressFromUsername(TestDomainLoginProvider.ValidUsername); @@ -649,14 +653,14 @@ await SendRequestAsync(r => r ); // Verify that a user account was not added - var user = await _userRepository.GetByEmailAddressAsync($"{email}.au"); + var user = await _userRepository!.GetByEmailAddressAsync($"{email}.au"); Assert.Null(user); } [Fact] public async Task LoginInvalidExistingActiveDirectoryAsync() { - _authOptions.EnableActiveDirectoryAuth = true; + _authOptions!.EnableActiveDirectoryAuth = true; var provider = new TestDomainLoginProvider(); string email = provider.GetEmailAddressFromUsername(TestDomainLoginProvider.ValidUsername); @@ -667,7 +671,7 @@ public async Task LoginInvalidExistingActiveDirectoryAsync() }; user.MarkEmailAddressVerified(); - await _userRepository.AddAsync(user); + await _userRepository!.AddAsync(user); await SendRequestAsync(r => r .Post() @@ -684,7 +688,7 @@ await SendRequestAsync(r => r [Fact] public async Task LoginInvalidExistingActiveDirectoryAccountUsingUserNameLoginAsync() { - _authOptions.EnableActiveDirectoryAuth = true; + _authOptions!.EnableActiveDirectoryAuth = true; var provider = new TestDomainLoginProvider(); string email = provider.GetEmailAddressFromUsername(TestDomainLoginProvider.ValidUsername); @@ -695,7 +699,7 @@ public async Task LoginInvalidExistingActiveDirectoryAccountUsingUserNameLoginAs }; user.MarkEmailAddressVerified(); - await _userRepository.AddAsync(user); + await _userRepository!.AddAsync(user); await SendRequestAsync(r => r .Post() @@ -727,7 +731,7 @@ public async Task CanChangePasswordAsync() }; user.MarkEmailAddressVerified(); - await _userRepository.AddAsync(user); + await _userRepository!.AddAsync(user); var result = await SendRequestAsAsync(r => r .Post() @@ -743,7 +747,7 @@ public async Task CanChangePasswordAsync() Assert.NotNull(result); Assert.NotEmpty(result.Token); - var token = await _tokenRepository.GetByIdAsync(result.Token); + var token = await _tokenRepository!.GetByIdAsync(result.Token); Assert.NotNull(token); var actualUser = await _userRepository.GetByIdAsync(token.UserId); @@ -788,7 +792,7 @@ public async Task ChangePasswordShouldFailWithCurrentPasswordAsync() }; user.MarkEmailAddressVerified(); - await _userRepository.AddAsync(user); + await _userRepository!.AddAsync(user); var result = await SendRequestAsAsync(r => r .Post() @@ -804,7 +808,7 @@ public async Task ChangePasswordShouldFailWithCurrentPasswordAsync() Assert.NotNull(result); Assert.NotEmpty(result.Token); - var token = await _tokenRepository.GetByIdAsync(result.Token); + var token = await _tokenRepository!.GetByIdAsync(result.Token); Assert.NotNull(token); var actualUser = await _userRepository.GetByIdAsync(token.UserId); @@ -852,7 +856,7 @@ public async Task CanResetPasswordAsync() Assert.NotNull(user.PasswordResetToken); Assert.True(user.PasswordResetTokenExpiration.IsAfter(TimeProvider.GetUtcNow().UtcDateTime)); - await _userRepository.AddAsync(user); + await _userRepository!.AddAsync(user); var result = await SendRequestAsAsync(r => r .Post() @@ -868,7 +872,7 @@ public async Task CanResetPasswordAsync() Assert.NotNull(result); Assert.NotEmpty(result.Token); - var token = await _tokenRepository.GetByIdAsync(result.Token); + var token = await _tokenRepository!.GetByIdAsync(result.Token); Assert.NotNull(token); var actualUser = await _userRepository.GetByIdAsync(token.UserId); @@ -913,7 +917,7 @@ public async Task ResetPasswordShouldFailWithCurrentPasswordAsync() Assert.NotNull(user.PasswordResetToken); Assert.True(user.PasswordResetTokenExpiration.IsAfter(TimeProvider.GetUtcNow().UtcDateTime)); - await _userRepository.AddAsync(user); + await _userRepository!.AddAsync(user); var result = await SendRequestAsAsync(r => r .Post() @@ -929,7 +933,7 @@ public async Task ResetPasswordShouldFailWithCurrentPasswordAsync() Assert.NotNull(result); Assert.NotEmpty(result.Token); - var token = await _tokenRepository.GetByIdAsync(result.Token); + var token = await _tokenRepository!.GetByIdAsync(result.Token); Assert.NotNull(token); var actualUser = await _userRepository.GetByIdAsync(token.UserId); @@ -973,7 +977,7 @@ public async Task CanLogoutUserAsync() }; user.MarkEmailAddressVerified(); - await _userRepository.AddAsync(user); + await _userRepository!.AddAsync(user); var result = await SendRequestAsAsync(r => r .Post() @@ -989,7 +993,7 @@ public async Task CanLogoutUserAsync() Assert.NotNull(result); // Verify that the token is valid - var token = await _tokenRepository.GetByIdAsync(result.Token); + var token = await _tokenRepository!.GetByIdAsync(result.Token); Assert.Equal(TokenType.Authentication, token.Type); Assert.False(token.IsDisabled); Assert.False(token.IsSuspended); @@ -1007,7 +1011,7 @@ await SendRequestAsync(r => r [Fact] public async Task CanLogoutUserAccessTokenAsync() { - var token = await _tokenRepository.GetByIdAsync(TestConstants.UserApiKey); + var token = await _tokenRepository!.GetByIdAsync(TestConstants.UserApiKey); Assert.NotNull(token); Assert.Equal(TokenType.Access, token.Type); Assert.False(token.IsDisabled); @@ -1028,7 +1032,7 @@ await SendRequestAsync(r => r [Fact] public async Task CanLogoutClientAccessTokenAsync() { - var token = await _tokenRepository.GetByIdAsync(TestConstants.ApiKey); + var token = await _tokenRepository!.GetByIdAsync(TestConstants.ApiKey); Assert.NotNull(token); Assert.Equal(TokenType.Access, token.Type); Assert.False(token.IsDisabled); diff --git a/tests/Exceptionless.Tests/Exceptionless.Tests.csproj b/tests/Exceptionless.Tests/Exceptionless.Tests.csproj index f5e6b679d..e18e4f4f0 100644 --- a/tests/Exceptionless.Tests/Exceptionless.Tests.csproj +++ b/tests/Exceptionless.Tests/Exceptionless.Tests.csproj @@ -15,7 +15,7 @@ - + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/tests/Exceptionless.Tests/IntegrationTestsBase.cs b/tests/Exceptionless.Tests/IntegrationTestsBase.cs index af6a429cd..70e9888e8 100644 --- a/tests/Exceptionless.Tests/IntegrationTestsBase.cs +++ b/tests/Exceptionless.Tests/IntegrationTestsBase.cs @@ -35,13 +35,12 @@ public abstract class IntegrationTestsBase : TestWithLoggingBase, Xunit.IAsyncLi { private static bool _indexesHaveBeenConfigured = false; private static readonly SemaphoreSlim _semaphoreSlim = new(1, 1); - private ExceptionlessElasticConfiguration? _configuration; - private ProxyTimeProvider? _timeProvider; + private readonly ExceptionlessElasticConfiguration _configuration; + protected readonly TestServer _server; + private readonly ProxyTimeProvider _timeProvider; protected readonly IList _disposables = new List(); - protected readonly AspireWebHostFactory _hostFixture; - protected TestServer? _server; - public IntegrationTestsBase(ITestOutputHelper output, AspireWebHostFactory hostFixture) : base(output) + public IntegrationTestsBase(ITestOutputHelper output, AspireWebHostFactory factory) : base(output) { Log.DefaultMinimumLevel = LogLevel.Information; Log.SetLogLevel(LogLevel.Warning); @@ -49,26 +48,12 @@ public IntegrationTestsBase(ITestOutputHelper output, AspireWebHostFactory hostF Log.SetLogLevel(LogLevel.Warning); Log.SetLogLevel("StartupActions", LogLevel.Warning); Log.SetLogLevel(LogLevel.Warning); - _hostFixture = hostFixture; - } - - public virtual async Task InitializeAsync() - { - //await _hostFixture.StartAsync(); - var configuredFactory = _hostFixture.Factories.Count > 0 ? _hostFixture.Factories[0] : null; + var configuredFactory = factory.Factories.Count > 0 ? factory.Factories[0] : null; if (configuredFactory is null) { - configuredFactory = _hostFixture.WithWebHostBuilder(builder => + configuredFactory = factory.WithWebHostBuilder(builder => { - builder.ConfigureAppConfiguration(c => - { - c.AddInMemoryCollection(new Dictionary - { - { "ConnectionStrings:Elasticsearch", _hostFixture.ElasticsearchConnectionString }, - { "ConnectionStrings:Redis", _hostFixture.RedisConnectionString } - }); - }); builder.ConfigureTestServices(RegisterServices); // happens after normal container configure and overrides services }); } @@ -88,21 +73,22 @@ public virtual async Task InitializeAsync() throw new InvalidOperationException("TimeProvider must be of type ProxyTimeProvider"); _disposables.Add(new DisposableAction(() => _timeProvider.Restore())); + } + public virtual async Task InitializeAsync() + { Log.SetLogLevel("Microsoft.AspNetCore.Hosting.Internal.WebHost", LogLevel.Warning); Log.SetLogLevel("Microsoft.Extensions.Diagnostics.HealthChecks.DefaultHealthCheckService", LogLevel.None); - await _server.WaitForReadyAsync(); - Log.SetLogLevel("Microsoft.AspNetCore.Hosting.Internal.WebHost", LogLevel.Information); Log.SetLogLevel("Microsoft.Extensions.Diagnostics.HealthChecks.DefaultHealthCheckService", LogLevel.Information); await ResetDataAsync(); } - protected ProxyTimeProvider TimeProvider => _timeProvider!; + protected ProxyTimeProvider TimeProvider => _timeProvider; - private IServiceProvider ServiceProvider { get; set; } = new ServiceCollection().BuildServiceProvider(); + private IServiceProvider ServiceProvider { get; } protected TService GetService() where TService : notnull { @@ -128,14 +114,14 @@ protected virtual void RegisterServices(IServiceCollection services) services.AddSingleton(); services.AddSingleton(); - services.ReplaceSingleton(s => _server!.CreateHandler()); + services.ReplaceSingleton(s => _server.CreateHandler()); } public async Task<(List Stacks, List Events)> CreateDataAsync(Action dataBuilderFunc) { var eventBuilders = new List(); - var dataBuilder = new DataBuilder(eventBuilders, ServiceProvider, _timeProvider!); + var dataBuilder = new DataBuilder(eventBuilders, ServiceProvider, _timeProvider); dataBuilderFunc(dataBuilder); var eventRepository = GetService(); @@ -168,13 +154,13 @@ protected virtual async Task ResetDataAsync() await RefreshDataAsync(); if (!_indexesHaveBeenConfigured) { - await _configuration!.DeleteIndexesAsync(); + await _configuration.DeleteIndexesAsync(); await _configuration.ConfigureIndexesAsync(); _indexesHaveBeenConfigured = true; } else { - string indexes = String.Join(',', _configuration!.Indexes.Select(i => i.Name)); + string indexes = String.Join(',', _configuration.Indexes.Select(i => i.Name)); await _configuration.Client.DeleteByQueryAsync(new DeleteByQueryRequest(indexes) { Query = new MatchAllQuery(), @@ -214,7 +200,7 @@ protected async Task RefreshDataAsync(Indices? indices = null) protected HttpClient CreateHttpClient() { - var client = _server!.CreateClient(); + var client = _server.CreateClient(); client.BaseAddress = new Uri(_server.BaseAddress + "api/v2/", UriKind.Absolute); return client; } diff --git a/tests/Exceptionless.Tests/TestWithServices.cs b/tests/Exceptionless.Tests/TestWithServices.cs index 8d6c7ca71..29c4a2dc2 100644 --- a/tests/Exceptionless.Tests/TestWithServices.cs +++ b/tests/Exceptionless.Tests/TestWithServices.cs @@ -22,7 +22,7 @@ public class TestWithServices : TestWithLoggingBase, IAsyncLifetime { private readonly IServiceProvider _container; private readonly ProxyTimeProvider _timeProvider; - private static bool _startupActionsRun; + //private static bool _startupActionsRun; public TestWithServices(ITestOutputHelper output) : base(output) { @@ -39,8 +39,11 @@ public TestWithServices(ITestOutputHelper output) : base(output) throw new InvalidOperationException("TimeProvider must be of type ProxyTimeProvider"); } - public virtual async Task InitializeAsync() + public virtual Task InitializeAsync() { + return Task.CompletedTask; + + /* if (_startupActionsRun) return; @@ -48,7 +51,7 @@ public virtual async Task InitializeAsync() if (!result.Success) throw new ApplicationException($"Startup action \"{result.FailedActionName}\" failed"); - _startupActionsRun = true; + _startupActionsRun = true;*/ } protected ProxyTimeProvider TimeProvider => _timeProvider;