-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Expand file tree
/
Copy pathGenericHostProviderTests.cs
More file actions
333 lines (281 loc) · 14.6 KB
/
GenericHostProviderTests.cs
File metadata and controls
333 lines (281 loc) · 14.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using GitCredentialManager.Authentication;
using GitCredentialManager.Authentication.OAuth;
using GitCredentialManager.Tests.Objects;
using Moq;
using Xunit;
namespace GitCredentialManager.Tests
{
public class GenericHostProviderTests
{
[Theory]
[InlineData("http", true)]
[InlineData("HTTP", true)]
[InlineData("hTtP", true)]
[InlineData("https", true)]
[InlineData("HTTPS", true)]
[InlineData("hTtPs", true)]
[InlineData("ssh", true)]
[InlineData("SSH", true)]
[InlineData("sSh", true)]
[InlineData("smpt", true)]
[InlineData("SmtP", true)]
[InlineData("SMTP", true)]
[InlineData("imap", true)]
[InlineData("iMAp", true)]
[InlineData("IMAP", true)]
[InlineData("file", true)]
[InlineData("fIlE", true)]
[InlineData("FILE", true)]
[InlineData("", false)]
[InlineData(null, false)]
public void GenericHostProvider_IsSupported(string protocol, bool expected)
{
var input = new InputArguments(new Dictionary<string, string>
{
["protocol"] = protocol,
["host"] = "example.com",
["path"] = "foo/bar",
});
var provider = new GenericHostProvider(new TestCommandContext());
Assert.Equal(expected, provider.IsSupported(input));
}
[Fact]
public void GenericHostProvider_GetCredentialServiceUrl_ReturnsCorrectKey()
{
const string expectedService = "https://example.com/foo/bar";
var input = new InputArguments(new Dictionary<string, string>
{
["protocol"] = "https",
["host"] = "example.com",
["path"] = "foo/bar",
["username"] = "john.doe",
});
var provider = new GenericHostProvider(new TestCommandContext());
string actualService = provider.GetServiceName(input);
Assert.Equal(expectedService, actualService);
}
[Fact]
public async Task GenericHostProvider_CreateCredentialAsync_WiaNotAllowed_ReturnsBasicCredentialNoWiaCheck()
{
var input = new InputArguments(new Dictionary<string, string>
{
["protocol"] = "https",
["host"] = "example.com",
});
const string testUserName = "basicUser";
const string testPassword = "basicPass";
var basicCredential = new GitCredential(testUserName, testPassword);
var context = new TestCommandContext
{
Settings = {IsWindowsIntegratedAuthenticationEnabled = false}
};
var basicAuthMock = new Mock<IBasicAuthentication>();
basicAuthMock.Setup(x => x.GetCredentialsAsync(It.IsAny<string>(), It.IsAny<string>()))
.ReturnsAsync(basicCredential)
.Verifiable();
var wiaAuthMock = new Mock<IWindowsIntegratedAuthentication>();
var oauthMock = new Mock<IOAuthAuthentication>();
var provider = new GenericHostProvider(context, basicAuthMock.Object, wiaAuthMock.Object, oauthMock.Object);
ICredential credential = await provider.GenerateCredentialAsync(input);
Assert.NotNull(credential);
Assert.Equal(testUserName, credential.Account);
Assert.Equal(testPassword, credential.Password);
wiaAuthMock.Verify(x => x.GetIsSupportedAsync(It.IsAny<Uri>()), Times.Never);
basicAuthMock.Verify(x => x.GetCredentialsAsync(It.IsAny<string>(), It.IsAny<string>()), Times.Once);
}
[Fact]
public async Task GenericHostProvider_CreateCredentialAsync_LegacyAuthorityBasic_ReturnsBasicCredentialNoWiaCheck()
{
var input = new InputArguments(new Dictionary<string, string>
{
["protocol"] = "https",
["host"] = "example.com",
});
const string testUserName = "basicUser";
const string testPassword = "basicPass";
var basicCredential = new GitCredential(testUserName, testPassword);
var context = new TestCommandContext
{
Settings = {LegacyAuthorityOverride = "basic"}
};
var basicAuthMock = new Mock<IBasicAuthentication>();
basicAuthMock.Setup(x => x.GetCredentialsAsync(It.IsAny<string>(), It.IsAny<string>()))
.ReturnsAsync(basicCredential)
.Verifiable();
var wiaAuthMock = new Mock<IWindowsIntegratedAuthentication>();
var oauthMock = new Mock<IOAuthAuthentication>();
var provider = new GenericHostProvider(context, basicAuthMock.Object, wiaAuthMock.Object, oauthMock.Object);
ICredential credential = await provider.GenerateCredentialAsync(input);
Assert.NotNull(credential);
Assert.Equal(testUserName, credential.Account);
Assert.Equal(testPassword, credential.Password);
wiaAuthMock.Verify(x => x.GetIsSupportedAsync(It.IsAny<Uri>()), Times.Never);
basicAuthMock.Verify(x => x.GetCredentialsAsync(It.IsAny<string>(), It.IsAny<string>()), Times.Once);
}
[Fact]
public async Task GenericHostProvider_CreateCredentialAsync_NonHttpProtocol_ReturnsBasicCredentialNoWiaCheck()
{
var input = new InputArguments(new Dictionary<string, string>
{
["protocol"] = "smtp",
["host"] = "example.com",
});
const string testUserName = "basicUser";
const string testPassword = "basicPass";
var basicCredential = new GitCredential(testUserName, testPassword);
var context = new TestCommandContext();
var basicAuthMock = new Mock<IBasicAuthentication>();
basicAuthMock.Setup(x => x.GetCredentialsAsync(It.IsAny<string>(), It.IsAny<string>()))
.ReturnsAsync(basicCredential)
.Verifiable();
var wiaAuthMock = new Mock<IWindowsIntegratedAuthentication>();
var oauthMock = new Mock<IOAuthAuthentication>();
var provider = new GenericHostProvider(context, basicAuthMock.Object, wiaAuthMock.Object, oauthMock.Object);
ICredential credential = await provider.GenerateCredentialAsync(input);
Assert.NotNull(credential);
Assert.Equal(testUserName, credential.Account);
Assert.Equal(testPassword, credential.Password);
wiaAuthMock.Verify(x => x.GetIsSupportedAsync(It.IsAny<Uri>()), Times.Never);
basicAuthMock.Verify(x => x.GetCredentialsAsync(It.IsAny<string>(), It.IsAny<string>()), Times.Once);
}
[PosixFact]
public async Task GenericHostProvider_CreateCredentialAsync_NonWindows_WiaSupported_ReturnsBasicCredential()
{
await TestCreateCredentialAsync_ReturnsBasicCredential(wiaSupported: true);
}
[WindowsFact]
public async Task GenericHostProvider_CreateCredentialAsync_Windows_WiaSupported_ReturnsEmptyCredential()
{
await TestCreateCredentialAsync_ReturnsEmptyCredential(wiaSupported: true);
}
[Fact]
public async Task GenericHostProvider_CreateCredentialAsync_WiaNotSupported_ReturnsBasicCredential()
{
await TestCreateCredentialAsync_ReturnsBasicCredential(wiaSupported: false);
}
[Fact]
public async Task GenericHostProvider_GenerateCredentialAsync_OAuth_CompleteOAuthConfig_UsesOAuth()
{
var input = new InputArguments(new Dictionary<string, string>
{
["protocol"] = "https",
["host"] = "git.example.com",
["path"] = "foo"
});
const string testUserName = "TEST_OAUTH_USER";
const string testAcessToken = "OAUTH_TOKEN";
const string testRefreshToken = "OAUTH_REFRESH_TOKEN";
const string testResource = "https://git.example.com/foo";
var authMode = OAuthAuthenticationModes.Browser;
string[] scopes = { "code:write", "code:read" };
string clientId = "3eadfc62-9e91-45d3-8c60-20ccd6d0c7cf";
string clientSecret = "C1DA8B93CCB5F5B93DA";
string redirectUri = "http://localhost";
string authzEndpoint = "/oauth/authorize";
string tokenEndpoint = "/oauth/token";
string deviceEndpoint = "/oauth/device";
string GetKey(string name) => $"{Constants.GitConfiguration.Credential.SectionName}.https://example.com.{name}";
var context = new TestCommandContext
{
Git =
{
Configuration =
{
Global =
{
[GetKey(Constants.GitConfiguration.Credential.OAuthClientId)] = new[] { clientId },
[GetKey(Constants.GitConfiguration.Credential.OAuthClientSecret)] = new[] { clientSecret },
[GetKey(Constants.GitConfiguration.Credential.OAuthRedirectUri)] = new[] { redirectUri },
[GetKey(Constants.GitConfiguration.Credential.OAuthScopes)] = new[] { string.Join(' ', scopes) },
[GetKey(Constants.GitConfiguration.Credential.OAuthAuthzEndpoint)] = new[] { authzEndpoint },
[GetKey(Constants.GitConfiguration.Credential.OAuthTokenEndpoint)] = new[] { tokenEndpoint },
[GetKey(Constants.GitConfiguration.Credential.OAuthDeviceEndpoint)] = new[] { deviceEndpoint },
[GetKey(Constants.GitConfiguration.Credential.OAuthDefaultUserName)] = new[] { testUserName },
}
}
},
Settings =
{
RemoteUri = new Uri(testResource)
}
};
var basicAuthMock = new Mock<IBasicAuthentication>();
var wiaAuthMock = new Mock<IWindowsIntegratedAuthentication>();
var oauthMock = new Mock<IOAuthAuthentication>();
oauthMock.Setup(x =>
x.GetAuthenticationModeAsync(It.IsAny<string>(), It.IsAny<OAuthAuthenticationModes>()))
.ReturnsAsync(authMode);
oauthMock.Setup(x => x.GetTokenByBrowserAsync(It.IsAny<OAuth2Client>(), It.IsAny<string[]>()))
.ReturnsAsync(new OAuth2TokenResult(testAcessToken, "access_token")
{
Scopes = scopes,
RefreshToken = testRefreshToken,
});
var provider = new GenericHostProvider(context, basicAuthMock.Object, wiaAuthMock.Object, oauthMock.Object);
ICredential credential = await provider.GenerateCredentialAsync(input);
Assert.NotNull(credential);
Assert.Equal(testUserName, credential.Account);
Assert.Equal(testAcessToken, credential.Password);
Assert.Equal(testRefreshToken, credential.OAuthRefreshToken);
oauthMock.Verify(x => x.GetAuthenticationModeAsync(testResource, OAuthAuthenticationModes.All), Times.Once);
oauthMock.Verify(x => x.GetTokenByBrowserAsync(It.IsAny<OAuth2Client>(), scopes), Times.Once);
oauthMock.Verify(x => x.GetTokenByDeviceCodeAsync(It.IsAny<OAuth2Client>(), scopes), Times.Never);
wiaAuthMock.Verify(x => x.GetIsSupportedAsync(It.IsAny<Uri>()), Times.Never);
basicAuthMock.Verify(x => x.GetCredentialsAsync(It.IsAny<string>(), It.IsAny<string>()), Times.Never);
}
#region Helpers
private static async Task TestCreateCredentialAsync_ReturnsEmptyCredential(bool wiaSupported)
{
var input = new InputArguments(new Dictionary<string, string>
{
["protocol"] = "https",
["host"] = "example.com",
});
var context = new TestCommandContext();
var basicAuthMock = new Mock<IBasicAuthentication>();
basicAuthMock.Setup(x => x.GetCredentialsAsync(It.IsAny<string>(), It.IsAny<string>()))
.Verifiable();
var wiaAuthMock = new Mock<IWindowsIntegratedAuthentication>();
wiaAuthMock.Setup(x => x.GetIsSupportedAsync(It.IsAny<Uri>()))
.ReturnsAsync(wiaSupported);
var oauthMock = new Mock<IOAuthAuthentication>();
var provider = new GenericHostProvider(context, basicAuthMock.Object, wiaAuthMock.Object, oauthMock.Object);
ICredential credential = await provider.GenerateCredentialAsync(input);
Assert.NotNull(credential);
Assert.Equal(string.Empty, credential.Account);
Assert.Equal(string.Empty, credential.Password);
basicAuthMock.Verify(x => x.GetCredentialsAsync(It.IsAny<string>(), It.IsAny<string>()), Times.Never);
}
private static async Task TestCreateCredentialAsync_ReturnsBasicCredential(bool wiaSupported)
{
var input = new InputArguments(new Dictionary<string, string>
{
["protocol"] = "https",
["host"] = "example.com",
});
const string testUserName = "basicUser";
const string testPassword = "basicPass";
var basicCredential = new GitCredential(testUserName, testPassword);
var context = new TestCommandContext();
var basicAuthMock = new Mock<IBasicAuthentication>();
basicAuthMock.Setup(x => x.GetCredentialsAsync(It.IsAny<string>(), It.IsAny<string>()))
.ReturnsAsync(basicCredential)
.Verifiable();
var wiaAuthMock = new Mock<IWindowsIntegratedAuthentication>();
wiaAuthMock.Setup(x => x.GetIsSupportedAsync(It.IsAny<Uri>()))
.ReturnsAsync(wiaSupported);
var oauthMock = new Mock<IOAuthAuthentication>();
var provider = new GenericHostProvider(context, basicAuthMock.Object, wiaAuthMock.Object, oauthMock.Object);
ICredential credential = await provider.GenerateCredentialAsync(input);
Assert.NotNull(credential);
Assert.Equal(testUserName, credential.Account);
Assert.Equal(testPassword, credential.Password);
basicAuthMock.Verify(x => x.GetCredentialsAsync(It.IsAny<string>(), It.IsAny<string>()), Times.Once);
}
#endregion
}
}