1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-25 12:45:18 +01:00

Remove moq (#3166)

This commit is contained in:
Oscar Hinton 2023-08-10 17:03:42 +02:00 committed by GitHub
parent dd82b8a56f
commit d4bcaf10ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 323 additions and 369 deletions

View File

@ -43,10 +43,6 @@
"groupName": "nuget minor",
"matchManagers": ["nuget"],
"matchUpdateTypes": ["minor", "patch"]
},
{
"matchPackageNames": ["Moq"],
"allowedVersions": "<=4.18.4"
}
]
}

View File

@ -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, )"
}
}
}

View File

@ -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, )"
}
}
}

View File

@ -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<IDataProtector>();
dataProtector
.Setup(d => d.Unprotect(It.IsAny<byte[]>()))
.Returns<byte[]>(data => Encoding.UTF8.GetBytes(Constants.DatabaseFieldProtectedPrefix + Encoding.UTF8.GetString(data)));
var dataProtector = Substitute.For<IDataProtector>();
dataProtector.Unprotect(Arg.Any<byte[]>())
.Returns(data =>
Encoding.UTF8.GetBytes(Constants.DatabaseFieldProtectedPrefix +
Encoding.UTF8.GetString((byte[])data[0])));
var dataProtectionProvider = new Mock<IDataProtectionProvider>();
dataProtectionProvider
.Setup(x => x.CreateProtector(Constants.DatabaseFieldProtectorPurpose))
.Returns(dataProtector.Object);
var dataProtectionProvider = Substitute.For<IDataProtectionProvider>();
dataProtectionProvider.CreateProtector(Constants.DatabaseFieldProtectorPurpose)
.Returns(dataProtector);
return dataProtectionProvider.Object;
return dataProtectionProvider;
}
return new NoSpecimen();

View File

@ -9,7 +9,6 @@
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(MicrosoftNetTestSdkVersion)" />
<PackageReference Include="Moq" Version="4.17.2" />
<PackageReference Include="NSubstitute" Version="$(NSubstituteVersion)" />
<PackageReference Include="xunit" Version="$(XUnitVersion)" />
<PackageReference Include="xunit.runner.visualstudio" Version="$(XUnitRunnerVisualStudioVersion)">

View File

@ -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<ICounterKeyBuilder> _mockCounterKeyBuilder = new();
private Mock<IDatabase> _mockDb;
private readonly ICounterKeyBuilder _mockCounterKeyBuilder = Substitute.For<ICounterKeyBuilder>();
private IDatabase _mockDb;
public CustomRedisProcessingStrategyTests()
{
_mockCounterKeyBuilder
.Setup(x =>
x.Build(It.IsAny<ClientRequestIdentity>(), It.IsAny<RateLimitRule>()))
_mockCounterKeyBuilder.Build(Arg.Any<ClientRequestIdentity>(), Arg.Any<RateLimitRule>())
.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<IMemoryCache>();
var mockCache = Substitute.For<IMemoryCache>();
object existingCount = new CustomRedisProcessingStrategy.TimeoutCounter
{
Count = _sampleSettings.DistributedIpRateLimiting.MaxRedisTimeoutsThreshold + 1
};
mockCache.Setup(x => x.TryGetValue(It.IsAny<object>(), out existingCount)).Returns(true);
mockCache.TryGetValue(Arg.Any<object>(), 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<IMemoryCache>();
var mockCacheEntry = new Mock<ICacheEntry>();
mockCacheEntry.SetupAllProperties();
mockCache.Setup(x => x.CreateEntry(It.IsAny<object>())).Returns(mockCacheEntry.Object);
var mockCache = Substitute.For<IMemoryCache>();
var mockCacheEntry = Substitute.For<ICacheEntry>();
mockCache.CreateEntry(Arg.Any<object>()).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<object>()));
Assert.NotNull(mockCacheEntry.AbsoluteExpiration);
mockCache.Received().CreateEntry(Arg.Any<object>());
}
[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<LuaScript>(), It.IsAny<object>(), It.IsAny<CommandFlags>()),
times);
_mockDb
.Received(times)
.ScriptEvaluateAsync(Arg.Any<LuaScript>(), Arg.Any<object>(), Arg.Any<CommandFlags>());
}
private void VerifyRedisNotCalled()
{
_mockDb
.DidNotReceive()
.ScriptEvaluateAsync(Arg.Any<LuaScript>(), Arg.Any<object>(), Arg.Any<CommandFlags>());
}
private CustomRedisProcessingStrategy BuildProcessingStrategy(
@ -163,36 +171,33 @@ public class CustomRedisProcessingStrategyTests
bool throwRedisTimeout = false,
IMemoryCache mockCache = null)
{
var mockRedisConnection = new Mock<IConnectionMultiplexer>();
var mockRedisConnection = Substitute.For<IConnectionMultiplexer>();
mockRedisConnection.Setup(x => x.IsConnected).Returns(isRedisConnected);
mockRedisConnection.IsConnected.Returns(isRedisConnected);
_mockDb = new Mock<IDatabase>();
_mockDb = Substitute.For<IDatabase>();
var mockScriptEvaluate = _mockDb
.Setup(x =>
x.ScriptEvaluateAsync(It.IsAny<LuaScript>(), It.IsAny<object>(), It.IsAny<CommandFlags>()));
.ScriptEvaluateAsync(Arg.Any<LuaScript>(), Arg.Any<object>(), Arg.Any<CommandFlags>());
if (throwRedisTimeout)
{
mockScriptEvaluate.ThrowsAsync(new RedisTimeoutException("Timeout", CommandStatus.WaitingToBeSent));
mockScriptEvaluate.Returns<RedisResult>(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<int>(), It.IsAny<object>()))
.Returns(_mockDb.Object);
mockRedisConnection.GetDatabase(Arg.Any<int>(), Arg.Any<object>())
.Returns(_mockDb);
var mockLogger = new Mock<ILogger<CustomRedisProcessingStrategy>>();
var mockConfig = new Mock<IRateLimitConfiguration>();
var mockLogger = Substitute.For<ILogger<CustomRedisProcessingStrategy>>();
var mockConfig = Substitute.For<IRateLimitConfiguration>();
mockCache ??= new Mock<IMemoryCache>().Object;
mockCache ??= Substitute.For<IMemoryCache>();
return new CustomRedisProcessingStrategy(mockRedisConnection.Object, mockConfig.Object,
mockLogger.Object, mockCache, _sampleSettings);
return new CustomRedisProcessingStrategy(mockRedisConnection, mockConfig,
mockLogger, mockCache, _sampleSettings);
}
}

