|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using GitCredentialManager.Tests.Objects; |
| 6 | +using Moq; |
| 7 | +using Moq.Protected; |
| 8 | +using Xunit; |
| 9 | + |
| 10 | +namespace GitHub.Tests |
| 11 | +{ |
| 12 | + public class GitHubAuthenticationTests |
| 13 | + { |
| 14 | + [Fact] |
| 15 | + public async Task GitHubAuthentication_GetAuthenticationAsync_AuthenticationModesNone_ThrowsException() |
| 16 | + { |
| 17 | + var context = new TestCommandContext(); |
| 18 | + var auth = new GitHubAuthentication(context); |
| 19 | + await Assert.ThrowsAsync<ArgumentException>("modes", |
| 20 | + () => auth.GetAuthenticationAsync(null, null, AuthenticationModes.None) |
| 21 | + ); |
| 22 | + } |
| 23 | + |
| 24 | + [Theory] |
| 25 | + [InlineData(AuthenticationModes.Browser)] |
| 26 | + [InlineData(AuthenticationModes.Device)] |
| 27 | + public async Task GitHubAuthentication_GetAuthenticationAsync_SingleChoice_TerminalAndInteractionNotRequired(GitHub.AuthenticationModes modes) |
| 28 | + { |
| 29 | + var context = new TestCommandContext(); |
| 30 | + context.Settings.IsTerminalPromptsEnabled = false; |
| 31 | + context.Settings.IsInteractionAllowed = false; |
| 32 | + context.SessionManager.IsDesktopSession = true; // necessary for browser |
| 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]; |
| 35 | + var auth = new GitHubAuthentication(context); |
| 36 | + var result = await auth.GetAuthenticationAsync(null, null, modes); |
| 37 | + Assert.Equal(modes, result.AuthenticationMode); |
| 38 | + } |
| 39 | + |
| 40 | + [Fact] |
| 41 | + public async Task GitHubAuthentication_GetAuthenticationAsync_TerminalPromptsDisabled_Throws() |
| 42 | + { |
| 43 | + var context = new TestCommandContext(); |
| 44 | + context.Settings.IsTerminalPromptsEnabled = false; |
| 45 | + var auth = new GitHubAuthentication(context); |
| 46 | + var exception = await Assert.ThrowsAsync<InvalidOperationException>( |
| 47 | + () => auth.GetAuthenticationAsync(null, null, AuthenticationModes.All) |
| 48 | + ); |
| 49 | + Assert.Equal("Cannot prompt because terminal prompts have been disabled.", exception.Message); |
| 50 | + } |
| 51 | + |
| 52 | + // reproduces https://github.com/GitCredentialManager/git-credential-manager/issues/453 |
| 53 | + [Fact] |
| 54 | + public async Task GitHubAuthentication_GetAuthenticationAsync_Terminal() |
| 55 | + { |
| 56 | + var context = new TestCommandContext(); |
| 57 | + context.FileSystem.Files["/usr/local/bin/GitHub.UI"] = new byte[0]; |
| 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(); |
| 69 | + context.Settings.IsInteractionAllowed = false; |
| 70 | + var auth = new GitHubAuthentication(context); |
| 71 | + var exception = await Assert.ThrowsAsync<InvalidOperationException>( |
| 72 | + () => auth.GetAuthenticationAsync(new Uri("https://github.com"), null, AuthenticationModes.All) |
| 73 | + ); |
| 74 | + Assert.Equal("Cannot prompt because user interactivity has been disabled.", exception.Message); |
| 75 | + } |
| 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 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments