Skip to content

Commit baaa2ad

Browse files
committed
azrepos: test legacy client fallback
The compatibility retry is the recovery path for authentication failures introduced by the new GCM Entra application. Without direct coverage, regressions would only surface for users during live authentication. Exercise both OAuth and PAT credential generation through a failed new-client attempt and successful legacy retry. Also ensure an already-legacy client propagates the failure rather than retrying indefinitely. Assisted-by: GPT-5.6 Sol Signed-off-by: Matthew John Cheetham <mjcheetham@outlook.com>
1 parent defc3c3 commit baaa2ad

1 file changed

Lines changed: 128 additions & 0 deletions

File tree

‎src/Microsoft.AzureRepos.Tests/AzureReposHostProviderTests.cs‎

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using GitCredentialManager.Authentication.Entra;
77
using GitCredentialManager.Tests;
88
using GitCredentialManager.Tests.Objects;
9+
using Microsoft.Identity.Client;
910
using Moq;
1011
using Xunit;
1112

@@ -444,6 +445,133 @@ public async Task AzureReposProvider_GetCredentialAsync_JwtMode_NoCachedAuthorit
444445
Assert.Equal(accessToken, credential.Password);
445446
}
446447

448+
[Theory]
449+
[InlineData(false)]
450+
[InlineData(true)]
451+
public async Task AzureReposProvider_GetCredentialAsync_MsalFailure_RetriesWithLegacyClient(bool usePat)
452+
{
453+
var request = new GitRequest(new Dictionary<string, string>
454+
{
455+
["protocol"] = "https",
456+
["host"] = "dev.azure.com",
457+
["path"] = "org/proj/_git/repo"
458+
});
459+
460+
var expectedOrgUri = new Uri("https://dev.azure.com/org");
461+
var authorityUrl = "https://login.microsoftonline.com/common";
462+
var accessToken = "ACCESS-TOKEN";
463+
var personalAccessToken = "PERSONAL-ACCESS-TOKEN";
464+
var account = "john.doe";
465+
var authResult = CreateAuthResult(account, accessToken);
466+
var msalException = new MsalException("test_error", "Test failure");
467+
var clientIds = new List<string>();
468+
469+
var context = new TestCommandContext();
470+
if (!usePat)
471+
{
472+
context.Environment.Variables[AzureDevOpsConstants.EnvironmentVariables.CredentialType] =
473+
AzureDevOpsConstants.OAuthCredentialType;
474+
}
475+
476+
var azDevOpsMock = new Mock<IAzureDevOpsRestApi>(MockBehavior.Strict);
477+
if (usePat)
478+
{
479+
azDevOpsMock.Setup(x => x.GetAuthorityAsync(expectedOrgUri)).ReturnsAsync(authorityUrl);
480+
azDevOpsMock.Setup(x => x.CreatePersonalAccessTokenAsync(
481+
expectedOrgUri, accessToken, It.IsAny<IEnumerable<string>>()))
482+
.ReturnsAsync(personalAccessToken);
483+
}
484+
485+
var newEntraAuthMock = new Mock<IEntraAuthentication>(MockBehavior.Strict);
486+
newEntraAuthMock.SetupGet(x => x.PublicClientConfig)
487+
.Returns(new PublicClientConfig { ClientId = AzureDevOpsConstants.ClientId });
488+
newEntraAuthMock.Setup(x => x.GetTokenForUserAsync(
489+
AzureDevOpsConstants.AzureDevOpsDefaultScopes, authorityUrl, null,
490+
InteractionMode.Auto, CancellationToken.None))
491+
.ThrowsAsync(msalException);
492+
493+
var legacyEntraAuthMock = new Mock<IEntraAuthentication>(MockBehavior.Strict);
494+
legacyEntraAuthMock.SetupGet(x => x.PublicClientConfig)
495+
.Returns(new PublicClientConfig { ClientId = AzureDevOpsConstants.LegacyClientId });
496+
legacyEntraAuthMock.Setup(x => x.GetTokenForUserAsync(
497+
AzureDevOpsConstants.AzureDevOpsDefaultScopes, authorityUrl, null,
498+
InteractionMode.Auto, CancellationToken.None))
499+
.ReturnsAsync(authResult);
500+
501+
IEntraAuthentication EntraAuthFactory(PublicClientConfig config)
502+
{
503+
clientIds.Add(config.ClientId);
504+
return config.ClientId == AzureDevOpsConstants.LegacyClientId
505+
? legacyEntraAuthMock.Object
506+
: newEntraAuthMock.Object;
507+
}
508+
509+
var authorityCacheMock = new Mock<IAzureDevOpsAuthorityCache>(MockBehavior.Strict);
510+
authorityCacheMock.Setup(x => x.GetAuthority(OrgName)).Returns(authorityUrl);
511+
512+
var userMgrMock = new Mock<IAzureReposBindingManager>(MockBehavior.Strict);
513+
userMgrMock.Setup(x => x.GetBinding(OrgName)).Returns((AzureReposBinding)null);
514+
515+
var provider = new AzureReposHostProvider(context, azDevOpsMock.Object, EntraAuthFactory,
516+
authorityCacheMock.Object, userMgrMock.Object);
517+
518+
GitResponse result = await provider.GetCredentialAsync(request);
519+
520+
Assert.Equal(account, result.Credential.Account);
521+
Assert.Equal(usePat ? personalAccessToken : accessToken, result.Credential.Password);
522+
Assert.Equal(
523+
new[] { AzureDevOpsConstants.ClientId, AzureDevOpsConstants.LegacyClientId },
524+
clientIds);
525+
}
526+
527+
[Fact]
528+
public async Task AzureReposProvider_GetCredentialAsync_LegacyClientMsalFailure_DoesNotRetry()
529+
{
530+
var request = new GitRequest(new Dictionary<string, string>
531+
{
532+
["protocol"] = "https",
533+
["host"] = "dev.azure.com",
534+
["path"] = "org/proj/_git/repo"
535+
});
536+
537+
var authorityUrl = "https://login.microsoftonline.com/common";
538+
var msalException = new MsalException("test_error", "Test failure");
539+
var clientIds = new List<string>();
540+
541+
var context = new TestCommandContext();
542+
context.Environment.Variables[AzureDevOpsConstants.EnvironmentVariables.CredentialType] =
543+
AzureDevOpsConstants.OAuthCredentialType;
544+
context.Environment.Variables[AzureDevOpsConstants.EnvironmentVariables.UseLegacyClientId] = "true";
545+
546+
var entraAuthMock = new Mock<IEntraAuthentication>(MockBehavior.Strict);
547+
entraAuthMock.Setup(x => x.GetTokenForUserAsync(
548+
AzureDevOpsConstants.AzureDevOpsDefaultScopes, authorityUrl, null,
549+
InteractionMode.Auto, CancellationToken.None))
550+
.ThrowsAsync(msalException);
551+
552+
IEntraAuthentication EntraAuthFactory(PublicClientConfig config)
553+
{
554+
clientIds.Add(config.ClientId);
555+
entraAuthMock.SetupGet(x => x.PublicClientConfig).Returns(config);
556+
return entraAuthMock.Object;
557+
}
558+
559+
var authorityCacheMock = new Mock<IAzureDevOpsAuthorityCache>(MockBehavior.Strict);
560+
authorityCacheMock.Setup(x => x.GetAuthority(OrgName)).Returns(authorityUrl);
561+
562+
var userMgrMock = new Mock<IAzureReposBindingManager>(MockBehavior.Strict);
563+
userMgrMock.Setup(x => x.GetBinding(OrgName)).Returns((AzureReposBinding)null);
564+
565+
var provider = new AzureReposHostProvider(context, Mock.Of<IAzureDevOpsRestApi>(),
566+
EntraAuthFactory, authorityCacheMock.Object, userMgrMock.Object);
567+
568+
MsalException exception = await Assert.ThrowsAsync<MsalException>(
569+
() => provider.GetCredentialAsync(request));
570+
571+
Assert.Same(msalException, exception);
572+
Assert.Equal(new[] { AzureDevOpsConstants.LegacyClientId }, clientIds);
573+
}
574+
447575
[Fact]
448576
public async Task AzureReposProvider_GetCredentialAsync_PatMode_OrgInUserName_NoExistingPat_GeneratesCredential()
449577
{

0 commit comments

Comments
 (0)