-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Expand file tree
/
Copy pathGetCommandTests.cs
More file actions
64 lines (54 loc) · 2.46 KB
/
GetCommandTests.cs
File metadata and controls
64 lines (54 loc) · 2.46 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using GitCredentialManager.Commands;
using GitCredentialManager.Tests.Objects;
using Moq;
using Xunit;
namespace GitCredentialManager.Tests.Commands
{
public class GetCommandTests
{
[Fact]
public async Task GetCommand_ExecuteAsync_CallsHostProviderAndWritesCredential()
{
const string testUserName = "john.doe";
const string testPassword = "letmein123"; // [SuppressMessage("Microsoft.Security", "CS001:SecretInline", Justification="Fake credential")]
const string testRefreshToken = "xyzzy";
const long testExpiry = 1919539847;
ICredential testCredential = new GitCredential(testUserName, testPassword) {
OAuthRefreshToken = testRefreshToken,
PasswordExpiry = DateTimeOffset.FromUnixTimeSeconds(testExpiry),
};
var stdin = $"protocol=http\nhost=example.com\n\n";
var expectedStdOutDict = new Dictionary<string, string>
{
["protocol"] = "http",
["host"] = "example.com",
["username"] = testUserName,
["password"] = testPassword,
["password_expiry_utc"] = testExpiry.ToString(),
["oauth_refresh_token"] = testRefreshToken,
};
var providerMock = new Mock<IHostProvider>();
providerMock.Setup(x => x.GetCredentialAsync(It.IsAny<InputArguments>()))
.ReturnsAsync(testCredential);
var providerRegistry = new TestHostProviderRegistry {Provider = providerMock.Object};
var context = new TestCommandContext
{
Streams = {In = stdin}
};
var command = new GetCommand(context, providerRegistry);
await command.ExecuteAsync();
IDictionary<string, string> actualStdOutDict = ParseDictionary(context.Streams.Out);
providerMock.Verify(x => x.GetCredentialAsync(It.IsAny<InputArguments>()), Times.Once);
Assert.Equal(expectedStdOutDict, actualStdOutDict);
}
#region Helpers
private static IDictionary<string, string> ParseDictionary(StringBuilder sb) => ParseDictionary(sb.ToString());
private static IDictionary<string, string> ParseDictionary(string str) => new StringReader(str).ReadDictionary();
#endregion
}
}