View File

@ -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, )"
}
}
}

View File

@ -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<IServiceProvider>();
var serviceProvider = Substitute.For<IServiceProvider>();
var dbContext = new DatabaseContext(_options);
serviceProvider
.Setup(x => x.GetService(typeof(DatabaseContext)))
.Returns(dbContext);
serviceProvider.GetService(typeof(DatabaseContext)).Returns(dbContext);
var serviceScope = new Mock<IServiceScope>();
serviceScope.Setup(x => x.ServiceProvider).Returns(serviceProvider.Object);
var serviceScope = Substitute.For<IServiceScope>();
serviceScope.ServiceProvider.Returns(serviceProvider);
var serviceScopeFactory = new Mock<IServiceScopeFactory>();
serviceScopeFactory
.Setup(x => x.CreateScope())
.Returns(serviceScope.Object);
return serviceScopeFactory.Object;
var serviceScopeFactory = Substitute.For<IServiceScopeFactory>();
serviceScopeFactory.CreateScope().Returns(serviceScope);
return serviceScopeFactory;
}
}

View File

@ -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<IDataProtector>();
dataProtector
.Setup(d => d.Unprotect(It.IsAny<byte[]>()))
.Returns<byte[]>(data => Encoding.UTF8.GetBytes(Constants.DatabaseFieldProtectedPrefix + Encoding.UTF8.GetString(data)));
var dataProtector = Substitute.For<IDataProtector>();
dataProtector.Unprotect(Arg.Any<byte[]>())
.Returns<byte[]>(data =>
Encoding.UTF8.GetBytes(Constants.DatabaseFieldProtectedPrefix +
Encoding.UTF8.GetString((byte[])data[0])));
var dataProtectionProvider = new Mock<IDataProtectionProvider>();
dataProtectionProvider
.Setup(x => x.CreateProtector(Constants.DatabaseFieldProtectorPurpose))
.Returns(dataProtector.Object);
var dataProtectionProvider = Substitute.For<IDataProtectionProvider>();
dataProtectionProvider.CreateProtector(Constants.DatabaseFieldProtectorPurpose)
.Returns(dataProtector);
return dataProtectionProvider.Object;
return dataProtectionProvider;
})
.BuildServiceProvider();

View File

@ -8,7 +8,6 @@
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(MicrosoftNetTestSdkVersion)" />
<PackageReference Include="Moq" Version="4.17.2" />
<PackageReference Include="NSubstitute" Version="$(NSubstituteVersion)" />
<PackageReference Include="xunit" Version="$(XUnitVersion)" />
<PackageReference Include="xunit.runner.visualstudio" Version="$(XUnitRunnerVisualStudioVersion)">

View File

@ -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, )"
}
}
}