mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-02-15 18:47:43 -05:00
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:
@@ -0,0 +1,113 @@
|
||||
//
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
//
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.SqlTools.Extensibility;
|
||||
using Microsoft.SqlTools.ResourceProvider.Core;
|
||||
using Microsoft.SqlTools.ResourceProvider.Core.Authentication;
|
||||
using Microsoft.SqlTools.ResourceProvider.Core.Contracts;
|
||||
using Microsoft.SqlTools.ResourceProvider.DefaultImpl;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.UnitTests.ResourceProvider.Azure
|
||||
{
|
||||
public class AzureAuthenticationManagerTest
|
||||
{
|
||||
private Mock<IAzureResourceManager> resourceManagerMock;
|
||||
private RegisteredServiceProvider serviceProvider;
|
||||
|
||||
public AzureAuthenticationManagerTest()
|
||||
{
|
||||
resourceManagerMock = new Mock<IAzureResourceManager>();
|
||||
serviceProvider = new RegisteredServiceProvider();
|
||||
serviceProvider.RegisterSingleService<IAzureResourceManager>(resourceManagerMock.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CurrentUserShouldBeNullWhenUserIsNotSignedIn()
|
||||
{
|
||||
IAzureAuthenticationManager accountManager = await CreateAccountManager(null, null);
|
||||
Assert.Null(await accountManager.GetCurrentAccountAsync());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetSubscriptionShouldReturnEmptyWhenUserIsNotSignedIn()
|
||||
{
|
||||
IAzureAuthenticationManager accountManager = await CreateAccountManager(null, null);
|
||||
IEnumerable<IAzureUserAccountSubscriptionContext> result =
|
||||
await accountManager.GetSelectedSubscriptionsAsync();
|
||||
Assert.False(result.Any());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetSubscriptionShouldThrowWhenUserNeedsAuthentication()
|
||||
{
|
||||
var currentUserAccount = CreateAccount();
|
||||
currentUserAccount.Account.IsStale = true;
|
||||
IAzureAuthenticationManager accountManager = await CreateAccountManager(currentUserAccount, null);
|
||||
await Assert.ThrowsAsync<UserNeedsAuthenticationException>(() => accountManager.GetSelectedSubscriptionsAsync());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetSubscriptionShouldThrowIfFailed()
|
||||
{
|
||||
var currentUserAccount = CreateAccount();
|
||||
IAzureAuthenticationManager accountManager = await CreateAccountManager(currentUserAccount, null, true);
|
||||
await Assert.ThrowsAsync<ServiceFailedException>(() => accountManager.GetSelectedSubscriptionsAsync());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetSubscriptionShouldReturnTheListSuccessfully()
|
||||
{
|
||||
List<IAzureUserAccountSubscriptionContext> subscriptions = new List<IAzureUserAccountSubscriptionContext> {
|
||||
new Mock<IAzureUserAccountSubscriptionContext>().Object
|
||||
};
|
||||
var currentUserAccount = CreateAccount();
|
||||
IAzureAuthenticationManager accountManager = await CreateAccountManager(currentUserAccount, subscriptions, false);
|
||||
IEnumerable<IAzureUserAccountSubscriptionContext> result =
|
||||
await accountManager.GetSelectedSubscriptionsAsync();
|
||||
Assert.True(result.Any());
|
||||
}
|
||||
|
||||
private AccountTokenWrapper CreateAccount(bool needsReauthentication = false)
|
||||
{
|
||||
return new AccountTokenWrapper(new Account()
|
||||
{
|
||||
Key = new AccountKey()
|
||||
{
|
||||
AccountId = "MyAccount",
|
||||
ProviderId = "MSSQL"
|
||||
},
|
||||
IsStale = needsReauthentication
|
||||
},
|
||||
new Dictionary<string, AccountSecurityToken>());
|
||||
}
|
||||
private async Task<AzureAuthenticationManager> CreateAccountManager(AccountTokenWrapper currentAccount,
|
||||
IEnumerable<IAzureUserAccountSubscriptionContext> subscriptions, bool shouldFail = false)
|
||||
{
|
||||
AzureAuthenticationManager azureAuthenticationManager = new AzureAuthenticationManager();
|
||||
azureAuthenticationManager.SetServiceProvider(serviceProvider);
|
||||
if (currentAccount != null)
|
||||
{
|
||||
await azureAuthenticationManager.SetCurrentAccountAsync(currentAccount);
|
||||
}
|
||||
|
||||
if (!shouldFail)
|
||||
{
|
||||
resourceManagerMock.Setup(x => x.GetSubscriptionContextsAsync(It.IsAny<IAzureUserAccount>())).Returns(Task.FromResult(subscriptions));
|
||||
}
|
||||
else
|
||||
{
|
||||
resourceManagerMock.Setup(x => x.GetSubscriptionContextsAsync(It.IsAny<IAzureUserAccount>())).Throws(new Exception());
|
||||
}
|
||||
|
||||
return azureAuthenticationManager;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
//
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
//
|
||||
|
||||
using Microsoft.Azure.Management.Sql.Models;
|
||||
using Microsoft.SqlTools.ResourceProvider.DefaultImpl;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.UnitTests.ResourceProvider.Azure
|
||||
{
|
||||
public class AzureResourceWrapperTest
|
||||
{
|
||||
[Fact]
|
||||
public void ShouldParseResourceGroupFromId()
|
||||
{
|
||||
// Given a resource with a known resource group
|
||||
TrackedResource trackedResource = CreateMockResource(
|
||||
"/subscriptions/aaaaaaaa-1234-cccc-dddd-a1234v12c23/resourceGroups/myresourcegroup/providers/Microsoft.Sql/servers/my-server",
|
||||
"my-server",
|
||||
"Microsoft.Sql");
|
||||
|
||||
// When I get the resource group name
|
||||
AzureResourceWrapper resource = new AzureResourceWrapper(trackedResource);
|
||||
string rgName = resource.ResourceGroupName;
|
||||
|
||||
// then I get it as expected
|
||||
Assert.Equal("myresourcegroup", rgName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ShouldHandleMissingResourceGroup()
|
||||
{
|
||||
// Given a resource without resource group in the ID
|
||||
TrackedResource trackedResource = CreateMockResource(
|
||||
"/subscriptions/aaaaaaaa-1234-cccc-dddd-a1234v12c23",
|
||||
"my-server",
|
||||
"Microsoft.Sql");
|
||||
|
||||
// When I get the resource group name
|
||||
AzureResourceWrapper resource = new AzureResourceWrapper(trackedResource);
|
||||
string rgName = resource.ResourceGroupName;
|
||||
|
||||
// then I get string.Empty
|
||||
Assert.Equal(string.Empty, rgName);
|
||||
}
|
||||
|
||||
private TrackedResource CreateMockResource(string id = null, string name = null, string type = null)
|
||||
{
|
||||
return new TrackedResource("Somewhere", id, name, type);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -45,7 +45,7 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.ResourceProvider.Azure
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetShouldReturnEmptyGivenNotSubscriptionFound()
|
||||
public async Task GetShouldReturnEmptyGivenNoSubscriptionFound()
|
||||
{
|
||||
Dictionary<string, List<string>> subscriptionToDatabaseMap = new Dictionary<string, List<string>>();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user