More webhook tests

This commit is contained in:
Rory&
2026-06-17 22:52:18 +02:00
parent edbb6048c1
commit ac0b8d5fe6
@@ -1,4 +1,5 @@
using System.Text.Json.Nodes;
using Spacebar.Models.Generic;
using Spacebar.Sdk.Core;
using Spacebar.Tests.Abstractions;
using Spacebar.Tests.Extensions;
@@ -17,6 +18,7 @@ public class WebhookTests(ITestOutputHelper testOutputHelper, TestFixture fixtur
private static SpacebarClientGuild? Guild;
private static SpacebarClientChannel? Channel = null!;
private static Webhook? Webhook = null!;
public async ValueTask InitializeAsync() {
Client = await _userAbstraction.GetSharedUser();
@@ -37,6 +39,15 @@ public class WebhookTests(ITestOutputHelper testOutputHelper, TestFixture fixtur
Assert.Equal("test", c.Result.Name);
Channel = Client.GetChannel(c.Result.Id);
});
if (Webhook is null)
await Channel!.CreateWebhookAsync(new() {
Name = "meow"
}).ContinueWith(w => {
Assert.Equal("meow", w.Result.Name);
Assert.StringNotNullOrWhitespace(w.Result.Url);
Webhook = w.Result;
});
}
[Fact]
@@ -48,6 +59,28 @@ public class WebhookTests(ITestOutputHelper testOutputHelper, TestFixture fixtur
Assert.Equal("meow", wh.Name);
Assert.StringNotNullOrWhitespace(wh.Url);
}
[Fact]
public async Task DeleteWebhookByToken() {
var wh = await Channel!.CreateWebhookAsync(new() {
Name = "meow"
});
Assert.Equal("meow", wh.Name);
Assert.StringNotNullOrWhitespace(wh.Url);
await Assert.HttpSuccess(await Client.ApiHttpClient.DeleteAsync(wh.Url, TestContext.Current.CancellationToken));
}
[Fact]
public async Task DeleteWebhook() {
var wh = await Channel!.CreateWebhookAsync(new() {
Name = "meow"
});
Assert.Equal("meow", wh.Name);
Assert.StringNotNullOrWhitespace(wh.Url);
await Assert.HttpSuccess(await Client.ApiHttpClient.DeleteAsync($"webhooks/{wh.Id}", TestContext.Current.CancellationToken));
}
[Fact]
public async Task CreateMultipleWebhooks() {
@@ -72,30 +105,59 @@ public class WebhookTests(ITestOutputHelper testOutputHelper, TestFixture fixtur
}
[Fact]
public async Task SendWebhookMessageWithWait() {
var wh = await Channel!.CreateWebhookAsync(new() {
Name = "meow"
});
Assert.Equal("meow", wh.Name);
Assert.StringNotNullOrWhitespace(wh.Url);
await Assert.SuccessfullyHttpPostAsJsonAsync(wh.Url + "?wait=true", new JsonObject() {
public async Task SendWebhookMessage() {
await Assert.SuccessfullyHttpPostAsJsonAsync(Webhook.Url, new JsonObject() {
{ "content", "meow" }
});
}
[Fact]
public async Task SendWebhookMessage() {
var wh = await Channel!.CreateWebhookAsync(new() {
Name = "meow"
});
Assert.Equal("meow", wh.Name);
Assert.StringNotNullOrWhitespace(wh.Url);
await Assert.SuccessfullyHttpPostAsJsonAsync(wh.Url, new JsonObject() {
public async Task SendWebhookMessageWithWait() {
await Assert.SuccessfullyHttpPostAsJsonAsync(Webhook.Url + "?wait=true", new JsonObject() {
{ "content", "meow" }
});
}
public static IEnumerable<object?[]> WebhookExecuteCombinations() {
string[] contents = ["meow", "meowmeow", "emma was here", "# hi!!!", "https://spacebar.chat/favicon.ico", "@everyone", "@here"];
string?[] usernames = [null, "meow"];
string?[] avatarUrls = [null, "https://spacebar.chat/favicon.ico"];
bool?[] ttsEnabled = [null, true, false];
int?[] messageFlags = [
null,
0, // default
1 << 2, // SUPPRESS_EMBEDS
1 << 12, // SUPPRESS_NOTIFICATIONS
1 << 13 // VOICE_MESSAGE
];
foreach (var content in contents)
foreach (var username in usernames)
foreach (var avatarUrl in avatarUrls)
foreach (var tts in ttsEnabled)
foreach (var flags in messageFlags)
yield return [content, username, avatarUrl, tts, flags];
}
[Theory()]
[MemberData(nameof(WebhookExecuteCombinations))]
public async Task SendWebhookMessageWithData(string content, string? username, string? avatarUrl, bool? tts, int? flags) {
var payload = new JsonObject() {
{ "content", content }
};
if (username != null) payload.Add("username", username);
if (avatarUrl != null) payload.Add("avatar_url", avatarUrl);
if (tts != null) payload.Add("tts", tts);
if (flags != null) payload.Add("flags", flags);
await Assert.SuccessfullyHttpPostAsJsonAsync(Webhook.Url + "?wait=true", payload);
}
[Fact]
public async Task SendWebhookMessageWithAvatarUrl() {
await Assert.SuccessfullyHttpPostAsJsonAsync(Webhook.Url + "?wait=true", new JsonObject() {
{ "content", "meow" },
{ "avatar_url", "https://spacebar.chat/favicon.ico" }
});
}
}