mirror of
https://github.com/bitwarden/server.git
synced 2025-06-20 10:58:07 -05:00
Add tests and cleanup
This commit is contained in:
parent
de3efbeaf7
commit
01e18ded94
@ -6,7 +6,7 @@ using Microsoft.AspNetCore.Authorization;
|
|||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using UserKeyResponseModel = Bit.Api.Models.Response.UserKeyResponseModel;
|
using UserKeyResponseModel = Bit.Api.Models.Response.UserKeyResponseModel;
|
||||||
|
|
||||||
namespace Bit.Api.Controllers;
|
namespace Bit.Api.KeyManagement.Controllers;
|
||||||
|
|
||||||
[Route("users")]
|
[Route("users")]
|
||||||
[Authorize("Application")]
|
[Authorize("Application")]
|
||||||
@ -15,7 +15,7 @@ public class UsersController(
|
|||||||
IUserAccountKeysQuery _userAccountKeysQuery) : Controller
|
IUserAccountKeysQuery _userAccountKeysQuery) : Controller
|
||||||
{
|
{
|
||||||
[HttpGet("{id}/public-key")]
|
[HttpGet("{id}/public-key")]
|
||||||
public async Task<UserKeyResponseModel> Get(string id)
|
public async Task<UserKeyResponseModel> GetPublicKey(string id)
|
||||||
{
|
{
|
||||||
var guidId = new Guid(id);
|
var guidId = new Guid(id);
|
||||||
var key = await _userRepository.GetPublicKeyAsync(guidId);
|
var key = await _userRepository.GetPublicKeyAsync(guidId);
|
||||||
|
@ -0,0 +1,90 @@
|
|||||||
|
#nullable enable
|
||||||
|
using Bit.Api.KeyManagement.Controllers;
|
||||||
|
using Bit.Core.Entities;
|
||||||
|
using Bit.Core.Enums;
|
||||||
|
using Bit.Core.Exceptions;
|
||||||
|
using Bit.Core.KeyManagement.Models.Data;
|
||||||
|
using Bit.Core.KeyManagement.Repositories;
|
||||||
|
using Bit.Core.Repositories;
|
||||||
|
using Bit.Test.Common.AutoFixture;
|
||||||
|
using Bit.Test.Common.AutoFixture.Attributes;
|
||||||
|
using NSubstitute;
|
||||||
|
using NSubstitute.ReturnsExtensions;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace Bit.Api.Test.KeyManagement.Controllers;
|
||||||
|
|
||||||
|
[ControllerCustomize(typeof(UsersController))]
|
||||||
|
[SutProviderCustomize]
|
||||||
|
[JsonDocumentCustomize]
|
||||||
|
public class UsersControllerTests
|
||||||
|
{
|
||||||
|
[Theory]
|
||||||
|
[BitAutoData]
|
||||||
|
public async Task GetPublicKey_NotFound_ThrowsNotFoundException(
|
||||||
|
SutProvider<UsersController> sutProvider)
|
||||||
|
{
|
||||||
|
sutProvider.GetDependency<IUserRepository>().GetPublicKeyAsync(Arg.Any<Guid>()).ReturnsNull();
|
||||||
|
await Assert.ThrowsAsync<NotFoundException>(() => sutProvider.Sut.GetPublicKey("invalid-id"));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[BitAutoData]
|
||||||
|
public async Task GetAccountKeys_UserNotFound_ThrowsNotFoundException(
|
||||||
|
SutProvider<UsersController> sutProvider)
|
||||||
|
{
|
||||||
|
sutProvider.GetDependency<IUserRepository>().GetByIdAsync(Arg.Any<Guid>()).ReturnsNull();
|
||||||
|
await Assert.ThrowsAsync<NotFoundException>(() => sutProvider.Sut.GetAccountKeys(new Guid().ToString()));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[BitAutoData]
|
||||||
|
public async Task GetAccountKeys_ReturnsPublicUserKeysResponseModel(
|
||||||
|
SutProvider<UsersController> sutProvider,
|
||||||
|
Guid userId)
|
||||||
|
{
|
||||||
|
var user = new User
|
||||||
|
{
|
||||||
|
Id = userId,
|
||||||
|
PublicKey = "publicKey",
|
||||||
|
SignedPublicKey = "signedPublicKey",
|
||||||
|
};
|
||||||
|
|
||||||
|
sutProvider.GetDependency<IUserRepository>().GetByIdAsync(userId).Returns(user);
|
||||||
|
sutProvider.GetDependency<IUserSignatureKeyPairRepository>().GetByUserIdAsync(userId).Returns(new SignatureKeyPairData
|
||||||
|
{
|
||||||
|
WrappedSigningKey = "signingKey",
|
||||||
|
VerifyingKey = "verifyingKey",
|
||||||
|
SignatureAlgorithm = SignatureAlgorithm.Ed25519
|
||||||
|
});
|
||||||
|
|
||||||
|
var result = await sutProvider.Sut.GetAccountKeys(userId.ToString());
|
||||||
|
Assert.NotNull(result);
|
||||||
|
Assert.Equal("publicKey", result.PublicKey);
|
||||||
|
Assert.Equal("signedPublicKey", result.SignedPublicKey);
|
||||||
|
Assert.Equal("verifyingKey", result.VerifyingKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[BitAutoData]
|
||||||
|
public async Task GetAccountKeys_ReturnsPublicUserKeysResponseModel_WithNullVerifyingKey(
|
||||||
|
SutProvider<UsersController> sutProvider,
|
||||||
|
Guid userId)
|
||||||
|
{
|
||||||
|
var user = new User
|
||||||
|
{
|
||||||
|
Id = userId,
|
||||||
|
PublicKey = "publicKey",
|
||||||
|
SignedPublicKey = null,
|
||||||
|
};
|
||||||
|
|
||||||
|
sutProvider.GetDependency<IUserRepository>().GetByIdAsync(userId).Returns(user);
|
||||||
|
sutProvider.GetDependency<IUserSignatureKeyPairRepository>().GetByUserIdAsync(userId).ReturnsNull();
|
||||||
|
|
||||||
|
var result = await sutProvider.Sut.GetAccountKeys(userId.ToString());
|
||||||
|
Assert.NotNull(result);
|
||||||
|
Assert.Equal("publicKey", result.PublicKey);
|
||||||
|
Assert.Null(result.SignedPublicKey);
|
||||||
|
Assert.Null(result.VerifyingKey);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user