From 6d7bd85debcf7aab3e80c1eeac249441be04f1c0 Mon Sep 17 00:00:00 2001 From: Garrett Beatty Date: Wed, 30 Oct 2024 16:48:12 -0400 Subject: [PATCH] more unit tests --- .../CredentialProfileStoreChainWrapper.cs | 22 ++++----- .../SharedCredentialsFileWrapper.cs | 10 ++-- .../AWSUtilitiesTests.cs | 31 ++++++++++++ ...CredentialProfileStoreChainWrapperTests.cs | 47 +++++++++++++++++++ .../SharedCredentialsFileWrapperTests.cs | 47 +++++++++++++++++++ 5 files changed, 142 insertions(+), 15 deletions(-) create mode 100644 test/AWS.Deploy.CLI.UnitTests/CredentialProfileStoreChainWrapperTests.cs create mode 100644 test/AWS.Deploy.CLI.UnitTests/SharedCredentialsFileWrapperTests.cs diff --git a/src/AWS.Deploy.CLI/CredentialProfileStoreChainWrapper.cs b/src/AWS.Deploy.CLI/CredentialProfileStoreChainWrapper.cs index c9de0231..743d49ec 100644 --- a/src/AWS.Deploy.CLI/CredentialProfileStoreChainWrapper.cs +++ b/src/AWS.Deploy.CLI/CredentialProfileStoreChainWrapper.cs @@ -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); } diff --git a/src/AWS.Deploy.CLI/SharedCredentialsFileWrapper.cs b/src/AWS.Deploy.CLI/SharedCredentialsFileWrapper.cs index 882b2910..a2782cf7 100644 --- a/src/AWS.Deploy.CLI/SharedCredentialsFileWrapper.cs +++ b/src/AWS.Deploy.CLI/SharedCredentialsFileWrapper.cs @@ -1,6 +1,3 @@ -// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -// SPDX-License-Identifier: Apache-2.0 - using Amazon.Runtime.CredentialManagement; using System.Collections.Generic; @@ -8,7 +5,12 @@ 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 ListProfileNames() => _sharedCredentialsFile.ListProfileNames(); } diff --git a/test/AWS.Deploy.CLI.UnitTests/AWSUtilitiesTests.cs b/test/AWS.Deploy.CLI.UnitTests/AWSUtilitiesTests.cs index 83946a8b..f0c32f55 100644 --- a/test/AWS.Deploy.CLI.UnitTests/AWSUtilitiesTests.cs +++ b/test/AWS.Deploy.CLI.UnitTests/AWSUtilitiesTests.cs @@ -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 { @@ -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>(), It.IsAny(), It.IsAny(), It.IsAny()), 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()); + + // Act & Assert + var exception = await Assert.ThrowsAsync( + () => 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); + } } } diff --git a/test/AWS.Deploy.CLI.UnitTests/CredentialProfileStoreChainWrapperTests.cs b/test/AWS.Deploy.CLI.UnitTests/CredentialProfileStoreChainWrapperTests.cs new file mode 100644 index 00000000..79346b83 --- /dev/null +++ b/test/AWS.Deploy.CLI.UnitTests/CredentialProfileStoreChainWrapperTests.cs @@ -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); + } + } +} diff --git a/test/AWS.Deploy.CLI.UnitTests/SharedCredentialsFileWrapperTests.cs b/test/AWS.Deploy.CLI.UnitTests/SharedCredentialsFileWrapperTests.cs new file mode 100644 index 00000000..3bcbd7eb --- /dev/null +++ b/test/AWS.Deploy.CLI.UnitTests/SharedCredentialsFileWrapperTests.cs @@ -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>(result); + } + } +}