-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a cache of the Azure backing authority for Azure DevOps orgs. This cache is only consulted when the credential type is "oauth" and not "pat". We use Git's configuration as the persistence mechanism.
- Loading branch information
1 parent
7e493d7
commit 8e351df
Showing
5 changed files
with
376 additions
and
14 deletions.
There are no files selected for viewing
189 changes: 189 additions & 0 deletions
189
src/shared/Microsoft.AzureRepos.Tests/AzureReposAuthorityCacheTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT license. | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using Microsoft.Git.CredentialManager; | ||
using Microsoft.Git.CredentialManager.Tests.Objects; | ||
using Xunit; | ||
|
||
namespace Microsoft.AzureRepos.Tests | ||
{ | ||
public class AzureReposAuthorityCacheTests | ||
{ | ||
[Fact] | ||
public void AzureReposAuthorityCache_GetAuthority_Null_ThrowException() | ||
{ | ||
var trace = new NullTrace(); | ||
var git = new TestGit(); | ||
var cache = new AzureDevOpsAuthorityCache(trace, git); | ||
|
||
Assert.Throws<ArgumentNullException>(() => cache.GetAuthority(null)); | ||
} | ||
|
||
[Fact] | ||
public void AzureReposAuthorityCache_GetAuthority_NoCachedAuthority_ReturnsNull() | ||
{ | ||
string key = CreateKey("contoso"); | ||
|
||
var trace = new NullTrace(); | ||
var git = new TestGit(); | ||
var cache = new AzureDevOpsAuthorityCache(trace, git); | ||
|
||
string authority = cache.GetAuthority(key); | ||
|
||
Assert.Null(authority); | ||
} | ||
|
||
[Fact] | ||
public void AzureReposAuthorityCache_GetAuthority_CachedAuthority_ReturnsAuthority() | ||
{ | ||
const string orgName = "contoso"; | ||
string key = CreateKey(orgName); | ||
const string expectedAuthority = "https://login.contoso.com"; | ||
|
||
var git = new TestGit | ||
{ | ||
Configuration = | ||
{ | ||
Global = | ||
{ | ||
[key] = new[] {expectedAuthority} | ||
} | ||
} | ||
}; | ||
|
||
var trace = new NullTrace(); | ||
var cache = new AzureDevOpsAuthorityCache(trace, git); | ||
|
||
string actualAuthority = cache.GetAuthority(orgName); | ||
|
||
Assert.Equal(expectedAuthority, actualAuthority); | ||
} | ||
|
||
[Fact] | ||
public void AzureReposAuthorityCache_UpdateAuthority_NoCachedAuthority_SetsAuthority() | ||
{ | ||
const string orgName = "contoso"; | ||
string key = CreateKey(orgName); | ||
const string expectedAuthority = "https://login.contoso.com"; | ||
|
||
var trace = new NullTrace(); | ||
var git = new TestGit(); | ||
var cache = new AzureDevOpsAuthorityCache(trace, git); | ||
|
||
cache.UpdateAuthority(orgName, expectedAuthority); | ||
|
||
Assert.True(git.Configuration.Global.TryGetValue(key, out IList<string> values)); | ||
Assert.Single(values); | ||
string actualAuthority = values[0]; | ||
Assert.Equal(expectedAuthority, actualAuthority); | ||
} | ||
|
||
[Fact] | ||
public void AzureReposAuthorityCache_UpdateAuthority_CachedAuthority_UpdatesAuthority() | ||
{ | ||
const string orgName = "contoso"; | ||
string key = CreateKey(orgName); | ||
const string oldAuthority = "https://old-login.contoso.com"; | ||
const string expectedAuthority = "https://login.contoso.com"; | ||
|
||
var git = new TestGit | ||
{ | ||
Configuration = | ||
{ | ||
Global = | ||
{ | ||
[key] = new[] {oldAuthority} | ||
} | ||
} | ||
}; | ||
|
||
var trace = new NullTrace(); | ||
var cache = new AzureDevOpsAuthorityCache(trace, git); | ||
|
||
cache.UpdateAuthority(orgName, expectedAuthority); | ||
|
||
Assert.True(git.Configuration.Global.TryGetValue(key, out IList<string> values)); | ||
Assert.Single(values); | ||
string actualAuthority = values[0]; | ||
Assert.Equal(expectedAuthority, actualAuthority); | ||
} | ||
|
||
[Fact] | ||
public void AzureReposAuthorityCache_EraseAuthority_NoCachedAuthority_DoesNothing() | ||
{ | ||
const string orgName = "contoso"; | ||
string key = CreateKey(orgName); | ||
string otherKey = CreateKey("org.fabrikam.authority"); | ||
const string otherAuthority = "https://fabrikam.com/login"; | ||
|
||
var git = new TestGit | ||
{ | ||
Configuration = | ||
{ | ||
Global = | ||
{ | ||
[otherKey] = new[] {otherAuthority} | ||
} | ||
} | ||
}; | ||
|
||
var trace = new NullTrace(); | ||
var cache = new AzureDevOpsAuthorityCache(trace, git); | ||
|
||
cache.EraseAuthority(orgName); | ||
|
||
// Other entries should remain | ||
Assert.False(git.Configuration.Global.ContainsKey(key)); | ||
Assert.Single(git.Configuration.Global); | ||
Assert.True(git.Configuration.Global.TryGetValue(otherKey, out IList<string> values)); | ||
Assert.Single(values); | ||
string actualOtherAuthority = values[0]; | ||
Assert.Equal(otherAuthority, actualOtherAuthority); | ||
} | ||
|
||
[Fact] | ||
public void AzureReposAuthorityCache_EraseAuthority_CachedAuthority_RemovesAuthority() | ||
{ | ||
const string orgName = "contoso"; | ||
string key = CreateKey(orgName); | ||
const string authority = "https://login.contoso.com"; | ||
string otherKey = CreateKey("fabrikam"); | ||
const string otherAuthority = "https://fabrikam.com/login"; | ||
|
||
var git = new TestGit | ||
{ | ||
Configuration = | ||
{ | ||
Global = | ||
{ | ||
[key] = new[] {authority}, | ||
[otherKey] = new[] {otherAuthority} | ||
} | ||
} | ||
}; | ||
|
||
var trace = new NullTrace(); | ||
var cache = new AzureDevOpsAuthorityCache(trace, git); | ||
|
||
cache.EraseAuthority(orgName); | ||
|
||
// Only the other entries should remain | ||
Assert.False(git.Configuration.Global.ContainsKey(key)); | ||
Assert.Single(git.Configuration.Global); | ||
Assert.True(git.Configuration.Global.TryGetValue(otherKey, out IList<string> values)); | ||
Assert.Single(values); | ||
string actualOtherAuthority = values[0]; | ||
Assert.Equal(otherAuthority, actualOtherAuthority); | ||
} | ||
|
||
private static string CreateKey(string orgName) | ||
{ | ||
return string.Format(CultureInfo.InvariantCulture, "{0}.{1}:{2}/{3}.{4}", | ||
Constants.GitConfiguration.Credential.SectionName, | ||
AzureDevOpsConstants.UrnScheme, AzureDevOpsConstants.UrnOrgPrefix, orgName, | ||
AzureDevOpsConstants.GitConfiguration.Credential.AzureAuthority); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.