summary refs log tree commit diff
path: root/extra/admin-api/Tests
diff options
context:
space:
mode:
authorRory& <root@rory.gay>2026-06-05 22:42:33 +0200
committerRory& <root@rory.gay>2026-06-05 22:42:33 +0200
commitd109d871d75d7ee707bb33342d5fed6865124166 (patch)
tree8685f6caa2dde878528eaa67ede8da803f734eb1 /extra/admin-api/Tests
parentReturn url on webhook create (diff)
downloadserver-ts-d109d871d75d7ee707bb33342d5fed6865124166.tar.xz
Unit tests for webhooks
Diffstat (limited to 'extra/admin-api/Tests')
-rw-r--r--extra/admin-api/Tests/Spacebar.Tests/Abstractions/UserAbstraction.cs6
-rw-r--r--extra/admin-api/Tests/Spacebar.Tests/Tests/AuthenticationTests.cs9
-rw-r--r--extra/admin-api/Tests/Spacebar.Tests/Tests/ChannelTests.cs8
-rw-r--r--extra/admin-api/Tests/Spacebar.Tests/Tests/GuildTests.cs4
-rw-r--r--extra/admin-api/Tests/Spacebar.Tests/Tests/Meta/UserAbstractionTests.cs2
-rw-r--r--extra/admin-api/Tests/Spacebar.Tests/Tests/WebhookTests.cs137
6 files changed, 156 insertions, 10 deletions
diff --git a/extra/admin-api/Tests/Spacebar.Tests/Abstractions/UserAbstraction.cs b/extra/admin-api/Tests/Spacebar.Tests/Abstractions/UserAbstraction.cs

