2022-06-30 01:46:41 +02:00
|
|
|
|
using Bit.Core.Entities;
|
2022-01-21 15:36:25 +01:00
|
|
|
|
using Bit.Core.Enums;
|
|
|
|
|
using Bit.Core.Models;
|
|
|
|
|
using Bit.Core.Services;
|
|
|
|
|
using Bit.Core.Utilities;
|
|
|
|
|
using Bit.Test.Common.AutoFixture;
|
|
|
|
|
using Bit.Test.Common.AutoFixture.Attributes;
|
|
|
|
|
using Microsoft.AspNetCore.Identity;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
|
using NSubstitute;
|
|
|
|
|
using Xunit;
|
|
|
|
|
|
2022-08-29 21:53:48 +02:00
|
|
|
|
namespace Bit.Core.Test.Identity
|
2022-01-21 15:36:25 +01:00
|
|
|
|
{
|
2022-08-29 21:53:48 +02:00
|
|
|
|
[SutProviderCustomize]
|
|
|
|
|
public abstract class BaseTokenProviderTests<T>
|
|
|
|
|
where T : IUserTwoFactorTokenProvider<User>
|
2022-08-29 20:53:16 +02:00
|
|
|
|
{
|
2022-08-29 21:53:48 +02:00
|
|
|
|
public abstract TwoFactorProviderType TwoFactorProviderType { get; }
|
2022-01-21 15:36:25 +01:00
|
|
|
|
|
2022-08-29 21:53:48 +02:00
|
|
|
|
#region Helpers
|
|
|
|
|
protected static IEnumerable<object[]> SetupCanGenerateData(params (Dictionary<string, object> MetaData, bool ExpectedResponse)[] data)
|
|
|
|
|
{
|
|
|
|
|
return data.Select(d =>
|
|
|
|
|
new object[]
|
|
|
|
|
{
|
|
|
|
|
d.MetaData,
|
|
|
|
|
d.ExpectedResponse,
|
|
|
|
|
});
|
|
|
|
|
}
|
2022-01-21 15:36:25 +01:00
|
|
|
|
|
2022-08-29 21:53:48 +02:00
|
|
|
|
protected virtual IUserService AdditionalSetup(SutProvider<T> sutProvider, User user)
|
|
|
|
|
{
|
|
|
|
|
var userService = Substitute.For<IUserService>();
|
2022-01-21 15:36:25 +01:00
|
|
|
|
|
2022-08-29 21:53:48 +02:00
|
|
|
|
sutProvider.GetDependency<IServiceProvider>()
|
|
|
|
|
.GetService(typeof(IUserService))
|
|
|
|
|
.Returns(userService);
|
2022-01-21 15:36:25 +01:00
|
|
|
|
|
2022-08-29 21:53:48 +02:00
|
|
|
|
SetupUserService(userService, user);
|
2022-01-21 15:36:25 +01:00
|
|
|
|
|
2022-08-29 21:53:48 +02:00
|
|
|
|
return userService;
|
|
|
|
|
}
|
2022-01-21 15:36:25 +01:00
|
|
|
|
|
2022-08-29 21:53:48 +02:00
|
|
|
|
protected virtual void SetupUserService(IUserService userService, User user)
|
|
|
|
|
{
|
|
|
|
|
userService
|
|
|
|
|
.TwoFactorProviderIsEnabledAsync(TwoFactorProviderType, user)
|
|
|
|
|
.Returns(true);
|
|
|
|
|
}
|
2022-01-21 15:36:25 +01:00
|
|
|
|
|
2022-08-29 21:53:48 +02:00
|
|
|
|
protected static UserManager<User> SubstituteUserManager()
|
2022-01-21 15:36:25 +01:00
|
|
|
|
{
|
2022-08-29 21:53:48 +02:00
|
|
|
|
return new UserManager<User>(Substitute.For<IUserStore<User>>(),
|
|
|
|
|
Substitute.For<IOptions<IdentityOptions>>(),
|
|
|
|
|
Substitute.For<IPasswordHasher<User>>(),
|
|
|
|
|
Enumerable.Empty<IUserValidator<User>>(),
|
|
|
|
|
Enumerable.Empty<IPasswordValidator<User>>(),
|
|
|
|
|
Substitute.For<ILookupNormalizer>(),
|
|
|
|
|
Substitute.For<IdentityErrorDescriber>(),
|
|
|
|
|
Substitute.For<IServiceProvider>(),
|
|
|
|
|
Substitute.For<ILogger<UserManager<User>>>());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void MockDatabase(User user, Dictionary<string, object> metaData)
|
|
|
|
|
{
|
|
|
|
|
var providers = new Dictionary<TwoFactorProviderType, TwoFactorProvider>
|
2022-01-21 15:36:25 +01:00
|
|
|
|
{
|
2022-08-29 21:53:48 +02:00
|
|
|
|
[TwoFactorProviderType] = new TwoFactorProvider
|
|
|
|
|
{
|
|
|
|
|
Enabled = true,
|
|
|
|
|
MetaData = metaData,
|
|
|
|
|
},
|
|
|
|
|
};
|
2022-01-21 15:36:25 +01:00
|
|
|
|
|
2022-08-29 21:53:48 +02:00
|
|
|
|
user.TwoFactorProviders = JsonHelpers.LegacySerialize(providers);
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
2022-01-21 15:36:25 +01:00
|
|
|
|
|
2022-08-29 21:53:48 +02:00
|
|
|
|
public virtual async Task RunCanGenerateTwoFactorTokenAsync(Dictionary<string, object> metaData, bool expectedResponse,
|
|
|
|
|
User user, SutProvider<T> sutProvider)
|
|
|
|
|
{
|
|
|
|
|
var userManager = SubstituteUserManager();
|
|
|
|
|
MockDatabase(user, metaData);
|
2022-01-21 15:36:25 +01:00
|
|
|
|
|
2022-08-29 21:53:48 +02:00
|
|
|
|
AdditionalSetup(sutProvider, user);
|
2022-01-21 15:36:25 +01:00
|
|
|
|
|
2022-08-29 21:53:48 +02:00
|
|
|
|
var response = await sutProvider.Sut.CanGenerateTwoFactorTokenAsync(userManager, user);
|
|
|
|
|
Assert.Equal(expectedResponse, response);
|
|
|
|
|
}
|
2022-01-21 15:36:25 +01:00
|
|
|
|
}
|
|
|
|
|
}
|