Files
server/test/Core.Test/Auth/Identity/EmailTokenProviderTests.cs
Patrick-Pimentel-Bitwarden e113dbd263 feat: [PM-32626] standardize unlock and authentication validation
- Standardize validation on `RegisterFinishRequestModel` so Auth and Unlock data are both required and consistently validated
  - Add salt validation to both unlock and authentication data
  - Enforce that Auth and Unlock data contain matching values
  - Keep validation backwards compatible with older clients
  - Add and update unit tests covering the new validation rules and error messages

Co-authored-by: Ike Kottlowski <ikottlowski@bitwarden.com>
2026-04-17 10:47:09 -04:00

70 lines
2.3 KiB
C#

using Bit.Core.Auth.Identity.TokenProviders;
using Bit.Core.Entities;
using Bit.Core.Services;
using Bit.Test.Common.AutoFixture.Attributes;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using NSubstitute;
using Xunit;
namespace Bit.Core.Test.Auth.Identity;
[SutProviderCustomize]
public class EmailTokenProviderTests
{
private readonly IDistributedCache _cache;
public EmailTokenProviderTests()
{
_cache = Substitute.For<IDistributedCache>();
}
[Theory]
[BitAutoData]
public async Task GenerateAsync_GeneratesSixDigitToken_WhenFeatureFlagIsEnabled(User user)
{
// Arrange
var purpose = "test-purpose";
var featureService = Substitute.For<IFeatureService>();
featureService.IsEnabled(FeatureFlagKeys.Otp6Digits).Returns(true);
var tokenProvider = new EmailTokenProvider(_cache, featureService);
// Act
var code = await tokenProvider.GenerateAsync(purpose, SubstituteUserManager(), user);
// Assert
Assert.Equal(6, code.Length);
}
[Theory]
[BitAutoData]
public async Task GenerateAsync_GeneratesEightDigitToken_WhenFeatureFlagIsDisabled(User user)
{
// Arrange
var purpose = "test-purpose";
var featureService = Substitute.For<IFeatureService>();
featureService.IsEnabled(FeatureFlagKeys.Otp6Digits).Returns(false);
var tokenProvider = new EmailTokenProvider(_cache, featureService);
// Act
var code = await tokenProvider.GenerateAsync(purpose, SubstituteUserManager(), user);
// Assert
Assert.Equal(8, code.Length);
}
protected static UserManager<User> SubstituteUserManager()
{
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>>>());
}
}