mirror of
https://github.com/bitwarden/server.git
synced 2024-12-11 15:17:44 +01:00
ab5d4738d6
refactor(TwoFactorAuthentication): Remove references to old Duo SDK version 2 code and replace them with the Duo SDK version 4 supported library DuoUniversal code. Increased unit test coverage in the Two Factor Authentication code space. We opted to use DI instead of Inheritance for the Duo and OrganizaitonDuo two factor tokens to increase testability, since creating a testing mock of the Duo.Client was non-trivial. Reviewed-by: @JaredSnider-Bitwarden
65 lines
1.7 KiB
C#
65 lines
1.7 KiB
C#
using System.ComponentModel.DataAnnotations;
|
|
using Bit.Api.Auth.Models.Request;
|
|
using Xunit;
|
|
|
|
namespace Bit.Api.Test.Auth.Models.Request;
|
|
|
|
public class TwoFactorDuoRequestModelValidationTests
|
|
{
|
|
[Fact]
|
|
public void ShouldReturnValidationError_WhenHostIsInvalid()
|
|
{
|
|
// Arrange
|
|
var model = new UpdateTwoFactorDuoRequestModel
|
|
{
|
|
Host = "invalidHost",
|
|
ClientId = "clientId",
|
|
ClientSecret = "clientSecret",
|
|
};
|
|
|
|
// Act
|
|
var result = model.Validate(new ValidationContext(model));
|
|
|
|
// Assert
|
|
Assert.Single(result);
|
|
Assert.Equal("Host is invalid.", result.First().ErrorMessage);
|
|
Assert.Equal("Host", result.First().MemberNames.First());
|
|
}
|
|
|
|
[Fact]
|
|
public void ShouldReturnValidationError_WhenValuesAreInvalid()
|
|
{
|
|
// Arrange
|
|
var model = new UpdateTwoFactorDuoRequestModel
|
|
{
|
|
Host = "api-12345abc.duosecurity.com"
|
|
};
|
|
|
|
// Act
|
|
var result = model.Validate(new ValidationContext(model));
|
|
|
|
// Assert
|
|
Assert.NotEmpty(result);
|
|
Assert.True(result.Select(x => x.MemberNames.Contains("ClientId")).Any());
|
|
Assert.True(result.Select(x => x.MemberNames.Contains("ClientSecret")).Any());
|
|
}
|
|
|
|
[Fact]
|
|
public void ShouldReturnSuccess_WhenValuesAreValid()
|
|
{
|
|
// Arrange
|
|
var model = new UpdateTwoFactorDuoRequestModel
|
|
{
|
|
Host = "api-12345abc.duosecurity.com",
|
|
ClientId = "clientId",
|
|
ClientSecret = "clientSecret",
|
|
};
|
|
|
|
// Act
|
|
var result = model.Validate(new ValidationContext(model));
|
|
|
|
// Assert
|
|
Assert.Empty(result);
|
|
}
|
|
}
|