mirror of
https://github.com/bitwarden/server.git
synced 2025-04-15 18:18:12 -05:00
Merge pull request #784 from themikecom/master
Add unit test coverage for AmazonSqsBlockIpService
This commit is contained in:
commit
fcd100d3eb
@ -7,7 +7,7 @@ namespace Bit.Core.Services
|
|||||||
{
|
{
|
||||||
public class AmazonSqsBlockIpService : IBlockIpService, IDisposable
|
public class AmazonSqsBlockIpService : IBlockIpService, IDisposable
|
||||||
{
|
{
|
||||||
private readonly AmazonSQSClient _client;
|
private readonly IAmazonSQS _client;
|
||||||
private string _blockIpQueueUrl;
|
private string _blockIpQueueUrl;
|
||||||
private string _unblockIpQueueUrl;
|
private string _unblockIpQueueUrl;
|
||||||
private bool _didInit = false;
|
private bool _didInit = false;
|
||||||
@ -15,6 +15,17 @@ namespace Bit.Core.Services
|
|||||||
|
|
||||||
public AmazonSqsBlockIpService(
|
public AmazonSqsBlockIpService(
|
||||||
GlobalSettings globalSettings)
|
GlobalSettings globalSettings)
|
||||||
|
: this(globalSettings, new AmazonSQSClient(
|
||||||
|
globalSettings.Amazon.AccessKeyId,
|
||||||
|
globalSettings.Amazon.AccessKeySecret,
|
||||||
|
RegionEndpoint.GetBySystemName(globalSettings.Amazon.Region))
|
||||||
|
)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public AmazonSqsBlockIpService(
|
||||||
|
GlobalSettings globalSettings,
|
||||||
|
IAmazonSQS amazonSqs)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrWhiteSpace(globalSettings.Amazon?.AccessKeyId))
|
if (string.IsNullOrWhiteSpace(globalSettings.Amazon?.AccessKeyId))
|
||||||
{
|
{
|
||||||
@ -28,8 +39,8 @@ namespace Bit.Core.Services
|
|||||||
{
|
{
|
||||||
throw new ArgumentNullException(nameof(globalSettings.Amazon.Region));
|
throw new ArgumentNullException(nameof(globalSettings.Amazon.Region));
|
||||||
}
|
}
|
||||||
_client = new AmazonSQSClient(globalSettings.Amazon.AccessKeyId,
|
|
||||||
globalSettings.Amazon.AccessKeySecret, RegionEndpoint.GetBySystemName(globalSettings.Amazon.Region));
|
_client = amazonSqs;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Amazon.SQS;
|
||||||
using Bit.Core.Services;
|
using Bit.Core.Services;
|
||||||
using NSubstitute;
|
using NSubstitute;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
@ -10,12 +12,23 @@ namespace Bit.Core.Test.Services
|
|||||||
private readonly AmazonSqsBlockIpService _sut;
|
private readonly AmazonSqsBlockIpService _sut;
|
||||||
|
|
||||||
private readonly GlobalSettings _globalSettings;
|
private readonly GlobalSettings _globalSettings;
|
||||||
|
private readonly IAmazonSQS _amazonSqs;
|
||||||
|
|
||||||
public AmazonSqsBlockIpServiceTests()
|
public AmazonSqsBlockIpServiceTests()
|
||||||
{
|
{
|
||||||
_globalSettings = new GlobalSettings();
|
_globalSettings = new GlobalSettings
|
||||||
|
{
|
||||||
|
Amazon =
|
||||||
|
{
|
||||||
|
AccessKeyId = "AccessKeyId-AmazonSesMailDeliveryServiceTests",
|
||||||
|
AccessKeySecret = "AccessKeySecret-AmazonSesMailDeliveryServiceTests",
|
||||||
|
Region = "Region-AmazonSesMailDeliveryServiceTests"
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
_sut = new AmazonSqsBlockIpService(_globalSettings);
|
_amazonSqs = Substitute.For<IAmazonSQS>();
|
||||||
|
|
||||||
|
_sut = new AmazonSqsBlockIpService(_globalSettings, _amazonSqs);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
@ -23,12 +36,43 @@ namespace Bit.Core.Test.Services
|
|||||||
_sut?.Dispose();
|
_sut?.Dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove this test when we add actual tests. It only proves that
|
[Fact]
|
||||||
// we've properly constructed the system under test.
|
public async Task BlockIpAsync_UnblockCalled_WhenNotPermanent()
|
||||||
[Fact(Skip = "Needs additional work")]
|
|
||||||
public void ServiceExists()
|
|
||||||
{
|
{
|
||||||
Assert.NotNull(_sut);
|
const string expectedIp = "ip";
|
||||||
|
|
||||||
|
await _sut.BlockIpAsync(expectedIp, false);
|
||||||
|
|
||||||
|
await _amazonSqs.Received(2).SendMessageAsync(
|
||||||
|
Arg.Any<string>(),
|
||||||
|
Arg.Is(expectedIp));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task BlockIpAsync_UnblockNotCalled_WhenPermanent()
|
||||||
|
{
|
||||||
|
const string expectedIp = "ip";
|
||||||
|
|
||||||
|
await _sut.BlockIpAsync(expectedIp, true);
|
||||||
|
|
||||||
|
await _amazonSqs.Received(1).SendMessageAsync(
|
||||||
|
Arg.Any<string>(),
|
||||||
|
Arg.Is(expectedIp));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task BlockIpAsync_NotBlocked_WhenAlreadyBlockedRecently()
|
||||||
|
{
|
||||||
|
const string expectedIp = "ip";
|
||||||
|
|
||||||
|
await _sut.BlockIpAsync(expectedIp, true);
|
||||||
|
|
||||||
|
// The second call should hit the already blocked guard clause
|
||||||
|
await _sut.BlockIpAsync(expectedIp, true);
|
||||||
|
|
||||||
|
await _amazonSqs.Received(1).SendMessageAsync(
|
||||||
|
Arg.Any<string>(),
|
||||||
|
Arg.Is(expectedIp));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user