diff --git a/.github/renovate.json b/.github/renovate.json index c21374d73..d45876656 100644 --- a/.github/renovate.json +++ b/.github/renovate.json @@ -43,10 +43,6 @@ "groupName": "nuget minor", "matchManagers": ["nuget"], "matchUpdateTypes": ["minor", "patch"] - }, - { - "matchPackageNames": ["Moq"], - "allowedVersions": "<=4.18.4" } ] } diff --git a/bitwarden_license/test/Commercial.Core.Test/packages.lock.json b/bitwarden_license/test/Commercial.Core.Test/packages.lock.json index 088250479..09019299e 100644 --- a/bitwarden_license/test/Commercial.Core.Test/packages.lock.json +++ b/bitwarden_license/test/Commercial.Core.Test/packages.lock.json @@ -940,15 +940,6 @@ "System.Security.Cryptography.Pkcs": "6.0.0" } }, - "Moq": { - "type": "Transitive", - "resolved": "4.17.2", - "contentHash": "HytUPJ3/uks2UgJ9hIcyXm3YxpFAR4OJzbQwTHltbKGun3lFLhEHs97hiiPj1dY8jV/kasXeihTzDxct6Zf3iQ==", - "dependencies": { - "Castle.Core": "4.4.1", - "System.Threading.Tasks.Extensions": "4.5.4" - } - }, "NETStandard.Library": { "type": "Transitive", "resolved": "1.6.1", @@ -2675,75 +2666,74 @@ "commercial.core": { "type": "Project", "dependencies": { - "Core": "2023.7.2" + "Core": "[2023.7.2, )" } }, "common": { "type": "Project", "dependencies": { - "AutoFixture.AutoNSubstitute": "4.17.0", - "AutoFixture.Xunit2": "4.17.0", - "Core": "2023.7.2", - "Kralizek.AutoFixture.Extensions.MockHttp": "1.2.0", - "Microsoft.NET.Test.Sdk": "17.1.0", - "NSubstitute": "4.3.0", - "xunit": "2.4.1" + "AutoFixture.AutoNSubstitute": "[4.17.0, )", + "AutoFixture.Xunit2": "[4.17.0, )", + "Core": "[2023.7.2, )", + "Kralizek.AutoFixture.Extensions.MockHttp": "[1.2.0, )", + "Microsoft.NET.Test.Sdk": "[17.1.0, )", + "NSubstitute": "[4.3.0, )", + "xunit": "[2.4.1, )" } }, "core": { "type": "Project", "dependencies": { - "AWSSDK.SQS": "3.7.2.47", - "AWSSDK.SimpleEmail": "3.7.0.150", - "AspNetCoreRateLimit": "4.0.2", - "AspNetCoreRateLimit.Redis": "1.0.1", - "Azure.Extensions.AspNetCore.DataProtection.Blobs": "1.3.2", - "Azure.Messaging.ServiceBus": "7.15.0", - "Azure.Storage.Blobs": "12.14.1", - "Azure.Storage.Queues": "12.12.0", - "BitPay.Light": "1.0.1907", - "Braintree": "5.12.0", - "DnsClient": "1.7.0", - "Fido2.AspNet": "3.0.1", - "Handlebars.Net": "2.1.2", - "IdentityServer4": "4.1.2", - "IdentityServer4.AccessTokenValidation": "3.0.1", - "LaunchDarkly.ServerSdk": "7.0.0", - "MailKit": "3.2.0", - "Microsoft.AspNetCore.Authentication.JwtBearer": "6.0.4", - "Microsoft.Azure.Cosmos.Table": "1.0.8", - "Microsoft.Azure.NotificationHubs": "4.1.0", - "Microsoft.Data.SqlClient": "5.0.1", - "Microsoft.Extensions.Caching.StackExchangeRedis": "6.0.6", - "Microsoft.Extensions.Configuration.EnvironmentVariables": "6.0.1", - "Microsoft.Extensions.Configuration.UserSecrets": "6.0.1", - "Microsoft.Extensions.Identity.Stores": "6.0.4", - "Newtonsoft.Json": "13.0.1", - "Otp.NET": "1.2.2", - "Quartz": "3.4.0", - "SendGrid": "9.27.0", - "Sentry.Serilog": "3.16.0", - "Serilog.AspNetCore": "5.0.0", - "Serilog.Extensions.Logging": "3.1.0", - "Serilog.Extensions.Logging.File": "2.0.0", - "Serilog.Sinks.AzureCosmosDB": "2.0.0", - "Serilog.Sinks.SyslogMessages": "2.0.6", - "Stripe.net": "40.0.0", - "YubicoDotNetClient": "1.2.0" + "AWSSDK.SQS": "[3.7.2.47, )", + "AWSSDK.SimpleEmail": "[3.7.0.150, )", + "AspNetCoreRateLimit": "[4.0.2, )", + "AspNetCoreRateLimit.Redis": "[1.0.1, )", + "Azure.Extensions.AspNetCore.DataProtection.Blobs": "[1.3.2, )", + "Azure.Messaging.ServiceBus": "[7.15.0, )", + "Azure.Storage.Blobs": "[12.14.1, )", + "Azure.Storage.Queues": "[12.12.0, )", + "BitPay.Light": "[1.0.1907, )", + "Braintree": "[5.12.0, )", + "DnsClient": "[1.7.0, )", + "Fido2.AspNet": "[3.0.1, )", + "Handlebars.Net": "[2.1.2, )", + "IdentityServer4": "[4.1.2, )", + "IdentityServer4.AccessTokenValidation": "[3.0.1, )", + "LaunchDarkly.ServerSdk": "[7.0.0, )", + "MailKit": "[3.2.0, )", + "Microsoft.AspNetCore.Authentication.JwtBearer": "[6.0.4, )", + "Microsoft.Azure.Cosmos.Table": "[1.0.8, )", + "Microsoft.Azure.NotificationHubs": "[4.1.0, )", + "Microsoft.Data.SqlClient": "[5.0.1, )", + "Microsoft.Extensions.Caching.StackExchangeRedis": "[6.0.6, )", + "Microsoft.Extensions.Configuration.EnvironmentVariables": "[6.0.1, )", + "Microsoft.Extensions.Configuration.UserSecrets": "[6.0.1, )", + "Microsoft.Extensions.Identity.Stores": "[6.0.4, )", + "Newtonsoft.Json": "[13.0.1, )", + "Otp.NET": "[1.2.2, )", + "Quartz": "[3.4.0, )", + "SendGrid": "[9.27.0, )", + "Sentry.Serilog": "[3.16.0, )", + "Serilog.AspNetCore": "[5.0.0, )", + "Serilog.Extensions.Logging": "[3.1.0, )", + "Serilog.Extensions.Logging.File": "[2.0.0, )", + "Serilog.Sinks.AzureCosmosDB": "[2.0.0, )", + "Serilog.Sinks.SyslogMessages": "[2.0.6, )", + "Stripe.net": "[40.0.0, )", + "YubicoDotNetClient": "[1.2.0, )" } }, "core.test": { "type": "Project", "dependencies": { - "AutoFixture.AutoNSubstitute": "4.17.0", - "AutoFixture.Xunit2": "4.17.0", - "Common": "2023.7.2", - "Core": "2023.7.2", - "Kralizek.AutoFixture.Extensions.MockHttp": "1.2.0", - "Microsoft.NET.Test.Sdk": "17.1.0", - "Moq": "4.17.2", - "NSubstitute": "4.3.0", - "xunit": "2.4.1" + "AutoFixture.AutoNSubstitute": "[4.17.0, )", + "AutoFixture.Xunit2": "[4.17.0, )", + "Common": "[2023.7.2, )", + "Core": "[2023.7.2, )", + "Kralizek.AutoFixture.Extensions.MockHttp": "[1.2.0, )", + "Microsoft.NET.Test.Sdk": "[17.1.0, )", + "NSubstitute": "[4.3.0, )", + "xunit": "[2.4.1, )" } } } diff --git a/test/Api.Test/packages.lock.json b/test/Api.Test/packages.lock.json index dec56bd45..7f490ffb1 100644 --- a/test/Api.Test/packages.lock.json +++ b/test/Api.Test/packages.lock.json @@ -1181,15 +1181,6 @@ "System.Security.Cryptography.Pkcs": "6.0.0" } }, - "Moq": { - "type": "Transitive", - "resolved": "4.17.2", - "contentHash": "HytUPJ3/uks2UgJ9hIcyXm3YxpFAR4OJzbQwTHltbKGun3lFLhEHs97hiiPj1dY8jV/kasXeihTzDxct6Zf3iQ==", - "dependencies": { - "Castle.Core": "4.4.1", - "System.Threading.Tasks.Extensions": "4.5.4" - } - }, "MySqlConnector": { "type": "Transitive", "resolved": "2.2.5", @@ -3015,129 +3006,128 @@ "api": { "type": "Project", "dependencies": { - "AspNetCore.HealthChecks.AzureServiceBus": "6.1.0", - "AspNetCore.HealthChecks.AzureStorage": "6.1.2", - "AspNetCore.HealthChecks.Network": "6.0.4", - "AspNetCore.HealthChecks.Redis": "6.0.4", - "AspNetCore.HealthChecks.SendGrid": "6.0.2", - "AspNetCore.HealthChecks.SqlServer": "6.0.2", - "AspNetCore.HealthChecks.Uris": "6.0.3", - "Azure.Messaging.EventGrid": "4.10.0", - "Commercial.Core": "2023.7.2", - "Commercial.Infrastructure.EntityFramework": "2023.7.2", - "Core": "2023.7.2", - "SharedWeb": "2023.7.2", - "Swashbuckle.AspNetCore": "6.5.0" + "AspNetCore.HealthChecks.AzureServiceBus": "[6.1.0, )", + "AspNetCore.HealthChecks.AzureStorage": "[6.1.2, )", + "AspNetCore.HealthChecks.Network": "[6.0.4, )", + "AspNetCore.HealthChecks.Redis": "[6.0.4, )", + "AspNetCore.HealthChecks.SendGrid": "[6.0.2, )", + "AspNetCore.HealthChecks.SqlServer": "[6.0.2, )", + "AspNetCore.HealthChecks.Uris": "[6.0.3, )", + "Azure.Messaging.EventGrid": "[4.10.0, )", + "Commercial.Core": "[2023.7.2, )", + "Commercial.Infrastructure.EntityFramework": "[2023.7.2, )", + "Core": "[2023.7.2, )", + "SharedWeb": "[2023.7.2, )", + "Swashbuckle.AspNetCore": "[6.5.0, )" } }, "commercial.core": { "type": "Project", "dependencies": { - "Core": "2023.7.2" + "Core": "[2023.7.2, )" } }, "commercial.infrastructure.entityframework": { "type": "Project", "dependencies": { - "AutoMapper.Extensions.Microsoft.DependencyInjection": "12.0.1", - "Core": "2023.7.2", - "Infrastructure.EntityFramework": "2023.7.2" + "AutoMapper.Extensions.Microsoft.DependencyInjection": "[12.0.1, )", + "Core": "[2023.7.2, )", + "Infrastructure.EntityFramework": "[2023.7.2, )" } }, "common": { "type": "Project", "dependencies": { - "AutoFixture.AutoNSubstitute": "4.17.0", - "AutoFixture.Xunit2": "4.17.0", - "Core": "2023.7.2", - "Kralizek.AutoFixture.Extensions.MockHttp": "1.2.0", - "Microsoft.NET.Test.Sdk": "17.1.0", - "NSubstitute": "4.3.0", - "xunit": "2.4.1" + "AutoFixture.AutoNSubstitute": "[4.17.0, )", + "AutoFixture.Xunit2": "[4.17.0, )", + "Core": "[2023.7.2, )", + "Kralizek.AutoFixture.Extensions.MockHttp": "[1.2.0, )", + "Microsoft.NET.Test.Sdk": "[17.1.0, )", + "NSubstitute": "[4.3.0, )", + "xunit": "[2.4.1, )" } }, "core": { "type": "Project", "dependencies": { - "AWSSDK.SQS": "3.7.2.47", - "AWSSDK.SimpleEmail": "3.7.0.150", - "AspNetCoreRateLimit": "4.0.2", - "AspNetCoreRateLimit.Redis": "1.0.1", - "Azure.Extensions.AspNetCore.DataProtection.Blobs": "1.3.2", - "Azure.Messaging.ServiceBus": "7.15.0", - "Azure.Storage.Blobs": "12.14.1", - "Azure.Storage.Queues": "12.12.0", - "BitPay.Light": "1.0.1907", - "Braintree": "5.12.0", - "DnsClient": "1.7.0", - "Fido2.AspNet": "3.0.1", - "Handlebars.Net": "2.1.2", - "IdentityServer4": "4.1.2", - "IdentityServer4.AccessTokenValidation": "3.0.1", - "LaunchDarkly.ServerSdk": "7.0.0", - "MailKit": "3.2.0", - "Microsoft.AspNetCore.Authentication.JwtBearer": "6.0.4", - "Microsoft.Azure.Cosmos.Table": "1.0.8", - "Microsoft.Azure.NotificationHubs": "4.1.0", - "Microsoft.Data.SqlClient": "5.0.1", - "Microsoft.Extensions.Caching.StackExchangeRedis": "6.0.6", - "Microsoft.Extensions.Configuration.EnvironmentVariables": "6.0.1", - "Microsoft.Extensions.Configuration.UserSecrets": "6.0.1", - "Microsoft.Extensions.Identity.Stores": "6.0.4", - "Newtonsoft.Json": "13.0.1", - "Otp.NET": "1.2.2", - "Quartz": "3.4.0", - "SendGrid": "9.27.0", - "Sentry.Serilog": "3.16.0", - "Serilog.AspNetCore": "5.0.0", - "Serilog.Extensions.Logging": "3.1.0", - "Serilog.Extensions.Logging.File": "2.0.0", - "Serilog.Sinks.AzureCosmosDB": "2.0.0", - "Serilog.Sinks.SyslogMessages": "2.0.6", - "Stripe.net": "40.0.0", - "YubicoDotNetClient": "1.2.0" + "AWSSDK.SQS": "[3.7.2.47, )", + "AWSSDK.SimpleEmail": "[3.7.0.150, )", + "AspNetCoreRateLimit": "[4.0.2, )", + "AspNetCoreRateLimit.Redis": "[1.0.1, )", + "Azure.Extensions.AspNetCore.DataProtection.Blobs": "[1.3.2, )", + "Azure.Messaging.ServiceBus": "[7.15.0, )", + "Azure.Storage.Blobs": "[12.14.1, )", + "Azure.Storage.Queues": "[12.12.0, )", + "BitPay.Light": "[1.0.1907, )", + "Braintree": "[5.12.0, )", + "DnsClient": "[1.7.0, )", + "Fido2.AspNet": "[3.0.1, )", + "Handlebars.Net": "[2.1.2, )", + "IdentityServer4": "[4.1.2, )", + "IdentityServer4.AccessTokenValidation": "[3.0.1, )", + "LaunchDarkly.ServerSdk": "[7.0.0, )", + "MailKit": "[3.2.0, )", + "Microsoft.AspNetCore.Authentication.JwtBearer": "[6.0.4, )", + "Microsoft.Azure.Cosmos.Table": "[1.0.8, )", + "Microsoft.Azure.NotificationHubs": "[4.1.0, )", + "Microsoft.Data.SqlClient": "[5.0.1, )", + "Microsoft.Extensions.Caching.StackExchangeRedis": "[6.0.6, )", + "Microsoft.Extensions.Configuration.EnvironmentVariables": "[6.0.1, )", + "Microsoft.Extensions.Configuration.UserSecrets": "[6.0.1, )", + "Microsoft.Extensions.Identity.Stores": "[6.0.4, )", + "Newtonsoft.Json": "[13.0.1, )", + "Otp.NET": "[1.2.2, )", + "Quartz": "[3.4.0, )", + "SendGrid": "[9.27.0, )", + "Sentry.Serilog": "[3.16.0, )", + "Serilog.AspNetCore": "[5.0.0, )", + "Serilog.Extensions.Logging": "[3.1.0, )", + "Serilog.Extensions.Logging.File": "[2.0.0, )", + "Serilog.Sinks.AzureCosmosDB": "[2.0.0, )", + "Serilog.Sinks.SyslogMessages": "[2.0.6, )", + "Stripe.net": "[40.0.0, )", + "YubicoDotNetClient": "[1.2.0, )" } }, "core.test": { "type": "Project", "dependencies": { - "AutoFixture.AutoNSubstitute": "4.17.0", - "AutoFixture.Xunit2": "4.17.0", - "Common": "2023.7.2", - "Core": "2023.7.2", - "Kralizek.AutoFixture.Extensions.MockHttp": "1.2.0", - "Microsoft.NET.Test.Sdk": "17.1.0", - "Moq": "4.17.2", - "NSubstitute": "4.3.0", - "xunit": "2.4.1" + "AutoFixture.AutoNSubstitute": "[4.17.0, )", + "AutoFixture.Xunit2": "[4.17.0, )", + "Common": "[2023.7.2, )", + "Core": "[2023.7.2, )", + "Kralizek.AutoFixture.Extensions.MockHttp": "[1.2.0, )", + "Microsoft.NET.Test.Sdk": "[17.1.0, )", + "NSubstitute": "[4.3.0, )", + "xunit": "[2.4.1, )" } }, "infrastructure.dapper": { "type": "Project", "dependencies": { - "Core": "2023.7.2", - "Dapper": "2.0.123" + "Core": "[2023.7.2, )", + "Dapper": "[2.0.123, )" } }, "infrastructure.entityframework": { "type": "Project", "dependencies": { - "AutoMapper.Extensions.Microsoft.DependencyInjection": "12.0.1", - "Core": "2023.7.2", - "Microsoft.EntityFrameworkCore.Relational": "7.0.5", - "Microsoft.EntityFrameworkCore.SqlServer": "7.0.5", - "Microsoft.EntityFrameworkCore.Sqlite": "7.0.5", - "Npgsql.EntityFrameworkCore.PostgreSQL": "7.0.4", - "Pomelo.EntityFrameworkCore.MySql": "7.0.0", - "linq2db.EntityFrameworkCore": "7.5.0" + "AutoMapper.Extensions.Microsoft.DependencyInjection": "[12.0.1, )", + "Core": "[2023.7.2, )", + "Microsoft.EntityFrameworkCore.Relational": "[7.0.5, )", + "Microsoft.EntityFrameworkCore.SqlServer": "[7.0.5, )", + "Microsoft.EntityFrameworkCore.Sqlite": "[7.0.5, )", + "Npgsql.EntityFrameworkCore.PostgreSQL": "[7.0.4, )", + "Pomelo.EntityFrameworkCore.MySql": "[7.0.0, )", + "linq2db.EntityFrameworkCore": "[7.5.0, )" } }, "sharedweb": { "type": "Project", "dependencies": { - "Core": "2023.7.2", - "Infrastructure.Dapper": "2023.7.2", - "Infrastructure.EntityFramework": "2023.7.2" + "Core": "[2023.7.2, )", + "Infrastructure.Dapper": "[2023.7.2, )", + "Infrastructure.EntityFramework": "[2023.7.2, )" } } } diff --git a/test/Core.Test/AutoFixture/GlobalSettingsFixtures.cs b/test/Core.Test/AutoFixture/GlobalSettingsFixtures.cs index 39f57389a..020b09707 100644 --- a/test/Core.Test/AutoFixture/GlobalSettingsFixtures.cs +++ b/test/Core.Test/AutoFixture/GlobalSettingsFixtures.cs @@ -6,7 +6,7 @@ using AutoFixture.Xunit2; using Bit.Core; using Bit.Core.Test.Helpers.Factories; using Microsoft.AspNetCore.DataProtection; -using Moq; +using NSubstitute; namespace Bit.Test.Common.AutoFixture; @@ -33,17 +33,17 @@ public class GlobalSettingsBuilder : ISpecimenBuilder if (pi.ParameterType == typeof(IDataProtectionProvider)) { - var dataProtector = new Mock(); - dataProtector - .Setup(d => d.Unprotect(It.IsAny())) - .Returns(data => Encoding.UTF8.GetBytes(Constants.DatabaseFieldProtectedPrefix + Encoding.UTF8.GetString(data))); + var dataProtector = Substitute.For(); + dataProtector.Unprotect(Arg.Any()) + .Returns(data => + Encoding.UTF8.GetBytes(Constants.DatabaseFieldProtectedPrefix + + Encoding.UTF8.GetString((byte[])data[0]))); - var dataProtectionProvider = new Mock(); - dataProtectionProvider - .Setup(x => x.CreateProtector(Constants.DatabaseFieldProtectorPurpose)) - .Returns(dataProtector.Object); + var dataProtectionProvider = Substitute.For(); + dataProtectionProvider.CreateProtector(Constants.DatabaseFieldProtectorPurpose) + .Returns(dataProtector); - return dataProtectionProvider.Object; + return dataProtectionProvider; } return new NoSpecimen(); diff --git a/test/Core.Test/Core.Test.csproj b/test/Core.Test/Core.Test.csproj index 875230cfe..d38040363 100644 --- a/test/Core.Test/Core.Test.csproj +++ b/test/Core.Test/Core.Test.csproj @@ -9,7 +9,6 @@ all - diff --git a/test/Core.Test/Utilities/CustomRedisProcessingStrategyTests.cs b/test/Core.Test/Utilities/CustomRedisProcessingStrategyTests.cs index e5b9bd554..10f15ca53 100644 --- a/test/Core.Test/Utilities/CustomRedisProcessingStrategyTests.cs +++ b/test/Core.Test/Utilities/CustomRedisProcessingStrategyTests.cs @@ -3,7 +3,7 @@ using Bit.Core.Settings; using Bit.Core.Utilities; using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Logging; -using Moq; +using NSubstitute; using StackExchange.Redis; using Xunit; @@ -37,14 +37,12 @@ public class CustomRedisProcessingStrategyTests #endregion - private readonly Mock _mockCounterKeyBuilder = new(); - private Mock _mockDb; + private readonly ICounterKeyBuilder _mockCounterKeyBuilder = Substitute.For(); + private IDatabase _mockDb; public CustomRedisProcessingStrategyTests() { - _mockCounterKeyBuilder - .Setup(x => - x.Build(It.IsAny(), It.IsAny())) + _mockCounterKeyBuilder.Build(Arg.Any(), Arg.Any()) .Returns(_sampleClientId.ClientId); } @@ -55,12 +53,12 @@ public class CustomRedisProcessingStrategyTests var strategy = BuildProcessingStrategy(); // Act - var result = await strategy.ProcessRequestAsync(_sampleClientId, _sampleRule, _mockCounterKeyBuilder.Object, _sampleOptions, + var result = await strategy.ProcessRequestAsync(_sampleClientId, _sampleRule, _mockCounterKeyBuilder, _sampleOptions, CancellationToken.None); // Assert Assert.Equal(1, result.Count); - VerifyRedisCalls(Times.Once()); + VerifyRedisCalls(1); } [Fact] @@ -70,60 +68,63 @@ public class CustomRedisProcessingStrategyTests var strategy = BuildProcessingStrategy(false); // Act - var result = await strategy.ProcessRequestAsync(_sampleClientId, _sampleRule, _mockCounterKeyBuilder.Object, _sampleOptions, + var result = await strategy.ProcessRequestAsync(_sampleClientId, _sampleRule, _mockCounterKeyBuilder, _sampleOptions, CancellationToken.None); // Assert Assert.Equal(0, result.Count); - VerifyRedisCalls(Times.Never()); + VerifyRedisNotCalled(); } [Fact] public async Task SkipRateLimit_When_TimeoutThresholdExceeded() { // Arrange - var mockCache = new Mock(); + var mockCache = Substitute.For(); object existingCount = new CustomRedisProcessingStrategy.TimeoutCounter { Count = _sampleSettings.DistributedIpRateLimiting.MaxRedisTimeoutsThreshold + 1 }; - mockCache.Setup(x => x.TryGetValue(It.IsAny(), out existingCount)).Returns(true); + mockCache.TryGetValue(Arg.Any(), out existingCount).ReturnsForAnyArgs(x => + { + x[1] = existingCount; + return true; + }); - var strategy = BuildProcessingStrategy(mockCache: mockCache.Object); + var strategy = BuildProcessingStrategy(mockCache: mockCache); // Act - var result = await strategy.ProcessRequestAsync(_sampleClientId, _sampleRule, _mockCounterKeyBuilder.Object, _sampleOptions, + var result = await strategy.ProcessRequestAsync(_sampleClientId, _sampleRule, _mockCounterKeyBuilder, _sampleOptions, CancellationToken.None); // Assert Assert.Equal(0, result.Count); - VerifyRedisCalls(Times.Never()); + VerifyRedisNotCalled(); } [Fact] public async Task SkipRateLimit_When_RedisTimeoutException() { // Arrange - var mockCache = new Mock(); - var mockCacheEntry = new Mock(); - mockCacheEntry.SetupAllProperties(); - mockCache.Setup(x => x.CreateEntry(It.IsAny())).Returns(mockCacheEntry.Object); + var mockCache = Substitute.For(); + var mockCacheEntry = Substitute.For(); + mockCache.CreateEntry(Arg.Any()).Returns(mockCacheEntry); - var strategy = BuildProcessingStrategy(mockCache: mockCache.Object, throwRedisTimeout: true); + var strategy = BuildProcessingStrategy(mockCache: mockCache, throwRedisTimeout: true); // Act - var result = await strategy.ProcessRequestAsync(_sampleClientId, _sampleRule, _mockCounterKeyBuilder.Object, _sampleOptions, + var result = await strategy.ProcessRequestAsync(_sampleClientId, _sampleRule, _mockCounterKeyBuilder, _sampleOptions, CancellationToken.None); - var timeoutCounter = ((CustomRedisProcessingStrategy.TimeoutCounter)mockCacheEntry.Object.Value); + var timeoutCounter = ((CustomRedisProcessingStrategy.TimeoutCounter)mockCacheEntry.Value); // Assert Assert.Equal(0, result.Count); // Skip rate limiting - VerifyRedisCalls(Times.Once()); + VerifyRedisCalls(1); Assert.Equal(1, timeoutCounter.Count); // Timeout count increased/cached - Assert.NotNull(mockCacheEntry.Object.AbsoluteExpiration); - mockCache.Verify(x => x.CreateEntry(It.IsAny())); + Assert.NotNull(mockCacheEntry.AbsoluteExpiration); + mockCache.Received().CreateEntry(Arg.Any()); } [Fact] @@ -136,26 +137,33 @@ public class CustomRedisProcessingStrategyTests // Act // Redis Timeout 1 - await strategy.ProcessRequestAsync(_sampleClientId, _sampleRule, _mockCounterKeyBuilder.Object, _sampleOptions, + await strategy.ProcessRequestAsync(_sampleClientId, _sampleRule, _mockCounterKeyBuilder, _sampleOptions, CancellationToken.None); // Redis Timeout 2 - await strategy.ProcessRequestAsync(_sampleClientId, _sampleRule, _mockCounterKeyBuilder.Object, _sampleOptions, + await strategy.ProcessRequestAsync(_sampleClientId, _sampleRule, _mockCounterKeyBuilder, _sampleOptions, CancellationToken.None); // Skip Redis - await strategy.ProcessRequestAsync(_sampleClientId, _sampleRule, _mockCounterKeyBuilder.Object, _sampleOptions, + await strategy.ProcessRequestAsync(_sampleClientId, _sampleRule, _mockCounterKeyBuilder, _sampleOptions, CancellationToken.None); // Assert - VerifyRedisCalls(Times.Exactly(_sampleSettings.DistributedIpRateLimiting.MaxRedisTimeoutsThreshold)); + VerifyRedisCalls(_sampleSettings.DistributedIpRateLimiting.MaxRedisTimeoutsThreshold); } - private void VerifyRedisCalls(Times times) + private void VerifyRedisCalls(int times) { - _mockDb.Verify(x => - x.ScriptEvaluateAsync(It.IsAny(), It.IsAny(), It.IsAny()), - times); + _mockDb + .Received(times) + .ScriptEvaluateAsync(Arg.Any(), Arg.Any(), Arg.Any()); + } + + private void VerifyRedisNotCalled() + { + _mockDb + .DidNotReceive() + .ScriptEvaluateAsync(Arg.Any(), Arg.Any(), Arg.Any()); } private CustomRedisProcessingStrategy BuildProcessingStrategy( @@ -163,36 +171,33 @@ public class CustomRedisProcessingStrategyTests bool throwRedisTimeout = false, IMemoryCache mockCache = null) { - var mockRedisConnection = new Mock(); + var mockRedisConnection = Substitute.For(); - mockRedisConnection.Setup(x => x.IsConnected).Returns(isRedisConnected); + mockRedisConnection.IsConnected.Returns(isRedisConnected); - _mockDb = new Mock(); + _mockDb = Substitute.For(); var mockScriptEvaluate = _mockDb - .Setup(x => - x.ScriptEvaluateAsync(It.IsAny(), It.IsAny(), It.IsAny())); + .ScriptEvaluateAsync(Arg.Any(), Arg.Any(), Arg.Any()); if (throwRedisTimeout) { - mockScriptEvaluate.ThrowsAsync(new RedisTimeoutException("Timeout", CommandStatus.WaitingToBeSent)); + mockScriptEvaluate.Returns(x => throw new RedisTimeoutException("Timeout", CommandStatus.WaitingToBeSent)); } else { - mockScriptEvaluate.ReturnsAsync(RedisResult.Create(1)); + mockScriptEvaluate.Returns(RedisResult.Create(1)); } - mockRedisConnection - .Setup(x => - x.GetDatabase(It.IsAny(), It.IsAny())) - .Returns(_mockDb.Object); + mockRedisConnection.GetDatabase(Arg.Any(), Arg.Any()) + .Returns(_mockDb); - var mockLogger = new Mock>(); - var mockConfig = new Mock(); + var mockLogger = Substitute.For>(); + var mockConfig = Substitute.For(); - mockCache ??= new Mock().Object; + mockCache ??= Substitute.For(); - return new CustomRedisProcessingStrategy(mockRedisConnection.Object, mockConfig.Object, - mockLogger.Object, mockCache, _sampleSettings); + return new CustomRedisProcessingStrategy(mockRedisConnection, mockConfig, + mockLogger, mockCache, _sampleSettings); } } diff --git a/test/Core.Test/packages.lock.json b/test/Core.Test/packages.lock.json index 6337c1a65..d319b95ed 100644 --- a/test/Core.Test/packages.lock.json +++ b/test/Core.Test/packages.lock.json @@ -48,16 +48,6 @@ "Microsoft.TestPlatform.TestHost": "17.1.0" } }, - "Moq": { - "type": "Direct", - "requested": "[4.17.2, )", - "resolved": "4.17.2", - "contentHash": "HytUPJ3/uks2UgJ9hIcyXm3YxpFAR4OJzbQwTHltbKGun3lFLhEHs97hiiPj1dY8jV/kasXeihTzDxct6Zf3iQ==", - "dependencies": { - "Castle.Core": "4.4.1", - "System.Threading.Tasks.Extensions": "4.5.4" - } - }, "NSubstitute": { "type": "Direct", "requested": "[4.3.0, )", @@ -2680,55 +2670,55 @@ "common": { "type": "Project", "dependencies": { - "AutoFixture.AutoNSubstitute": "4.17.0", - "AutoFixture.Xunit2": "4.17.0", - "Core": "2023.7.2", - "Kralizek.AutoFixture.Extensions.MockHttp": "1.2.0", - "Microsoft.NET.Test.Sdk": "17.1.0", - "NSubstitute": "4.3.0", - "xunit": "2.4.1" + "AutoFixture.AutoNSubstitute": "[4.17.0, )", + "AutoFixture.Xunit2": "[4.17.0, )", + "Core": "[2023.7.2, )", + "Kralizek.AutoFixture.Extensions.MockHttp": "[1.2.0, )", + "Microsoft.NET.Test.Sdk": "[17.1.0, )", + "NSubstitute": "[4.3.0, )", + "xunit": "[2.4.1, )" } }, "core": { "type": "Project", "dependencies": { - "AWSSDK.SQS": "3.7.2.47", - "AWSSDK.SimpleEmail": "3.7.0.150", - "AspNetCoreRateLimit": "4.0.2", - "AspNetCoreRateLimit.Redis": "1.0.1", - "Azure.Extensions.AspNetCore.DataProtection.Blobs": "1.3.2", - "Azure.Messaging.ServiceBus": "7.15.0", - "Azure.Storage.Blobs": "12.14.1", - "Azure.Storage.Queues": "12.12.0", - "BitPay.Light": "1.0.1907", - "Braintree": "5.12.0", - "DnsClient": "1.7.0", - "Fido2.AspNet": "3.0.1", - "Handlebars.Net": "2.1.2", - "IdentityServer4": "4.1.2", - "IdentityServer4.AccessTokenValidation": "3.0.1", - "LaunchDarkly.ServerSdk": "7.0.0", - "MailKit": "3.2.0", - "Microsoft.AspNetCore.Authentication.JwtBearer": "6.0.4", - "Microsoft.Azure.Cosmos.Table": "1.0.8", - "Microsoft.Azure.NotificationHubs": "4.1.0", - "Microsoft.Data.SqlClient": "5.0.1", - "Microsoft.Extensions.Caching.StackExchangeRedis": "6.0.6", - "Microsoft.Extensions.Configuration.EnvironmentVariables": "6.0.1", - "Microsoft.Extensions.Configuration.UserSecrets": "6.0.1", - "Microsoft.Extensions.Identity.Stores": "6.0.4", - "Newtonsoft.Json": "13.0.1", - "Otp.NET": "1.2.2", - "Quartz": "3.4.0", - "SendGrid": "9.27.0", - "Sentry.Serilog": "3.16.0", - "Serilog.AspNetCore": "5.0.0", - "Serilog.Extensions.Logging": "3.1.0", - "Serilog.Extensions.Logging.File": "2.0.0", - "Serilog.Sinks.AzureCosmosDB": "2.0.0", - "Serilog.Sinks.SyslogMessages": "2.0.6", - "Stripe.net": "40.0.0", - "YubicoDotNetClient": "1.2.0" + "AWSSDK.SQS": "[3.7.2.47, )", + "AWSSDK.SimpleEmail": "[3.7.0.150, )", + "AspNetCoreRateLimit": "[4.0.2, )", + "AspNetCoreRateLimit.Redis": "[1.0.1, )", + "Azure.Extensions.AspNetCore.DataProtection.Blobs": "[1.3.2, )", + "Azure.Messaging.ServiceBus": "[7.15.0, )", + "Azure.Storage.Blobs": "[12.14.1, )", + "Azure.Storage.Queues": "[12.12.0, )", + "BitPay.Light": "[1.0.1907, )", + "Braintree": "[5.12.0, )", + "DnsClient": "[1.7.0, )", + "Fido2.AspNet": "[3.0.1, )", + "Handlebars.Net": "[2.1.2, )", + "IdentityServer4": "[4.1.2, )", + "IdentityServer4.AccessTokenValidation": "[3.0.1, )", + "LaunchDarkly.ServerSdk": "[7.0.0, )", + "MailKit": "[3.2.0, )", + "Microsoft.AspNetCore.Authentication.JwtBearer": "[6.0.4, )", + "Microsoft.Azure.Cosmos.Table": "[1.0.8, )", + "Microsoft.Azure.NotificationHubs": "[4.1.0, )", + "Microsoft.Data.SqlClient": "[5.0.1, )", + "Microsoft.Extensions.Caching.StackExchangeRedis": "[6.0.6, )", + "Microsoft.Extensions.Configuration.EnvironmentVariables": "[6.0.1, )", + "Microsoft.Extensions.Configuration.UserSecrets": "[6.0.1, )", + "Microsoft.Extensions.Identity.Stores": "[6.0.4, )", + "Newtonsoft.Json": "[13.0.1, )", + "Otp.NET": "[1.2.2, )", + "Quartz": "[3.4.0, )", + "SendGrid": "[9.27.0, )", + "Sentry.Serilog": "[3.16.0, )", + "Serilog.AspNetCore": "[5.0.0, )", + "Serilog.Extensions.Logging": "[3.1.0, )", + "Serilog.Extensions.Logging.File": "[2.0.0, )", + "Serilog.Sinks.AzureCosmosDB": "[2.0.0, )", + "Serilog.Sinks.SyslogMessages": "[2.0.6, )", + "Stripe.net": "[40.0.0, )", + "YubicoDotNetClient": "[1.2.0, )" } } } diff --git a/test/Infrastructure.EFIntegration.Test/AutoFixture/EntityFrameworkRepositoryFixtures.cs b/test/Infrastructure.EFIntegration.Test/AutoFixture/EntityFrameworkRepositoryFixtures.cs index c1325af21..fd14cde65 100644 --- a/test/Infrastructure.EFIntegration.Test/AutoFixture/EntityFrameworkRepositoryFixtures.cs +++ b/test/Infrastructure.EFIntegration.Test/AutoFixture/EntityFrameworkRepositoryFixtures.cs @@ -10,7 +10,7 @@ using Bit.Infrastructure.EntityFramework.Repositories; using Bit.Infrastructure.EntityFramework.Vault.Models; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; -using Moq; +using NSubstitute; namespace Bit.Infrastructure.EFIntegration.Test.AutoFixture; @@ -25,20 +25,16 @@ internal class ServiceScopeFactoryBuilder : ISpecimenBuilder public object Create(object request, ISpecimenContext context) { var fixture = new Fixture(); - var serviceProvider = new Mock(); + var serviceProvider = Substitute.For(); var dbContext = new DatabaseContext(_options); - serviceProvider - .Setup(x => x.GetService(typeof(DatabaseContext))) - .Returns(dbContext); + serviceProvider.GetService(typeof(DatabaseContext)).Returns(dbContext); - var serviceScope = new Mock(); - serviceScope.Setup(x => x.ServiceProvider).Returns(serviceProvider.Object); + var serviceScope = Substitute.For(); + serviceScope.ServiceProvider.Returns(serviceProvider); - var serviceScopeFactory = new Mock(); - serviceScopeFactory - .Setup(x => x.CreateScope()) - .Returns(serviceScope.Object); - return serviceScopeFactory.Object; + var serviceScopeFactory = Substitute.For(); + serviceScopeFactory.CreateScope().Returns(serviceScope); + return serviceScopeFactory; } } diff --git a/test/Infrastructure.EFIntegration.Test/Helpers/DatabaseOptionsFactory.cs b/test/Infrastructure.EFIntegration.Test/Helpers/DatabaseOptionsFactory.cs index 4ee68aa8e..cc1dba46c 100644 --- a/test/Infrastructure.EFIntegration.Test/Helpers/DatabaseOptionsFactory.cs +++ b/test/Infrastructure.EFIntegration.Test/Helpers/DatabaseOptionsFactory.cs @@ -5,7 +5,7 @@ using Bit.Infrastructure.EntityFramework.Repositories; using Microsoft.AspNetCore.DataProtection; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; -using Moq; +using NSubstitute; namespace Bit.Infrastructure.EFIntegration.Test.Helpers; @@ -18,17 +18,17 @@ public static class DatabaseOptionsFactory var services = new ServiceCollection() .AddSingleton(sp => { - var dataProtector = new Mock(); - dataProtector - .Setup(d => d.Unprotect(It.IsAny())) - .Returns(data => Encoding.UTF8.GetBytes(Constants.DatabaseFieldProtectedPrefix + Encoding.UTF8.GetString(data))); + var dataProtector = Substitute.For(); + dataProtector.Unprotect(Arg.Any()) + .Returns(data => + Encoding.UTF8.GetBytes(Constants.DatabaseFieldProtectedPrefix + + Encoding.UTF8.GetString((byte[])data[0]))); - var dataProtectionProvider = new Mock(); - dataProtectionProvider - .Setup(x => x.CreateProtector(Constants.DatabaseFieldProtectorPurpose)) - .Returns(dataProtector.Object); + var dataProtectionProvider = Substitute.For(); + dataProtectionProvider.CreateProtector(Constants.DatabaseFieldProtectorPurpose) + .Returns(dataProtector); - return dataProtectionProvider.Object; + return dataProtectionProvider; }) .BuildServiceProvider(); diff --git a/test/Infrastructure.EFIntegration.Test/Infrastructure.EFIntegration.Test.csproj b/test/Infrastructure.EFIntegration.Test/Infrastructure.EFIntegration.Test.csproj index 352e8b682..9ce0eb755 100644 --- a/test/Infrastructure.EFIntegration.Test/Infrastructure.EFIntegration.Test.csproj +++ b/test/Infrastructure.EFIntegration.Test/Infrastructure.EFIntegration.Test.csproj @@ -8,7 +8,6 @@ all - diff --git a/test/Infrastructure.EFIntegration.Test/packages.lock.json b/test/Infrastructure.EFIntegration.Test/packages.lock.json index a17d032f3..377737a92 100644 --- a/test/Infrastructure.EFIntegration.Test/packages.lock.json +++ b/test/Infrastructure.EFIntegration.Test/packages.lock.json @@ -38,16 +38,6 @@ "Microsoft.TestPlatform.TestHost": "17.1.0" } }, - "Moq": { - "type": "Direct", - "requested": "[4.17.2, )", - "resolved": "4.17.2", - "contentHash": "HytUPJ3/uks2UgJ9hIcyXm3YxpFAR4OJzbQwTHltbKGun3lFLhEHs97hiiPj1dY8jV/kasXeihTzDxct6Zf3iQ==", - "dependencies": { - "Castle.Core": "4.4.1", - "System.Threading.Tasks.Extensions": "4.5.4" - } - }, "NSubstitute": { "type": "Direct", "requested": "[4.3.0, )", @@ -2843,89 +2833,88 @@ "common": { "type": "Project", "dependencies": { - "AutoFixture.AutoNSubstitute": "4.17.0", - "AutoFixture.Xunit2": "4.17.0", - "Core": "2023.7.2", - "Kralizek.AutoFixture.Extensions.MockHttp": "1.2.0", - "Microsoft.NET.Test.Sdk": "17.1.0", - "NSubstitute": "4.3.0", - "xunit": "2.4.1" + "AutoFixture.AutoNSubstitute": "[4.17.0, )", + "AutoFixture.Xunit2": "[4.17.0, )", + "Core": "[2023.7.2, )", + "Kralizek.AutoFixture.Extensions.MockHttp": "[1.2.0, )", + "Microsoft.NET.Test.Sdk": "[17.1.0, )", + "NSubstitute": "[4.3.0, )", + "xunit": "[2.4.1, )" } }, "core": { "type": "Project", "dependencies": { - "AWSSDK.SQS": "3.7.2.47", - "AWSSDK.SimpleEmail": "3.7.0.150", - "AspNetCoreRateLimit": "4.0.2", - "AspNetCoreRateLimit.Redis": "1.0.1", - "Azure.Extensions.AspNetCore.DataProtection.Blobs": "1.3.2", - "Azure.Messaging.ServiceBus": "7.15.0", - "Azure.Storage.Blobs": "12.14.1", - "Azure.Storage.Queues": "12.12.0", - "BitPay.Light": "1.0.1907", - "Braintree": "5.12.0", - "DnsClient": "1.7.0", - "Fido2.AspNet": "3.0.1", - "Handlebars.Net": "2.1.2", - "IdentityServer4": "4.1.2", - "IdentityServer4.AccessTokenValidation": "3.0.1", - "LaunchDarkly.ServerSdk": "7.0.0", - "MailKit": "3.2.0", - "Microsoft.AspNetCore.Authentication.JwtBearer": "6.0.4", - "Microsoft.Azure.Cosmos.Table": "1.0.8", - "Microsoft.Azure.NotificationHubs": "4.1.0", - "Microsoft.Data.SqlClient": "5.0.1", - "Microsoft.Extensions.Caching.StackExchangeRedis": "6.0.6", - "Microsoft.Extensions.Configuration.EnvironmentVariables": "6.0.1", - "Microsoft.Extensions.Configuration.UserSecrets": "6.0.1", - "Microsoft.Extensions.Identity.Stores": "6.0.4", - "Newtonsoft.Json": "13.0.1", - "Otp.NET": "1.2.2", - "Quartz": "3.4.0", - "SendGrid": "9.27.0", - "Sentry.Serilog": "3.16.0", - "Serilog.AspNetCore": "5.0.0", - "Serilog.Extensions.Logging": "3.1.0", - "Serilog.Extensions.Logging.File": "2.0.0", - "Serilog.Sinks.AzureCosmosDB": "2.0.0", - "Serilog.Sinks.SyslogMessages": "2.0.6", - "Stripe.net": "40.0.0", - "YubicoDotNetClient": "1.2.0" + "AWSSDK.SQS": "[3.7.2.47, )", + "AWSSDK.SimpleEmail": "[3.7.0.150, )", + "AspNetCoreRateLimit": "[4.0.2, )", + "AspNetCoreRateLimit.Redis": "[1.0.1, )", + "Azure.Extensions.AspNetCore.DataProtection.Blobs": "[1.3.2, )", + "Azure.Messaging.ServiceBus": "[7.15.0, )", + "Azure.Storage.Blobs": "[12.14.1, )", + "Azure.Storage.Queues": "[12.12.0, )", + "BitPay.Light": "[1.0.1907, )", + "Braintree": "[5.12.0, )", + "DnsClient": "[1.7.0, )", + "Fido2.AspNet": "[3.0.1, )", + "Handlebars.Net": "[2.1.2, )", + "IdentityServer4": "[4.1.2, )", + "IdentityServer4.AccessTokenValidation": "[3.0.1, )", + "LaunchDarkly.ServerSdk": "[7.0.0, )", + "MailKit": "[3.2.0, )", + "Microsoft.AspNetCore.Authentication.JwtBearer": "[6.0.4, )", + "Microsoft.Azure.Cosmos.Table": "[1.0.8, )", + "Microsoft.Azure.NotificationHubs": "[4.1.0, )", + "Microsoft.Data.SqlClient": "[5.0.1, )", + "Microsoft.Extensions.Caching.StackExchangeRedis": "[6.0.6, )", + "Microsoft.Extensions.Configuration.EnvironmentVariables": "[6.0.1, )", + "Microsoft.Extensions.Configuration.UserSecrets": "[6.0.1, )", + "Microsoft.Extensions.Identity.Stores": "[6.0.4, )", + "Newtonsoft.Json": "[13.0.1, )", + "Otp.NET": "[1.2.2, )", + "Quartz": "[3.4.0, )", + "SendGrid": "[9.27.0, )", + "Sentry.Serilog": "[3.16.0, )", + "Serilog.AspNetCore": "[5.0.0, )", + "Serilog.Extensions.Logging": "[3.1.0, )", + "Serilog.Extensions.Logging.File": "[2.0.0, )", + "Serilog.Sinks.AzureCosmosDB": "[2.0.0, )", + "Serilog.Sinks.SyslogMessages": "[2.0.6, )", + "Stripe.net": "[40.0.0, )", + "YubicoDotNetClient": "[1.2.0, )" } }, "core.test": { "type": "Project", "dependencies": { - "AutoFixture.AutoNSubstitute": "4.17.0", - "AutoFixture.Xunit2": "4.17.0", - "Common": "2023.7.2", - "Core": "2023.7.2", - "Kralizek.AutoFixture.Extensions.MockHttp": "1.2.0", - "Microsoft.NET.Test.Sdk": "17.1.0", - "Moq": "4.17.2", - "NSubstitute": "4.3.0", - "xunit": "2.4.1" + "AutoFixture.AutoNSubstitute": "[4.17.0, )", + "AutoFixture.Xunit2": "[4.17.0, )", + "Common": "[2023.7.2, )", + "Core": "[2023.7.2, )", + "Kralizek.AutoFixture.Extensions.MockHttp": "[1.2.0, )", + "Microsoft.NET.Test.Sdk": "[17.1.0, )", + "NSubstitute": "[4.3.0, )", + "xunit": "[2.4.1, )" } }, "infrastructure.dapper": { "type": "Project", "dependencies": { - "Core": "2023.7.2", - "Dapper": "2.0.123" + "Core": "[2023.7.2, )", + "Dapper": "[2.0.123, )" } }, "infrastructure.entityframework": { "type": "Project", "dependencies": { - "AutoMapper.Extensions.Microsoft.DependencyInjection": "12.0.1", - "Core": "2023.7.2", - "Microsoft.EntityFrameworkCore.Relational": "7.0.5", - "Microsoft.EntityFrameworkCore.SqlServer": "7.0.5", - "Microsoft.EntityFrameworkCore.Sqlite": "7.0.5", - "Npgsql.EntityFrameworkCore.PostgreSQL": "7.0.4", - "Pomelo.EntityFrameworkCore.MySql": "7.0.0", - "linq2db.EntityFrameworkCore": "7.5.0" + "AutoMapper.Extensions.Microsoft.DependencyInjection": "[12.0.1, )", + "Core": "[2023.7.2, )", + "Microsoft.EntityFrameworkCore.Relational": "[7.0.5, )", + "Microsoft.EntityFrameworkCore.SqlServer": "[7.0.5, )", + "Microsoft.EntityFrameworkCore.Sqlite": "[7.0.5, )", + "Npgsql.EntityFrameworkCore.PostgreSQL": "[7.0.4, )", + "Pomelo.EntityFrameworkCore.MySql": "[7.0.0, )", + "linq2db.EntityFrameworkCore": "[7.5.0, )" } } }