Skip to content

Commit d8aa30b

Browse files
committed
generic: add OAuth test for generic provider
1 parent 1aeb9fc commit d8aa30b

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

src/shared/Core.Tests/GenericHostProviderTests.cs

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Net.Http;
34
using System.Threading.Tasks;
45
using GitCredentialManager.Authentication;
6+
using GitCredentialManager.Authentication.OAuth;
57
using GitCredentialManager.Tests.Objects;
68
using Moq;
79
using Xunit;
@@ -185,6 +187,90 @@ public async Task GenericHostProvider_CreateCredentialAsync_WiaNotSupported_Retu
185187
await TestCreateCredentialAsync_ReturnsBasicCredential(wiaSupported: false);
186188
}
187189

190+
[Fact]
191+
public async Task GenericHostProvider_GenerateCredentialAsync_OAuth_CompleteOAuthConfig_UsesOAuth()
192+
{
193+
var input = new InputArguments(new Dictionary<string, string>
194+
{
195+
["protocol"] = "https",
196+
["host"] = "git.example.com",
197+
["path"] = "foo"
198+
});
199+
200+
const string testUserName = "TEST_OAUTH_USER";
201+
const string testAcessToken = "OAUTH_TOKEN";
202+
const string testRefreshToken = "OAUTH_REFRESH_TOKEN";
203+
const string testResource = "https://git.example.com/foo";
204+
const string expectedRefreshTokenService = "https://refresh_token.git.example.com/foo";
205+
206+
var authMode = OAuthAuthenticationModes.Browser;
207+
string[] scopes = { "code:write", "code:read" };
208+
string clientId = "3eadfc62-9e91-45d3-8c60-20ccd6d0c7cf";
209+
string clientSecret = "C1DA8B93CCB5F5B93DA";
210+
string redirectUri = "http://localhost";
211+
string authzEndpoint = "/oauth/authorize";
212+
string tokenEndpoint = "/oauth/token";
213+
string deviceEndpoint = "/oauth/device";
214+
215+
string GetKey(string name) => $"{Constants.GitConfiguration.Credential.SectionName}.https://example.com.{name}";
216+
217+
var context = new TestCommandContext
218+
{
219+
Git =
220+
{
221+
Configuration =
222+
{
223+
Global =
224+
{
225+
[GetKey(Constants.GitConfiguration.Credential.OAuthClientId)] = new[] { clientId },
226+
[GetKey(Constants.GitConfiguration.Credential.OAuthClientSecret)] = new[] { clientSecret },
227+
[GetKey(Constants.GitConfiguration.Credential.OAuthRedirectUri)] = new[] { redirectUri },
228+
[GetKey(Constants.GitConfiguration.Credential.OAuthScopes)] = new[] { string.Join(' ', scopes) },
229+
[GetKey(Constants.GitConfiguration.Credential.OAuthAuthzEndpoint)] = new[] { authzEndpoint },
230+
[GetKey(Constants.GitConfiguration.Credential.OAuthTokenEndpoint)] = new[] { tokenEndpoint },
231+
[GetKey(Constants.GitConfiguration.Credential.OAuthDeviceEndpoint)] = new[] { deviceEndpoint },
232+
[GetKey(Constants.GitConfiguration.Credential.OAuthDefaultUserName)] = new[] { testUserName },
233+
}
234+
}
235+
},
236+
Settings =
237+
{
238+
RemoteUri = new Uri(testResource)
239+
}
240+
};
241+
242+
var basicAuthMock = new Mock<IBasicAuthentication>();
243+
var wiaAuthMock = new Mock<IWindowsIntegratedAuthentication>();
244+
var oauthMock = new Mock<IOAuthAuthentication>();
245+
oauthMock.Setup(x =>
246+
x.GetAuthenticationModeAsync(It.IsAny<string>(), It.IsAny<OAuthAuthenticationModes>()))
247+
.ReturnsAsync(authMode);
248+
oauthMock.Setup(x => x.GetTokenByBrowserAsync(It.IsAny<OAuth2Client>(), It.IsAny<string[]>()))
249+
.ReturnsAsync(new OAuth2TokenResult(testAcessToken, "access_token")
250+
{
251+
Scopes = scopes,
252+
RefreshToken = testRefreshToken
253+
});
254+
255+
var provider = new GenericHostProvider(context, basicAuthMock.Object, wiaAuthMock.Object, oauthMock.Object);
256+
257+
ICredential credential = await provider.GenerateCredentialAsync(input);
258+
259+
Assert.NotNull(credential);
260+
Assert.Equal(testUserName, credential.Account);
261+
Assert.Equal(testAcessToken, credential.Password);
262+
263+
Assert.True(context.CredentialStore.TryGet(expectedRefreshTokenService, null, out TestCredential refreshToken));
264+
Assert.Equal(testUserName, refreshToken.Account);
265+
Assert.Equal(testRefreshToken, refreshToken.Password);
266+
267+
oauthMock.Verify(x => x.GetAuthenticationModeAsync(testResource, OAuthAuthenticationModes.All), Times.Once);
268+
oauthMock.Verify(x => x.GetTokenByBrowserAsync(It.IsAny<OAuth2Client>(), scopes), Times.Once);
269+
oauthMock.Verify(x => x.GetTokenByDeviceCodeAsync(It.IsAny<OAuth2Client>(), scopes), Times.Never);
270+
wiaAuthMock.Verify(x => x.GetIsSupportedAsync(It.IsAny<Uri>()), Times.Never);
271+
basicAuthMock.Verify(x => x.GetCredentialsAsync(It.IsAny<string>(), It.IsAny<string>()), Times.Never);
272+
}
273+
188274
#region Helpers
189275

190276
private static async Task TestCreateCredentialAsync_ReturnsEmptyCredential(bool wiaSupported)

0 commit comments

Comments
 (0)