Skip to content

Commit fe3a8c2

Browse files
authored
[PM-31820] added a null check to the id/partial route (#7066)
1 parent e0a0871 commit fe3a8c2

2 files changed

Lines changed: 26 additions & 2 deletions

File tree

src/Api/Vault/Controllers/CiphersController.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -709,12 +709,18 @@ private async Task<bool> CanEditItemsInCollections(Guid organizationId, IEnumera
709709
public async Task<CipherResponseModel> PutPartial(Guid id, [FromBody] CipherPartialRequestModel model)
710710
{
711711
var user = await _userService.GetUserByPrincipalAsync(User);
712+
var cipher = await GetByIdAsync(id, user.Id);
713+
if (cipher == null)
714+
{
715+
throw new NotFoundException();
716+
}
717+
712718
var folderId = string.IsNullOrWhiteSpace(model.FolderId) ? null : (Guid?)new Guid(model.FolderId);
713719
await _cipherRepository.UpdatePartialAsync(id, user.Id, folderId, model.Favorite);
714720

715-
var cipher = await GetByIdAsync(id, user.Id);
721+
var updatedCipher = await GetByIdAsync(id, user.Id);
716722
var response = new CipherResponseModel(
717-
cipher,
723+
updatedCipher,
718724
user,
719725
await _applicationCacheService.GetOrganizationAbilitiesAsync(),
720726
_globalSettings);

test/Api.Test/Vault/Controllers/CiphersControllerTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,24 @@ public async Task PutPartialShouldReturnCipherWithGivenFolderAndFavoriteValues(U
6060
Assert.Equal(isFavorite, result.Favorite);
6161
}
6262

63+
[Theory, BitAutoData]
64+
public async Task PutPartialShouldThrowNotFoundExceptionWhenCipherDoesNotExist(User user, Guid folderId, SutProvider<CiphersController> sutProvider)
65+
{
66+
var isFavorite = true;
67+
var cipherId = Guid.NewGuid();
68+
69+
sutProvider.GetDependency<IUserService>().GetUserByPrincipalAsync(default).ReturnsForAnyArgs(user);
70+
sutProvider.GetDependency<ICipherRepository>().GetByIdAsync(cipherId, user.Id).ReturnsNull();
71+
72+
var requestAction = async () => await sutProvider.Sut.PutPartial(cipherId, new CipherPartialRequestModel { Favorite = isFavorite, FolderId = folderId.ToString() });
73+
74+
await Assert.ThrowsAsync<NotFoundException>(requestAction);
75+
76+
await sutProvider.GetDependency<ICipherRepository>()
77+
.DidNotReceive()
78+
.UpdatePartialAsync(Arg.Any<Guid>(), Arg.Any<Guid>(), Arg.Any<Guid?>(), Arg.Any<bool>());
79+
}
80+
6381
[Theory, BitAutoData]
6482
public async Task PutCollections_vNextShouldThrowExceptionWhenCipherIsNullOrNoOrgValue(Guid id, CipherCollectionsRequestModel model, User user,
6583
SutProvider<CiphersController> sutProvider)

0 commit comments

Comments
 (0)