2024-09-30 20:21:30 +02:00
|
|
|
|
#nullable enable
|
|
|
|
|
|
|
|
|
|
using Bit.Admin.Models;
|
|
|
|
|
using Bit.Core.Entities;
|
2024-10-30 15:45:53 +01:00
|
|
|
|
using Bit.Core.Vault.Entities;
|
2024-09-30 20:21:30 +02:00
|
|
|
|
using Bit.Test.Common.AutoFixture.Attributes;
|
|
|
|
|
|
|
|
|
|
namespace Admin.Test.Models;
|
|
|
|
|
|
|
|
|
|
public class UserViewModelTests
|
|
|
|
|
{
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
|
|
|
|
public void IsTwoFactorEnabled_GivenUserAndIsInLookup_WhenUserHasTwoFactorEnabled_ThenReturnsTrue(User user)
|
|
|
|
|
{
|
|
|
|
|
var lookup = new List<(Guid, bool)>
|
|
|
|
|
{
|
|
|
|
|
(user.Id, true)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var actual = UserViewModel.IsTwoFactorEnabled(user, lookup);
|
|
|
|
|
|
|
|
|
|
Assert.True(actual);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
|
|
|
|
public void IsTwoFactorEnabled_GivenUserAndIsInLookup_WhenUserDoesNotHaveTwoFactorEnabled_ThenReturnsFalse(User user)
|
|
|
|
|
{
|
|
|
|
|
var lookup = new List<(Guid, bool)>
|
|
|
|
|
{
|
|
|
|
|
(Guid.NewGuid(), true)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var actual = UserViewModel.IsTwoFactorEnabled(user, lookup);
|
|
|
|
|
|
|
|
|
|
Assert.False(actual);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
|
|
|
|
public void IsTwoFactorEnabled_GivenUserAndIsNotInLookup_WhenUserDoesNotHaveTwoFactorEnabled_ThenReturnsFalse(User user)
|
|
|
|
|
{
|
|
|
|
|
var lookup = new List<(Guid, bool)>();
|
|
|
|
|
|
|
|
|
|
var actual = UserViewModel.IsTwoFactorEnabled(user, lookup);
|
|
|
|
|
|
|
|
|
|
Assert.False(actual);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
|
|
|
|
public void MapUserViewModel_GivenUser_WhenPopulated_ThenMapsToUserViewModel(User user)
|
|
|
|
|
{
|
|
|
|
|
var actual = UserViewModel.MapViewModel(user, true);
|
|
|
|
|
|
|
|
|
|
Assert.Equal(actual.Id, user.Id);
|
|
|
|
|
Assert.Equal(actual.Email, user.Email);
|
|
|
|
|
Assert.Equal(actual.CreationDate, user.CreationDate);
|
|
|
|
|
Assert.Equal(actual.PremiumExpirationDate, user.PremiumExpirationDate);
|
|
|
|
|
Assert.Equal(actual.Premium, user.Premium);
|
|
|
|
|
Assert.Equal(actual.MaxStorageGb, user.MaxStorageGb);
|
|
|
|
|
Assert.Equal(actual.EmailVerified, user.EmailVerified);
|
|
|
|
|
Assert.True(actual.TwoFactorEnabled);
|
|
|
|
|
Assert.Equal(actual.AccountRevisionDate, user.AccountRevisionDate);
|
|
|
|
|
Assert.Equal(actual.RevisionDate, user.RevisionDate);
|
|
|
|
|
Assert.Equal(actual.LastEmailChangeDate, user.LastEmailChangeDate);
|
|
|
|
|
Assert.Equal(actual.LastKdfChangeDate, user.LastKdfChangeDate);
|
|
|
|
|
Assert.Equal(actual.LastKeyRotationDate, user.LastKeyRotationDate);
|
|
|
|
|
Assert.Equal(actual.LastPasswordChangeDate, user.LastPasswordChangeDate);
|
|
|
|
|
Assert.Equal(actual.Gateway, user.Gateway);
|
|
|
|
|
Assert.Equal(actual.GatewayCustomerId, user.GatewayCustomerId);
|
|
|
|
|
Assert.Equal(actual.GatewaySubscriptionId, user.GatewaySubscriptionId);
|
|
|
|
|
Assert.Equal(actual.LicenseKey, user.LicenseKey);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
|
|
|
|
public void MapUserViewModel_GivenUserWithTwoFactorEnabled_WhenPopulated_ThenMapsToUserViewModel(User user)
|
|
|
|
|
{
|
|
|
|
|
var lookup = new List<(Guid, bool)> { (user.Id, true) };
|
|
|
|
|
|
2024-10-30 15:45:53 +01:00
|
|
|
|
var actual = UserViewModel.MapViewModel(user, lookup, false);
|
2024-09-30 20:21:30 +02:00
|
|
|
|
|
|
|
|
|
Assert.True(actual.TwoFactorEnabled);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
|
|
|
|
public void MapUserViewModel_GivenUserWithoutTwoFactorEnabled_WhenPopulated_ThenTwoFactorIsEnabled(User user)
|
|
|
|
|
{
|
|
|
|
|
var lookup = new List<(Guid, bool)> { (user.Id, false) };
|
|
|
|
|
|
2024-10-30 15:45:53 +01:00
|
|
|
|
var actual = UserViewModel.MapViewModel(user, lookup, false);
|
2024-09-30 20:21:30 +02:00
|
|
|
|
|
|
|
|
|
Assert.False(actual.TwoFactorEnabled);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
|
|
|
|
public void MapUserViewModel_GivenUser_WhenNotInLookUpList_ThenTwoFactorIsDisabled(User user)
|
|
|
|
|
{
|
|
|
|
|
var lookup = new List<(Guid, bool)> { (Guid.NewGuid(), true) };
|
|
|
|
|
|
2024-10-30 15:45:53 +01:00
|
|
|
|
var actual = UserViewModel.MapViewModel(user, lookup, false);
|
2024-09-30 20:21:30 +02:00
|
|
|
|
|
|
|
|
|
Assert.False(actual.TwoFactorEnabled);
|
|
|
|
|
}
|
2024-10-30 15:45:53 +01:00
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
|
|
|
|
public void MapUserViewModel_WithVerifiedDomain_ReturnsUserViewModel(User user)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
var verifiedDomain = true;
|
|
|
|
|
|
|
|
|
|
var actual = UserViewModel.MapViewModel(user, true, Array.Empty<Cipher>(), verifiedDomain);
|
|
|
|
|
|
|
|
|
|
Assert.True(actual.DomainVerified);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
|
|
|
|
public void MapUserViewModel_WithoutVerifiedDomain_ReturnsUserViewModel(User user)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
var verifiedDomain = false;
|
|
|
|
|
|
|
|
|
|
var actual = UserViewModel.MapViewModel(user, true, Array.Empty<Cipher>(), verifiedDomain);
|
|
|
|
|
|
|
|
|
|
Assert.False(actual.DomainVerified);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData]
|
|
|
|
|
public void MapUserViewModel_WithNullVerifiedDomain_ReturnsUserViewModel(User user)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
var actual = UserViewModel.MapViewModel(user, true, Array.Empty<Cipher>(), null);
|
|
|
|
|
|
|
|
|
|
Assert.Null(actual.DomainVerified);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-09-30 20:21:30 +02:00
|
|
|
|
}
|