Files
sqltoolsservice/test/Microsoft.SqlTools.ServiceLayer.UnitTests/ResourceProvider/Azure/AzureAuthenticationManagerTest.cs
David Shiflet 839acf67cd Convert most tools service tests to nunit (#1037)
* Remove xunit dependency from testdriver

* swap expected/actual as needed

* Convert Test.Common to nunit

* port hosting unit tests to nunit

* port batchparser integration tests to nunit

* port testdriver.tests to nunit

* fix target to copy dependency

* port servicelayer unittests to nunit

* more unit test fixes

* port integration tests to nunit

* fix test method type

* try using latest windows build for PRs

* reduce test memory use
2020-08-05 13:43:14 -04:00

114 lines
4.7 KiB
C#

//
// 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 NUnit.Framework;
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);
}
[Test]
public async Task CurrentUserShouldBeNullWhenUserIsNotSignedIn()
{
IAzureAuthenticationManager accountManager = await CreateAccountManager(null, null);
Assert.Null(await accountManager.GetCurrentAccountAsync());
}
[Test]
public async Task GetSubscriptionShouldReturnEmptyWhenUserIsNotSignedIn()
{
IAzureAuthenticationManager accountManager = await CreateAccountManager(null, null);
IEnumerable<IAzureUserAccountSubscriptionContext> result =
await accountManager.GetSelectedSubscriptionsAsync();
Assert.False(result.Any());
}
[Test]
public async Task GetSubscriptionShouldThrowWhenUserNeedsAuthentication()
{
var currentUserAccount = CreateAccount();
currentUserAccount.Account.IsStale = true;
IAzureAuthenticationManager accountManager = await CreateAccountManager(currentUserAccount, null);
Assert.ThrowsAsync<ExpiredTokenException>(() => accountManager.GetSelectedSubscriptionsAsync());
}
[Test]
public async Task GetSubscriptionShouldThrowIfFailed()
{
var currentUserAccount = CreateAccount();
IAzureAuthenticationManager accountManager = await CreateAccountManager(currentUserAccount, null, true);
Assert.ThrowsAsync<ServiceFailedException>(() => accountManager.GetSelectedSubscriptionsAsync());
}
[Test]
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;
}
}
}