mirror of
https://github.com/bitwarden/server.git
synced 2025-06-30 07:36:14 -05:00
[PM-19883] Add untrust devices endpoint (#5619)
* Add untrust devices endpoint * Fix tests * Update src/Core/Auth/UserFeatures/DeviceTrust/UntrustDevicesCommand.cs Co-authored-by: Jared Snider <116684653+JaredSnider-Bitwarden@users.noreply.github.com> * Fix whitespace --------- Co-authored-by: Jared Snider <116684653+JaredSnider-Bitwarden@users.noreply.github.com>
This commit is contained in:
@ -2,6 +2,7 @@
|
||||
using Bit.Api.Models.Response;
|
||||
using Bit.Core.Auth.Models.Api.Response;
|
||||
using Bit.Core.Auth.Models.Data;
|
||||
using Bit.Core.Auth.UserFeatures.DeviceTrust;
|
||||
using Bit.Core.Context;
|
||||
using Bit.Core.Entities;
|
||||
using Bit.Core.Enums;
|
||||
@ -19,6 +20,7 @@ public class DevicesControllerTest
|
||||
private readonly IDeviceRepository _deviceRepositoryMock;
|
||||
private readonly IDeviceService _deviceServiceMock;
|
||||
private readonly IUserService _userServiceMock;
|
||||
private readonly IUntrustDevicesCommand _untrustDevicesCommand;
|
||||
private readonly IUserRepository _userRepositoryMock;
|
||||
private readonly ICurrentContext _currentContextMock;
|
||||
private readonly IGlobalSettings _globalSettingsMock;
|
||||
@ -30,6 +32,7 @@ public class DevicesControllerTest
|
||||
_deviceRepositoryMock = Substitute.For<IDeviceRepository>();
|
||||
_deviceServiceMock = Substitute.For<IDeviceService>();
|
||||
_userServiceMock = Substitute.For<IUserService>();
|
||||
_untrustDevicesCommand = Substitute.For<IUntrustDevicesCommand>();
|
||||
_userRepositoryMock = Substitute.For<IUserRepository>();
|
||||
_currentContextMock = Substitute.For<ICurrentContext>();
|
||||
_loggerMock = Substitute.For<ILogger<DevicesController>>();
|
||||
@ -38,6 +41,7 @@ public class DevicesControllerTest
|
||||
_deviceRepositoryMock,
|
||||
_deviceServiceMock,
|
||||
_userServiceMock,
|
||||
_untrustDevicesCommand,
|
||||
_userRepositoryMock,
|
||||
_currentContextMock,
|
||||
_loggerMock);
|
||||
|
@ -0,0 +1,55 @@
|
||||
using Bit.Core.Auth.UserFeatures.DeviceTrust;
|
||||
using Bit.Core.Entities;
|
||||
using Bit.Core.Repositories;
|
||||
using Bit.Test.Common.AutoFixture;
|
||||
using Bit.Test.Common.AutoFixture.Attributes;
|
||||
using NSubstitute;
|
||||
using Xunit;
|
||||
|
||||
namespace Bit.Core.Test.Auth.UserFeatures.WebAuthnLogin;
|
||||
|
||||
[SutProviderCustomize]
|
||||
public class UntrustDevicesCommandTests
|
||||
{
|
||||
[Theory, BitAutoData]
|
||||
public async Task SetsKeysToNull(SutProvider<UntrustDevicesCommand> sutProvider, User user)
|
||||
{
|
||||
var deviceId = Guid.NewGuid();
|
||||
// Arrange
|
||||
sutProvider.GetDependency<IDeviceRepository>()
|
||||
.GetManyByUserIdAsync(user.Id)
|
||||
.Returns([new Device
|
||||
{
|
||||
Id = deviceId,
|
||||
EncryptedPrivateKey = "encryptedPrivateKey",
|
||||
EncryptedPublicKey = "encryptedPublicKey",
|
||||
EncryptedUserKey = "encryptedUserKey"
|
||||
}]);
|
||||
|
||||
// Act
|
||||
await sutProvider.Sut.UntrustDevices(user, new List<Guid> { deviceId });
|
||||
|
||||
// Assert
|
||||
await sutProvider.GetDependency<IDeviceRepository>()
|
||||
.Received()
|
||||
.UpsertAsync(Arg.Is<Device>(d =>
|
||||
d.Id == deviceId &&
|
||||
d.EncryptedPrivateKey == null &&
|
||||
d.EncryptedPublicKey == null &&
|
||||
d.EncryptedUserKey == null));
|
||||
}
|
||||
|
||||
[Theory, BitAutoData]
|
||||
public async Task RejectsWrongUser(SutProvider<UntrustDevicesCommand> sutProvider, User user)
|
||||
{
|
||||
var deviceId = Guid.NewGuid();
|
||||
// Arrange
|
||||
sutProvider.GetDependency<IDeviceRepository>()
|
||||
.GetManyByUserIdAsync(user.Id)
|
||||
.Returns([]);
|
||||
|
||||
// Act
|
||||
await Assert.ThrowsAsync<UnauthorizedAccessException>(async () =>
|
||||
await sutProvider.Sut.UntrustDevices(user, new List<Guid> { deviceId }));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user