diff --git a/src/AWS.Deploy.CLI/AWSUtilities.cs b/src/AWS.Deploy.CLI/AWSUtilities.cs index 04b05fe8..d000415d 100644 --- a/src/AWS.Deploy.CLI/AWSUtilities.cs +++ b/src/AWS.Deploy.CLI/AWSUtilities.cs @@ -69,7 +69,7 @@ public AWSUtilities( { _toolInteractiveService.WriteLine($"Configuring AWS Credentials from Profile {profileName}."); chain.TryGetProfile(profileName, out var profile); - return Tuple.Create(profileCredentials, profile.Region.SystemName); + return Tuple.Create(profileCredentials, profile.Region?.SystemName); } else { @@ -107,7 +107,7 @@ public AWSUtilities( (await CanLoadCredentials(selectedProfileCredentials))) { chain.TryGetProfile(selectedProfileName, out var profile); - return Tuple.Create(selectedProfileCredentials, profile.Region.SystemName); + return Tuple.Create(selectedProfileCredentials, profile.Region?.SystemName); } throw new NoAWSCredentialsFoundException(DeployToolErrorCode.UnableToCreateAWSCredentials, $"Unable to create AWS credentials for profile {selectedProfileName}."); diff --git a/test/AWS.Deploy.CLI.UnitTests/AWSUtilitiesTests.cs b/test/AWS.Deploy.CLI.UnitTests/AWSUtilitiesTests.cs index 52758b9e..8d36818d 100644 --- a/test/AWS.Deploy.CLI.UnitTests/AWSUtilitiesTests.cs +++ b/test/AWS.Deploy.CLI.UnitTests/AWSUtilitiesTests.cs @@ -194,6 +194,105 @@ public async Task ResolveAWSCredentials_WithNoCredentials_PromptsUserToChoosePro _mockConsoleUtilities.Verify(c => c.AskUserToChoose(It.IsAny>(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); } - } + [Fact] + public async Task ResolveAWSCredentials_WithValidProfileNameButNullRegion_ReturnsCredentialsWithNullRegion() + { + // Arrange + var awsUtilities = CreateAWSUtilities(); + var profileName = "valid-profile-null-region"; + var mockCredentials = new Mock(); + var mockProfile = new CredentialProfile(profileName, new CredentialProfileOptions()) + { + Region = null // Set the region to null + }; + + _mockCredentialProfileStoreChain + .Setup(c => c.TryGetAWSCredentials(It.IsAny(), out It.Ref.IsAny)) + .Callback(new CredentialsCallback((string name, out AWSCredentials creds) => + { + creds = mockCredentials.Object; + })) + .Returns(new CredentialsReturns((string name, out AWSCredentials creds) => + { + creds = mockCredentials.Object; + return true; + })); + _mockCredentialProfileStoreChain + .Setup(c => c.TryGetProfile(It.IsAny(), out It.Ref.IsAny)) + .Callback(new ProfileCallback((string name, out CredentialProfile profile) => + { + profile = mockProfile; + })) + .Returns(new ProfileReturns((string name, out CredentialProfile profile) => + { + profile = mockProfile; + return true; + })); + + // Act + var result = await awsUtilities.ResolveAWSCredentials(profileName); + + // Assert + Assert.NotNull(result); + Assert.Equal(mockCredentials.Object, result.Item1); + Assert.Null(result.Item2); // Expect the region to be null + } + + [Fact] + public async Task ResolveAWSCredentials_WithNoCredentialsAndNullRegion_PromptsUserToChooseProfile() + { + // Arrange + var awsUtilities = CreateAWSUtilities(); + var profileNames = new List { "profile1", "profile2" }; + var selectedProfileName = "profile1"; + var mockCredentials = new Mock(); + var mockProfile = new CredentialProfile(selectedProfileName, new CredentialProfileOptions()) + { + Region = null // Set the region to null + }; + + _mockFallbackCredentialsFactory + .Setup(f => f.GetCredentials()) + .Throws(new AmazonServiceException("No credentials found")); + + _mockSharedCredentialsFile + .Setup(s => s.ListProfileNames()) + .Returns(profileNames); + + _mockConsoleUtilities + .Setup(c => c.AskUserToChoose( + It.Is>(list => list.SequenceEqual(profileNames)), + It.Is(s => s == "Select AWS Credentials Profile"), + It.IsAny(), + It.IsAny() + )) + .Returns(selectedProfileName); + + _mockCredentialProfileStoreChain + .Setup(c => c.TryGetAWSCredentials(It.IsAny(), out It.Ref.IsAny)) + .Returns(new CredentialsReturns((string name, out AWSCredentials creds) => + { + creds = mockCredentials.Object; + return true; + })); + + _mockCredentialProfileStoreChain + .Setup(c => c.TryGetProfile(It.IsAny(), out It.Ref.IsAny)) + .Returns(new ProfileReturns((string name, out CredentialProfile profile) => + { + profile = mockProfile; + return true; + })); + + // Act + var result = await awsUtilities.ResolveAWSCredentials(null); + + // Assert + Assert.NotNull(result); + Assert.Equal(mockCredentials.Object, result.Item1); + 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); + } + } }