Unit tests for webhooks

This commit is contained in:
Rory&
2026-06-05 22:42:33 +02:00
parent 46b73308b1
commit d109d871d7
10 changed files with 255 additions and 11 deletions
@@ -0,0 +1,11 @@
using System.Text.Json.Serialization;
namespace Spacebar.Models.Api;
public class CreateWebhookRequest {
[JsonPropertyName("name")]
public required string Name { get; set; }
[JsonPropertyName("avatar")]
public string? AvatarData { get; set; }
}
@@ -14,6 +14,8 @@ public class SpacebarApiException : Exception {
public JsonObject? Errors { get; set; }
public JsonObject?[]? AjvErrors { get; set; }
public JsonObject? OriginalErrorData { get; init; }
public class FieldErrorList {
// public
@@ -32,6 +34,7 @@ public class SpacebarApiException : Exception {
}
var ex = new SpacebarApiException(msg) {
OriginalErrorData = resp,
Code = resp["code"]!.GetValue<int>(),
ErrorMessage = resp["message"]!.GetValue<string>(),
Request = resp["request"]?.GetValue<string>(),
@@ -42,7 +45,7 @@ public class SpacebarApiException : Exception {
return ex;
}
public JsonObject AsJsonObject() => new() {
public JsonObject AsJsonObject() => OriginalErrorData?.DeepClone().AsObject() ?? new() {
{ "message", Message },
{ "code", Code },
{ "request", Request },
@@ -0,0 +1,48 @@
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
namespace Spacebar.Models.Generic;
public class Webhook {
[JsonPropertyName("id"), JsonNumberHandling(JsonNumberHandling.AllowReadingFromString | JsonNumberHandling.WriteAsString)]
public required long Id { get; set; }
[JsonPropertyName("type")]
public WebhookType WebhookType { get; set; }
[JsonPropertyName("guild_id"), JsonNumberHandling(JsonNumberHandling.AllowReadingFromString | JsonNumberHandling.WriteAsString)]
public long? GuildId { get; set; }
[JsonPropertyName("channel_id"), JsonNumberHandling(JsonNumberHandling.AllowReadingFromString | JsonNumberHandling.WriteAsString)]
public long? ChannelId { get; set; }
[JsonPropertyName("user")]
public PartialUser? User { get; set; }
[JsonPropertyName("name")]
public string? Name { get; set; }
[JsonPropertyName("avatar")]
public string? AvatarData { get; set; }
[JsonPropertyName("token")]
public string? Token { get; set; }
[JsonPropertyName("application_id"), JsonNumberHandling(JsonNumberHandling.AllowReadingFromString | JsonNumberHandling.WriteAsString)]
public long? ApplicationId { get; set; }
[JsonPropertyName("source_guild")]
public JsonObject? SourceGuild { get; set; } // TODO type
[JsonPropertyName("source_channel")]
public JsonObject? SourceChannel { get; set; } // TODO type
[JsonPropertyName("url")]
public string? Url { get; set; }
}
public enum WebhookType : byte {
Incomming = 1,
ChannelFollower = 2,
Application = 3
}