mirror of
https://github.com/bitwarden/server.git
synced 2024-11-22 12:15:36 +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
62 lines
2.5 KiB
C#
62 lines
2.5 KiB
C#
using Bit.Api.Auth.Models.Request;
|
|
using Bit.Core.AdminConsole.Entities;
|
|
using Bit.Core.Auth.Enums;
|
|
using Bit.Core.Auth.Models;
|
|
using Xunit;
|
|
|
|
namespace Bit.Api.Test.Auth.Models.Request;
|
|
|
|
public class OrganizationTwoFactorDuoRequestModelTests
|
|
{
|
|
|
|
[Fact]
|
|
public void ShouldAddOrUpdateTwoFactorProvider_WhenExistingProviderDoesNotExist()
|
|
{
|
|
// Arrange
|
|
var existingOrg = new Organization();
|
|
var model = new UpdateTwoFactorDuoRequestModel
|
|
{
|
|
ClientId = "clientId",
|
|
ClientSecret = "clientSecret",
|
|
Host = "example.com"
|
|
};
|
|
|
|
// Act
|
|
var result = model.ToOrganization(existingOrg);
|
|
|
|
// Assert
|
|
Assert.True(result.GetTwoFactorProviders().ContainsKey(TwoFactorProviderType.OrganizationDuo));
|
|
Assert.Equal("clientId", result.GetTwoFactorProviders()[TwoFactorProviderType.OrganizationDuo].MetaData["ClientId"]);
|
|
Assert.Equal("clientSecret", result.GetTwoFactorProviders()[TwoFactorProviderType.OrganizationDuo].MetaData["ClientSecret"]);
|
|
Assert.Equal("example.com", result.GetTwoFactorProviders()[TwoFactorProviderType.OrganizationDuo].MetaData["Host"]);
|
|
Assert.True(result.GetTwoFactorProviders()[TwoFactorProviderType.OrganizationDuo].Enabled);
|
|
}
|
|
|
|
[Fact]
|
|
public void ShouldUpdateTwoFactorProvider_WhenExistingProviderExists()
|
|
{
|
|
// Arrange
|
|
var existingOrg = new Organization();
|
|
existingOrg.SetTwoFactorProviders(new Dictionary<TwoFactorProviderType, TwoFactorProvider>
|
|
{
|
|
{ TwoFactorProviderType.OrganizationDuo, new TwoFactorProvider() }
|
|
});
|
|
var model = new UpdateTwoFactorDuoRequestModel
|
|
{
|
|
ClientId = "newClientId",
|
|
ClientSecret = "newClientSecret",
|
|
Host = "newExample.com"
|
|
};
|
|
|
|
// Act
|
|
var result = model.ToOrganization(existingOrg);
|
|
|
|
// Assert
|
|
Assert.True(result.GetTwoFactorProviders().ContainsKey(TwoFactorProviderType.OrganizationDuo));
|
|
Assert.Equal("newClientId", result.GetTwoFactorProviders()[TwoFactorProviderType.OrganizationDuo].MetaData["ClientId"]);
|
|
Assert.Equal("newClientSecret", result.GetTwoFactorProviders()[TwoFactorProviderType.OrganizationDuo].MetaData["ClientSecret"]);
|
|
Assert.Equal("newExample.com", result.GetTwoFactorProviders()[TwoFactorProviderType.OrganizationDuo].MetaData["Host"]);
|
|
Assert.True(result.GetTwoFactorProviders()[TwoFactorProviderType.OrganizationDuo].Enabled);
|
|
}
|
|
}
|