2024-06-17 20:46:57 +02:00
|
|
|
|
using Bit.Core.Auth.Entities;
|
|
|
|
|
using Bit.Core.Auth.Models.Data;
|
|
|
|
|
using Bit.Core.Auth.Repositories;
|
2023-11-09 20:56:08 +01:00
|
|
|
|
using Bit.Core.Auth.UserFeatures.UserKey.Implementations;
|
2023-12-05 18:05:51 +01:00
|
|
|
|
using Bit.Core.Entities;
|
2023-11-09 20:56:08 +01:00
|
|
|
|
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]
|
2023-12-05 18:05:51 +01:00
|
|
|
|
public async Task RotateUserKeyAsync_Success(SutProvider<RotateUserKeyCommand> sutProvider, User user,
|
|
|
|
|
RotateUserKeyData model)
|
2023-11-09 20:56:08 +01:00
|
|
|
|
{
|
2023-12-05 18:05:51 +01:00
|
|
|
|
sutProvider.GetDependency<IUserService>().CheckPasswordAsync(user, model.MasterPasswordHash)
|
2023-11-09 20:56:08 +01:00
|
|
|
|
.Returns(true);
|
2024-06-17 20:46:57 +02:00
|
|
|
|
foreach (var webauthnCred in model.WebAuthnKeys)
|
|
|
|
|
{
|
|
|
|
|
var dbWebauthnCred = new WebAuthnCredential
|
|
|
|
|
{
|
|
|
|
|
EncryptedPublicKey = "encryptedPublicKey",
|
|
|
|
|
EncryptedUserKey = "encryptedUserKey"
|
|
|
|
|
};
|
|
|
|
|
sutProvider.GetDependency<IWebAuthnCredentialRepository>().GetByIdAsync(webauthnCred.Id, user.Id)
|
|
|
|
|
.Returns(dbWebauthnCred);
|
|
|
|
|
}
|
2023-11-09 20:56:08 +01:00
|
|
|
|
|
2023-12-05 18:05:51 +01:00
|
|
|
|
var result = await sutProvider.Sut.RotateUserKeyAsync(user, model);
|
2023-11-09 20:56:08 +01:00
|
|
|
|
|
|
|
|
|
Assert.Equal(IdentityResult.Success, result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory, BitAutoData]
|
|
|
|
|
public async Task RotateUserKeyAsync_InvalidMasterPasswordHash_ReturnsFailedIdentityResult(
|
2023-12-05 18:05:51 +01:00
|
|
|
|
SutProvider<RotateUserKeyCommand> sutProvider, User user, RotateUserKeyData model)
|
2023-11-09 20:56:08 +01:00
|
|
|
|
{
|
2023-12-05 18:05:51 +01:00
|
|
|
|
sutProvider.GetDependency<IUserService>().CheckPasswordAsync(user, model.MasterPasswordHash)
|
2023-11-09 20:56:08 +01:00
|
|
|
|
.Returns(false);
|
2024-06-17 20:46:57 +02:00
|
|
|
|
foreach (var webauthnCred in model.WebAuthnKeys)
|
|
|
|
|
{
|
|
|
|
|
var dbWebauthnCred = new WebAuthnCredential
|
|
|
|
|
{
|
|
|
|
|
EncryptedPublicKey = "encryptedPublicKey",
|
|
|
|
|
EncryptedUserKey = "encryptedUserKey"
|
|
|
|
|
};
|
|
|
|
|
sutProvider.GetDependency<IWebAuthnCredentialRepository>().GetByIdAsync(webauthnCred.Id, user.Id)
|
|
|
|
|
.Returns(dbWebauthnCred);
|
|
|
|
|
}
|
2023-11-09 20:56:08 +01:00
|
|
|
|
|
2023-12-05 18:05:51 +01:00
|
|
|
|
var result = await sutProvider.Sut.RotateUserKeyAsync(user, model);
|
2023-11-09 20:56:08 +01:00
|
|
|
|
|
|
|
|
|
Assert.False(result.Succeeded);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory, BitAutoData]
|
|
|
|
|
public async Task RotateUserKeyAsync_LogsOutUser(
|
2023-12-05 18:05:51 +01:00
|
|
|
|
SutProvider<RotateUserKeyCommand> sutProvider, User user, RotateUserKeyData model)
|
2023-11-09 20:56:08 +01:00
|
|
|
|
{
|
2023-12-05 18:05:51 +01:00
|
|
|
|
sutProvider.GetDependency<IUserService>().CheckPasswordAsync(user, model.MasterPasswordHash)
|
2023-11-09 20:56:08 +01:00
|
|
|
|
.Returns(true);
|
2024-06-17 20:46:57 +02:00
|
|
|
|
foreach (var webauthnCred in model.WebAuthnKeys)
|
|
|
|
|
{
|
|
|
|
|
var dbWebauthnCred = new WebAuthnCredential
|
|
|
|
|
{
|
|
|
|
|
EncryptedPublicKey = "encryptedPublicKey",
|
|
|
|
|
EncryptedUserKey = "encryptedUserKey"
|
|
|
|
|
};
|
|
|
|
|
sutProvider.GetDependency<IWebAuthnCredentialRepository>().GetByIdAsync(webauthnCred.Id, user.Id)
|
|
|
|
|
.Returns(dbWebauthnCred);
|
|
|
|
|
}
|
2023-11-09 20:56:08 +01:00
|
|
|
|
|
2023-12-05 18:05:51 +01:00
|
|
|
|
await sutProvider.Sut.RotateUserKeyAsync(user, model);
|
2023-11-09 20:56:08 +01:00
|
|
|
|
|
|
|
|
|
await sutProvider.GetDependency<IPushNotificationService>().ReceivedWithAnyArgs()
|
|
|
|
|
.PushLogOutAsync(default, default);
|
|
|
|
|
}
|
2024-05-30 11:08:26 +02:00
|
|
|
|
|
2023-11-09 20:56:08 +01:00
|
|
|
|
}
|