Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🕸️ [3/n] Null Accounts: Update Web to use CachedAuth #278

Merged
merged 5 commits into from
Apr 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 43 additions & 52 deletions src/MSALWrapper.Test/AuthFlow/WebTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,22 @@ namespace Microsoft.Authentication.MSALWrapper.Test
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

using FluentAssertions;

using Microsoft.Authentication.MSALWrapper;
using Microsoft.Authentication.MSALWrapper.AuthFlow;
using Microsoft.Authentication.TestHelper;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Identity.Client;
using Microsoft.IdentityModel.JsonWebTokens;

using Moq;

using NLog.Extensions.Logging;
using NLog.Targets;

using NUnit.Framework;

internal class WebTest
Expand Down Expand Up @@ -57,21 +62,25 @@ public void Setup()
this.tokenResult = new TokenResult(new JsonWebToken(TokenResultTest.FakeToken), Guid.NewGuid());
}

[TearDown]
public void TearDown()
{
this.pcaWrapperMock.VerifyAll();
}

public AuthFlow.Web Subject() => new AuthFlow.Web(this.logger, ClientId, TenantId, this.scopes, pcaWrapper: this.pcaWrapperMock.Object, promptHint: this.promptHint);

[Test]
public async Task WebAuthFlow_CachedToken()
{
this.SilentAuthResult();

this.MockAccount();
this.SilentAuthResult();

// Act
AuthFlow.Web web = this.Subject();
var authFlowResult = await web.GetTokenAsync();

// Assert
this.pcaWrapperMock.VerifyAll();
authFlowResult.TokenResult.Should().Be(this.tokenResult);
authFlowResult.TokenResult.IsSilent.Should().BeTrue();
authFlowResult.Errors.Should().BeEmpty();
Expand All @@ -82,53 +91,51 @@ public async Task WebAuthFlow_CachedToken()
public async Task WebAuthFlow_NoAccount()
{
this.pcaWrapperMock.Setup(pca => pca.TryToGetCachedAccountAsync(It.IsAny<string>())).ReturnsAsync((IAccount)null);
this.SilentAuthUIRequired();
this.SetupWithPromptHint();
this.InteractiveAuthResult();

// Act
AuthFlow.Web deviceCode = this.Subject();
var authFlowResult = await deviceCode.GetTokenAsync();

// Assert
this.pcaWrapperMock.VerifyAll();
authFlowResult.TokenResult.Should().Be(this.tokenResult);
authFlowResult.TokenResult.IsSilent.Should().BeFalse();
authFlowResult.Errors.Should().HaveCount(1);
authFlowResult.Errors.Should().BeEmpty();
authFlowResult.AuthFlowName.Should().Be("web");
}

[Test]
public async Task WebAuthFlow_GetTokenSilent_ReturnsNull()
{
this.SilentAuthReturnsNull();

this.MockAccount();
this.SilentAuthReturnsNull();
this.SetupWithPromptHint();
this.InteractiveAuthResult();

// Act
AuthFlow.Web web = this.Subject();
var authFlowResult = await web.GetTokenAsync();

// Assert
this.pcaWrapperMock.VerifyAll();
authFlowResult.TokenResult.Should().Be(null);
authFlowResult.TokenResult.Should().Be(this.tokenResult);
authFlowResult.Errors.Should().BeEmpty();
authFlowResult.AuthFlowName.Should().Be("web");
}

[Test]
public async Task WebAuthFlow_MsalUIException()
{
this.MockAccount();
this.SilentAuthUIRequired();
this.SetupWithPromptHint();
this.InteractiveAuthResult();

this.MockAccount();

// Act
AuthFlow.Web web = this.Subject();
var authFlowResult = await web.GetTokenAsync();

// Assert
this.pcaWrapperMock.VerifyAll();
authFlowResult.TokenResult.Should().Be(this.tokenResult);
authFlowResult.TokenResult.IsSilent.Should().BeFalse();
authFlowResult.Errors.Should().HaveCount(1);
Expand All @@ -139,17 +146,16 @@ public async Task WebAuthFlow_MsalUIException()
[Test]
public async Task WebAuthFlow_MsalUIException_InteractiveAuthResultReturnsNullWithoutClaims()
{
this.MockAccount();
this.SilentAuthUIRequired();
this.SetupWithPromptHint();
this.InteractiveAuthResultReturnsNullWithoutClaims();

this.MockAccount();

// Act
AuthFlow.Web web = this.Subject();
var authFlowResult = await web.GetTokenAsync();

// Assert
this.pcaWrapperMock.VerifyAll();
authFlowResult.TokenResult.Should().Be(null);
authFlowResult.Errors.Should().HaveCount(1);
authFlowResult.Errors[0].Should().BeOfType(typeof(MsalUiRequiredException));
Expand All @@ -172,9 +178,6 @@ public void WebAuthFlow_General_Exceptions_Are_ReThrown()

// Assert
subject.Should().ThrowExactlyAsync<Exception>().WithMessage(message);

// This VerifyAll must come after the assert, since the assert is what execute the lambda
this.pcaWrapperMock.VerifyAll();
}

[Test]
Expand All @@ -190,7 +193,6 @@ public async Task WebAuthFlow_GetTokenSilent_MsalServiceException()

// Assert - this method should not throw for known types of excpeptions, instead return null, so
// our caller can retry auth another way.
this.pcaWrapperMock.VerifyAll();
authFlowResult.TokenResult.Should().Be(null);
authFlowResult.Errors.Should().HaveCount(1);
authFlowResult.Errors[0].Should().BeOfType(typeof(MsalServiceException));
Expand All @@ -200,36 +202,34 @@ public async Task WebAuthFlow_GetTokenSilent_MsalServiceException()
[Test]
public async Task WebAuthFlow_GetTokenSilent_OperationCanceledException()
{
this.SilentAuthTimeout();

this.MockAccount();
this.SilentAuthTimeout();
this.SetupWithPromptHint();
this.InteractiveAuthResult();

// Act
AuthFlow.Web web = this.Subject();
var authFlowResult = await web.GetTokenAsync();

// Assert
this.pcaWrapperMock.VerifyAll();
authFlowResult.TokenResult.Should().Be(null);
authFlowResult.TokenResult.Should().Be(this.tokenResult);
authFlowResult.Errors.Should().HaveCount(1);
authFlowResult.Errors[0].Should().BeOfType(typeof(AuthenticationTimeoutException));
authFlowResult.Errors[0].Message.Should().Be("Get Token Silent timed out after 00:00:15");
authFlowResult.Errors[0].Message.Should().Be("Get Token Silent timed out after 00:00:30");
authFlowResult.AuthFlowName.Should().Be("web");
}

[Test]
public async Task WebAuthFlow_GetTokenSilent_MsalClientException()
{
this.SilentAuthClientException();

this.MockAccount();
this.SilentAuthClientException();

// Act
AuthFlow.Web web = this.Subject();
var authFlowResult = await web.GetTokenAsync();

// Assert
this.pcaWrapperMock.VerifyAll();
authFlowResult.TokenResult.Should().Be(null);
authFlowResult.Errors.Should().HaveCount(1);
authFlowResult.Errors[0].Should().BeOfType(typeof(MsalClientException));
Expand All @@ -239,16 +239,14 @@ public async Task WebAuthFlow_GetTokenSilent_MsalClientException()
[Test]
public async Task WebAuthFlow_GetTokenSilent_NullReferenceException()
{
this.SilentAuthNullReferenceException();

this.MockAccount();
this.SilentAuthNullReferenceException();

// Act
AuthFlow.Web web = this.Subject();
var authFlowResult = await web.GetTokenAsync();

// Assert
this.pcaWrapperMock.VerifyAll();
authFlowResult.TokenResult.Should().Be(null);
authFlowResult.Errors.Should().HaveCount(1);
authFlowResult.Errors[0].Should().BeOfType(typeof(NullReferenceException));
Expand All @@ -258,18 +256,17 @@ public async Task WebAuthFlow_GetTokenSilent_NullReferenceException()
[Test]
public async Task WebAuthFlow_GetTokenInteractive_MsalUIException_For_Claims()
{
this.MockAccount();
this.SilentAuthUIRequired();
this.SetupWithPromptHint();
this.InteractiveAuthExtraClaimsRequired();
this.InteractiveAuthWithClaimsResult();

this.MockAccount();

// Act
AuthFlow.Web web = this.Subject();
var authFlowResult = await web.GetTokenAsync();

// Assert
this.pcaWrapperMock.VerifyAll();
authFlowResult.TokenResult.Should().Be(this.tokenResult);
authFlowResult.TokenResult.IsSilent.Should().BeFalse();
authFlowResult.Errors.Should().HaveCount(2);
Expand All @@ -280,18 +277,17 @@ public async Task WebAuthFlow_GetTokenInteractive_MsalUIException_For_Claims()
[Test]
public async Task WebAuthFlow_MsalUIException_InteractiveAuthResultReturnsNullWithClaims()
{
this.MockAccount();
this.SilentAuthUIRequired();
this.SetupWithPromptHint();
this.InteractiveAuthExtraClaimsRequired();
this.InteractiveAuthResultReturnsNullWithClaims();

this.MockAccount();

// Act
AuthFlow.Web web = this.Subject();
var authFlowResult = await web.GetTokenAsync();

// Assert
this.pcaWrapperMock.VerifyAll();
authFlowResult.TokenResult.Should().Be(null);
authFlowResult.Errors.Should().HaveCount(2);
authFlowResult.Errors.Should().AllBeOfType(typeof(MsalUiRequiredException));
Expand All @@ -301,18 +297,17 @@ public async Task WebAuthFlow_MsalUIException_InteractiveAuthResultReturnsNullWi
[Test]
public async Task WebAuthFlow_GetTokenInteractive_MsalServiceException_After_Using_Claims()
{
this.MockAccount();
this.SilentAuthUIRequired();
this.SetupWithPromptHint();
this.InteractiveAuthExtraClaimsRequired();
this.InteractiveAuthWithClaimsServiceException();

this.MockAccount();

// Act
AuthFlow.Web web = this.Subject();
var authFlowResult = await web.GetTokenAsync();

// Assert
this.pcaWrapperMock.VerifyAll();
authFlowResult.TokenResult.Should().Be(null);
authFlowResult.Errors.Should().HaveCount(3);
authFlowResult.Errors[0].Should().BeOfType(typeof(MsalUiRequiredException));
Expand All @@ -324,17 +319,16 @@ public async Task WebAuthFlow_GetTokenInteractive_MsalServiceException_After_Usi
[Test]
public async Task WebAuthFlow_GetTokenInteractive_MsalServiceException()
{
this.MockAccount();
this.SilentAuthUIRequired();
this.SetupWithPromptHint();
this.InteractiveAuthServiceException();

this.MockAccount();

// Act
AuthFlow.Web web = this.Subject();
var authFlowResult = await web.GetTokenAsync();

// Assert
this.pcaWrapperMock.VerifyAll();
authFlowResult.TokenResult.Should().Be(null);
authFlowResult.Errors.Should().HaveCount(2);
authFlowResult.Errors[0].Should().BeOfType(typeof(MsalUiRequiredException));
Expand All @@ -345,40 +339,38 @@ public async Task WebAuthFlow_GetTokenInteractive_MsalServiceException()
[Test]
public async Task WebAuthFlow_GetTokenInteractive_OperationCanceledException()
{
this.MockAccount();
this.SilentAuthUIRequired();
this.SetupWithPromptHint();
this.InteractiveAuthTimeout();

this.MockAccount();

// Act
AuthFlow.Web web = this.Subject();
var authFlowResult = await web.GetTokenAsync();

// Assert
this.pcaWrapperMock.VerifyAll();
authFlowResult.TokenResult.Should().Be(null);
authFlowResult.Errors.Should().HaveCount(2);
authFlowResult.Errors[0].Should().BeOfType(typeof(MsalUiRequiredException));
authFlowResult.Errors[1].Should().BeOfType(typeof(AuthenticationTimeoutException));
authFlowResult.Errors[1].Message.Should().Be("Interactive Auth timed out after 00:15:00");
authFlowResult.Errors[1].Message.Should().Be("web interactive auth timed out after 00:15:00");
authFlowResult.AuthFlowName.Should().Be("web");
}

[Test]
public async Task WebAuthFlow_GetTokenInteractive_OperationCanceledException_For_Claims()
{
this.MockAccount();
this.SilentAuthUIRequired();
this.SetupWithPromptHint();
this.InteractiveAuthExtraClaimsRequired();
this.InteractiveAuthWithClaimsTimeout();

this.MockAccount();

// Act
AuthFlow.Web web = this.Subject();
var authFlowResult = await web.GetTokenAsync();

// Assert
this.pcaWrapperMock.VerifyAll();
authFlowResult.TokenResult.Should().Be(null);
authFlowResult.Errors.Should().HaveCount(3);
authFlowResult.Errors[0].Should().BeOfType(typeof(MsalUiRequiredException));
Expand Down Expand Up @@ -407,7 +399,6 @@ private void SilentAuthUIRequired()
this.pcaWrapperMock
.Setup((pca) => pca.GetTokenSilentAsync(this.scopes, It.IsAny<IAccount>(), It.IsAny<CancellationToken>()))
.Throws(new MsalUiRequiredException("1", "UI is required"));
this.SetupInteractiveAuthWithPromptHint();
}

private void SilentAuthServiceException()
Expand Down Expand Up @@ -508,7 +499,7 @@ private void MockAccount()
.ReturnsAsync(this.testAccount.Object);
}

private void SetupInteractiveAuthWithPromptHint()
private void SetupWithPromptHint()
{
this.pcaWrapperMock
.Setup(pca => pca.WithPromptHint(It.IsAny<string>()))
Expand Down
Loading