Unit tests for Azure scenarios (#495)

* Test firewall rule handling is able to process through the service layer

* Additional tests for authentication and the resource wrapper code

* Positive test case for CreateFirewallRule

* Fixed copyright and usings
This commit is contained in:
Kevin Cunnane
2017-10-12 17:23:34 -07:00
committed by GitHub
parent 14e3b3a3f6
commit b416951414
14 changed files with 536 additions and 139 deletions

View File

@@ -27,7 +27,7 @@ namespace Microsoft.SqlTools.ResourceProvider.DefaultImpl
"Microsoft.SqlTools.ResourceProvider.DefaultImpl.AzureAuthenticationManager",
1)
]
class AzureAuthenticationManager : ExportableBase, IAzureAuthenticationManager
public class AzureAuthenticationManager : ExportableBase, IAzureAuthenticationManager
{
private Dictionary<string, AzureUserAccount> accountsMap;
private string currentAccountId = null;
@@ -88,38 +88,49 @@ namespace Microsoft.SqlTools.ResourceProvider.DefaultImpl
public AzureUserAccount CreateUserAccount(AccountTokenWrapper accountTokenWrapper)
{
Account account = accountTokenWrapper.Account;
CommonUtil.CheckForNull(accountTokenWrapper.Account, nameof(account));
CommonUtil.CheckForNull(accountTokenWrapper, nameof(accountTokenWrapper));
CommonUtil.CheckForNull(account, nameof(account));
CommonUtil.CheckForNull(account.Key, nameof(account) + ".Key");
CommonUtil.CheckForNull(accountTokenWrapper.SecurityTokenMappings, nameof(account) + ".SecurityTokenMappings");
AzureUserAccount userAccount = new AzureUserAccount();
userAccount.UniqueId = account.Key.AccountId;
userAccount.DisplayInfo = ToDisplayInfo(account);
IList<IAzureTenant> tenants = new List<IAzureTenant>();
foreach (Tenant tenant in account.Properties.Tenants)
{
AccountSecurityToken token;
if (accountTokenWrapper.SecurityTokenMappings.TryGetValue(tenant.Id, out token))
{
AzureTenant azureTenant = new AzureTenant()
{
TenantId = tenant.Id,
AccountDisplayableId = tenant.DisplayName,
Resource = token.Resource,
AccessToken = token.Token,
TokenType = token.TokenType
};
tenants.Add(azureTenant);
}
// else ignore for now as we can't handle a request to get a tenant without an access key
}
userAccount.AllTenants = tenants;
userAccount.NeedsReauthentication = account.IsStale;
userAccount.AllTenants = ProcessTenants(accountTokenWrapper, account);
return userAccount;
}
private static IList<IAzureTenant> ProcessTenants(AccountTokenWrapper accountTokenWrapper, Account account)
{
IList<IAzureTenant> tenants = new List<IAzureTenant>();
if (account.Properties != null && account.Properties.Tenants != null)
{
foreach (Tenant tenant in account.Properties.Tenants)
{
AccountSecurityToken token;
if (accountTokenWrapper.SecurityTokenMappings.TryGetValue(tenant.Id, out token))
{
AzureTenant azureTenant = new AzureTenant()
{
TenantId = tenant.Id,
AccountDisplayableId = tenant.DisplayName,
Resource = token.Resource,
AccessToken = token.Token,
TokenType = token.TokenType
};
tenants.Add(azureTenant);
}
// else ignore for now as we can't handle a request to get a tenant without an access key
}
}
return tenants;
}
private AzureUserAccountDisplayInfo ToDisplayInfo(Account account)
{
return new AzureUserAccountDisplayInfo()
{
AccountDisplayName = account.DisplayInfo.DisplayName,
AccountDisplayName = account.DisplayInfo != null ? account.DisplayInfo.DisplayName : account.Key.AccountId,
ProviderDisplayName = account.Key.ProviderId
};
}

View File

@@ -20,7 +20,6 @@ using Microsoft.Rest;
using System.Globalization;
using Microsoft.Rest.Azure;
using Microsoft.SqlTools.ResourceProvider.Core;
using System.Collections;
using System.Threading;
namespace Microsoft.SqlTools.ResourceProvider.DefaultImpl

View File

@@ -81,7 +81,7 @@ namespace Microsoft.SqlTools.ResourceProvider.DefaultImpl
{
this.resourceGroupName = ParseResourceGroupNameFromId();
}
return this.resourceGroupName;
return this.resourceGroupName ?? string.Empty;
}
set
{

View File

@@ -27,7 +27,7 @@ namespace Microsoft.SqlTools.ResourceProvider.DefaultImpl
Keys.Culture = value;
}
}
public static string FailedToGetAzureDatabasesErrorMessage
{
@@ -35,7 +35,7 @@ namespace Microsoft.SqlTools.ResourceProvider.DefaultImpl
{
return Keys.GetString(Keys.FailedToGetAzureDatabasesErrorMessage);
}
}
}
public static string FailedToGetAzureSubscriptionsErrorMessage
{
@@ -43,7 +43,7 @@ namespace Microsoft.SqlTools.ResourceProvider.DefaultImpl
{
return Keys.GetString(Keys.FailedToGetAzureSubscriptionsErrorMessage);
}
}
}
public static string FailedToGetAzureResourceGroupsErrorMessage
{
@@ -51,7 +51,7 @@ namespace Microsoft.SqlTools.ResourceProvider.DefaultImpl
{
return Keys.GetString(Keys.FailedToGetAzureResourceGroupsErrorMessage);
}
}
}
public static string FailedToGetAzureSqlServersErrorMessage
{
@@ -59,7 +59,7 @@ namespace Microsoft.SqlTools.ResourceProvider.DefaultImpl
{
return Keys.GetString(Keys.FailedToGetAzureSqlServersErrorMessage);
}
}
}
public static string FailedToGetAzureSqlServersWithError
{
@@ -67,7 +67,7 @@ namespace Microsoft.SqlTools.ResourceProvider.DefaultImpl
{
return Keys.GetString(Keys.FailedToGetAzureSqlServersWithError);
}
}
}
public static string FirewallRuleCreationFailed
{
@@ -75,7 +75,7 @@ namespace Microsoft.SqlTools.ResourceProvider.DefaultImpl
{
return Keys.GetString(Keys.FirewallRuleCreationFailed);
}
}
}
public static string FirewallRuleCreationFailedWithError
{
@@ -83,7 +83,7 @@ namespace Microsoft.SqlTools.ResourceProvider.DefaultImpl
{
return Keys.GetString(Keys.FirewallRuleCreationFailedWithError);
}
}
}
public static string AzureSubscriptionFailedErrorMessage
{
@@ -91,7 +91,7 @@ namespace Microsoft.SqlTools.ResourceProvider.DefaultImpl
{
return Keys.GetString(Keys.AzureSubscriptionFailedErrorMessage);
}
}
}
public static string UnsupportedAuthType
{
@@ -99,7 +99,7 @@ namespace Microsoft.SqlTools.ResourceProvider.DefaultImpl
{
return Keys.GetString(Keys.UnsupportedAuthType);
}
}
}
[System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
public class Keys
@@ -107,34 +107,34 @@ namespace Microsoft.SqlTools.ResourceProvider.DefaultImpl
static ResourceManager resourceManager = new ResourceManager("Microsoft.SqlTools.ResourceProvider.DefaultImpl.Localization.SR", typeof(SR).GetTypeInfo().Assembly);
static CultureInfo _culture = null;
public const string FailedToGetAzureDatabasesErrorMessage = "FailedToGetAzureDatabasesErrorMessage";
public const string FailedToGetAzureSubscriptionsErrorMessage = "FailedToGetAzureSubscriptionsErrorMessage";
public const string FailedToGetAzureResourceGroupsErrorMessage = "FailedToGetAzureResourceGroupsErrorMessage";
public const string FailedToGetAzureSqlServersErrorMessage = "FailedToGetAzureSqlServersErrorMessage";
public const string FailedToGetAzureSqlServersWithError = "FailedToGetAzureSqlServersWithError";
public const string FirewallRuleCreationFailed = "FirewallRuleCreationFailed";
public const string FirewallRuleCreationFailedWithError = "FirewallRuleCreationFailedWithError";
public const string AzureSubscriptionFailedErrorMessage = "AzureSubscriptionFailedErrorMessage";
public const string UnsupportedAuthType = "UnsupportedAuthType";
public const string FailedToGetAzureDatabasesErrorMessage = "FailedToGetAzureDatabasesErrorMessage";
public const string FailedToGetAzureSubscriptionsErrorMessage = "FailedToGetAzureSubscriptionsErrorMessage";
public const string FailedToGetAzureResourceGroupsErrorMessage = "FailedToGetAzureResourceGroupsErrorMessage";
public const string FailedToGetAzureSqlServersErrorMessage = "FailedToGetAzureSqlServersErrorMessage";
public const string FailedToGetAzureSqlServersWithError = "FailedToGetAzureSqlServersWithError";
public const string FirewallRuleCreationFailed = "FirewallRuleCreationFailed";
public const string FirewallRuleCreationFailedWithError = "FirewallRuleCreationFailedWithError";
public const string AzureSubscriptionFailedErrorMessage = "AzureSubscriptionFailedErrorMessage";
public const string UnsupportedAuthType = "UnsupportedAuthType";
private Keys()
{ }
@@ -155,7 +155,7 @@ namespace Microsoft.SqlTools.ResourceProvider.DefaultImpl
{
return resourceManager.GetString(key, _culture);
}
}
}
}
}
}
}

