Add tests for guild/app emojis

This commit is contained in:
Rory&
2026-07-01 15:15:02 +02:00
parent 2f7c3eb0ee
commit fcfd6114d6
3 changed files with 268 additions and 2 deletions
@@ -0,0 +1,135 @@
using System.Diagnostics;
using System.Net.Http.Json;
using System.Text.Json.Nodes;
using ArcaneLibs.Extensions;
using Spacebar.Models.AdminApi;
using Spacebar.Models.Generic;
using Spacebar.Sdk.Core;
using Spacebar.Tests.Abstractions;
using Spacebar.Tests.Extensions;
using Spacebar.Tests.Fixtures;
using Xunit.Internal;
using Xunit.Microsoft.DependencyInjection.Abstracts;
namespace Spacebar.Tests.Tests;
public class ApplicationEmojiTests(ITestOutputHelper testOutputHelper, TestFixture fixture) : TestBed<TestFixture>(testOutputHelper, fixture), IAsyncLifetime {
private readonly Config _config = fixture.GetRequiredService<Config>(testOutputHelper);
private readonly SpacebarClientWellKnownResolverService _wellKnownResolver = fixture.GetRequiredService<SpacebarClientWellKnownResolverService>(testOutputHelper);
private readonly SpacebarClientProviderService _clientProvider = fixture.GetRequiredService<SpacebarClientProviderService>(testOutputHelper);
private readonly UserAbstraction _userAbstraction = fixture.GetRequiredService<UserAbstraction>(testOutputHelper);
private static AuthenticatedSpacebarClient Client { get; set; } = null!;
private static ApplicationModel? Application { get; set; }
private const string Base64ImageData =
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAIAAABMXPacAAAAAXNSR0IB2cksfwAAAARnQU1BAACxjwv8YQUAAAAgY0hSTQAAeiYAAICEAAD6AAAAgOgAAHUwAADqYAAAOpgAABdwnLpRPAAAA8hJREFUeNrtnD9oE1EYwL8XOxwIphksdIjQoUNQB41TumVrpoJLwUUI2EEpQltwyOaN7SI4KHRwELqomeLmZqdWBwMdKgjNUMmS1qnbuahoTXq5u/fu3r38fputzd19v/e97/27KFFXBbKjQAgQgABAAAIAAQgABCAAEIAAQAACAAEIAAQgABCAAEAAAgABCAAEIAAQgABAAAIAAQiA5Eylcxnl9//+Z/BmVT7vWBUI1WzLXO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SuQmCC";
public async ValueTask InitializeAsync() {
testOutputHelper.WriteLine("Running InitializeAsync");
// All these tests can share a single client
Client = await _userAbstraction.GetSharedUser();
if (Application is null) {
var res = await Assert.HttpSuccess(await Client.ApiHttpClient.PostAsJsonAsync("applications", new JsonObject() {
{ "name", "meow" }
}));
Application = await res.Content.ReadFromJsonAsync<ApplicationModel>();
}
}
[Fact]
public async Task GetEmojis() {
var res = await Assert.HttpSuccess(await Client.ApiHttpClient.GetAsync($"/api/v9/applications/{Application.Id}/emojis", TestContext.Current.CancellationToken));
var resEmojisObj = await res.Content.ReadFromJsonAsync<JsonObject>();
var resEmojis = resEmojisObj?["items"]?.AsArray();
resEmojis!.ForEach(eJson => {
Assert.StringNotNullOrWhitespace(eJson?["id"]?.GetValue<string>());
Assert.StringNotNullOrWhitespace(eJson?["name"]?.GetValue<string>());
});
}
[Fact]
public async Task UploadEmoji() {
var reqJson = new JsonObject() {
{ "name", "meow" },
{ "image", Base64ImageData }
};
var res = await Assert.HttpSuccess(await Client.ApiHttpClient.PostAsJsonAsync($"/api/v9/applications/{Application.Id}/emojis", reqJson, TestContext.Current.CancellationToken));
var resEmoji = await res.Content.ReadFromJsonAsync<JsonObject>();
Assert.StringNotNullOrWhitespace(resEmoji?["id"]?.GetValue<string>());
Assert.StringNotNullOrWhitespace(resEmoji?["name"]?.GetValue<string>());
Assert.Equal("meow", resEmoji?["name"]?.GetValue<string>());
}
[Fact]
public async Task UpdateEmoji() {
var reqJson = new JsonObject() {
{ "name", "meow" },
{ "image", Base64ImageData }
};
var res = await Assert.HttpSuccess(await Client.ApiHttpClient.PostAsJsonAsync($"/api/v9/applications/{Application.Id}/emojis", reqJson, TestContext.Current.CancellationToken));
var resEmoji = await res.Content.ReadFromJsonAsync<JsonObject>();
Assert.StringNotNullOrWhitespace(resEmoji?["id"]?.GetValue<string>());
Assert.StringNotNullOrWhitespace(resEmoji?["name"]?.GetValue<string>());
Assert.Equal("meow", resEmoji?["name"]?.GetValue<string>());
var reqJson2 = new JsonObject() {
{ "name", "meowtwo" }
};
var res2 = await Assert.HttpSuccess(await Client.ApiHttpClient.PatchAsJsonAsync($"/api/v9/applications/{Application.Id}/emojis/{resEmoji?["id"]?.GetValue<string>()}", reqJson2,
TestContext.Current.CancellationToken));
var resEmoji2 = await res2.Content.ReadFromJsonAsync<JsonObject>();
Assert.StringNotNullOrWhitespace(resEmoji2?["id"]?.GetValue<string>());
Assert.StringNotNullOrWhitespace(resEmoji2?["name"]?.GetValue<string>());
Assert.Equal("meowtwo", resEmoji2?["name"]?.GetValue<string>());
}
[Fact]
public async Task DeleteEmoji() {
var reqJson = new JsonObject() {
{ "name", "meow" },
{ "image", Base64ImageData }
};
var res = await Assert.HttpSuccess(await Client.ApiHttpClient.PostAsJsonAsync($"/api/v9/applications/{Application.Id}/emojis", reqJson, TestContext.Current.CancellationToken));
var resEmoji = await res.Content.ReadFromJsonAsync<JsonObject>();
Assert.StringNotNullOrWhitespace(resEmoji?["id"]?.GetValue<string>());
Assert.StringNotNullOrWhitespace(resEmoji?["name"]?.GetValue<string>());
Assert.Equal("meow", resEmoji?["name"]?.GetValue<string>());
await Assert.HttpSuccess(await Client.ApiHttpClient.DeleteAsync($"/api/v9/applications/{Application.Id}/emojis/{resEmoji?["id"]?.GetValue<string>()}",
TestContext.Current.CancellationToken));
}
[Fact]
public async Task GetEmojiSource() {
var reqJson = new JsonObject() {
{ "name", "meow" },
{ "image", Base64ImageData }
};
var res = await Assert.HttpSuccess(await Client.ApiHttpClient.PostAsJsonAsync($"/api/v9/applications/{Application.Id}/emojis", reqJson, TestContext.Current.CancellationToken));
var resEmoji = await res.Content.ReadFromJsonAsync<JsonObject>();
Assert.StringNotNullOrWhitespace(resEmoji?["id"]?.GetValue<string>());
Assert.StringNotNullOrWhitespace(resEmoji?["name"]?.GetValue<string>());
Assert.Equal("meow", resEmoji?["name"]?.GetValue<string>());
var sourceRes = await Assert.HttpSuccess(await Client.ApiHttpClient.GetAsync($"/api/v9/emojis/{resEmoji?["id"]?.GetValue<string>()}/source",
TestContext.Current.CancellationToken));
var sourceResJson = await sourceRes.Content.ReadFromJsonAsync<JsonObject>(cancellationToken: TestContext.Current.CancellationToken);
Assert.StringNotNullOrWhitespace(sourceResJson?["type"]?.GetValue<string>());
Assert.Equal("APPLICATION", sourceResJson?["type"]?.GetValue<string>());
Assert.StringNotNullOrWhitespace(sourceResJson?["application"]?.AsObject()["id"]?.GetValue<string>());
Assert.Equal(Application.Id.ToString(), sourceResJson?["application"]?.AsObject()["id"]?.GetValue<string>());
}
}
@@ -0,0 +1,131 @@
using System.Diagnostics;
using System.Net.Http.Json;
using System.Text.Json.Nodes;
using ArcaneLibs.Extensions;
using Spacebar.Models.Generic;
using Spacebar.Sdk.Core;
using Spacebar.Tests.Abstractions;
using Spacebar.Tests.Extensions;
using Spacebar.Tests.Fixtures;
using Xunit.Internal;
using Xunit.Microsoft.DependencyInjection.Abstracts;
namespace Spacebar.Tests.Tests;
public class GuildEmojiTests(ITestOutputHelper testOutputHelper, TestFixture fixture) : TestBed<TestFixture>(testOutputHelper, fixture), IAsyncLifetime {
private readonly Config _config = fixture.GetRequiredService<Config>(testOutputHelper);
private readonly SpacebarClientWellKnownResolverService _wellKnownResolver = fixture.GetRequiredService<SpacebarClientWellKnownResolverService>(testOutputHelper);
private readonly SpacebarClientProviderService _clientProvider = fixture.GetRequiredService<SpacebarClientProviderService>(testOutputHelper);
private readonly UserAbstraction _userAbstraction = fixture.GetRequiredService<UserAbstraction>(testOutputHelper);
private static AuthenticatedSpacebarClient Client { get; set; } = null!;
private static Guild? Guild { get; set; }
private const string Base64ImageData =
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAIAAABMXPacAAAAAXNSR0IB2cksfwAAAARnQU1BAACxjwv8YQUAAAAgY0hSTQAAeiYAAICEAAD6AAAAgOgAAHUwAADqYAAAOpgAABdwnLpRPAAAA8hJREFUeNrtnD9oE1EYwL8XOxwIphksdIjQoUNQB41TumVrpoJLwU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HIp3lC/VUZDDDkw8WiEvN+pk9A3OA4LWjJwcORL63RfmmlTWX108qQ0/PQH51eDKt6efo9lWFh+hCD7tyNvVNK+YugBIpwgDAhAACEAAIAABgAAEAAIQAAhAACAAAYAABAACEAAIQAACCAECEAAIQAAgAAGAAAQAAiaKn3vk4S2WTtVqAAAAAElFTkSuQmCC";
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"
});
}
[Fact]
public async Task GetEmojis() {
var res = await Assert.HttpSuccess(await Client.ApiHttpClient.GetAsync($"/api/v9/guilds/{Guild.Id}/emojis", TestContext.Current.CancellationToken));
var resEmojis = await res.Content.ReadFromJsonAsync<JsonArray>();
resEmojis!.ForEach(eJson => {
Assert.StringNotNullOrWhitespace(eJson?["id"]?.GetValue<string>());
Assert.StringNotNullOrWhitespace(eJson?["name"]?.GetValue<string>());
});
}
[Fact]
public async Task UploadEmoji() {
var reqJson = new JsonObject() {
{ "name", "meow" },
{ "image", Base64ImageData }
};
var res = await Assert.HttpSuccess(await Client.ApiHttpClient.PostAsJsonAsync($"/api/v9/guilds/{Guild.Id}/emojis", reqJson, TestContext.Current.CancellationToken));
var resEmoji = await res.Content.ReadFromJsonAsync<JsonObject>();
Assert.StringNotNullOrWhitespace(resEmoji?["id"]?.GetValue<string>());
Assert.StringNotNullOrWhitespace(resEmoji?["name"]?.GetValue<string>());
Assert.Equal("meow", resEmoji?["name"]?.GetValue<string>());
}
[Fact]
public async Task UpdateEmoji() {
var reqJson = new JsonObject() {
{ "name", "meow" },
{ "image", Base64ImageData }
};
var res = await Assert.HttpSuccess(await Client.ApiHttpClient.PostAsJsonAsync($"/api/v9/guilds/{Guild.Id}/emojis", reqJson, TestContext.Current.CancellationToken));
var resEmoji = await res.Content.ReadFromJsonAsync<JsonObject>();
Assert.StringNotNullOrWhitespace(resEmoji?["id"]?.GetValue<string>());
Assert.StringNotNullOrWhitespace(resEmoji?["name"]?.GetValue<string>());
Assert.Equal("meow", resEmoji?["name"]?.GetValue<string>());
var reqJson2 = new JsonObject() {
{ "name", "meowtwo" }
};
var res2 = await Assert.HttpSuccess(await Client.ApiHttpClient.PatchAsJsonAsync($"/api/v9/guilds/{Guild.Id}/emojis/{resEmoji?["id"]?.GetValue<string>()}", reqJson2,
TestContext.Current.CancellationToken));
var resEmoji2 = await res2.Content.ReadFromJsonAsync<JsonObject>();
Assert.StringNotNullOrWhitespace(resEmoji2?["id"]?.GetValue<string>());
Assert.StringNotNullOrWhitespace(resEmoji2?["name"]?.GetValue<string>());
Assert.Equal("meowtwo", resEmoji2?["name"]?.GetValue<string>());
}
[Fact]
public async Task DeleteEmoji() {
var reqJson = new JsonObject() {
{ "name", "meow" },
{ "image", Base64ImageData }
};
var res = await Assert.HttpSuccess(await Client.ApiHttpClient.PostAsJsonAsync($"/api/v9/guilds/{Guild.Id}/emojis", reqJson, TestContext.Current.CancellationToken));
var resEmoji = await res.Content.ReadFromJsonAsync<JsonObject>();
Assert.StringNotNullOrWhitespace(resEmoji?["id"]?.GetValue<string>());
Assert.StringNotNullOrWhitespace(resEmoji?["name"]?.GetValue<string>());
Assert.Equal("meow", resEmoji?["name"]?.GetValue<string>());
await Assert.HttpSuccess(await Client.ApiHttpClient.DeleteAsync($"/api/v9/guilds/{Guild.Id}/emojis/{resEmoji?["id"]?.GetValue<string>()}",
TestContext.Current.CancellationToken));
}
[Fact]
public async Task GetEmojiSource() {
var reqJson = new JsonObject() {
{ "name", "meow" },
{ "image", Base64ImageData }
};
var res = await Assert.HttpSuccess(await Client.ApiHttpClient.PostAsJsonAsync($"/api/v9/guilds/{Guild.Id}/emojis", reqJson, TestContext.Current.CancellationToken));
var resEmoji = await res.Content.ReadFromJsonAsync<JsonObject>();
Assert.StringNotNullOrWhitespace(resEmoji?["id"]?.GetValue<string>());
Assert.StringNotNullOrWhitespace(resEmoji?["name"]?.GetValue<string>());
Assert.Equal("meow", resEmoji?["name"]?.GetValue<string>());
var sourceRes = await Assert.HttpSuccess(await Client.ApiHttpClient.GetAsync($"/api/v9/emojis/{resEmoji?["id"]?.GetValue<string>()}/source",
TestContext.Current.CancellationToken));
var sourceResJson = await sourceRes.Content.ReadFromJsonAsync<JsonObject>(cancellationToken: TestContext.Current.CancellationToken);
Assert.StringNotNullOrWhitespace(sourceResJson?["type"]?.GetValue<string>());
Assert.Equal("GUILD", sourceResJson?["type"]?.GetValue<string>());
Assert.StringNotNullOrWhitespace(sourceResJson?["guild"]?.AsObject()["id"]?.GetValue<string>());
Assert.Equal(Guild.Id.ToString(), sourceResJson?["guild"]?.AsObject()["id"]?.GetValue<string>());
}
}
@@ -1,7 +1,7 @@
{
"Configuration": {
// "TestInstance": "http://localhost:3001",
"TestInstance": "http://localhost:5085",
"TestInstance": "http://localhost:3001",
// "TestInstance": "http://localhost:5085",
"RegisterConcurrentCount": 150
}
}