Skip to content

Commit

Permalink
fix(runner): add ability to load credentials in Create method
Browse files Browse the repository at this point in the history
closes #139
  • Loading branch information
ostridm committed Dec 15, 2022
1 parent 905ccbd commit 8ac37ca
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/SecTester.Runner/SecRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,13 @@ public async ValueTask DisposeAsync()
GC.SuppressFinalize(this);
}

public static SecRunner Create(Configuration configuration)
public static async Task<SecRunner> Create(Configuration configuration)
{
if (configuration.Credentials is null)
{
await configuration.LoadCredentials().ConfigureAwait(false);
}

var collection = new ServiceCollection()
.AddSecTesterConfig(configuration)
.AddSecTesterBus()
Expand Down
35 changes: 34 additions & 1 deletion test/SecTester.Runner.Tests/SecRunnerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public class SecRunnerTests
private readonly IRepeaterFactory _repeaterFactory = Substitute.For<IRepeaterFactory>();
private readonly IRepeaters _repeatersManager = Substitute.For<IRepeaters>();
private readonly IScanFactory _scanFactory = Substitute.For<IScanFactory>();
private readonly ICredentialProvider _credentialProvider = Substitute.For<ICredentialProvider>();

private readonly SecRunner _sut;

public SecRunnerTests()
Expand All @@ -31,12 +33,43 @@ public SecRunnerTests()
public async Task Create_CreatesCompositeRoot()
{
// act
await using var secRunner = SecRunner.Create(_configuration);
await using var secRunner = await SecRunner.Create(_configuration);

// assert
secRunner.Should().BeOfType<SecRunner>();
}

[Fact]
public async Task Create_AbleToLoadCredentials_CreatesCompositeRoot()
{
// arrange
var configuration = new Configuration(Hostname,
credentialProviders: new List<ICredentialProvider> { _credentialProvider });

_credentialProvider.Get().Returns(new Credentials(Token));

// act
await using var secRunner = await SecRunner.Create(configuration);

// assert
secRunner.Should().BeOfType<SecRunner>();
await _credentialProvider.Received(1).Get();
}

[Fact]
public async Task Create_CouldNotLoadCredentials_ThrowsError()
{
// arrange
var configuration = new Configuration(Hostname,
credentialProviders: new List<ICredentialProvider> { _credentialProvider });

// act
var act = () => SecRunner.Create(configuration);

// assert
await act.Should().ThrowAsync<InvalidOperationException>("Could not load credentials from any providers");
}

[Fact]
public async Task Init_StartsRepeater()
{
Expand Down

0 comments on commit 8ac37ca

Please sign in to comment.