2024-06-05 11:42:02 -07:00
|
|
|
|
|
|
|
|
|
|
using Bit.Api.Auth.Models.Response.TwoFactor;
|
|
|
|
|
|
using Bit.Core.Entities;
|
|
|
|
|
|
using Bit.Test.Common.AutoFixture.Attributes;
|
|
|
|
|
|
using Xunit;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Bit.Api.Test.Auth.Models.Response;
|
|
|
|
|
|
|
|
|
|
|
|
public class UserTwoFactorDuoResponseModelTests
|
|
|
|
|
|
{
|
|
|
|
|
|
[Theory]
|
|
|
|
|
|
[BitAutoData]
|
2024-11-18 15:58:05 -08:00
|
|
|
|
public void User_WithDuo_UserNull_ThrowsArgumentException(User user)
|
2024-06-05 11:42:02 -07:00
|
|
|
|
{
|
|
|
|
|
|
// Arrange
|
2024-11-18 15:58:05 -08:00
|
|
|
|
user.TwoFactorProviders = GetTwoFactorDuoProvidersJson();
|
2024-06-05 11:42:02 -07:00
|
|
|
|
|
|
|
|
|
|
// Act
|
2024-11-18 15:58:05 -08:00
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var model = new TwoFactorDuoResponseModel(null as User);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (ArgumentNullException e)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Assert
|
|
|
|
|
|
Assert.Equal("Value cannot be null. (Parameter 'user')", e.Message);
|
|
|
|
|
|
}
|
2024-06-05 11:42:02 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
|
[BitAutoData]
|
|
|
|
|
|
public void User_WithDuo_ShouldBuildModel(User user)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Arrange
|
|
|
|
|
|
user.TwoFactorProviders = GetTwoFactorDuoProvidersJson();
|
|
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
|
var model = new TwoFactorDuoResponseModel(user);
|
|
|
|
|
|
|
2024-11-18 15:58:05 -08:00
|
|
|
|
// Assert
|
2024-06-05 11:42:02 -07:00
|
|
|
|
Assert.NotNull(model);
|
|
|
|
|
|
Assert.Equal("clientId", model.ClientId);
|
2024-07-22 11:21:14 -04:00
|
|
|
|
Assert.Equal("secret************", model.ClientSecret);
|
2024-06-05 11:42:02 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
|
[BitAutoData]
|
|
|
|
|
|
public void User_WithDuoEmpty_ShouldFail(User user)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Arrange
|
|
|
|
|
|
user.TwoFactorProviders = "{\"2\" : {}}";
|
|
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
|
var model = new TwoFactorDuoResponseModel(user);
|
|
|
|
|
|
|
|
|
|
|
|
/// Assert
|
|
|
|
|
|
Assert.False(model.Enabled);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
|
[BitAutoData]
|
|
|
|
|
|
public void User_WithTwoFactorProvidersNull_ShouldFail(User user)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Arrange
|
|
|
|
|
|
user.TwoFactorProviders = null;
|
|
|
|
|
|
|
|
|
|
|
|
// Act
|
2024-11-18 15:58:05 -08:00
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var model = new TwoFactorDuoResponseModel(user);
|
2024-06-05 11:42:02 -07:00
|
|
|
|
|
2024-11-18 15:58:05 -08:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Assert
|
|
|
|
|
|
Assert.IsType<ArgumentNullException>(ex);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2024-06-05 11:42:02 -07:00
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-18 15:58:05 -08:00
|
|
|
|
private string GetTwoFactorDuoProvidersJson()
|
2024-06-05 11:42:02 -07:00
|
|
|
|
{
|
2024-07-22 11:21:14 -04:00
|
|
|
|
return
|
|
|
|
|
|
"{\"2\":{\"Enabled\":true,\"MetaData\":{\"ClientSecret\":\"secretClientSecret\",\"ClientId\":\"clientId\",\"Host\":\"example.com\"}}}";
|
2024-06-05 11:42:02 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|