mirror of
https://github.com/JogmansonReal/Radicade.git
synced 2026-10-25 23:18:56 -07:00
102 lines
4.4 KiB
C#
102 lines
4.4 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using RadicadeServer.API.Config;
|
|
using RadicadeServer.DB;
|
|
using RadicadeServer.RecNet.Accounts.Local;
|
|
using Xunit;
|
|
|
|
namespace RadicadeServer.Tests;
|
|
|
|
public class PlayerSettingsPersistenceTests
|
|
{
|
|
[Theory]
|
|
[InlineData("text/plain", "{\"Key\":\"MusicVolume\",\"Value\":0.25}", "MusicVolume", "0.25")]
|
|
[InlineData("application/json", "{\"Key\":\"AutoSprint\",\"Value\":false}", "AutoSprint", "False")]
|
|
[InlineData("application/x-www-form-urlencoded", "Key=MusicVolume&Value=0.75", "MusicVolume", "0.75")]
|
|
public async Task PostRequestSurvivesFreshDatabaseContext(string contentType, string body, string key, string expected)
|
|
{
|
|
var options = TestDbOptions.Create()
|
|
.UseInMemoryDatabase(Guid.NewGuid().ToString()).Options;
|
|
await using (var db = new RecDBContext(options))
|
|
{
|
|
var api = CreateApi(db);
|
|
api.Request.ContentType = contentType;
|
|
api.Request.Body = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(body));
|
|
var result = await api.SetSettingRequest();
|
|
Assert.IsType<Microsoft.AspNetCore.Mvc.OkObjectResult>(result);
|
|
}
|
|
await using var reloaded = new RecDBContext(options);
|
|
Assert.Equal(expected, CreateApi(reloaded).GetSettings().Single(x => x.Key == key).Value);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task InvalidBulkBodyReturnsFailureAndPreservesSavedValues()
|
|
{
|
|
var options = TestDbOptions.Create()
|
|
.UseInMemoryDatabase(Guid.NewGuid().ToString()).Options;
|
|
await using var db = new RecDBContext(options);
|
|
var api = CreateApi(db);
|
|
await api.SetSetting(new Setting { Key = "MusicVolume", Value = "0.2" });
|
|
api.Request.Body = new MemoryStream(System.Text.Encoding.UTF8.GetBytes("not-json"));
|
|
Assert.IsType<Microsoft.AspNetCore.Mvc.BadRequestObjectResult>(await api.PutSettings());
|
|
Assert.Equal("0.2", api.GetSettings().Single(x => x.Key == "MusicVolume").Value);
|
|
}
|
|
|
|
private static PlayerSettingsAPI CreateApi(RecDBContext db)
|
|
{
|
|
var context = new Microsoft.AspNetCore.Http.DefaultHttpContext();
|
|
context.User = new System.Security.Claims.ClaimsPrincipal(new System.Security.Claims.ClaimsIdentity(
|
|
[new System.Security.Claims.Claim(System.Security.Claims.ClaimTypes.NameIdentifier, "2")], "test"));
|
|
return new PlayerSettingsAPI(db)
|
|
{
|
|
ControllerContext = new Microsoft.AspNetCore.Mvc.ControllerContext { HttpContext = context }
|
|
};
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetDoesNotReplaceSavedToggleWithDefault()
|
|
{
|
|
var options = TestDbOptions.Create()
|
|
.UseInMemoryDatabase(Guid.NewGuid().ToString()).Options;
|
|
await using var db = new RecDBContext(options);
|
|
db.PlayerSettings.Add(new PlayerSettings
|
|
{
|
|
AccountId = 2,
|
|
Settings = "[{\"Key\":\"ShowNames\",\"Value\":\"0\"}]"
|
|
});
|
|
await db.SaveChangesAsync();
|
|
|
|
var api = new PlayerSettingsAPI(db);
|
|
var context = new Microsoft.AspNetCore.Http.DefaultHttpContext();
|
|
context.User = new System.Security.Claims.ClaimsPrincipal(
|
|
new System.Security.Claims.ClaimsIdentity([
|
|
new System.Security.Claims.Claim(System.Security.Claims.ClaimTypes.NameIdentifier, "2")
|
|
], "test"));
|
|
api.ControllerContext = new Microsoft.AspNetCore.Mvc.ControllerContext
|
|
{
|
|
HttpContext = context
|
|
};
|
|
|
|
var settings = api.GetSettings();
|
|
Assert.Equal("0", settings.Single(x => x.Key == "ShowNames").Value);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task PutSettingsSurvivesASecondRead()
|
|
{
|
|
var options = TestDbOptions.Create()
|
|
.UseInMemoryDatabase(Guid.NewGuid().ToString()).Options;
|
|
await using var db = new RecDBContext(options);
|
|
var api = new PlayerSettingsAPI(db);
|
|
var context = new Microsoft.AspNetCore.Http.DefaultHttpContext();
|
|
context.User = new System.Security.Claims.ClaimsPrincipal(
|
|
new System.Security.Claims.ClaimsIdentity([
|
|
new System.Security.Claims.Claim(System.Security.Claims.ClaimTypes.NameIdentifier, "2")
|
|
], "test"));
|
|
api.ControllerContext = new Microsoft.AspNetCore.Mvc.ControllerContext { HttpContext = context };
|
|
|
|
await api.SetSetting(new Setting { Key = "AutoSprint", Value = "False" });
|
|
|
|
Assert.Equal("False", api.GetSettings().Single(x => x.Key == "AutoSprint").Value);
|
|
}
|
|
}
|