2022-06-30 01:46:41 +02:00
|
|
|
|
using Amazon.SQS;
|
2019-07-06 05:35:54 +02:00
|
|
|
|
using Bit.Core.Services;
|
2021-02-22 22:35:16 +01:00
|
|
|
|
using Bit.Core.Settings;
|
2019-07-06 05:35:54 +02:00
|
|
|
|
using NSubstitute;
|
|
|
|
|
using Xunit;
|
|
|
|
|
|
2022-08-29 22:06:55 +02:00
|
|
|
|
namespace Bit.Core.Test.Services;
|
|
|
|
|
|
|
|
|
|
public class AmazonSqsBlockIpServiceTests : IDisposable
|
2019-07-06 05:35:54 +02:00
|
|
|
|
{
|
2022-08-29 22:06:55 +02:00
|
|
|
|
private readonly AmazonSqsBlockIpService _sut;
|
2019-07-06 05:35:54 +02:00
|
|
|
|
|
2022-08-29 22:06:55 +02:00
|
|
|
|
private readonly GlobalSettings _globalSettings;
|
|
|
|
|
private readonly IAmazonSQS _amazonSqs;
|
2019-07-06 05:35:54 +02:00
|
|
|
|
|
2022-08-29 22:06:55 +02:00
|
|
|
|
public AmazonSqsBlockIpServiceTests()
|
|
|
|
|
{
|
|
|
|
|
_globalSettings = new GlobalSettings
|
2019-07-06 05:35:54 +02:00
|
|
|
|
{
|
2022-08-29 22:06:55 +02:00
|
|
|
|
Amazon =
|
2020-06-18 19:16:04 +02:00
|
|
|
|
{
|
2022-08-29 22:06:55 +02:00
|
|
|
|
AccessKeyId = "AccessKeyId-AmazonSesMailDeliveryServiceTests",
|
|
|
|
|
AccessKeySecret = "AccessKeySecret-AmazonSesMailDeliveryServiceTests",
|
|
|
|
|
Region = "Region-AmazonSesMailDeliveryServiceTests"
|
|
|
|
|
}
|
|
|
|
|
};
|
2019-07-06 05:35:54 +02:00
|
|
|
|
|
2022-08-29 22:06:55 +02:00
|
|
|
|
_amazonSqs = Substitute.For<IAmazonSQS>();
|
2020-06-18 19:16:04 +02:00
|
|
|
|
|
2022-08-29 22:06:55 +02:00
|
|
|
|
_sut = new AmazonSqsBlockIpService(_globalSettings, _amazonSqs);
|
|
|
|
|
}
|
2019-07-06 05:35:54 +02:00
|
|
|
|
|
2022-08-29 22:06:55 +02:00
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
_sut?.Dispose();
|
|
|
|
|
}
|
2019-07-06 05:35:54 +02:00
|
|
|
|
|
2022-08-29 22:06:55 +02:00
|
|
|
|
[Fact]
|
|
|
|
|
public async Task BlockIpAsync_UnblockCalled_WhenNotPermanent()
|
|
|
|
|
{
|
|
|
|
|
const string expectedIp = "ip";
|
2020-06-18 19:16:04 +02:00
|
|
|
|
|
2022-08-29 22:06:55 +02:00
|
|
|
|
await _sut.BlockIpAsync(expectedIp, false);
|
2020-06-18 19:16:04 +02:00
|
|
|
|
|
2022-08-29 22:06:55 +02:00
|
|
|
|
await _amazonSqs.Received(2).SendMessageAsync(
|
|
|
|
|
Arg.Any<string>(),
|
|
|
|
|
Arg.Is(expectedIp));
|
|
|
|
|
}
|
2020-06-18 19:16:04 +02:00
|
|
|
|
|
2022-08-29 22:06:55 +02:00
|
|
|
|
[Fact]
|
|
|
|
|
public async Task BlockIpAsync_UnblockNotCalled_WhenPermanent()
|
|
|
|
|
{
|
|
|
|
|
const string expectedIp = "ip";
|
2020-06-18 19:16:04 +02:00
|
|
|
|
|
2022-08-29 22:06:55 +02:00
|
|
|
|
await _sut.BlockIpAsync(expectedIp, true);
|
2020-06-18 19:16:04 +02:00
|
|
|
|
|
2022-08-29 22:06:55 +02:00
|
|
|
|
await _amazonSqs.Received(1).SendMessageAsync(
|
|
|
|
|
Arg.Any<string>(),
|
|
|
|
|
Arg.Is(expectedIp));
|
|
|
|
|
}
|
2020-06-18 19:16:04 +02:00
|
|
|
|
|
2022-08-29 22:06:55 +02:00
|
|
|
|
[Fact]
|
|
|
|
|
public async Task BlockIpAsync_NotBlocked_WhenAlreadyBlockedRecently()
|
|
|
|
|
{
|
|
|
|
|
const string expectedIp = "ip";
|
2020-06-18 19:16:04 +02:00
|
|
|
|
|
2022-08-29 22:06:55 +02:00
|
|
|
|
await _sut.BlockIpAsync(expectedIp, true);
|
2020-06-18 19:16:04 +02:00
|
|
|
|
|
2022-08-29 22:06:55 +02:00
|
|
|
|
// The second call should hit the already blocked guard clause
|
|
|
|
|
await _sut.BlockIpAsync(expectedIp, true);
|
2020-06-18 19:16:04 +02:00
|
|
|
|
|
2022-08-29 22:06:55 +02:00
|
|
|
|
await _amazonSqs.Received(1).SendMessageAsync(
|
|
|
|
|
Arg.Any<string>(),
|
|
|
|
|
Arg.Is(expectedIp));
|
2019-07-06 05:35:54 +02:00
|
|
|
|
}
|
|
|
|
|
}
|