1
1
using System ;
2
+ using System . Collections . Generic ;
3
+ using System . Threading . Tasks ;
2
4
using GitCredentialManager . Authentication ;
3
5
using GitCredentialManager . Tests . Objects ;
4
6
using Moq ;
@@ -14,11 +16,11 @@ public void BasicAuthentication_GetCredentials_NullResource_ThrowsException()
14
16
var context = new TestCommandContext ( ) ;
15
17
var basicAuth = new BasicAuthentication ( context ) ;
16
18
17
- Assert . Throws < ArgumentNullException > ( ( ) => basicAuth . GetCredentials ( null ) ) ;
19
+ Assert . ThrowsAsync < ArgumentNullException > ( ( ) => basicAuth . GetCredentialsAsync ( null ) ) ;
18
20
}
19
21
20
22
[ Fact ]
21
- public void BasicAuthentication_GetCredentials_NonDesktopSession_ResourceAndUserName_PasswordPromptReturnsCredentials ( )
23
+ public async Task BasicAuthentication_GetCredentials_NonDesktopSession_ResourceAndUserName_PasswordPromptReturnsCredentials ( )
22
24
{
23
25
const string testResource = "https://example.com" ;
24
26
const string testUserName = "john.doe" ;
@@ -29,14 +31,14 @@ public void BasicAuthentication_GetCredentials_NonDesktopSession_ResourceAndUser
29
31
30
32
var basicAuth = new BasicAuthentication ( context ) ;
31
33
32
- ICredential credential = basicAuth . GetCredentials ( testResource , testUserName ) ;
34
+ ICredential credential = await basicAuth . GetCredentialsAsync ( testResource , testUserName ) ;
33
35
34
36
Assert . Equal ( testUserName , credential . Account ) ;
35
37
Assert . Equal ( testPassword , credential . Password ) ;
36
38
}
37
39
38
40
[ Fact ]
39
- public void BasicAuthentication_GetCredentials_NonDesktopSession_Resource_UserPassPromptReturnsCredentials ( )
41
+ public async Task BasicAuthentication_GetCredentials_NonDesktopSession_Resource_UserPassPromptReturnsCredentials ( )
40
42
{
41
43
const string testResource = "https://example.com" ;
42
44
const string testUserName = "john.doe" ;
@@ -48,7 +50,7 @@ public void BasicAuthentication_GetCredentials_NonDesktopSession_Resource_UserPa
48
50
49
51
var basicAuth = new BasicAuthentication ( context ) ;
50
52
51
- ICredential credential = basicAuth . GetCredentials ( testResource ) ;
53
+ ICredential credential = await basicAuth . GetCredentialsAsync ( testResource ) ;
52
54
53
55
Assert . Equal ( testUserName , credential . Account ) ;
54
56
Assert . Equal ( testPassword , credential . Password ) ;
@@ -67,100 +69,78 @@ public void BasicAuthentication_GetCredentials_NonDesktopSession_NoTerminalPromp
67
69
68
70
var basicAuth = new BasicAuthentication ( context ) ;
69
71
70
- Assert . Throws < InvalidOperationException > ( ( ) => basicAuth . GetCredentials ( testResource ) ) ;
72
+ Assert . ThrowsAsync < InvalidOperationException > ( ( ) => basicAuth . GetCredentialsAsync ( testResource ) ) ;
71
73
}
72
74
73
- [ PlatformFact ( Platforms . Windows ) ]
74
- public void BasicAuthentication_GetCredentials_DesktopSession_Resource_UserPassPromptReturnsCredentials ( )
75
+ [ Fact ]
76
+ public async Task BasicAuthentication_GetCredentials_DesktopSession_CallsHelper ( )
75
77
{
76
78
const string testResource = "https://example.com" ;
77
79
const string testUserName = "john.doe" ;
78
80
const string testPassword = "letmein123" ; // [SuppressMessage("Microsoft.Security", "CS001:SecretInline", Justification="Fake credential")]
79
81
80
82
var context = new TestCommandContext
81
83
{
82
- SessionManager = { IsDesktopSession = true } ,
83
- SystemPrompts =
84
- {
85
- CredentialPrompt = ( resource , userName ) =>
86
- {
87
- Assert . Equal ( testResource , resource ) ;
88
- Assert . Null ( userName ) ;
89
-
90
- return new GitCredential ( testUserName , testPassword ) ;
91
- }
92
- }
84
+ SessionManager = { IsDesktopSession = true }
93
85
} ;
94
86
95
- var basicAuth = new BasicAuthentication ( context ) ;
96
-
97
- ICredential credential = basicAuth . GetCredentials ( testResource ) ;
98
-
99
- Assert . NotNull ( credential ) ;
100
- Assert . Equal ( testUserName , credential . Account ) ;
101
- Assert . Equal ( testPassword , credential . Password ) ;
102
- }
103
-
104
- [ PlatformFact ( Platforms . Windows ) ]
105
- public void BasicAuthentication_GetCredentials_DesktopSession_ResourceAndUser_PassPromptReturnsCredentials ( )
106
- {
107
- const string testResource = "https://example.com" ;
108
- const string testUserName = "john.doe" ;
109
- const string testPassword = "letmein123" ; // [SuppressMessage("Microsoft.Security", "CS001:SecretInline", Justification="Fake credential")]
110
-
111
- var context = new TestCommandContext
112
- {
113
- SessionManager = { IsDesktopSession = true } ,
114
- SystemPrompts =
115
- {
116
- CredentialPrompt = ( resource , userName ) =>
87
+ context . FileSystem . Files [ "/usr/local/bin/git-credential-manager-ui" ] = new byte [ 0 ] ;
88
+ context . FileSystem . Files [ @"C:\Program Files\Git Credential Manager Core\git-credential-manager-ui.exe" ] = new byte [ 0 ] ;
89
+
90
+ var auth = new Mock < BasicAuthentication > ( MockBehavior . Strict , context ) ;
91
+ auth . Setup ( x => x . InvokeHelperAsync (
92
+ It . IsAny < string > ( ) ,
93
+ $ "basic --resource { testResource } ",
94
+ It . IsAny < IDictionary < string , string > > ( ) ,
95
+ It . IsAny < System . Threading . CancellationToken > ( ) ) )
96
+ . ReturnsAsync (
97
+ new Dictionary < string , string >
117
98
{
118
- Assert . Equal ( testResource , resource ) ;
119
- Assert . Equal ( testUserName , userName ) ;
120
-
121
- return new GitCredential ( testUserName , testPassword ) ;
99
+ [ "username" ] = testUserName ,
100
+ [ "password" ] = testPassword
122
101
}
123
- }
124
- } ;
125
-
126
- var basicAuth = new BasicAuthentication ( context ) ;
102
+ ) ;
127
103
128
- ICredential credential = basicAuth . GetCredentials ( testResource , testUserName ) ;
104
+ ICredential credential = await auth . Object . GetCredentialsAsync ( testResource ) ;
129
105
130
106
Assert . NotNull ( credential ) ;
131
107
Assert . Equal ( testUserName , credential . Account ) ;
132
108
Assert . Equal ( testPassword , credential . Password ) ;
133
109
}
134
110
135
- [ PlatformFact ( Platforms . Windows ) ]
136
- public void BasicAuthentication_GetCredentials_DesktopSession_ResourceAndUser_PassPromptDiffUserReturnsCredentials ( )
111
+ [ Fact ]
112
+ public async Task BasicAuthentication_GetCredentials_DesktopSession_UserName_CallsHelper ( )
137
113
{
138
114
const string testResource = "https://example.com" ;
139
115
const string testUserName = "john.doe" ;
140
- const string newUserName = "jane.doe" ;
141
116
const string testPassword = "letmein123" ; // [SuppressMessage("Microsoft.Security", "CS001:SecretInline", Justification="Fake credential")]
142
117
143
118
var context = new TestCommandContext
144
119
{
145
- SessionManager = { IsDesktopSession = true } ,
146
- SystemPrompts =
147
- {
148
- CredentialPrompt = ( resource , userName ) =>
149
- {
150
- Assert . Equal ( testResource , resource ) ;
151
- Assert . Equal ( testUserName , userName ) ;
152
-
153
- return new GitCredential ( newUserName , testPassword ) ;
154
- }
155
- }
120
+ SessionManager = { IsDesktopSession = true }
156
121
} ;
157
122
158
- var basicAuth = new BasicAuthentication ( context ) ;
123
+ context . FileSystem . Files [ "/usr/local/bin/git-credential-manager-ui" ] = new byte [ 0 ] ;
124
+ context . FileSystem . Files [ @"C:\Program Files\Git Credential Manager Core\git-credential-manager-ui.exe" ] = new byte [ 0 ] ;
125
+
126
+ var auth = new Mock < BasicAuthentication > ( MockBehavior . Strict , context ) ;
127
+ auth . Setup ( x => x . InvokeHelperAsync (
128
+ It . IsAny < string > ( ) ,
129
+ $ "basic --resource { testResource } --username { testUserName } ",
130
+ It . IsAny < IDictionary < string , string > > ( ) ,
131
+ It . IsAny < System . Threading . CancellationToken > ( ) ) )
132
+ . ReturnsAsync (
133
+ new Dictionary < string , string >
134
+ {
135
+ [ "username" ] = testUserName ,
136
+ [ "password" ] = testPassword
137
+ }
138
+ ) ;
159
139
160
- ICredential credential = basicAuth . GetCredentials ( testResource , testUserName ) ;
140
+ ICredential credential = await auth . Object . GetCredentialsAsync ( testResource , testUserName ) ;
161
141
162
142
Assert . NotNull ( credential ) ;
163
- Assert . Equal ( newUserName , credential . Account ) ;
143
+ Assert . Equal ( testUserName , credential . Account ) ;
164
144
Assert . Equal ( testPassword , credential . Password ) ;
165
145
}
166
146
}
0 commit comments