1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-22 12:15:36 +01:00
bitwarden-server/test/Core.Test/Utilities/PermissiveStringConverterTests.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

169 lines
6.2 KiB
C#
Raw Normal View History

2022-07-05 18:39:43 +02:00
using System.Globalization;
using System.Text.Json;
using System.Text.Json.Serialization;
using Bit.Core.Utilities;
using Bit.Test.Common.Helpers;
using Xunit;
namespace Bit.Core.Test.Utilities;
2022-08-29 22:06:55 +02:00
public class PermissiveStringConverterTests
{
private const string numberJson = "{ \"StringProp\": 1, \"EnumerableStringProp\": [ 2, 3 ]}";
private const string stringJson = "{ \"StringProp\": \"1\", \"EnumerableStringProp\": [ \"2\", \"3\" ]}";
private const string nullAndEmptyJson = "{ \"StringProp\": null, \"EnumerableStringProp\": [] }";
private const string singleValueJson = "{ \"StringProp\": 1, \"EnumerableStringProp\": \"Hello!\" }";
private const string nullJson = "{ \"StringProp\": null, \"EnumerableStringProp\": null }";
private const string boolJson = "{ \"StringProp\": true, \"EnumerableStringProp\": [ false, 1.2]}";
private const string objectJsonOne = "{ \"StringProp\": { \"Message\": \"Hi\"}, \"EnumerableStringProp\": []}";
private const string objectJsonTwo = "{ \"StringProp\": \"Hi\", \"EnumerableStringProp\": {}}";
private readonly string bigNumbersJson =
"{ \"StringProp\":" + decimal.MinValue + ", \"EnumerableStringProp\": [" + ulong.MaxValue + ", " + long.MinValue + "]}";
2022-08-29 22:06:55 +02:00
[Theory]
[InlineData(numberJson)]
[InlineData(stringJson)]
public void Read_Success(string json)
{
var obj = JsonSerializer.Deserialize<TestObject>(json);
Assert.Equal("1", obj.StringProp);
Assert.Equal(2, obj.EnumerableStringProp.Count());
Assert.Equal("2", obj.EnumerableStringProp.ElementAt(0));
Assert.Equal("3", obj.EnumerableStringProp.ElementAt(1));
2022-08-29 20:53:16 +02:00
}
2022-08-29 22:06:55 +02:00
[Fact]
public void Read_Boolean_Success()
2022-08-29 22:06:55 +02:00
{
var obj = JsonSerializer.Deserialize<TestObject>(boolJson);
Assert.Equal("True", obj.StringProp);
Assert.Equal(2, obj.EnumerableStringProp.Count());
Assert.Equal("False", obj.EnumerableStringProp.ElementAt(0));
Assert.Equal("1.2", obj.EnumerableStringProp.ElementAt(1));
2022-08-29 20:53:16 +02:00
}
[Fact]
public void Read_Float_Success_Culture()
{
var ci = new CultureInfo("sv-SE");
Thread.CurrentThread.CurrentCulture = ci;
Thread.CurrentThread.CurrentUICulture = ci;
var obj = JsonSerializer.Deserialize<TestObject>(boolJson);
Assert.Equal("1.2", obj.EnumerableStringProp.ElementAt(1));
}
[Fact]
public void Read_BigNumbers_Success()
{
var obj = JsonSerializer.Deserialize<TestObject>(bigNumbersJson);
Assert.Equal(decimal.MinValue.ToString(), obj.StringProp);
Assert.Equal(2, obj.EnumerableStringProp.Count());
Assert.Equal(ulong.MaxValue.ToString(), obj.EnumerableStringProp.ElementAt(0));
Assert.Equal(long.MinValue.ToString(), obj.EnumerableStringProp.ElementAt(1));
}
2022-08-29 22:06:55 +02:00
[Fact]
public void Read_SingleValue_Success()
2022-08-29 22:06:55 +02:00
{
var obj = JsonSerializer.Deserialize<TestObject>(singleValueJson);
Assert.Equal("1", obj.StringProp);
Assert.Single(obj.EnumerableStringProp);
Assert.Equal("Hello!", obj.EnumerableStringProp.ElementAt(0));
2022-08-29 22:06:55 +02:00
}
[Fact]
public void Read_NullAndEmptyJson_Success()
2022-08-29 22:06:55 +02:00
{
var obj = JsonSerializer.Deserialize<TestObject>(nullAndEmptyJson);
Assert.Null(obj.StringProp);
Assert.Empty(obj.EnumerableStringProp);
2022-08-29 22:06:55 +02:00
}
[Fact]
public void Read_Null_Success()
2022-08-29 22:06:55 +02:00
{
var obj = JsonSerializer.Deserialize<TestObject>(nullJson);
Assert.Null(obj.StringProp);
Assert.Null(obj.EnumerableStringProp);
2022-08-29 22:06:55 +02:00
}
2022-08-29 20:53:16 +02:00
[Theory]
[InlineData(objectJsonOne)]
[InlineData(objectJsonTwo)]
public void Read_Object_Throws(string json)
2022-08-29 22:06:55 +02:00
{
var exception = Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<TestObject>(json));
}
[Fact]
public void Write_Success()
2022-08-29 22:06:55 +02:00
{
var json = JsonSerializer.Serialize(new TestObject
2022-08-29 20:53:16 +02:00
{
StringProp = "1",
EnumerableStringProp = new List<string>
{
2022-08-29 22:06:55 +02:00
"2",
"3",
},
});
var jsonElement = JsonDocument.Parse(json).RootElement;
2022-08-29 22:06:55 +02:00
var stringProp = AssertHelper.AssertJsonProperty(jsonElement, "StringProp", JsonValueKind.String);
Assert.Equal("1", stringProp.GetString());
var list = AssertHelper.AssertJsonProperty(jsonElement, "EnumerableStringProp", JsonValueKind.Array);
Assert.Equal(2, list.GetArrayLength());
var firstElement = list[0];
Assert.Equal(JsonValueKind.String, firstElement.ValueKind);
Assert.Equal("2", firstElement.GetString());
var secondElement = list[1];
Assert.Equal(JsonValueKind.String, secondElement.ValueKind);
Assert.Equal("3", secondElement.GetString());
}
2022-08-29 22:06:55 +02:00
[Fact]
public void Write_Null()
2022-08-29 22:06:55 +02:00
{
// When the values are null the converters aren't actually ran and it automatically serializes null
var json = JsonSerializer.Serialize(new TestObject
2022-08-29 22:06:55 +02:00
{
StringProp = null,
EnumerableStringProp = null,
});
2022-08-29 20:53:16 +02:00
var jsonElement = JsonDocument.Parse(json).RootElement;
2022-08-29 22:06:55 +02:00
AssertHelper.AssertJsonProperty(jsonElement, "StringProp", JsonValueKind.Null);
AssertHelper.AssertJsonProperty(jsonElement, "EnumerableStringProp", JsonValueKind.Null);
}
2022-08-29 20:53:16 +02:00
[Fact]
public void Write_Empty()
2022-08-29 20:53:16 +02:00
{
// When the values are null the converters aren't actually ran and it automatically serializes null
var json = JsonSerializer.Serialize(new TestObject
2022-08-29 22:06:55 +02:00
{
StringProp = "",
EnumerableStringProp = Enumerable.Empty<string>(),
});
2022-08-29 20:53:16 +02:00
var jsonElement = JsonDocument.Parse(json).RootElement;
2022-08-29 22:06:55 +02:00
var stringVal = AssertHelper.AssertJsonProperty(jsonElement, "StringProp", JsonValueKind.String).GetString();
Assert.Equal("", stringVal);
var array = AssertHelper.AssertJsonProperty(jsonElement, "EnumerableStringProp", JsonValueKind.Array);
Assert.Equal(0, array.GetArrayLength());
}
2022-08-29 20:53:16 +02:00
}
2022-08-29 22:06:55 +02:00
public class TestObject
2022-08-29 22:06:55 +02:00
{
[JsonConverter(typeof(PermissiveStringConverter))]
public string StringProp { get; set; }
2022-08-29 22:06:55 +02:00
[JsonConverter(typeof(PermissiveStringEnumerableConverter))]
public IEnumerable<string> EnumerableStringProp { get; set; }
2022-08-29 22:06:55 +02:00
}