Files
Radicade/ServerTests/AdminBundleBindingTests.cs
2026-10-16 23:33:13 -07:00

53 lines
2.4 KiB
C#

using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using RadicadeServer.Admin;
using RadicadeServer.DB;
using RadicadeServer.RecNet.Rooms;
using Xunit;
namespace RadicadeServer.Tests;
[Collection(DbFactoryCollection.Name)]
public class AdminBundleBindingTests
{
[Fact]
public async Task BindingRequiresAdminExistingBundleAndMatchingRoomAndPreservesRoomSave()
{
var root = Directory.CreateTempSubdirectory("radicade-bundle-binding-").FullName;
var oldStorage = DBConfig.CDNStorage;
var oldKey = DBConfig.AdminApiKey;
DBConfig.CDNStorage = root;
DBConfig.AdminApiKey = "binding-test-key";
try
{
await using var db = new RecDBContext(TestDbOptions.Create()
.UseInMemoryDatabase(Guid.NewGuid().ToString()).Options);
var sub = new SubRoom { RoomId = 228, SubRoomId = 280, DataBlob = "original.room" };
db.SubRooms.Add(sub);
await db.SaveChangesAsync();
var api = new AdminRoomAPI(db);
var id = new string('a', 40);
Assert.IsType<UnauthorizedObjectResult>(await api.BindAssetBundle("wrong", 228, 280, id));
Assert.IsType<BadRequestObjectResult>(await api.BindAssetBundle("binding-test-key", 228, 280, "../bad"));
Assert.IsType<BadRequestObjectResult>(await api.BindAssetBundle("binding-test-key", 228, 280, id));
await File.WriteAllTextAsync(Path.Join(root, id + ".assetbundle"), "test bundle");
Assert.IsType<NotFoundObjectResult>(await api.BindAssetBundle("binding-test-key", 229, 280, id));
Assert.Equal("", sub.UnityAssetId);
Assert.IsType<OkObjectResult>(await api.BindAssetBundle("binding-test-key", 228, 280, id));
db.ChangeTracker.Clear();
var stored = await db.SubRooms.SingleAsync();
Assert.Equal(id, stored.UnityAssetId);
Assert.Equal("original.room", stored.DataBlob);
Assert.IsType<OkObjectResult>(await api.BindAssetBundle("binding-test-key", 228, 280, id));
await File.WriteAllTextAsync(Path.Join(root, id), "shadowing manifest");
Assert.IsType<BadRequestObjectResult>(await api.BindAssetBundle("binding-test-key", 228, 280, id));
}
finally
{
DBConfig.CDNStorage = oldStorage;
DBConfig.AdminApiKey = oldKey;
Directory.Delete(root, recursive: true);
}
}
}