using Bit.Core.AdminConsole.Errors; using Bit.Core.AdminConsole.Shared.Validation; using Xunit; namespace Bit.Core.Test.AdminConsole.Shared; public class IValidatorTests { public class TestClass { public string Name { get; set; } = string.Empty; } public record InvalidRequestError(T ErroredValue) : Error(Code, ErroredValue) { public const string Code = "InvalidRequest"; } public class TestClassValidator : IValidator { public Task> ValidateAsync(TestClass value) { if (string.IsNullOrWhiteSpace(value.Name)) { return Task.FromResult>(new Invalid { Errors = [new InvalidRequestError(value)] }); } return Task.FromResult>(new Valid { Value = value }); } } [Fact] public async Task ValidateAsync_WhenSomethingIsInvalid_ReturnsInvalidWithError() { var example = new TestClass(); var result = await new TestClassValidator().ValidateAsync(example); Assert.IsType>(result); var invalidResult = result as Invalid; Assert.Equal(InvalidRequestError.Code, invalidResult.Errors.First().Message); } [Fact] public async Task ValidateAsync_WhenIsValid_ReturnsValid() { var example = new TestClass { Name = "Valid" }; var result = await new TestClassValidator().ValidateAsync(example); Assert.IsType>(result); var validResult = result as Valid; Assert.Equal(example.Name, validResult.Value.Name); } }