index b6cef1ac9..300e8035f 100644 --- a/extra/admin-api/Tests/Spacebar.Tests/Abstractions/UserAbstraction.cs +++ b/extra/admin-api/Tests/Spacebar.Tests/Abstractions/UserAbstraction.cs
@@ -15,10 +15,12 @@ public class UserAbstraction(Config _config, SpacebarClientProviderService _clie var client = await _clientProvider.GetAuthenticatedClientAsync(_config.TestInstance, tokenResponse.Token); if (!withAutojoinGuilds) { - + await Task.Delay(1000); + var leaves = (await client.GetJoinedGuilds()).Select(x => client.GetGuild(x.Id).LeaveAsync()).ToList(); + await Task.WhenAll(leaves); + await Task.Delay(1000); } return client; } - } \ 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 04bd5f912..f927d7c59 100644 --- a/extra/admin-api/Tests/Spacebar.Tests/Tests/AuthenticationTests.cs +++ b/extra/admin-api/Tests/Spacebar.Tests/Tests/AuthenticationTests.cs
@@ -1,4 +1,5 @@ -using System.Net.Http.Json; +using System.Diagnostics; +using System.Net.Http.Json; using System.Text.Json.Nodes; using ArcaneLibs.Extensions; using Spacebar.Models.Api; @@ -33,6 +34,7 @@ public class AuthenticationTests(ITestOutputHelper testOutputHelper, TestFixture [Fact] public async Task ConcurrentRegister50Users() { var tasks = Enumerable.Range(0, 50).Select(async _ => { + var sw = Stopwatch.StartNew(); var rr = new RegisterRequest() { Email = $"{Guid.NewGuid().ToString()}@{Guid.NewGuid().ToString()}.tld", Username = Guid.NewGuid().ToString(), @@ -40,7 +42,10 @@ public class AuthenticationTests(ITestOutputHelper testOutputHelper, TestFixture DateOfBirth = new(), Consent = true }; - return (rr, await Assert.SuccessfullyHttpPostAsJsonAsync($"{_config.TestInstance}/api/v9/auth/register", rr)); + + var result = await Assert.SuccessfullyHttpPostAsJsonAsync($"{_config.TestInstance}/api/v9/auth/register", rr); + testOutputHelper.WriteLine($"Registered {rr.Email} in {sw.Elapsed}..."); + return (rr, result); }).ToList(); await Task.WhenAll(tasks); diff --git a/extra/admin-api/Tests/Spacebar.Tests/Tests/ChannelTests.cs b/extra/admin-api/Tests/Spacebar.Tests/Tests/ChannelTests.cs
index d04ee994b..cdee86b49 100644 --- a/extra/admin-api/Tests/Spacebar.Tests/Tests/ChannelTests.cs +++ b/extra/admin-api/Tests/Spacebar.Tests/Tests/ChannelTests.cs
@@ -23,7 +23,7 @@ public class ChannelTests(ITestOutputHelper testOutputHelper, TestFixture fixtur [Fact] public async Task CreateChannel() { - var client = await _userAbstraction.GetFreshUser(); + var client = await _userAbstraction.GetFreshUser(withAutojoinGuilds: true); var guild = await client.CreateGuild(new() { Name = "Test guild" }); @@ -40,13 +40,14 @@ public class ChannelTests(ITestOutputHelper testOutputHelper, TestFixture fixtur [Fact] public async Task GetChannel() { - var client = await _userAbstraction.GetFreshUser(); + 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() { Name = "test", Type = 0 @@ -54,6 +55,7 @@ public class ChannelTests(ITestOutputHelper testOutputHelper, TestFixture fixtur 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); await Assert.HttpSuccess(res); diff --git a/extra/admin-api/Tests/Spacebar.Tests/Tests/GuildTests.cs b/extra/admin-api/Tests/Spacebar.Tests/Tests/GuildTests.cs
index 12e566f03..88490e0de 100644 --- a/extra/admin-api/Tests/Spacebar.Tests/Tests/GuildTests.cs +++ b/extra/admin-api/Tests/Spacebar.Tests/Tests/GuildTests.cs
@@ -21,7 +21,7 @@ public class GuildTests(ITestOutputHelper testOutputHelper, TestFixture fixture) [Fact] public async Task CreateGuild() { - var client = await _userAbstraction.GetFreshUser(); + var client = await _userAbstraction.GetFreshUser(withAutojoinGuilds: true); var guild = await client.CreateGuild(new() { Name = "Test guild" }); @@ -31,7 +31,7 @@ public class GuildTests(ITestOutputHelper testOutputHelper, TestFixture fixture) [Fact] public async Task GetChannels() { - var client = await _userAbstraction.GetFreshUser(); + var client = await _userAbstraction.GetFreshUser(withAutojoinGuilds: true); 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 81f53f7f3..64f80d0a9 100644 --- a/extra/admin-api/Tests/Spacebar.Tests/Tests/Meta/UserAbstractionTests.cs +++ b/extra/admin-api/Tests/Spacebar.Tests/Tests/Meta/UserAbstractionTests.cs
@@ -10,7 +10,7 @@ public class UserAbstractionTests(ITestOutputHelper testOutputHelper, TestFixtur [Fact] public async Task CanGetUser() { - var res = await _config.GetFreshUser(); + var res = await _config.GetFreshUser(withAutojoinGuilds: true); Assert.StringNotNullOrWhitespace(res.ApiHttpClient.BaseAddress!.ToString()); } } \ No newline at end of file diff --git a/extra/admin-api/Tests/Spacebar.Tests/Tests/WebhookTests.cs b/extra/admin-api/Tests/Spacebar.Tests/Tests/WebhookTests.cs new file mode 100644
index 000000000..a03ee6334 --- /dev/null +++ b/extra/admin-api/Tests/Spacebar.Tests/Tests/WebhookTests.cs
@@ -0,0 +1,137 @@ +using System.Net; +using System.Net.Http.Json; +using System.Text.Json.Nodes; +using Spacebar.Models.Generic; +using Spacebar.Sdk.Core; +using Spacebar.Tests.Abstractions; +using Spacebar.Tests.Extensions; +using Spacebar.Tests.Fixtures; +using Xunit.Microsoft.DependencyInjection.Abstracts; + +namespace Spacebar.Tests.Tests; + +public class WebhookTests(ITestOutputHelper testOutputHelper, TestFixture fixture) : TestBed<TestFixture>(testOutputHelper, fixture) { + private readonly Config _config = fixture.GetService<Config>(testOutputHelper) ?? throw new InvalidOperationException($"Failed to get {nameof(Config)}"); + + private readonly SpacebarClientWellKnownResolverService _wellKnownResolver = fixture.GetService<SpacebarClientWellKnownResolverService>(testOutputHelper) ?? + throw new InvalidOperationException( + $"Failed to get {nameof(SpacebarClientWellKnownResolverService)}"); + + private readonly SpacebarClientProviderService _clientProvider = fixture.GetService<SpacebarClientProviderService>(testOutputHelper) ?? + throw new InvalidOperationException($"Failed to get {nameof(SpacebarClientProviderService)}"); + + private readonly UserAbstraction _userAbstraction = fixture.GetService<UserAbstraction>(testOutputHelper) ?? + throw new InvalidOperationException($"Failed to get {nameof(SpacebarClientProviderService)}"); + + [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() { + 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() { + Name = "test", + Type = 0 + }); + + Assert.Equal("test", channel.Name); + + var cChannel = client.GetChannel(channel.Id); + + var count = Random.Shared.Next(10); + testOutputHelper.WriteLine($"Creating {count} webhooks..."); + await Task.WhenAll(Enumerable.Range(0, count).Select(i => cChannel.CreateWebhookAsync(new() { + Name = "meow" + i + })).ToList()); + + var wh = await cChannel.GetWebhooksAsync(); + Assert.All(wh, h => Assert.StartsWith("meow", h.Name)); + Assert.All(wh, h => Assert.StringNotNullOrWhitespace(h.Url)); + } + + [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() { + Name = "meow" + }); + + Assert.Equal("meow", wh.Name); + Assert.StringNotNullOrWhitespace(wh.Url); + + await Assert.SuccessfullyHttpPostAsJsonAsync(wh.Url + "?wait=true", new JsonObject() { + { "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() { + Name = "meow" + }); + + Assert.Equal("meow", wh.Name); + Assert.StringNotNullOrWhitespace(wh.Url); + + await Assert.SuccessfullyHttpPostAsJsonAsync(wh.Url, new JsonObject() { + { "content", "meow" } + }); + } +} \ No newline at end of file