diff --git a/src/SecTester.Repeater/Api/CreateRepeaterRequest.cs b/src/SecTester.Repeater/Api/CreateRepeaterRequest.cs deleted file mode 100644 index 32134b7..0000000 --- a/src/SecTester.Repeater/Api/CreateRepeaterRequest.cs +++ /dev/null @@ -1,22 +0,0 @@ -using System.Net.Http; -using System.Text; -using SecTester.Core.Commands; -using SecTester.Core.Dispatchers; - -namespace SecTester.Repeater.Api; - -internal record CreateRepeaterRequest : HttpRequest -{ - public CreateRepeaterRequest(string name, string? description) : - base("/api/v1/repeaters", HttpMethod.Post) - { - var data = new - { - Name = name, - Description = description, - ClientRole = "dev-centric" - }; - var content = MessageSerializer.Serialize(data); - Body = new StringContent(content, Encoding.UTF8, "application/json"); - } -} diff --git a/src/SecTester.Repeater/Api/DefaultRepeaters.cs b/src/SecTester.Repeater/Api/DefaultRepeaters.cs deleted file mode 100644 index d3cf91f..0000000 --- a/src/SecTester.Repeater/Api/DefaultRepeaters.cs +++ /dev/null @@ -1,34 +0,0 @@ -using System.Threading.Tasks; -using SecTester.Core.Bus; -using SecTester.Core.Exceptions; - -namespace SecTester.Repeater.Api; - -public class DefaultRepeaters : IRepeaters -{ - private readonly ICommandDispatcher _commandDispatcher; - - public DefaultRepeaters(ICommandDispatcher commandDispatcher) - { - _commandDispatcher = commandDispatcher; - } - - public async Task CreateRepeater(string name, string? description = default) - { - var repeaterId = (await _commandDispatcher.Execute(new CreateRepeaterRequest(name, description)).ConfigureAwait(false))?.Id; - - if (string.IsNullOrEmpty(repeaterId)) - { - throw new SecTesterException("Cannot create repeater"); - } - - return repeaterId!; - } - - public async Task DeleteRepeater(string repeaterId) - { - await _commandDispatcher.Execute( - new DeleteRepeaterRequest(repeaterId) - ).ConfigureAwait(false); - } -} diff --git a/src/SecTester.Repeater/Api/DeleteRepeaterRequest.cs b/src/SecTester.Repeater/Api/DeleteRepeaterRequest.cs deleted file mode 100644 index 2fb23d2..0000000 --- a/src/SecTester.Repeater/Api/DeleteRepeaterRequest.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System.Net.Http; -using SecTester.Core; -using SecTester.Core.Commands; - -namespace SecTester.Repeater.Api; - -internal record DeleteRepeaterRequest : HttpRequest -{ - public DeleteRepeaterRequest(string repeaterId) : - base($"/api/v1/repeaters/{repeaterId}", HttpMethod.Delete, expectReply: false) - { - } -} diff --git a/src/SecTester.Repeater/Api/IRepeaters.cs b/src/SecTester.Repeater/Api/IRepeaters.cs deleted file mode 100644 index 059cc95..0000000 --- a/src/SecTester.Repeater/Api/IRepeaters.cs +++ /dev/null @@ -1,10 +0,0 @@ -using System.Threading.Tasks; - -namespace SecTester.Repeater.Api; - -public interface IRepeaters -{ - Task CreateRepeater(string name, string? description = default); - - Task DeleteRepeater(string repeaterId); -} diff --git a/src/SecTester.Repeater/Api/RepeaterIdentity.cs b/src/SecTester.Repeater/Api/RepeaterIdentity.cs deleted file mode 100644 index 59399c1..0000000 --- a/src/SecTester.Repeater/Api/RepeaterIdentity.cs +++ /dev/null @@ -1,8 +0,0 @@ -using System; - -namespace SecTester.Repeater.Api; - -internal record RepeaterIdentity(string Id) -{ - public string Id { get; } = Id ?? throw new ArgumentNullException(nameof(Id)); -} diff --git a/src/SecTester.Repeater/Bus/DefaultRepeaterBusFactory.cs b/src/SecTester.Repeater/Bus/DefaultRepeaterBusFactory.cs index db3ae74..9001e8a 100644 --- a/src/SecTester.Repeater/Bus/DefaultRepeaterBusFactory.cs +++ b/src/SecTester.Repeater/Bus/DefaultRepeaterBusFactory.cs @@ -22,7 +22,7 @@ public DefaultRepeaterBusFactory(Configuration config, ILoggerFactory loggerFact _scopeFactory = scopeFactory ?? throw new ArgumentNullException(nameof(scopeFactory)); } - public IRepeaterBus Create(string repeaterId) + public IRepeaterBus Create(string? namePrefix = default) { if (_config.Credentials is null) { @@ -39,7 +39,7 @@ public IRepeaterBus Create(string repeaterId) ReconnectionDelayMax = options.ReconnectionDelayMax, ConnectionTimeout = options.ConnectionTimeout, Transport = TransportProtocol.WebSocket, - Auth = new { token = _config.Credentials.Token, domain = repeaterId } + Auth = new { token = _config.Credentials.Token, domain = namePrefix ?? System.Net.Dns.GetHostName() } }) { Serializer = new SocketIOMessagePackSerializer() diff --git a/src/SecTester.Repeater/Bus/IRepeaterBus.cs b/src/SecTester.Repeater/Bus/IRepeaterBus.cs index af02b33..cb8113b 100644 --- a/src/SecTester.Repeater/Bus/IRepeaterBus.cs +++ b/src/SecTester.Repeater/Bus/IRepeaterBus.cs @@ -11,5 +11,5 @@ public interface IRepeaterBus : IAsyncDisposable event Action UpgradeAvailable; Task Connect(); - Task Deploy(string repeaterId, CancellationToken? cancellationToken = null); + Task Deploy(string? repeaterId, CancellationToken? cancellationToken = null); } diff --git a/src/SecTester.Repeater/Bus/IRepeaterBusFactory.cs b/src/SecTester.Repeater/Bus/IRepeaterBusFactory.cs index 505fa63..13157d5 100644 --- a/src/SecTester.Repeater/Bus/IRepeaterBusFactory.cs +++ b/src/SecTester.Repeater/Bus/IRepeaterBusFactory.cs @@ -2,5 +2,5 @@ namespace SecTester.Repeater.Bus; public interface IRepeaterBusFactory { - IRepeaterBus Create(string repeaterId); + IRepeaterBus Create(string? namePrefix = default); } diff --git a/src/SecTester.Repeater/Bus/RepeaterInfo.cs b/src/SecTester.Repeater/Bus/RepeaterInfo.cs index 4671e90..51006b9 100644 --- a/src/SecTester.Repeater/Bus/RepeaterInfo.cs +++ b/src/SecTester.Repeater/Bus/RepeaterInfo.cs @@ -1,10 +1,11 @@ +using System; using MessagePack; namespace SecTester.Repeater.Bus; [MessagePackObject] -public sealed record RepeaterInfo +public sealed record RepeaterInfo(string RepeaterId) { [Key("repeaterId")] - public string RepeaterId { get; set; } = null!; + public string RepeaterId { get; init; } = RepeaterId ?? throw new ArgumentNullException(nameof(RepeaterId)); } diff --git a/src/SecTester.Repeater/Bus/SocketIoRepeaterBus.cs b/src/SecTester.Repeater/Bus/SocketIoRepeaterBus.cs index db5e7fd..7a4d167 100644 --- a/src/SecTester.Repeater/Bus/SocketIoRepeaterBus.cs +++ b/src/SecTester.Repeater/Bus/SocketIoRepeaterBus.cs @@ -90,7 +90,7 @@ public async ValueTask DisposeAsync() GC.SuppressFinalize(this); } - public async Task Deploy(string repeaterId, CancellationToken? cancellationToken = null) + public async Task Deploy(string? repeaterId, CancellationToken? cancellationToken = null) { try { @@ -98,13 +98,24 @@ public async Task Deploy(string repeaterId, CancellationToken? cancellationToken _connection.On("deployed", response => tcs.TrySetResult(response.GetValue())); - await _connection.EmitAsync("deploy", new RepeaterInfo { RepeaterId = repeaterId }).ConfigureAwait(false); + var args = string.IsNullOrWhiteSpace(repeaterId) ? Array.Empty() : new object[] { new RepeaterInfo(repeaterId!) }; + + await _connection + .EmitAsync("deploy", args) + .ConfigureAwait(false); using var _ = cancellationToken?.Register(() => tcs.TrySetCanceled()); var result = await tcs.Task.ConfigureAwait(false); _logger.LogDebug("Repeater ({RepeaterId}) deployed", result?.RepeaterId); + + if (null == result || string.IsNullOrWhiteSpace(result.RepeaterId)) + { + throw new InvalidOperationException("An error occured while repeater is being deployed"); + } + + return result.RepeaterId; } finally { diff --git a/src/SecTester.Repeater/DefaultRepeaterFactory.cs b/src/SecTester.Repeater/DefaultRepeaterFactory.cs index 0a1a4a5..aa6babc 100644 --- a/src/SecTester.Repeater/DefaultRepeaterFactory.cs +++ b/src/SecTester.Repeater/DefaultRepeaterFactory.cs @@ -3,7 +3,6 @@ using Microsoft.Extensions.Logging; using SecTester.Core; using SecTester.Core.Logger; -using SecTester.Repeater.Api; using SecTester.Repeater.Bus; namespace SecTester.Repeater; @@ -12,14 +11,12 @@ public class DefaultRepeaterFactory : IRepeaterFactory { private readonly Configuration _configuration; private readonly IRepeaterBusFactory _busFactory; - private readonly IRepeaters _repeaters; private readonly ILoggerFactory _loggerFactory; private readonly IAnsiCodeColorizer _ansiCodeColorizer; private readonly RequestRunnerResolver _requestRunnerResolver; - public DefaultRepeaterFactory(IRepeaters repeaters, IRepeaterBusFactory busFactory, Configuration configuration, ILoggerFactory loggerFactory, IAnsiCodeColorizer ansiCodeColorizer, RequestRunnerResolver requestRunnerResolver) + public DefaultRepeaterFactory(IRepeaterBusFactory busFactory, Configuration configuration, ILoggerFactory loggerFactory, IAnsiCodeColorizer ansiCodeColorizer, RequestRunnerResolver requestRunnerResolver) { - _repeaters = repeaters ?? throw new ArgumentNullException(nameof(repeaters)); _loggerFactory = loggerFactory ?? throw new ArgumentNullException(nameof(loggerFactory)); _busFactory = busFactory ?? throw new ArgumentNullException(nameof(busFactory)); _configuration = configuration ?? throw new ArgumentNullException(nameof(configuration)); @@ -27,15 +24,13 @@ public DefaultRepeaterFactory(IRepeaters repeaters, IRepeaterBusFactory busFacto _requestRunnerResolver = requestRunnerResolver ?? throw new ArgumentNullException(nameof(requestRunnerResolver)); } - public async Task CreateRepeater(RepeaterOptions? options = default) + public Task CreateRepeater(RepeaterOptions? options = default) { options ??= new RepeaterOptions(); - var repeaterId = await _repeaters.CreateRepeater($"{options.NamePrefix}-{Guid.NewGuid()}", options.Description).ConfigureAwait(false); - - var bus = _busFactory.Create(repeaterId); + var bus = _busFactory.Create(options.NamePrefix); var version = new Version(_configuration.RepeaterVersion); - return new Repeater(repeaterId, bus, version, _loggerFactory.CreateLogger(), _ansiCodeColorizer, _requestRunnerResolver); + return Task.FromResult(new Repeater(bus, version, _loggerFactory.CreateLogger(), _ansiCodeColorizer, _requestRunnerResolver)); } } diff --git a/src/SecTester.Repeater/Extensions/ServiceCollectionExtensions.cs b/src/SecTester.Repeater/Extensions/ServiceCollectionExtensions.cs index 92dd443..a61ba30 100644 --- a/src/SecTester.Repeater/Extensions/ServiceCollectionExtensions.cs +++ b/src/SecTester.Repeater/Extensions/ServiceCollectionExtensions.cs @@ -5,7 +5,6 @@ using Microsoft.Extensions.DependencyInjection; using SecTester.Core.Extensions; using SecTester.Core.Utils; -using SecTester.Repeater.Api; using SecTester.Repeater.Bus; using SecTester.Repeater.Runners; @@ -22,7 +21,6 @@ public static IServiceCollection AddSecTesterRepeater(this IServiceCollection co .AddHttpCommandDispatcher() .AddSingleton() .AddScoped() - .AddScoped() .AddScoped() .AddScoped() .AddScoped(sp => diff --git a/src/SecTester.Repeater/README.md b/src/SecTester.Repeater/README.md index e024f73..79f77de 100644 --- a/src/SecTester.Repeater/README.md +++ b/src/SecTester.Repeater/README.md @@ -44,36 +44,6 @@ The `CreateRepeater` method accepts the options described below: | Option | Description | | :--------------------- | ----------------------------------------------------------------------------------------------------- | | `namePrefix` | Enter a name prefix that will be used as a constant part of the unique name. By default, `sectester`. | -| `description` | Set a short description of the Repeater. | -| `requestRunnerOptions` | Custom the request runner settings that will be used to execute requests to your application. | - -The default `requestRunnerOptions` is as follows: - -```json -{ - "timeout": 30000, - "maxContentLength": 100, - "reuseConnection": false, - "allowedMimes": [ - "text/html", - "text/plain", - "text/css", - "text/javascript", - "text/markdown", - "text/xml", - "application/javascript", - "application/x-javascript", - "application/json", - "application/xml", - "application/x-www-form-urlencoded", - "application/msgpack", - "application/ld+json", - "application/graphql" - ] -} -``` - -The `RequestRunnerOptions` exposes the following options that can used to customize the request runner's behavior: [RequestRunnerOptions.cs](https://github.com/NeuraLegion/sectester-net/blob/master/src/SecTester.Repeater/Runners/RequestRunnerOptions.cs) The `Repeater` instance provides the `Start` method. This method is required to establish a connection with the Bright cloud engine and interact with other services. diff --git a/src/SecTester.Repeater/Repeater.cs b/src/SecTester.Repeater/Repeater.cs index 75e2305..8bcbd9e 100644 --- a/src/SecTester.Repeater/Repeater.cs +++ b/src/SecTester.Repeater/Repeater.cs @@ -11,7 +11,6 @@ namespace SecTester.Repeater; public delegate IRequestRunner? RequestRunnerResolver(Protocol key); - public class Repeater : IRepeater { private readonly IRepeaterBus _bus; @@ -21,10 +20,9 @@ public class Repeater : IRepeater private readonly IAnsiCodeColorizer _ansiCodeColorizer; private readonly RequestRunnerResolver _requestRunnersAccessor; - public Repeater(string repeaterId, IRepeaterBus bus, Version version, ILogger logger, + public Repeater(IRepeaterBus bus, Version version, ILogger logger, IAnsiCodeColorizer ansiCodeColorizer, RequestRunnerResolver requestRunnersAccessor) { - RepeaterId = repeaterId ?? throw new ArgumentNullException(nameof(repeaterId)); _logger = logger ?? throw new ArgumentNullException(nameof(logger)); _version = version ?? throw new ArgumentNullException(nameof(version)); _bus = bus ?? throw new ArgumentNullException(nameof(bus)); @@ -33,7 +31,7 @@ public Repeater(string repeaterId, IRepeaterBus bus, Version version, ILogger HandleIncomingRequest(IncomingRequest message) + private async Task HandleIncomingRequest(IncomingRequest message) { var runner = _requestRunnersAccessor(message.Protocol); diff --git a/src/SecTester.Repeater/RepeaterOptions.cs b/src/SecTester.Repeater/RepeaterOptions.cs index cd3f709..8c0fc67 100644 --- a/src/SecTester.Repeater/RepeaterOptions.cs +++ b/src/SecTester.Repeater/RepeaterOptions.cs @@ -5,7 +5,7 @@ namespace SecTester.Repeater; public record RepeaterOptions { private const int MaxPrefixLength = 44; - private readonly string _namePrefix = "sectester"; + private readonly string _namePrefix = System.Net.Dns.GetHostName(); public string NamePrefix { @@ -19,6 +19,4 @@ public string NamePrefix _namePrefix = value; } } - - public string? Description { get; init; } } diff --git a/src/SecTester.Repeater/SecTester.Repeater.csproj b/src/SecTester.Repeater/SecTester.Repeater.csproj index 4a83f72..2dbb762 100644 --- a/src/SecTester.Repeater/SecTester.Repeater.csproj +++ b/src/SecTester.Repeater/SecTester.Repeater.csproj @@ -14,7 +14,6 @@ - diff --git a/src/SecTester.Runner/SecRunner.cs b/src/SecTester.Runner/SecRunner.cs index deb9878..0c151e0 100644 --- a/src/SecTester.Runner/SecRunner.cs +++ b/src/SecTester.Runner/SecRunner.cs @@ -5,7 +5,6 @@ using SecTester.Core; using SecTester.Core.Extensions; using SecTester.Repeater; -using SecTester.Repeater.Api; using SecTester.Repeater.Extensions; using SecTester.Reporter; using SecTester.Scan; @@ -18,17 +17,15 @@ public class SecRunner : IAsyncDisposable private readonly Configuration _configuration; private readonly IFormatter _formatter; private readonly IRepeaterFactory _repeaterFactory; - private readonly IRepeaters _repeatersManager; private readonly IScanFactory _scanFactory; private IRepeater? _repeater; - public SecRunner(Configuration configuration, IRepeaterFactory repeaterFactory, IScanFactory scanFactory, IRepeaters repeatersManager, + public SecRunner(Configuration configuration, IRepeaterFactory repeaterFactory, IScanFactory scanFactory, IFormatter formatter) { _configuration = configuration ?? throw new ArgumentNullException(nameof(configuration)); _repeaterFactory = repeaterFactory ?? throw new ArgumentNullException(nameof(repeaterFactory)); _scanFactory = scanFactory ?? throw new ArgumentNullException(nameof(scanFactory)); - _repeatersManager = repeatersManager ?? throw new ArgumentNullException(nameof(repeatersManager)); _formatter = formatter ?? throw new ArgumentNullException(nameof(formatter)); } @@ -80,7 +77,6 @@ public async Task Clear(CancellationToken cancellationToken = default) if (_repeater is not null) { await _repeater.Stop(cancellationToken).ConfigureAwait(false); - await _repeatersManager.DeleteRepeater(_repeater.RepeaterId).ConfigureAwait(false); await _repeater.DisposeAsync().ConfigureAwait(false); } } diff --git a/test/SecTester.Repeater.Tests/Api/DefaultRepeatersTests.cs b/test/SecTester.Repeater.Tests/Api/DefaultRepeatersTests.cs deleted file mode 100644 index 3921232..0000000 --- a/test/SecTester.Repeater.Tests/Api/DefaultRepeatersTests.cs +++ /dev/null @@ -1,58 +0,0 @@ -namespace SecTester.Repeater.Tests.Api; - -public class DefaultRepeatersTests : IDisposable -{ - private const string Id = "99138d92-69db-44cb-952a-1cd9ec031e20"; - - private readonly ICommandDispatcher _commandDispatcher; - private readonly DefaultRepeaters _sut; - - public DefaultRepeatersTests() - { - _commandDispatcher = Substitute.For(); - _sut = new DefaultRepeaters(_commandDispatcher); - } - - public void Dispose() - { - _commandDispatcher.ClearSubstitute(); - GC.SuppressFinalize(this); - } - - [Fact] - public async Task CreateRepeater_CreatesRepeater() - { - // arrange - _commandDispatcher.Execute(Arg.Any()).Returns(new RepeaterIdentity(Id)); - - // act - var result = await _sut.CreateRepeater("foo"); - - // assert - result.Should().Be(Id); - } - - [Fact] - public async Task CreateRepeater_ThrowsError() - { - // arrange - _commandDispatcher.Execute(Arg.Any()).Returns(new RepeaterIdentity("")); - - // act - var act = () => _sut.CreateRepeater("foo"); - - // assert - await act.Should().ThrowAsync() - .WithMessage("Cannot create repeater"); - } - - [Fact] - public async Task DeleteRepeater_RemovesRepeater() - { - // act - await _sut.DeleteRepeater("foo"); - - // assert - await _commandDispatcher.Received(1).Execute(Arg.Any()); - } -} diff --git a/test/SecTester.Repeater.Tests/Bus/DefaultRepeaterBusFactoryTests.cs b/test/SecTester.Repeater.Tests/Bus/DefaultRepeaterBusFactoryTests.cs index 842c0c7..d85d186 100644 --- a/test/SecTester.Repeater.Tests/Bus/DefaultRepeaterBusFactoryTests.cs +++ b/test/SecTester.Repeater.Tests/Bus/DefaultRepeaterBusFactoryTests.cs @@ -2,7 +2,6 @@ namespace SecTester.Repeater.Tests.Bus; public class DefaultRepeaterBusFactoryTests : IDisposable { - private const string Id = "g5MvgM74sweGcK1U6hvs76"; private const string Hostname = "app.brightsec.com"; private const string Token = "0zmcwpe.nexr.0vlon8mp7lvxzjuvgjy88olrhadhiukk"; @@ -32,7 +31,7 @@ public async Task Create_CreatesBus() DefaultRepeaterBusFactory sut = new(config, _loggerFactory, _serviceScopeFactory); // act - await using var bus = sut.Create(Id); + await using var bus = sut.Create(); // assert bus.Should().BeAssignableTo(); @@ -48,7 +47,7 @@ public async Task Create_CredentialsNotDefined_ThrowsError() // act var act = async () => { - await using var _ = sut.Create(Id); + await using var _ = sut.Create(); }; // assert diff --git a/test/SecTester.Repeater.Tests/Bus/SocketIoRepeaterBusTests.cs b/test/SecTester.Repeater.Tests/Bus/SocketIoRepeaterBusTests.cs index 17351e8..a757883 100644 --- a/test/SecTester.Repeater.Tests/Bus/SocketIoRepeaterBusTests.cs +++ b/test/SecTester.Repeater.Tests/Bus/SocketIoRepeaterBusTests.cs @@ -151,13 +151,41 @@ public async Task Connect_ShouldSendPingMessage() public async Task Deploy_Success() { // arrange + _socketIoMessage.GetValue().Returns(new RepeaterInfo(RepeaterId)); + _connection.On("deployed", Arg.Invoke(_socketIoMessage)); + + // act + await _sut.Deploy(null); + + // assert + await _connection.Received().EmitAsync("deploy", Arg.Is(x => x.Length == 0)); + } + + [Fact] + public async Task Deploy_GivenRepeaterId_Success() + { + // arrange + _socketIoMessage.GetValue().Returns(new RepeaterInfo(RepeaterId)); _connection.On("deployed", Arg.Invoke(_socketIoMessage)); // act await _sut.Deploy(RepeaterId); // assert - await _connection.Received().EmitAsync("deploy", Arg.Is(x => x.RepeaterId.Equals(RepeaterId, StringComparison.OrdinalIgnoreCase))); + await _connection.Received().EmitAsync("deploy", Arg.Is(x => x.Length == 1 && ((x[0] as RepeaterInfo)!).RepeaterId == RepeaterId)); + } + + [Fact] + public async Task Deploy_NotReceivingRepeaterId_ThrowsError() + { + // arrange + _connection.On("deployed", Arg.Invoke(_socketIoMessage)); + + // act + var act = () => _sut.Deploy(null); + + // assert + await act.Should().ThrowAsync().WithMessage("An error occured while repeater is being deployed"); } [Fact] @@ -168,7 +196,7 @@ public async Task Deploy_GivenCancellationToken_ThrowsError() cancellationTokenSource.Cancel(); // act - var act = () => _sut.Deploy(RepeaterId, cancellationTokenSource.Token); + var act = () => _sut.Deploy(null, cancellationTokenSource.Token); // assert await act.Should().ThrowAsync(); diff --git a/test/SecTester.Repeater.Tests/DefaultRepeaterFactoryTests.cs b/test/SecTester.Repeater.Tests/DefaultRepeaterFactoryTests.cs index 6be5247..f5331a5 100644 --- a/test/SecTester.Repeater.Tests/DefaultRepeaterFactoryTests.cs +++ b/test/SecTester.Repeater.Tests/DefaultRepeaterFactoryTests.cs @@ -3,13 +3,11 @@ namespace SecTester.Repeater.Tests; public class DefaultRepeaterFactoryTests : IDisposable { private const string Id = "99138d92-69db-44cb-952a-1cd9ec031e20"; - private const string DefaultNamePrefix = "sectester"; private const string Hostname = "app.brightsec.com"; private readonly IRepeaterBusFactory _busFactory = Substitute.For(); private readonly Configuration _configuration = new(Hostname); - private readonly IRepeaters _repeaters = Substitute.For(); private readonly ILoggerFactory _loggerFactory = Substitute.For(); private readonly IAnsiCodeColorizer _ansiCodeColorizer = Substitute.For(); private readonly RequestRunnerResolver _resolver = Substitute.For(); @@ -18,14 +16,13 @@ public class DefaultRepeaterFactoryTests : IDisposable public DefaultRepeaterFactoryTests() { - _sut = new DefaultRepeaterFactory(_repeaters, _busFactory, _configuration, _loggerFactory, _ansiCodeColorizer, _resolver); + _sut = new DefaultRepeaterFactory(_busFactory, _configuration, _loggerFactory, _ansiCodeColorizer, _resolver); } public void Dispose() { _ansiCodeColorizer.ClearSubstitute(); _busFactory.ClearSubstitute(); - _repeaters.ClearSubstitute(); _loggerFactory.ClearSubstitute(); _resolver.ClearSubstitute(); GC.SuppressFinalize(this); @@ -35,19 +32,14 @@ public void Dispose() public async Task CreateRepeater_CreatesRepeater() { // arrange - _repeaters.CreateRepeater(Arg.Is(s => s.Contains(DefaultNamePrefix))) - .Returns(Id); + var defaultNamePrefix = Dns.GetHostName(); // act await using var repeater = await _sut.CreateRepeater(); // assert repeater.Should().BeOfType(); - repeater.Should().BeEquivalentTo( - new - { - RepeaterId = Id - }); + _busFactory.Received().Create(Arg.Is(s => s.Contains(defaultNamePrefix))); } [Fact] @@ -56,21 +48,14 @@ public async Task CreateRepeater_GivenOptions_CreatesRepeater() // arrange var options = new RepeaterOptions { - NamePrefix = "foo", - Description = "bar" + NamePrefix = "foo" }; - _repeaters.CreateRepeater(Arg.Is(s => s.Contains(options.NamePrefix)), options.Description) - .Returns(Id); // act await using var repeater = await _sut.CreateRepeater(options); // assert repeater.Should().BeOfType(); - repeater.Should().BeEquivalentTo( - new - { - RepeaterId = Id - }); + _busFactory.Received().Create(Arg.Is(s => s.Contains("foo"))); } } diff --git a/test/SecTester.Repeater.Tests/Extensions/ServiceCollectionExtensionsTests.cs b/test/SecTester.Repeater.Tests/Extensions/ServiceCollectionExtensionsTests.cs index 91e4b39..1baa2de 100644 --- a/test/SecTester.Repeater.Tests/Extensions/ServiceCollectionExtensionsTests.cs +++ b/test/SecTester.Repeater.Tests/Extensions/ServiceCollectionExtensionsTests.cs @@ -20,16 +20,6 @@ public void AddSecTesterRepeater_RegistersRepeaterFactory() _sut.Received().AddScoped(); } - [Fact] - public void AddSecTesterRepeater_RegistersRepeaters() - { - // act - _sut.AddSecTesterRepeater(); - - // assert - _sut.Received().AddScoped(); - } - [Fact] public void AddSecTesterRepeater_RegistersTimerProvider() { diff --git a/test/SecTester.Repeater.Tests/RepeaterOptionsTests.cs b/test/SecTester.Repeater.Tests/RepeaterOptionsTests.cs index 2f3aed7..6a2ddf8 100644 --- a/test/SecTester.Repeater.Tests/RepeaterOptionsTests.cs +++ b/test/SecTester.Repeater.Tests/RepeaterOptionsTests.cs @@ -48,7 +48,7 @@ public void RepeaterOptions_NamePrefixIsNotPassed_KeepsDefaultValue() result.Should().BeEquivalentTo( new { - NamePrefix = "sectester" + NamePrefix = System.Net.Dns.GetHostName() }); } } diff --git a/test/SecTester.Repeater.Tests/RepeaterTests.cs b/test/SecTester.Repeater.Tests/RepeaterTests.cs index 8b72118..9f55bbf 100644 --- a/test/SecTester.Repeater.Tests/RepeaterTests.cs +++ b/test/SecTester.Repeater.Tests/RepeaterTests.cs @@ -15,7 +15,7 @@ public class RepeaterTests : IDisposable, IAsyncDisposable public RepeaterTests() { var version = new Version(Version); - _sut = new Repeater(Id, _bus, version, _logger, _ansiCodeColorizer, _resolver); + _sut = new Repeater(_bus, version, _logger, _ansiCodeColorizer, _resolver); } public async ValueTask DisposeAsync() @@ -39,11 +39,14 @@ public void Dispose() [Fact] public async Task Start_DeploysItself() { + // arrange + _bus.Deploy(null, Arg.Any()).Returns(Task.FromResult(Id)); + // act await _sut.Start(); // assert - await _bus.Received().Deploy(Id, Arg.Any()); + _sut.RepeaterId.Should().Be(Id); } [Fact] diff --git a/test/SecTester.Repeater.Tests/SecTester.Repeater.Tests.csproj b/test/SecTester.Repeater.Tests/SecTester.Repeater.Tests.csproj index ad694f3..a525a41 100644 --- a/test/SecTester.Repeater.Tests/SecTester.Repeater.Tests.csproj +++ b/test/SecTester.Repeater.Tests/SecTester.Repeater.Tests.csproj @@ -9,7 +9,6 @@ - diff --git a/test/SecTester.Repeater.Tests/Usings.cs b/test/SecTester.Repeater.Tests/Usings.cs index 6b72f15..5b7390a 100644 --- a/test/SecTester.Repeater.Tests/Usings.cs +++ b/test/SecTester.Repeater.Tests/Usings.cs @@ -23,7 +23,6 @@ global using SecTester.Core.RetryStrategies; global using SecTester.Core.Logger; global using SecTester.Core.Utils; -global using SecTester.Repeater.Api; global using SecTester.Repeater.Bus; global using SecTester.Repeater.Extensions; global using SecTester.Repeater.Runners; diff --git a/test/SecTester.Runner.Tests/SecRunnerTests.cs b/test/SecTester.Runner.Tests/SecRunnerTests.cs index e5d504e..61b0e0a 100644 --- a/test/SecTester.Runner.Tests/SecRunnerTests.cs +++ b/test/SecTester.Runner.Tests/SecRunnerTests.cs @@ -1,7 +1,6 @@ using NSubstitute.ExceptionExtensions; using SecTester.Core; using SecTester.Repeater; -using SecTester.Repeater.Api; namespace SecTester.Runner.Tests; @@ -16,7 +15,6 @@ public class SecRunnerTests private readonly RepeaterOptions _options = new(); private readonly IRepeater _repeater = Substitute.For(); private readonly IRepeaterFactory _repeaterFactory = Substitute.For(); - private readonly IRepeaters _repeatersManager = Substitute.For(); private readonly IScanFactory _scanFactory = Substitute.For(); private readonly ICredentialProvider _credentialProvider = Substitute.For(); @@ -26,7 +24,7 @@ public SecRunnerTests() { _repeater.RepeaterId.Returns(RepeaterId); _repeaterFactory.CreateRepeater(_options).Returns(_repeater); - _sut = new SecRunner(_configuration, _repeaterFactory, _scanFactory, _repeatersManager, _formatter); + _sut = new SecRunner(_configuration, _repeaterFactory, _scanFactory, _formatter); } [Fact] @@ -119,19 +117,6 @@ public async Task Clear_StopRepeater() await _repeater.Received(1).Stop(); } - [Fact] - public async Task Clear_DeletesRepeater() - { - // arrange - await _sut.Init(_options); - - // act - await _sut.Clear(); - - // assert - await _repeatersManager.Received(1).DeleteRepeater(RepeaterId); - } - [Fact] public async Task Clear_RepeaterRemoved_RepeaterIsNotDefined() { @@ -216,19 +201,6 @@ public void CreateScan_NotInitializedYet_ThrowsException() act.Should().Throw().WithMessage("Must be initialized first."); } - [Fact] - public async Task DisposeAsync_RemoveRepeaters() - { - // arrange - await _sut.Init(_options); - - // act - await _sut.DisposeAsync(); - - // assert - await _repeatersManager.Received(1).DeleteRepeater(RepeaterId); - } - [Fact] public async Task DisposeAsync_StopsRepeater() { diff --git a/test/SecTester.Scan.Tests/Usings.cs b/test/SecTester.Scan.Tests/Usings.cs index 94bcba8..1cdced7 100644 --- a/test/SecTester.Scan.Tests/Usings.cs +++ b/test/SecTester.Scan.Tests/Usings.cs @@ -12,10 +12,10 @@ global using NSubstitute.ExceptionExtensions; global using SecTester.Core; global using SecTester.Core.Bus; -global using SecTester.Core.Exceptions; -global using SecTester.Core.Utils; global using SecTester.Core.Dispatchers; +global using SecTester.Core.Exceptions; global using SecTester.Core.Extensions; +global using SecTester.Core.Utils; global using SecTester.Scan.CI; global using SecTester.Scan.Commands; global using SecTester.Scan.Exceptions;