mirror of
https://github.com/bitwarden/server.git
synced 2024-11-25 12:45:18 +01:00
5268f2781e
* Start switch to System.Text.Json * Work on switching to System.Text.Json * Main work on STJ refactor * Fix build errors * Run formatting * Delete unused file * Use legacy for two factor providers * Run formatter * Add TokenProviderTests * Run formatting * Fix merge issues * Switch to use JsonSerializer * Address PR feedback * Fix formatting * Ran formatter * Switch to async * Ensure Enums are serialized as strings * Fix formatting * Enqueue single items as arrays * Remove CreateAsync method on AzureQueueService
69 lines
2.7 KiB
C#
69 lines
2.7 KiB
C#
using System;
|
|
using System.Linq;
|
|
using System.Text.Json;
|
|
using Bit.Core.Utilities;
|
|
using Xunit;
|
|
using Xunit.Sdk;
|
|
|
|
namespace Bit.Test.Common.Helpers
|
|
{
|
|
public static class AssertHelper
|
|
{
|
|
public static void AssertPropertyEqual(object expected, object actual, params string[] excludedPropertyStrings)
|
|
{
|
|
var relevantExcludedProperties = excludedPropertyStrings.Where(name => !name.Contains('.')).ToList();
|
|
if (expected == null)
|
|
{
|
|
Assert.Null(actual);
|
|
return;
|
|
}
|
|
|
|
if (actual == null)
|
|
{
|
|
throw new Exception("Expected object is null but actual is not");
|
|
}
|
|
|
|
foreach (var expectedPropInfo in expected.GetType().GetProperties().Where(pi => !relevantExcludedProperties.Contains(pi.Name)))
|
|
{
|
|
var actualPropInfo = actual.GetType().GetProperty(expectedPropInfo.Name);
|
|
|
|
if (actualPropInfo == null)
|
|
{
|
|
throw new Exception(string.Concat($"Expected actual object to contain a property named {expectedPropInfo.Name}, but it does not\n",
|
|
$"Expected:\n{JsonSerializer.Serialize(expected, JsonHelpers.Indented)}\n",
|
|
$"Actual:\n{JsonSerializer.Serialize(actual, JsonHelpers.Indented)}"));
|
|
}
|
|
|
|
if (expectedPropInfo.PropertyType == typeof(string) || expectedPropInfo.PropertyType.IsValueType)
|
|
{
|
|
Assert.Equal(expectedPropInfo.GetValue(expected), actualPropInfo.GetValue(actual));
|
|
}
|
|
else
|
|
{
|
|
var prefix = $"{expectedPropInfo.PropertyType.Name}.";
|
|
var nextExcludedProperties = excludedPropertyStrings.Where(name => name.StartsWith(prefix))
|
|
.Select(name => name[prefix.Length..]).ToArray();
|
|
AssertPropertyEqual(expectedPropInfo.GetValue(expected), actualPropInfo.GetValue(actual), nextExcludedProperties);
|
|
}
|
|
}
|
|
}
|
|
|
|
public static Predicate<T> AssertEqualExpectedPredicate<T>(T expected) => (actual) =>
|
|
{
|
|
Assert.Equal(expected, actual);
|
|
return true;
|
|
};
|
|
|
|
public static JsonElement AssertJsonProperty(JsonElement element, string propertyName, JsonValueKind jsonValueKind)
|
|
{
|
|
if (!element.TryGetProperty(propertyName, out var subElement))
|
|
{
|
|
throw new XunitException($"Could not find property by name '{propertyName}'");
|
|
}
|
|
|
|
Assert.Equal(jsonValueKind, subElement.ValueKind);
|
|
return subElement;
|
|
}
|
|
}
|
|
}
|