mirror of
https://github.com/bitwarden/server.git
synced 2025-04-05 13:08:17 -05:00

* Add check to verify the vault state for rotation is not obviously desynced (empty) * Add unit test for key rotation guardrail * Move de-synced vault detection to validators * Add tests
54 lines
1.8 KiB
C#
54 lines
1.8 KiB
C#
using Bit.Core.Auth.Models.Data;
|
|
using Bit.Core.Auth.UserFeatures.UserKey.Implementations;
|
|
using Bit.Core.Entities;
|
|
using Bit.Core.Services;
|
|
using Bit.Test.Common.AutoFixture;
|
|
using Bit.Test.Common.AutoFixture.Attributes;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using NSubstitute;
|
|
using Xunit;
|
|
|
|
namespace Bit.Core.Test.Auth.UserFeatures.UserKey;
|
|
|
|
[SutProviderCustomize]
|
|
public class RotateUserKeyCommandTests
|
|
{
|
|
[Theory, BitAutoData]
|
|
public async Task RotateUserKeyAsync_Success(SutProvider<RotateUserKeyCommand> sutProvider, User user,
|
|
RotateUserKeyData model)
|
|
{
|
|
sutProvider.GetDependency<IUserService>().CheckPasswordAsync(user, model.MasterPasswordHash)
|
|
.Returns(true);
|
|
|
|
var result = await sutProvider.Sut.RotateUserKeyAsync(user, model);
|
|
|
|
Assert.Equal(IdentityResult.Success, result);
|
|
}
|
|
|
|
[Theory, BitAutoData]
|
|
public async Task RotateUserKeyAsync_InvalidMasterPasswordHash_ReturnsFailedIdentityResult(
|
|
SutProvider<RotateUserKeyCommand> sutProvider, User user, RotateUserKeyData model)
|
|
{
|
|
sutProvider.GetDependency<IUserService>().CheckPasswordAsync(user, model.MasterPasswordHash)
|
|
.Returns(false);
|
|
|
|
var result = await sutProvider.Sut.RotateUserKeyAsync(user, model);
|
|
|
|
Assert.False(result.Succeeded);
|
|
}
|
|
|
|
[Theory, BitAutoData]
|
|
public async Task RotateUserKeyAsync_LogsOutUser(
|
|
SutProvider<RotateUserKeyCommand> sutProvider, User user, RotateUserKeyData model)
|
|
{
|
|
sutProvider.GetDependency<IUserService>().CheckPasswordAsync(user, model.MasterPasswordHash)
|
|
.Returns(true);
|
|
|
|
await sutProvider.Sut.RotateUserKeyAsync(user, model);
|
|
|
|
await sutProvider.GetDependency<IPushNotificationService>().ReceivedWithAnyArgs()
|
|
.PushLogOutAsync(default, default);
|
|
}
|
|
|
|
}
|