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