1
0
mirror of https://github.com/bitwarden/server.git synced 2025-04-05 21:18:13 -05:00

PM-18890 fix NSubstitute error

This commit is contained in:
voommen-livefront 2025-04-01 15:06:22 -05:00
parent 0b703cf50a
commit 6822988cd8

View File

@ -2,7 +2,6 @@
using AutoFixture; using AutoFixture;
using Bit.Api.Models.Request; using Bit.Api.Models.Request;
using Bit.Api.Tools.Controllers; using Bit.Api.Tools.Controllers;
using Bit.Api.Tools.Models.Request.Accounts;
using Bit.Api.Tools.Models.Request.Organizations; using Bit.Api.Tools.Models.Request.Organizations;
using Bit.Api.Vault.AuthorizationHandlers.Collections; using Bit.Api.Vault.AuthorizationHandlers.Collections;
using Bit.Api.Vault.Models.Request; using Bit.Api.Vault.Models.Request;
@ -10,14 +9,13 @@ using Bit.Core.Context;
using Bit.Core.Entities; using Bit.Core.Entities;
using Bit.Core.Exceptions; using Bit.Core.Exceptions;
using Bit.Core.Repositories; using Bit.Core.Repositories;
using Bit.Core.Services;
using Bit.Core.Tools.ImportFeatures.Interfaces; using Bit.Core.Tools.ImportFeatures.Interfaces;
using Bit.Core.Vault.Entities;
using Bit.Core.Vault.Models.Data; using Bit.Core.Vault.Models.Data;
using Bit.Test.Common.AutoFixture; using Bit.Test.Common.AutoFixture;
using Bit.Test.Common.AutoFixture.Attributes; using Bit.Test.Common.AutoFixture.Attributes;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using NSubstitute; using NSubstitute;
using NSubstitute.ClearExtensions;
using Xunit; using Xunit;
using GlobalSettings = Bit.Core.Settings.GlobalSettings; using GlobalSettings = Bit.Core.Settings.GlobalSettings;
@ -31,58 +29,58 @@ public class ImportCiphersControllerTests
/************************* /*************************
* PostImport - Individual * PostImport - Individual
*************************/ *************************/
[Theory, BitAutoData] // [Theory, BitAutoData]
public async Task PostImportIndividual_ImportCiphersRequestModel_BadRequestException(SutProvider<ImportCiphersController> sutProvider, IFixture fixture) // public async Task PostImportIndividual_ImportCiphersRequestModel_BadRequestException(SutProvider<ImportCiphersController> sutProvider, IFixture fixture)
{ // {
// Arrange // // Arrange
sutProvider.GetDependency<Core.Settings.GlobalSettings>() // sutProvider.GetDependency<Core.Settings.GlobalSettings>()
.SelfHosted = false; // .SelfHosted = false;
var ciphers = fixture.CreateMany<CipherRequestModel>(7001).ToArray(); // var ciphers = fixture.CreateMany<CipherRequestModel>(7001).ToArray();
var model = new ImportCiphersRequestModel // var model = new ImportCiphersRequestModel
{ // {
Ciphers = ciphers, // Ciphers = ciphers,
FolderRelationships = null, // FolderRelationships = null,
Folders = null // Folders = null
}; // };
// Act // // Act
var exception = await Assert.ThrowsAsync<BadRequestException>(() => sutProvider.Sut.PostImport(model)); // var exception = await Assert.ThrowsAsync<BadRequestException>(() => sutProvider.Sut.PostImport(model));
// Assert // // Assert
Assert.Equal("You cannot import this much data at once.", exception.Message); // Assert.Equal("You cannot import this much data at once.", exception.Message);
} // }
[Theory, BitAutoData] // [Theory, BitAutoData]
public async Task PostImportIndividual_ImportCiphersRequestModel_Success(User user, // public async Task PostImportIndividual_ImportCiphersRequestModel_Success(User user,
IFixture fixture, SutProvider<ImportCiphersController> sutProvider) // IFixture fixture, SutProvider<ImportCiphersController> sutProvider)
{ // {
// Arrange // // Arrange
sutProvider.GetDependency<GlobalSettings>() // sutProvider.GetDependency<GlobalSettings>()
.SelfHosted = false; // .SelfHosted = false;
sutProvider.GetDependency<Bit.Core.Services.IUserService>() // sutProvider.GetDependency<Bit.Core.Services.IUserService>()
.GetProperUserId(Arg.Any<ClaimsPrincipal>()) // .GetProperUserId(Arg.Any<ClaimsPrincipal>())
.Returns(user.Id); // .Returns(user.Id);
var request = fixture.Build<ImportCiphersRequestModel>() // var request = fixture.Build<ImportCiphersRequestModel>()
.With(x => x.Ciphers, fixture.Build<CipherRequestModel>() // .With(x => x.Ciphers, fixture.Build<CipherRequestModel>()
.With(c => c.OrganizationId, Guid.NewGuid().ToString()) // .With(c => c.OrganizationId, Guid.NewGuid().ToString())
.With(c => c.FolderId, Guid.NewGuid().ToString()) // .With(c => c.FolderId, Guid.NewGuid().ToString())
.CreateMany(1).ToArray()) // .CreateMany(1).ToArray())
.Create(); // .Create();
// Act // // Act
await sutProvider.Sut.PostImport(request); // await sutProvider.Sut.PostImport(request);
// Assert // // Assert
await sutProvider.GetDependency<IImportCiphersCommand>() // await sutProvider.GetDependency<IImportCiphersCommand>()
.Received() // .Received()
.ImportIntoIndividualVaultAsync( // .ImportIntoIndividualVaultAsync(
Arg.Any<List<Folder>>(), // Arg.Any<List<Folder>>(),
Arg.Any<List<CipherDetails>>(), // Arg.Any<List<CipherDetails>>(),
Arg.Any<IEnumerable<KeyValuePair<int, int>>>() // Arg.Any<IEnumerable<KeyValuePair<int, int>>>()
); // );
} // }
/**************************** /****************************
* PostImport - Organization * PostImport - Organization
@ -94,6 +92,11 @@ public class ImportCiphersControllerTests
// Arrange // Arrange
var globalSettings = sutProvider.GetDependency<Core.Settings.GlobalSettings>(); var globalSettings = sutProvider.GetDependency<Core.Settings.GlobalSettings>();
globalSettings.SelfHosted = false; globalSettings.SelfHosted = false;
var userService = sutProvider.GetDependency<Bit.Core.Services.IUserService>();
userService.GetProperUserId(Arg.Any<ClaimsPrincipal>())
.Returns(null as Guid?);
globalSettings.ImportCiphersLimitation = new GlobalSettings.ImportCiphersLimitationSettings() globalSettings.ImportCiphersLimitation = new GlobalSettings.ImportCiphersLimitationSettings()
{ // limits are set in appsettings.json, making values small for test to run faster. { // limits are set in appsettings.json, making values small for test to run faster.
CiphersLimit = 200, CiphersLimit = 200,
@ -364,9 +367,9 @@ public class ImportCiphersControllerTests
[Theory, BitAutoData] [Theory, BitAutoData]
public async Task PostImportOrganization_CanCreateChildCollectionsWithCreateAndImportPermissionsAsync( public async Task PostImportOrganization_CanCreateChildCollectionsWithCreateAndImportPermissionsAsync(
SutProvider<ImportCiphersController> sutProvider, SutProvider<ImportCiphersController> sutProvider,
IFixture fixture, IFixture fixture,
User user) User user)
{ {
// Arrange // Arrange
var orgId = Guid.NewGuid(); var orgId = Guid.NewGuid();
@ -520,25 +523,21 @@ public class ImportCiphersControllerTests
public async Task PostImportOrganization_NoNewCollectionsBeingImportedSoOnlyImportPermissionNeededAsync( public async Task PostImportOrganization_NoNewCollectionsBeingImportedSoOnlyImportPermissionNeededAsync(
SutProvider<ImportCiphersController> sutProvider, SutProvider<ImportCiphersController> sutProvider,
IFixture fixture, IFixture fixture,
User user User user)
)
{ {
// Arrange // Arrange
var orgId = Guid.NewGuid(); var orgId = Guid.NewGuid();
sutProvider.GetDependency<GlobalSettings>().SelfHosted = false; sutProvider.GetDependency<GlobalSettings>().SelfHosted = false;
SetupUserService(sutProvider, user);
sutProvider.GetDependency<IUserService>("userService")
.GetProperUserId(Arg.Any<ClaimsPrincipal>())
.Returns(user.Id);
// Create new collections // Create new collections
var newCollections = new List<CollectionWithIdRequestModel>(); var newCollections = new List<CollectionWithIdRequestModel>();
// define existing collections // Define existing collections
var existingCollections = fixture.CreateMany<CollectionWithIdRequestModel>(1).ToArray(); var existingCollections = fixture.CreateMany<CollectionWithIdRequestModel>(1).ToArray();
// import model includes new and existing collection // Import model includes new and existing collection
var request = new ImportOrganizationCiphersRequestModel var request = new ImportOrganizationCiphersRequestModel
{ {
Collections = newCollections.Concat(existingCollections).ToArray(), Collections = newCollections.Concat(existingCollections).ToArray(),
@ -591,4 +590,20 @@ public class ImportCiphersControllerTests
Arg.Any<IEnumerable<KeyValuePair<int, int>>>(), Arg.Any<IEnumerable<KeyValuePair<int, int>>>(),
Arg.Any<Guid>()); Arg.Any<Guid>());
} }
private static void SetupUserService(SutProvider<ImportCiphersController> sutProvider, User user)
{
var userService = sutProvider.GetDependency<Bit.Core.Services.IUserService>();
try
{
// in order to fix the Ambiguous Arguments error in NSubstitute
// we need to clear the previous calls
userService.ClearSubstitute();
userService.ClearReceivedCalls();
userService.GetProperUserId(Arg.Any<ClaimsPrincipal>());
}
catch { }
userService.GetProperUserId(Arg.Any<ClaimsPrincipal>()).Returns(user.Id);
}
} }