View File

@@ -120,37 +120,37 @@
<data name="FailedToGetAzureDatabasesErrorMessage" xml:space="preserve">
<value>An error occurred while getting Azure databases</value>
<comment></comment>
</data>
</data>
<data name="FailedToGetAzureSubscriptionsErrorMessage" xml:space="preserve">
<value>An error occurred while getting Azure subscriptions: {0}</value>
<comment></comment>
</data>
</data>
<data name="FailedToGetAzureResourceGroupsErrorMessage" xml:space="preserve">
<value>An error occurred while getting Azure resource groups: {0}</value>
<comment></comment>
</data>
</data>
<data name="FailedToGetAzureSqlServersErrorMessage" xml:space="preserve">
<value>An error occurred while getting Azure Sql Servers</value>
<comment></comment>
</data>
</data>
<data name="FailedToGetAzureSqlServersWithError" xml:space="preserve">
<value>An error occurred while getting Azure Sql Servers: '{0}'</value>
<comment></comment>
</data>
</data>
<data name="FirewallRuleCreationFailed" xml:space="preserve">
<value>An error occurred while creating a new firewall rule.</value>
<comment></comment>
</data>
</data>
<data name="FirewallRuleCreationFailedWithError" xml:space="preserve">
<value>An error occurred while creating a new firewall rule: '{0}'</value>
<comment></comment>
</data>
</data>
<data name="AzureSubscriptionFailedErrorMessage" xml:space="preserve">
<value>An error occurred while getting Azure subscriptions</value>
<comment></comment>
</data>
</data>
<data name="UnsupportedAuthType" xml:space="preserve">
<value>Unsupported account type '{0}' for this provider</value>
<comment></comment>
</data>
</root>
</data>
</root>