mirror of
https://github.com/bitwarden/server.git
synced 2025-05-07 12:42:24 -05:00
[PM-15621] Remove the tests for the mapper until we align as a team on the desired approach.
This commit is contained in:
parent
9533d83cb2
commit
0aa596d9e4
@ -602,7 +602,7 @@ public class OrganizationUsersController : Controller
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return result.MapToActionResultWithSingleErrorMessage();
|
return result.MapToAction();
|
||||||
}
|
}
|
||||||
|
|
||||||
[RequireFeature(FeatureFlagKeys.AccountDeprovisioning)]
|
[RequireFeature(FeatureFlagKeys.AccountDeprovisioning)]
|
||||||
|
@ -5,19 +5,8 @@ namespace Bit.Api.Utilities;
|
|||||||
|
|
||||||
public static class CommandResultExtensions
|
public static class CommandResultExtensions
|
||||||
{
|
{
|
||||||
public static IActionResult MapToActionResultWithErrorMessages<T>(this CommandResult<T> commandResult)
|
|
||||||
{
|
|
||||||
return commandResult switch
|
|
||||||
{
|
|
||||||
NoRecordFoundFailure<T> failure => new ObjectResult(failure.ErrorMessages) { StatusCode = StatusCodes.Status404NotFound },
|
|
||||||
BadRequestFailure<T> failure => new ObjectResult(failure.ErrorMessages) { StatusCode = StatusCodes.Status400BadRequest },
|
|
||||||
Failure<T> failure => new ObjectResult(failure.ErrorMessages) { StatusCode = StatusCodes.Status400BadRequest },
|
|
||||||
Success<T> success => new ObjectResult(success.Value) { StatusCode = StatusCodes.Status200OK },
|
|
||||||
_ => throw new InvalidOperationException($"Unhandled commandResult type: {commandResult.GetType().Name}")
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
public static IActionResult MapToActionResultWithSingleErrorMessage<T>(this CommandResult<T> commandResult)
|
public static IActionResult MapToAction<T>(this CommandResult<T> commandResult)
|
||||||
{
|
{
|
||||||
return commandResult switch
|
return commandResult switch
|
||||||
{
|
{
|
||||||
@ -28,16 +17,4 @@ public static class CommandResultExtensions
|
|||||||
_ => throw new InvalidOperationException($"Unhandled commandResult type: {commandResult.GetType().Name}")
|
_ => throw new InvalidOperationException($"Unhandled commandResult type: {commandResult.GetType().Name}")
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public static IActionResult MapToActionResult(this CommandResult commandResult)
|
|
||||||
{
|
|
||||||
return commandResult switch
|
|
||||||
{
|
|
||||||
NoRecordFoundFailure failure => new ObjectResult(failure.ErrorMessages) { StatusCode = StatusCodes.Status404NotFound },
|
|
||||||
BadRequestFailure failure => new ObjectResult(failure.ErrorMessages) { StatusCode = StatusCodes.Status400BadRequest },
|
|
||||||
Failure failure => new ObjectResult(failure.ErrorMessages) { StatusCode = StatusCodes.Status400BadRequest },
|
|
||||||
Success => new ObjectResult(new { }) { StatusCode = StatusCodes.Status200OK },
|
|
||||||
_ => throw new InvalidOperationException($"Unhandled commandResult type: {commandResult.GetType().Name}")
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,107 +0,0 @@
|
|||||||
using Bit.Api.Utilities;
|
|
||||||
using Bit.Core.Models.Commands;
|
|
||||||
using Bit.Core.Vault.Entities;
|
|
||||||
using Microsoft.AspNetCore.Http;
|
|
||||||
using Microsoft.AspNetCore.Mvc;
|
|
||||||
using Xunit;
|
|
||||||
|
|
||||||
namespace Bit.Api.Test.Utilities;
|
|
||||||
|
|
||||||
public class CommandResultExtensionTests
|
|
||||||
{
|
|
||||||
public static IEnumerable<object[]> WithGenericTypeTestCases()
|
|
||||||
{
|
|
||||||
yield return new object[]
|
|
||||||
{
|
|
||||||
new NoRecordFoundFailure<Cipher>(new[] { "Error 1", "Error 2" }),
|
|
||||||
new ObjectResult(new[] { "Error 1", "Error 2" }) { StatusCode = StatusCodes.Status404NotFound }
|
|
||||||
};
|
|
||||||
yield return new object[]
|
|
||||||
{
|
|
||||||
new BadRequestFailure<Cipher>("Error 3"),
|
|
||||||
new ObjectResult(new[] { "Error 3" }) { StatusCode = StatusCodes.Status400BadRequest }
|
|
||||||
};
|
|
||||||
yield return new object[]
|
|
||||||
{
|
|
||||||
new Failure<Cipher>("Error 4"),
|
|
||||||
new ObjectResult(new[] { "Error 4" }) { StatusCode = StatusCodes.Status400BadRequest }
|
|
||||||
};
|
|
||||||
var cipher = new Cipher() { Id = Guid.NewGuid() };
|
|
||||||
|
|
||||||
yield return new object[]
|
|
||||||
{
|
|
||||||
new Success<Cipher>(cipher),
|
|
||||||
new ObjectResult(cipher) { StatusCode = StatusCodes.Status200OK }
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
[Theory]
|
|
||||||
[MemberData(nameof(WithGenericTypeTestCases))]
|
|
||||||
public void MapToActionResultWithErrorMessages_WithGenericType_ShouldMapToHttpResponse(CommandResult<Cipher> input, ObjectResult expected)
|
|
||||||
{
|
|
||||||
var result = input.MapToActionResultWithErrorMessages();
|
|
||||||
|
|
||||||
Assert.Equivalent(expected, result);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void MapToActionResultWithErrorMessages_WithGenericType_ShouldThrowExceptionForUnhandledCommandResult()
|
|
||||||
{
|
|
||||||
var result = new NotImplementedCommandResult();
|
|
||||||
|
|
||||||
Assert.Throws<InvalidOperationException>(() => result.MapToActionResult());
|
|
||||||
}
|
|
||||||
|
|
||||||
public static IEnumerable<object[]> TestCases()
|
|
||||||
{
|
|
||||||
yield return new object[]
|
|
||||||
{
|
|
||||||
new NoRecordFoundFailure(new[] { "Error 1", "Error 2" }),
|
|
||||||
new ObjectResult(new[] { "Error 1", "Error 2" }) { StatusCode = StatusCodes.Status404NotFound }
|
|
||||||
};
|
|
||||||
yield return new object[]
|
|
||||||
{
|
|
||||||
new BadRequestFailure("Error 3"),
|
|
||||||
new ObjectResult(new[] { "Error 3" }) { StatusCode = StatusCodes.Status400BadRequest }
|
|
||||||
};
|
|
||||||
yield return new object[]
|
|
||||||
{
|
|
||||||
new Failure("Error 4"),
|
|
||||||
new ObjectResult(new[] { "Error 4" }) { StatusCode = StatusCodes.Status400BadRequest }
|
|
||||||
};
|
|
||||||
yield return new object[]
|
|
||||||
{
|
|
||||||
new Success(),
|
|
||||||
new ObjectResult(new { }) { StatusCode = StatusCodes.Status200OK }
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
[Theory]
|
|
||||||
[MemberData(nameof(TestCases))]
|
|
||||||
public void MapToActionResultWithErrorMessages_ShouldMapToHttpResponse(CommandResult input, ObjectResult expected)
|
|
||||||
{
|
|
||||||
var result = input.MapToActionResult();
|
|
||||||
|
|
||||||
Assert.Equivalent(expected, result);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void MapToActionResultWithErrorMessages_ShouldThrowExceptionForUnhandledCommandResult()
|
|
||||||
{
|
|
||||||
var result = new NotImplementedCommandResult<Cipher>();
|
|
||||||
|
|
||||||
Assert.Throws<InvalidOperationException>(() => result.MapToActionResultWithErrorMessages());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public class NotImplementedCommandResult<T> : CommandResult<T>
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public class NotImplementedCommandResult : CommandResult
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user