mirror of
https://github.com/bitwarden/server.git
synced 2025-06-30 15:42:48 -05:00
[BEEEP] Integration tests (#1945)
* Add api integration tests * Add some stuff * Make program mockable * Work on IntegrationTests for Identity * Formatting * Update packages.lock.json * Update more packages.lock.json * Update all packages.lock.json * Fix InMemory configuration * Actually fix test configuration * Fix tests for CI * Fix event service * Force EF EventRepository * Add client_credentials test * Remove Api.IntegrationTest * Remove Api Program changes * Cleanup * Add more Auth-Email tests * Run formatting * Address some PR feedback * Move integration stuff to it's own common project * Ran linter * Add shared project to test solution * Remove sln changes * Clean usings * Add more coverage * Address PR feedback
This commit is contained in:
@ -1,9 +1,12 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
using Bit.Core.Utilities;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Xunit;
|
||||
using Xunit.Sdk;
|
||||
|
||||
@ -84,6 +87,116 @@ namespace Bit.Test.Common.Helpers
|
||||
return subElement;
|
||||
}
|
||||
|
||||
public static void AssertEqualJson(JsonElement a, JsonElement b)
|
||||
{
|
||||
switch (a.ValueKind)
|
||||
{
|
||||
case JsonValueKind.Array:
|
||||
Assert.Equal(JsonValueKind.Array, b.ValueKind);
|
||||
AssertEqualJsonArray(a, b);
|
||||
break;
|
||||
case JsonValueKind.Object:
|
||||
Assert.Equal(JsonValueKind.Object, b.ValueKind);
|
||||
AssertEqualJsonObject(a, b);
|
||||
break;
|
||||
case JsonValueKind.False:
|
||||
Assert.Equal(JsonValueKind.False, b.ValueKind);
|
||||
break;
|
||||
case JsonValueKind.True:
|
||||
Assert.Equal(JsonValueKind.True, b.ValueKind);
|
||||
break;
|
||||
case JsonValueKind.Number:
|
||||
Assert.Equal(JsonValueKind.Number, b.ValueKind);
|
||||
Assert.Equal(a.GetDouble(), b.GetDouble());
|
||||
break;
|
||||
case JsonValueKind.String:
|
||||
Assert.Equal(JsonValueKind.String, b.ValueKind);
|
||||
Assert.Equal(a.GetString(), b.GetString());
|
||||
break;
|
||||
case JsonValueKind.Null:
|
||||
Assert.Equal(JsonValueKind.Null, b.ValueKind);
|
||||
break;
|
||||
default:
|
||||
throw new XunitException($"Bad JsonValueKind '{a.ValueKind}'");
|
||||
}
|
||||
}
|
||||
|
||||
private static void AssertEqualJsonObject(JsonElement a, JsonElement b)
|
||||
{
|
||||
Debug.Assert(a.ValueKind == JsonValueKind.Object && b.ValueKind == JsonValueKind.Object);
|
||||
|
||||
var aObjectEnumerator = a.EnumerateObject();
|
||||
var bObjectEnumerator = b.EnumerateObject();
|
||||
|
||||
while (true)
|
||||
{
|
||||
var aCanMove = aObjectEnumerator.MoveNext();
|
||||
var bCanMove = bObjectEnumerator.MoveNext();
|
||||
|
||||
if (aCanMove)
|
||||
{
|
||||
Assert.True(bCanMove, $"a was able to enumerate over object '{a}' but b was NOT able to '{b}'");
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.False(bCanMove, $"a was NOT able to enumerate over object '{a}' but b was able to '{b}'");
|
||||
}
|
||||
|
||||
if (aCanMove == false && bCanMove == false)
|
||||
{
|
||||
// They both can't continue to enumerate at the same time, that is valid
|
||||
break;
|
||||
}
|
||||
|
||||
var aProp = aObjectEnumerator.Current;
|
||||
var bProp = bObjectEnumerator.Current;
|
||||
|
||||
Assert.Equal(aProp.Name, bProp.Name);
|
||||
// Recursion!
|
||||
AssertEqualJson(aProp.Value, bProp.Value);
|
||||
}
|
||||
}
|
||||
|
||||
private static void AssertEqualJsonArray(JsonElement a, JsonElement b)
|
||||
{
|
||||
Debug.Assert(a.ValueKind == JsonValueKind.Array && b.ValueKind == JsonValueKind.Array);
|
||||
|
||||
var aArrayEnumerator = a.EnumerateArray();
|
||||
var bArrayEnumerator = b.EnumerateArray();
|
||||
|
||||
while (true)
|
||||
{
|
||||
var aCanMove = aArrayEnumerator.MoveNext();
|
||||
var bCanMove = bArrayEnumerator.MoveNext();
|
||||
|
||||
if (aCanMove)
|
||||
{
|
||||
Assert.True(bCanMove, $"a was able to enumerate over array '{a}' but b was NOT able to '{b}'");
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.False(bCanMove, $"a was NOT able to enumerate over array '{a}' but b was able to '{b}'");
|
||||
}
|
||||
|
||||
if (aCanMove == false && bCanMove == false)
|
||||
{
|
||||
// They both can't continue to enumerate at the same time, that is valid
|
||||
break;
|
||||
}
|
||||
|
||||
var aElement = aArrayEnumerator.Current;
|
||||
var bElement = bArrayEnumerator.Current;
|
||||
|
||||
// Recursion!
|
||||
AssertEqualJson(aElement, bElement);
|
||||
}
|
||||
}
|
||||
|
||||
public async static Task<T> AssertResponseTypeIs<T>(HttpContext context)
|
||||
{
|
||||
return await JsonSerializer.DeserializeAsync<T>(context.Response.Body);
|
||||
}
|
||||
|
||||
public static TimeSpan AssertRecent(DateTime dateTime, int skewSeconds = 2)
|
||||
=> AssertRecent(dateTime, TimeSpan.FromSeconds(skewSeconds));
|
||||
|
||||
|
Reference in New Issue
Block a user