Skip to content

Commit

Permalink
Add null safety check and more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gcbeattyAWS committed Oct 30, 2024
1 parent a8b0275 commit 95dcd6c
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/AWS.Deploy.CLI/AWSUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public AWSUtilities(
{
_toolInteractiveService.WriteLine($"Configuring AWS Credentials from Profile {profileName}.");
chain.TryGetProfile(profileName, out var profile);
return Tuple.Create<AWSCredentials, string?>(profileCredentials, profile.Region.SystemName);
return Tuple.Create<AWSCredentials, string?>(profileCredentials, profile.Region?.SystemName);
}
else
{
Expand Down Expand Up @@ -107,7 +107,7 @@ public AWSUtilities(
(await CanLoadCredentials(selectedProfileCredentials)))
{
chain.TryGetProfile(selectedProfileName, out var profile);
return Tuple.Create<AWSCredentials, string?>(selectedProfileCredentials, profile.Region.SystemName);
return Tuple.Create<AWSCredentials, string?>(selectedProfileCredentials, profile.Region?.SystemName);
}

throw new NoAWSCredentialsFoundException(DeployToolErrorCode.UnableToCreateAWSCredentials, $"Unable to create AWS credentials for profile {selectedProfileName}.");
Expand Down
101 changes: 100 additions & 1 deletion test/AWS.Deploy.CLI.UnitTests/AWSUtilitiesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,105 @@ public async Task ResolveAWSCredentials_WithNoCredentials_PromptsUserToChoosePro
_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_WithValidProfileNameButNullRegion_ReturnsCredentialsWithNullRegion()
{
// Arrange
var awsUtilities = CreateAWSUtilities();
var profileName = "valid-profile-null-region";
var mockCredentials = new Mock<AWSCredentials>();
var mockProfile = new CredentialProfile(profileName, new CredentialProfileOptions())
{
Region = null // Set the region to null
};

_mockCredentialProfileStoreChain
.Setup(c => c.TryGetAWSCredentials(It.IsAny<string>(), out It.Ref<AWSCredentials>.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<string>(), out It.Ref<CredentialProfile>.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<string> { "profile1", "profile2" };
var selectedProfileName = "profile1";
var mockCredentials = new Mock<AWSCredentials>();
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<string>>(list => list.SequenceEqual(profileNames)),
It.Is<string>(s => s == "Select AWS Credentials Profile"),
It.IsAny<string>(),
It.IsAny<string>()
))
.Returns(selectedProfileName);

_mockCredentialProfileStoreChain
.Setup(c => c.TryGetAWSCredentials(It.IsAny<string>(), out It.Ref<AWSCredentials>.IsAny))
.Returns(new CredentialsReturns((string name, out AWSCredentials creds) =>
{
creds = mockCredentials.Object;
return true;
}));

_mockCredentialProfileStoreChain
.Setup(c => c.TryGetProfile(It.IsAny<string>(), out It.Ref<CredentialProfile>.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<List<string>>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()), Times.Once);
}
}
}

0 comments on commit 95dcd6c

Please sign in to comment.