using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Amazon.Runtime;
using Amazon.Runtime.CredentialManagement;
using Amazon.Runtime.Credentials.Internal;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
namespace AWSSDK.UnitTests.Runtime
{
///
/// Tests
///
[TestClass]
public class ProfileTokenProviderTests
{
[TestMethod]
[TestCategory("UnitTest")]
[TestCategory("Runtime")]
[DataTestMethod]
[DynamicData(nameof(TestCases), DynamicDataSourceType.Method)]
public void SharedProfileConfigurationAndResolutionTests(TestCase testCase)
{
using (var fixture = testCase.BuildTestFixture())
{
var sharedConfigFile = fixture.CredentialsFile;
var profileName = testCase.Config.GetPrimaryProfileName();
var mockSSOTokenManger = new Mock();
mockSSOTokenManger
.Setup(x => x.GetToken(It.IsAny()))
.Returns(new SsoToken());
var mockSSOTokenProviderFactory = new Mock();
mockSSOTokenProviderFactory
.Setup(x => x.Build(It.Is(p => p.Name == profileName)))
.Returns(
new SSOTokenProvider(
mockSSOTokenManger.Object,
"session",
"startUrl",
"region"));
var provider = new ProfileTokenProvider(
mockSSOTokenProviderFactory.Object,
sharedConfigFile,
profileName);
AWSToken token = null;
bool success = false;
Exception exception = null;
try
{
success = provider.TryResolveToken(out token);
}
catch (Exception e)
{
exception = e;
}
Assert.AreEqual(testCase.Resolves, success);
if (null == testCase.AssertException)
{
Assert.IsNull(exception, $"Unexpected exception was thrown: {exception?.Message}");
}
else
{
Assert.IsNotNull(exception);
testCase.AssertException(exception);
}
}
}
[TestMethod]
[TestCategory("UnitTest")]
[TestCategory("Runtime")]
[DataTestMethod]
[DynamicData(nameof(TestCases), DynamicDataSourceType.Method)]
public async Task SharedProfileConfigurationAndResolutionTestsAsync(TestCase testCase)
{
using (var fixture = testCase.BuildTestFixture())
{
var sharedConfigFile = fixture.CredentialsFile;
var profileName = testCase.Config.GetPrimaryProfileName();
var mockSSOTokenManger = new Mock();
mockSSOTokenManger
.Setup(x =>
x.GetTokenAsync(
It.IsAny(),
It.IsAny()))
.Returns(Task.FromResult(new SsoToken()));
var mockSSOTokenProviderFactory = new Mock();
mockSSOTokenProviderFactory
.Setup(x => x.Build(It.Is(p => p.Name == profileName)))
.Returns(
new SSOTokenProvider(
mockSSOTokenManger.Object,
"session",
"startUrl",
"region"));
var provider = new ProfileTokenProvider(
mockSSOTokenProviderFactory.Object,
sharedConfigFile,
profileName);
AWSToken token = null;
bool success = false;
Exception exception = null;
try
{
var result = await provider.TryResolveTokenAsync();
success = result.Success;
token = result.Value;
}
catch (Exception e)
{
exception = e;
}
Assert.AreEqual(testCase.Resolves, success);
if (null == testCase.AssertException)
{
Assert.IsNull(exception, $"Unexpected exception was thrown: {exception?.Message}");
}
else
{
Assert.IsNotNull(exception);
testCase.AssertException(exception);
}
}
}
public static IEnumerable