diff --git a/extra/admin-api/Tests/Spacebar.Tests/Abstractions/UserAbstraction.cs b/extra/admin-api/Tests/Spacebar.Tests/Abstractions/UserAbstraction.cs index 300e803..ac90c75 100644 --- a/extra/admin-api/Tests/Spacebar.Tests/Abstractions/UserAbstraction.cs +++ b/extra/admin-api/Tests/Spacebar.Tests/Abstractions/UserAbstraction.cs @@ -23,4 +23,9 @@ return client; } + + private static AuthenticatedSpacebarClient? _authenticatedSpacebarClient; + public async Task GetSharedUser() { + return _authenticatedSpacebarClient ??= await GetFreshUser(); + } } \ No newline at end of file diff --git a/extra/admin-api/Tests/Spacebar.Tests/Config.cs b/extra/admin-api/Tests/Spacebar.Tests/Config.cs index d1aa9de..7419caa 100644 --- a/extra/admin-api/Tests/Spacebar.Tests/Config.cs +++ b/extra/admin-api/Tests/Spacebar.Tests/Config.cs @@ -8,4 +8,5 @@ } public string TestInstance { get; set; } + public int RegisterConcurrentCount { get; set; } } \ No newline at end of file diff --git a/extra/admin-api/Tests/Spacebar.Tests/Extensions/TestBedFixtureExtensions.cs b/extra/admin-api/Tests/Spacebar.Tests/Extensions/TestBedFixtureExtensions.cs new file mode 100644 index 0000000..53113f9 --- /dev/null +++ b/extra/admin-api/Tests/Spacebar.Tests/Extensions/TestBedFixtureExtensions.cs @@ -0,0 +1,11 @@ +using Xunit.Microsoft.DependencyInjection.Abstracts; + +namespace Spacebar.Tests.Extensions; + +public static class TestBedFixtureExtensions { + extension(TestBedFixture tbf) { + public T GetRequiredService(ITestOutputHelper toh) { + return tbf.GetService(toh) ?? throw new InvalidOperationException($"Failed to get service: {typeof(T).FullName}"); + } + } +} \ No newline at end of file diff --git a/extra/admin-api/Tests/Spacebar.Tests/Tests/AuthenticationTests.cs b/extra/admin-api/Tests/Spacebar.Tests/Tests/AuthenticationTests.cs index f927d7c..ca63222 100644 --- a/extra/admin-api/Tests/Spacebar.Tests/Tests/AuthenticationTests.cs +++ b/extra/admin-api/Tests/Spacebar.Tests/Tests/AuthenticationTests.cs @@ -7,18 +7,14 @@ using Spacebar.Tests.Extensions; using Spacebar.Tests.Fixtures; using Xunit.Microsoft.DependencyInjection.Abstracts; +using Xunit.Sdk; namespace Spacebar.Tests.Tests; public class AuthenticationTests(ITestOutputHelper testOutputHelper, TestFixture fixture) : TestBed(testOutputHelper, fixture) { - private readonly Config _config = fixture.GetService(testOutputHelper) ?? throw new InvalidOperationException($"Failed to get {nameof(Config)}"); - - private readonly SpacebarClientWellKnownResolverService _wellKnownResolver = fixture.GetService(testOutputHelper) ?? - throw new InvalidOperationException( - $"Failed to get {nameof(SpacebarClientWellKnownResolverService)}"); - - private readonly SpacebarClientProviderService _clientProvider = fixture.GetService(testOutputHelper) ?? - throw new InvalidOperationException($"Failed to get {nameof(SpacebarClientProviderService)}"); + private readonly Config _config = fixture.GetRequiredService(testOutputHelper); + private readonly SpacebarClientWellKnownResolverService _wellKnownResolver = fixture.GetRequiredService(testOutputHelper); + private readonly SpacebarClientProviderService _clientProvider = fixture.GetRequiredService(testOutputHelper); [Fact] public async Task RegisterUser() { @@ -29,11 +25,13 @@ DateOfBirth = new(), Consent = true }); + await Assert.HttpSuccess(res); } [Fact] - public async Task ConcurrentRegister50Users() { - var tasks = Enumerable.Range(0, 50).Select(async _ => { + public async Task RegisterUsersConcurrent() { + testOutputHelper.WriteLine($"Registering {_config.RegisterConcurrentCount} users concurrently..."); + var tasks = Enumerable.Range(0, _config.RegisterConcurrentCount).Select(async _ => { var sw = Stopwatch.StartNew(); var rr = new RegisterRequest() { Email = $"{Guid.NewGuid().ToString()}@{Guid.NewGuid().ToString()}.tld", diff --git a/extra/admin-api/Tests/Spacebar.Tests/Tests/BasicWellKnownTests.cs b/extra/admin-api/Tests/Spacebar.Tests/Tests/BasicWellKnownTests.cs index 56254a6..22c2555 100644 --- a/extra/admin-api/Tests/Spacebar.Tests/Tests/BasicWellKnownTests.cs +++ b/extra/admin-api/Tests/Spacebar.Tests/Tests/BasicWellKnownTests.cs @@ -6,10 +6,8 @@ namespace Spacebar.Tests.Tests; public class BasicWellKnownTests(ITestOutputHelper testOutputHelper, TestFixture fixture) : TestBed(testOutputHelper, fixture) { - private readonly Config _config = fixture.GetService(testOutputHelper) ?? throw new InvalidOperationException($"Failed to get {nameof(Config)}"); - - private readonly SpacebarClientWellKnownResolverService _wellKnownResolver = fixture.GetService(testOutputHelper) ?? - throw new InvalidOperationException($"Failed to get {nameof(Config)}"); + private readonly Config _config = fixture.GetRequiredService(testOutputHelper); + private readonly SpacebarClientWellKnownResolverService _wellKnownResolver = fixture.GetRequiredService(testOutputHelper); [Fact] public async Task ValidateTestConfig() { diff --git a/extra/admin-api/Tests/Spacebar.Tests/Tests/ChannelTests.cs b/extra/admin-api/Tests/Spacebar.Tests/Tests/ChannelTests.cs index cdee86b..cd85226 100644 --- a/extra/admin-api/Tests/Spacebar.Tests/Tests/ChannelTests.cs +++ b/extra/admin-api/Tests/Spacebar.Tests/Tests/ChannelTests.cs @@ -8,60 +8,50 @@ namespace Spacebar.Tests.Tests; -public class ChannelTests(ITestOutputHelper testOutputHelper, TestFixture fixture) : TestBed(testOutputHelper, fixture) { - private readonly Config _config = fixture.GetService(testOutputHelper) ?? throw new InvalidOperationException($"Failed to get {nameof(Config)}"); +public class ChannelTests(ITestOutputHelper testOutputHelper, TestFixture fixture) : TestBed(testOutputHelper, fixture), IAsyncLifetime { + private readonly Config _config = fixture.GetRequiredService(testOutputHelper); + private readonly SpacebarClientWellKnownResolverService _wellKnownResolver = fixture.GetRequiredService(testOutputHelper); + private readonly SpacebarClientProviderService _clientProvider = fixture.GetRequiredService(testOutputHelper); + private readonly UserAbstraction _userAbstraction = fixture.GetRequiredService(testOutputHelper); - private readonly SpacebarClientWellKnownResolverService _wellKnownResolver = fixture.GetService(testOutputHelper) ?? - throw new InvalidOperationException( - $"Failed to get {nameof(SpacebarClientWellKnownResolverService)}"); + private static AuthenticatedSpacebarClient Client { get; set; } = null!; + private static Guild? Guild { get; set; } - private readonly SpacebarClientProviderService _clientProvider = fixture.GetService(testOutputHelper) ?? - throw new InvalidOperationException($"Failed to get {nameof(SpacebarClientProviderService)}"); - - private readonly UserAbstraction _userAbstraction = fixture.GetService(testOutputHelper) ?? - throw new InvalidOperationException($"Failed to get {nameof(SpacebarClientProviderService)}"); - - [Fact] - public async Task CreateChannel() { - var client = await _userAbstraction.GetFreshUser(withAutojoinGuilds: true); - var guild = await client.CreateGuild(new() { + public async ValueTask InitializeAsync() { + testOutputHelper.WriteLine("Running InitializeAsync"); + // All these tests can share a single client + Client = await _userAbstraction.GetSharedUser(); + // ...and a guild + Guild ??= await Client.CreateGuild(new() { Name = "Test guild" }); - - Assert.Equal("Test guild", guild.Name); + } - var channel = await client.GetGuild(guild.Id).CreateChannelAsync(new() { + [Fact] + public async Task CreateChannel() { + Assert.Equal("Test guild", Guild!.Name); + var channel = await Client!.GetGuild(Guild.Id).CreateChannelAsync(new() { Name = "test", Type = 0 }); - + Assert.Equal("test", channel.Name); } [Fact] public async Task GetChannel() { - var client = await _userAbstraction.GetFreshUser(withAutojoinGuilds: true); - var guild = await client.CreateGuild(new() { - Name = "Test guild" - }); - - Assert.Equal("Test guild", guild.Name); - - // await Task.Delay(1000, TestContext.Current.CancellationToken); // TODO: unflake - var channel = await client.GetGuild(guild.Id).CreateChannelAsync(new() { + Assert.Equal("Test guild", Guild!.Name); + var channel = await Client!.GetGuild(Guild.Id).CreateChannelAsync(new() { Name = "test", Type = 0 }); - Assert.Equal("test", channel.Name); - // await Task.Delay(1000, TestContext.Current.CancellationToken); // TODO: unflake - var res = await client.ApiHttpClient.GetAsync("channels/" + channel.Id, TestContext.Current.CancellationToken); + var res = await Client.ApiHttpClient.GetAsync("channels/" + channel.Id, TestContext.Current.CancellationToken); await Assert.HttpSuccess(res); - + var channelResp = await res.Content.ReadFromJsonAsync(cancellationToken: TestContext.Current.CancellationToken); Assert.Equal(channel.Name, channelResp!.Name); Assert.Equal(channel.Id, channelResp!.Id); - } } \ No newline at end of file diff --git a/extra/admin-api/Tests/Spacebar.Tests/Tests/GuildTests.cs b/extra/admin-api/Tests/Spacebar.Tests/Tests/GuildTests.cs index 88490e0..24d2a17 100644 --- a/extra/admin-api/Tests/Spacebar.Tests/Tests/GuildTests.cs +++ b/extra/admin-api/Tests/Spacebar.Tests/Tests/GuildTests.cs @@ -7,21 +7,14 @@ namespace Spacebar.Tests.Tests; public class GuildTests(ITestOutputHelper testOutputHelper, TestFixture fixture) : TestBed(testOutputHelper, fixture) { - private readonly Config _config = fixture.GetService(testOutputHelper) ?? throw new InvalidOperationException($"Failed to get {nameof(Config)}"); - - private readonly SpacebarClientWellKnownResolverService _wellKnownResolver = fixture.GetService(testOutputHelper) ?? - throw new InvalidOperationException( - $"Failed to get {nameof(SpacebarClientWellKnownResolverService)}"); - - private readonly SpacebarClientProviderService _clientProvider = fixture.GetService(testOutputHelper) ?? - throw new InvalidOperationException($"Failed to get {nameof(SpacebarClientProviderService)}"); - - private readonly UserAbstraction _userAbstraction = fixture.GetService(testOutputHelper) ?? - throw new InvalidOperationException($"Failed to get {nameof(SpacebarClientProviderService)}"); + private readonly Config _config = fixture.GetRequiredService(testOutputHelper); + private readonly SpacebarClientWellKnownResolverService _wellKnownResolver = fixture.GetRequiredService(testOutputHelper); + private readonly SpacebarClientProviderService _clientProvider = fixture.GetRequiredService(testOutputHelper); + private readonly UserAbstraction _userAbstraction = fixture.GetRequiredService(testOutputHelper); [Fact] public async Task CreateGuild() { - var client = await _userAbstraction.GetFreshUser(withAutojoinGuilds: true); + var client = await _userAbstraction.GetSharedUser(); var guild = await client.CreateGuild(new() { Name = "Test guild" }); @@ -31,7 +24,7 @@ [Fact] public async Task GetChannels() { - var client = await _userAbstraction.GetFreshUser(withAutojoinGuilds: true); + var client = await _userAbstraction.GetSharedUser(); var guild = await client.CreateGuild(new() { Name = "Test guild" }); diff --git a/extra/admin-api/Tests/Spacebar.Tests/Tests/Meta/UserAbstractionTests.cs b/extra/admin-api/Tests/Spacebar.Tests/Tests/Meta/UserAbstractionTests.cs index 64f80d0..49c9da5 100644 --- a/extra/admin-api/Tests/Spacebar.Tests/Tests/Meta/UserAbstractionTests.cs +++ b/extra/admin-api/Tests/Spacebar.Tests/Tests/Meta/UserAbstractionTests.cs @@ -6,7 +6,7 @@ namespace Spacebar.Tests.Tests.Meta; public class UserAbstractionTests(ITestOutputHelper testOutputHelper, TestFixture fixture) : TestBed(testOutputHelper, fixture) { - private readonly UserAbstraction _config = fixture.GetService(testOutputHelper) ?? throw new InvalidOperationException($"Failed to get {nameof(UserAbstraction)}"); + private readonly UserAbstraction _config = fixture.GetRequiredService(testOutputHelper); [Fact] public async Task CanGetUser() { diff --git a/extra/admin-api/Tests/Spacebar.Tests/Tests/WebhookTests.cs b/extra/admin-api/Tests/Spacebar.Tests/Tests/WebhookTests.cs index 93a4d38..4386323 100644 --- a/extra/admin-api/Tests/Spacebar.Tests/Tests/WebhookTests.cs +++ b/extra/admin-api/Tests/Spacebar.Tests/Tests/WebhookTests.cs @@ -7,61 +7,58 @@ namespace Spacebar.Tests.Tests; -public class WebhookTests(ITestOutputHelper testOutputHelper, TestFixture fixture) : TestBed(testOutputHelper, fixture) { - private readonly Config _config = fixture.GetService(testOutputHelper) ?? throw new InvalidOperationException($"Failed to get {nameof(Config)}"); +public class WebhookTests(ITestOutputHelper testOutputHelper, TestFixture fixture) : TestBed(testOutputHelper, fixture), IAsyncLifetime { + private readonly Config _config = fixture.GetRequiredService(testOutputHelper); + private readonly SpacebarClientWellKnownResolverService _wellKnownResolver = fixture.GetRequiredService(testOutputHelper); + private readonly SpacebarClientProviderService _clientProvider = fixture.GetRequiredService(testOutputHelper); + private readonly UserAbstraction _userAbstraction = fixture.GetRequiredService(testOutputHelper); - private readonly SpacebarClientWellKnownResolverService _wellKnownResolver = fixture.GetService(testOutputHelper) ?? - throw new InvalidOperationException( - $"Failed to get {nameof(SpacebarClientWellKnownResolverService)}"); + private static AuthenticatedSpacebarClient Client = null!; + private static SpacebarClientGuild? Guild; - private readonly SpacebarClientProviderService _clientProvider = fixture.GetService(testOutputHelper) ?? - throw new InvalidOperationException($"Failed to get {nameof(SpacebarClientProviderService)}"); + private static SpacebarClientChannel? Channel = null!; - private readonly UserAbstraction _userAbstraction = fixture.GetService(testOutputHelper) ?? - throw new InvalidOperationException($"Failed to get {nameof(SpacebarClientProviderService)}"); - + public async ValueTask InitializeAsync() { + Client = await _userAbstraction.GetSharedUser(); + + if (Guild is null) + await Client.CreateGuild(new() { + Name = "Test guild" + }).ContinueWith(g => { + Assert.Equal("Test guild", g.Result.Name); + Guild = Client.GetGuild(g.Result.Id); + }); + + if (Channel is null) + await Guild!.CreateChannelAsync(new() { + Name = "test", + Type = 0 + }).ContinueWith(c => { + Assert.Equal("test", c.Result.Name); + Channel = Client.GetChannel(c.Result.Id); + }); + } + [Fact] public async Task CreateWebhook() { - var client = await _userAbstraction.GetFreshUser(withAutojoinGuilds: true); - var guild = await client.CreateGuild(new() { - Name = "Test guild" - }); - - Assert.Equal("Test guild", guild.Name); - - var channel = await client.GetGuild(guild.Id).CreateChannelAsync(new() { - Name = "test", - Type = 0 - }); - - Assert.Equal("test", channel.Name); - - var cChannel = client.GetChannel(channel.Id); - var wh = await cChannel.CreateWebhookAsync(new() { + var wh = await Channel!.CreateWebhookAsync(new() { Name = "meow" }); Assert.Equal("meow", wh.Name); Assert.StringNotNullOrWhitespace(wh.Url); } - + [Fact] public async Task CreateMultipleWebhooks() { - var client = await _userAbstraction.GetFreshUser(withAutojoinGuilds: true); - var guild = await client.CreateGuild(new() { - Name = "Test guild" - }); - - Assert.Equal("Test guild", guild.Name); - - var channel = await client.GetGuild(guild.Id).CreateChannelAsync(new() { + var channel = await Guild!.CreateChannelAsync(new() { Name = "test", Type = 0 }); - + Assert.Equal("test", channel.Name); - var cChannel = client.GetChannel(channel.Id); + var cChannel = Client.GetChannel(channel.Id); var count = Random.Shared.Next(10); testOutputHelper.WriteLine($"Creating {count} webhooks..."); @@ -76,22 +73,7 @@ [Fact] public async Task SendWebhookMessageWithWait() { - var client = await _userAbstraction.GetFreshUser(withAutojoinGuilds: true); - var guild = await client.CreateGuild(new() { - Name = "Test guild" - }); - - Assert.Equal("Test guild", guild.Name); - - var channel = await client.GetGuild(guild.Id).CreateChannelAsync(new() { - Name = "test", - Type = 0 - }); - - Assert.Equal("test", channel.Name); - - var cChannel = client.GetChannel(channel.Id); - var wh = await cChannel.CreateWebhookAsync(new() { + var wh = await Channel!.CreateWebhookAsync(new() { Name = "meow" }); @@ -102,25 +84,10 @@ { "content", "meow" } }); } - + [Fact] public async Task SendWebhookMessage() { - var client = await _userAbstraction.GetFreshUser(withAutojoinGuilds: true); - var guild = await client.CreateGuild(new() { - Name = "Test guild" - }); - - Assert.Equal("Test guild", guild.Name); - - var channel = await client.GetGuild(guild.Id).CreateChannelAsync(new() { - Name = "test", - Type = 0 - }); - - Assert.Equal("test", channel.Name); - - var cChannel = client.GetChannel(channel.Id); - var wh = await cChannel.CreateWebhookAsync(new() { + var wh = await Channel!.CreateWebhookAsync(new() { Name = "meow" }); diff --git a/extra/admin-api/Tests/Spacebar.Tests/appsettings.json b/extra/admin-api/Tests/Spacebar.Tests/appsettings.json index d1d45a9..fa03afc 100644 --- a/extra/admin-api/Tests/Spacebar.Tests/appsettings.json +++ b/extra/admin-api/Tests/Spacebar.Tests/appsettings.json @@ -1,5 +1,6 @@ { "Configuration": { - "TestInstance": "http://localhost:3001" + "TestInstance": "http://localhost:3001", + "RegisterConcurrentCount": 15 } } \ No newline at end of file