Skip to content

Commit

Permalink
more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gcbeattyAWS committed Oct 30, 2024
1 parent d0d0ca0 commit 6d7bd85
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 15 deletions.
22 changes: 11 additions & 11 deletions src/AWS.Deploy.CLI/CredentialProfileStoreChainWrapper.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

using Amazon.Runtime.CredentialManagement;
using Amazon.Runtime;
using AWS.Deploy.CLI;

namespace AWS.Deploy.CLI
public class CredentialProfileStoreChainWrapper : ICredentialProfileStoreChain
{
public class CredentialProfileStoreChainWrapper : ICredentialProfileStoreChain
private readonly CredentialProfileStoreChain _chain;

public CredentialProfileStoreChainWrapper(CredentialProfileStoreChain? chain = null)
{
private readonly CredentialProfileStoreChain _chain = new CredentialProfileStoreChain();
_chain = chain ?? new CredentialProfileStoreChain();
}

public bool TryGetAWSCredentials(string profileName, out AWSCredentials credentials)
=> _chain.TryGetAWSCredentials(profileName, out credentials);
public bool TryGetAWSCredentials(string profileName, out AWSCredentials credentials)
=> _chain.TryGetAWSCredentials(profileName, out credentials);

public bool TryGetProfile(string profileName, out CredentialProfile profile)
=> _chain.TryGetProfile(profileName, out profile);
}
public bool TryGetProfile(string profileName, out CredentialProfile profile)
=> _chain.TryGetProfile(profileName, out profile);
}
10 changes: 6 additions & 4 deletions src/AWS.Deploy.CLI/SharedCredentialsFileWrapper.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

using Amazon.Runtime.CredentialManagement;
using System.Collections.Generic;

namespace AWS.Deploy.CLI
{
public class SharedCredentialsFileWrapper : ISharedCredentialsFile
{
private readonly SharedCredentialsFile _sharedCredentialsFile = new SharedCredentialsFile();
private readonly SharedCredentialsFile _sharedCredentialsFile;

public SharedCredentialsFileWrapper(SharedCredentialsFile? sharedCredentialsFile = null)
{
_sharedCredentialsFile = sharedCredentialsFile ?? new SharedCredentialsFile();
}

public List<string> ListProfileNames() => _sharedCredentialsFile.ListProfileNames();
}
Expand Down
31 changes: 31 additions & 0 deletions test/AWS.Deploy.CLI.UnitTests/AWSUtilitiesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
using Amazon;
using System.Linq;
using System.Collections.Generic;
using AWS.Deploy.Common;
using AWS.Deploy.CLI.Utilities;

namespace AWS.Deploy.CLI.UnitTests
{
Expand Down Expand Up @@ -297,5 +299,34 @@ public async Task ResolveAWSCredentials_WithNoCredentialsAndNullRegion_PromptsUs
Assert.Null(result.Item2); // Expect the region to be null
_mockConsoleUtilities.Verify(c => c.AskUserToChoose(It.IsAny<List<string>>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Once);
}

[Fact]
public async Task ResolveAWSCredentials_WithNoProfiles_ThrowsNoAWSCredentialsFoundException()
{
// Arrange
var awsUtilities = CreateAWSUtilities();

// Setup FallbackCredentialsFactory to throw an exception
_mockFallbackCredentialsFactory
.Setup(f => f.GetCredentials())
.Throws(new AmazonServiceException("No credentials found"));

// Setup SharedCredentialsFile to return an empty list of profile names
_mockSharedCredentialsFile
.Setup(s => s.ListProfileNames())
.Returns(new List<string>());

// Act & Assert
var exception = await Assert.ThrowsAsync<NoAWSCredentialsFoundException>(
() => awsUtilities.ResolveAWSCredentials(null)
);

// Verify the exception details
Assert.Equal(DeployToolErrorCode.UnableToResolveAWSCredentials, exception.ErrorCode);
Assert.Equal("Unable to resolve AWS credentials to access AWS.", exception.Message);

// Verify that ListProfileNames was called
_mockSharedCredentialsFile.Verify(s => s.ListProfileNames(), Times.Once);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using Xunit;

namespace AWS.Deploy.CLI.UnitTests
{
public class CredentialProfileStoreChainWrapperTests
{
[Fact]
public void TryGetAWSCredentials_ReturnsExpectedResult()
{
// Arrange
var wrapper = new CredentialProfileStoreChainWrapper();
var profileName = "non-existent-profile"; // Use a profile name that's unlikely to exist

// Act
bool result = wrapper.TryGetAWSCredentials(profileName, out var credentials);

// Assert
Assert.False(result);
Assert.Null(credentials);
}

[Fact]
public void TryGetProfile_ReturnsExpectedResult()
{
// Arrange
var wrapper = new CredentialProfileStoreChainWrapper();
var profileName = "non-existent-profile"; // Use a profile name that's unlikely to exist

// Act
bool result = wrapper.TryGetProfile(profileName, out var profile);

// Assert
Assert.False(result);
Assert.Null(profile);
}

[Fact]
public void Constructor_CreatesNonNullWrapper()
{
// Arrange & Act
var wrapper = new CredentialProfileStoreChainWrapper();

// Assert
Assert.NotNull(wrapper);
}
}
}
47 changes: 47 additions & 0 deletions test/AWS.Deploy.CLI.UnitTests/SharedCredentialsFileWrapperTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using Amazon.Runtime.CredentialManagement;
using Xunit;

namespace AWS.Deploy.CLI.UnitTests
{
public class SharedCredentialsFileWrapperTests
{
[Fact]
public void ListProfileNames_ReturnsNonNullList()
{
// Arrange
var wrapper = new SharedCredentialsFileWrapper();

// Act
var result = wrapper.ListProfileNames();

// Assert
Assert.NotNull(result);
// The list might be empty depending on the environment, but it should never be null
}

[Fact]
public void Constructor_CreatesNonNullWrapper()
{
// Arrange & Act
var wrapper = new SharedCredentialsFileWrapper();

// Assert
Assert.NotNull(wrapper);
}

[Fact]
public void ListProfileNames_ReturnsListOfStrings()
{
// Arrange
var wrapper = new SharedCredentialsFileWrapper();

// Act
var result = wrapper.ListProfileNames();

// Assert
Assert.IsType<List<string>>(result);
}
}
}

0 comments on commit 6d7bd85

Please sign in to comment.