Skip to content

Commit b9cfa9b

Browse files
committed
more unit tests
1 parent fc477ee commit b9cfa9b

File tree

4 files changed

+47
-17
lines changed

4 files changed

+47
-17
lines changed

src/shared/Core/Authentication/AuthenticationBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ protected Task<IDictionary<string, string>> InvokeHelperAsync(string path, strin
2727
return InvokeHelperAsync(path, args, null, CancellationToken.None);
2828
}
2929

30-
protected async Task<IDictionary<string, string>> InvokeHelperAsync(string path, string args,
30+
internal protected virtual async Task<IDictionary<string, string>> InvokeHelperAsync(string path, string args,
3131
IDictionary<string, string> standardInput, CancellationToken ct)
3232
{
3333
var procStartInfo = new ProcessStartInfo(path)

src/shared/Core/InternalsVisibleTo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
using System.Runtime.CompilerServices;
22

33
[assembly: InternalsVisibleTo("Core.Tests")]
4-
4+
[assembly: InternalsVisibleTo("GitHub.Tests")]
Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
24
using System.Threading.Tasks;
35
using GitCredentialManager.Tests.Objects;
46
using Moq;
7+
using Moq.Protected;
58
using Xunit;
69

710
namespace GitHub.Tests
@@ -19,30 +22,25 @@ await Assert.ThrowsAsync<ArgumentException>("modes",
1922
}
2023

2124
[Theory]
22-
[InlineData(AuthenticationModes.Browser, true)]
23-
[InlineData(AuthenticationModes.Browser, false)]
24-
[InlineData(AuthenticationModes.Device, true)]
25-
[InlineData(AuthenticationModes.Device, false)]
26-
public async Task GitHubAuthentication_GetAuthenticationAsync_SingleChoice_TerminalAndInteractionNotRequired(GitHub.AuthenticationModes modes, bool useHelper)
25+
[InlineData(AuthenticationModes.Browser)]
26+
[InlineData(AuthenticationModes.Device)]
27+
public async Task GitHubAuthentication_GetAuthenticationAsync_SingleChoice_TerminalAndInteractionNotRequired(GitHub.AuthenticationModes modes)
2728
{
2829
var context = new TestCommandContext();
2930
context.Settings.IsTerminalPromptsEnabled = false;
3031
context.Settings.IsInteractionAllowed = false;
3132
context.SessionManager.IsDesktopSession = true; // necessary for browser
32-
if (useHelper)
33-
{
34-
context.FileSystem.Files["/usr/local/bin/GitHub.UI"] = new byte[0];
35-
}
33+
context.FileSystem.Files["/usr/local/bin/GitHub.UI"] = new byte[0];
34+
context.FileSystem.Files[@"C:\Program Files\Git Credential Manager Core\GitHub.UI.exe"] = new byte[0];
3635
var auth = new GitHubAuthentication(context);
37-
var result = await auth.GetAuthenticationAsync(new Uri("https://github.com"), null, modes);
36+
var result = await auth.GetAuthenticationAsync(null, null, modes);
3837
Assert.Equal(modes, result.AuthenticationMode);
3938
}
4039

4140
[Fact]
42-
public async Task GitHubAuthentication_GetAuthenticationAsync_NonDesktopSession_RequiresTerminal()
41+
public async Task GitHubAuthentication_GetAuthenticationAsync_TerminalPromptsDisabled_Throws()
4342
{
4443
var context = new TestCommandContext();
45-
context.FileSystem.Files["/usr/local/bin/GitHub.UI"] = new byte[0];
4644
context.Settings.IsTerminalPromptsEnabled = false;
4745
var auth = new GitHubAuthentication(context);
4846
var exception = await Assert.ThrowsAsync<InvalidOperationException>(
@@ -51,18 +49,51 @@ public async Task GitHubAuthentication_GetAuthenticationAsync_NonDesktopSession_
5149
Assert.Equal("Cannot prompt because terminal prompts have been disabled.", exception.Message);
5250
}
5351

52+
// reproduces https://github.com/GitCredentialManager/git-credential-manager/issues/453
5453
[Fact]
55-
public async Task GitHubAuthentication_GetAuthenticationAsync_DesktopSession_RequiresInteraction()
54+
public async Task GitHubAuthentication_GetAuthenticationAsync_Terminal()
5655
{
5756
var context = new TestCommandContext();
5857
context.FileSystem.Files["/usr/local/bin/GitHub.UI"] = new byte[0];
59-
context.SessionManager.IsDesktopSession = true;
58+
context.FileSystem.Files[@"C:\Program Files\Git Credential Manager Core\GitHub.UI.exe"] = new byte[0];
59+
var auth = new GitHubAuthentication(context);
60+
context.Terminal.Prompts["option (enter for default)"] = "";
61+
var result = await auth.GetAuthenticationAsync(null, null, AuthenticationModes.All);
62+
Assert.Equal(AuthenticationModes.Device, result.AuthenticationMode);
63+
}
64+
65+
[Fact]
66+
public async Task GitHubAuthentication_GetAuthenticationAsync_AuthenticationModesAll_RequiresInteraction()
67+
{
68+
var context = new TestCommandContext();
6069
context.Settings.IsInteractionAllowed = false;
6170
var auth = new GitHubAuthentication(context);
6271
var exception = await Assert.ThrowsAsync<InvalidOperationException>(
6372
() => auth.GetAuthenticationAsync(new Uri("https://github.com"), null, AuthenticationModes.All)
6473
);
6574
Assert.Equal("Cannot prompt because user interactivity has been disabled.", exception.Message);
6675
}
76+
77+
[Fact]
78+
public async Task GitHubAuthentication_GetAuthenticationAsync_Helper_Basic()
79+
{
80+
var context = new TestCommandContext();
81+
context.FileSystem.Files["/usr/local/bin/GitHub.UI"] = new byte[0];
82+
context.FileSystem.Files[@"C:\Program Files\Git Credential Manager Core\GitHub.UI.exe"] = new byte[0];
83+
context.SessionManager.IsDesktopSession = true;
84+
var auth = new Mock<GitHubAuthentication>(MockBehavior.Strict, context);
85+
auth.Setup(x => x.InvokeHelperAsync(It.IsAny<string>(), "prompt --all", It.IsAny<IDictionary<string, string>>(), It.IsAny<System.Threading.CancellationToken>()))
86+
.Returns(Task.FromResult<IDictionary<string, string>>(
87+
new Dictionary<string, string>
88+
{
89+
["mode"] = "basic",
90+
["username"] = "tim",
91+
["password"] = "hunter2"
92+
}));
93+
var result = await auth.Object.GetAuthenticationAsync(new Uri("https://github.com"), null, AuthenticationModes.All);
94+
Assert.Equal(AuthenticationModes.Basic, result.AuthenticationMode);
95+
Assert.Equal("tim", result.Credential.Account);
96+
Assert.Equal("hunter2", result.Credential.Password);
97+
}
6798
}
6899
}

src/shared/GitHub/GitHubAuthentication.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Threading.Tasks;
33
using System.Collections.Generic;
4-
using System.Globalization;
54
using System.Net.Http;
65
using System.Text;
76
using System.Threading;

0 commit comments

Comments
 (0)