3
3
using System ;
4
4
using Microsoft . Git . CredentialManager . Authentication ;
5
5
using Microsoft . Git . CredentialManager . Tests . Objects ;
6
+ using Moq ;
6
7
using Xunit ;
7
8
8
9
namespace Microsoft . Git . CredentialManager . Tests . Authentication
@@ -19,49 +20,50 @@ public void BasicAuthentication_GetCredentials_NullResource_ThrowsException()
19
20
}
20
21
21
22
[ Fact ]
22
- public void BasicAuthentication_GetCredentials_ResourceAndUserName_PasswordPromptReturnsCredentials ( )
23
+ public void BasicAuthentication_GetCredentials_NonDesktopSession_ResourceAndUserName_PasswordPromptReturnsCredentials ( )
23
24
{
24
25
const string testResource = "https://example.com" ;
25
26
const string testUserName = "john.doe" ;
26
27
const string testPassword = "letmein123" ;
27
28
28
- var context = new TestCommandContext ( ) ;
29
+ var context = new TestCommandContext { IsDesktopSession = false } ;
29
30
context . Terminal . SecretPrompts [ "Password" ] = testPassword ;
30
31
31
32
var basicAuth = new BasicAuthentication ( context ) ;
32
33
33
- GitCredential credential = basicAuth . GetCredentials ( testResource , testUserName ) ;
34
+ ICredential credential = basicAuth . GetCredentials ( testResource , testUserName ) ;
34
35
35
36
Assert . Equal ( testUserName , credential . UserName ) ;
36
37
Assert . Equal ( testPassword , credential . Password ) ;
37
38
}
38
39
39
40
[ Fact ]
40
- public void BasicAuthentication_GetCredentials_Resource_UserPassPromptReturnsCredentials ( )
41
+ public void BasicAuthentication_GetCredentials_NonDesktopSession_Resource_UserPassPromptReturnsCredentials ( )
41
42
{
42
43
const string testResource = "https://example.com" ;
43
44
const string testUserName = "john.doe" ;
44
45
const string testPassword = "letmein123" ;
45
46
46
- var context = new TestCommandContext ( ) ;
47
+ var context = new TestCommandContext { IsDesktopSession = false } ;
47
48
context . Terminal . Prompts [ "Username" ] = testUserName ;
48
49
context . Terminal . SecretPrompts [ "Password" ] = testPassword ;
49
50
50
51
var basicAuth = new BasicAuthentication ( context ) ;
51
52
52
- GitCredential credential = basicAuth . GetCredentials ( testResource ) ;
53
+ ICredential credential = basicAuth . GetCredentials ( testResource ) ;
53
54
54
55
Assert . Equal ( testUserName , credential . UserName ) ;
55
56
Assert . Equal ( testPassword , credential . Password ) ;
56
57
}
57
58
58
59
[ Fact ]
59
- public void BasicAuthentication_GetCredentials_NoInteraction_ThrowsException ( )
60
+ public void BasicAuthentication_GetCredentials_NonDesktopSession_NoTerminalPrompts_ThrowsException ( )
60
61
{
61
62
const string testResource = "https://example.com" ;
62
63
63
64
var context = new TestCommandContext
64
65
{
66
+ IsDesktopSession = false ,
65
67
Settings = { IsInteractionAllowed = false } ,
66
68
} ;
67
69
@@ -70,19 +72,98 @@ public void BasicAuthentication_GetCredentials_NoInteraction_ThrowsException()
70
72
Assert . Throws < InvalidOperationException > ( ( ) => basicAuth . GetCredentials ( testResource ) ) ;
71
73
}
72
74
73
- [ Fact ]
74
- public void BasicAuthentication_GetCredentials_NoTerminalPrompts_ThrowsException ( )
75
+ [ PlatformFact ( Platform . Windows ) ]
76
+ public void BasicAuthentication_GetCredentials_DesktopSession_Resource_UserPassPromptReturnsCredentials ( )
75
77
{
76
78
const string testResource = "https://example.com" ;
79
+ const string testUserName = "john.doe" ;
80
+ const string testPassword = "letmein123" ;
77
81
78
82
var context = new TestCommandContext
79
83
{
80
- Settings = { IsTerminalPromptsEnabled = false } ,
84
+ IsDesktopSession = true ,
85
+ SystemPrompts =
86
+ {
87
+ CredentialPrompt = ( resource , userName ) =>
88
+ {
89
+ Assert . Equal ( testResource , resource ) ;
90
+ Assert . Null ( userName ) ;
91
+
92
+ return new GitCredential ( testUserName , testPassword ) ;
93
+ }
94
+ }
81
95
} ;
82
96
83
97
var basicAuth = new BasicAuthentication ( context ) ;
84
98
85
- Assert . Throws < InvalidOperationException > ( ( ) => basicAuth . GetCredentials ( testResource ) ) ;
99
+ ICredential credential = basicAuth . GetCredentials ( testResource ) ;
100
+
101
+ Assert . NotNull ( credential ) ;
102
+ Assert . Equal ( testUserName , credential . UserName ) ;
103
+ Assert . Equal ( testPassword , credential . Password ) ;
104
+ }
105
+
106
+ [ PlatformFact ( Platform . Windows ) ]
107
+ public void BasicAuthentication_GetCredentials_DesktopSession_ResourceAndUser_PassPromptReturnsCredentials ( )
108
+ {
109
+ const string testResource = "https://example.com" ;
110
+ const string testUserName = "john.doe" ;
111
+ const string testPassword = "letmein123" ;
112
+
113
+ var context = new TestCommandContext
114
+ {
115
+ IsDesktopSession = true ,
116
+ SystemPrompts =
117
+ {
118
+ CredentialPrompt = ( resource , userName ) =>
119
+ {
120
+ Assert . Equal ( testResource , resource ) ;
121
+ Assert . Equal ( testUserName , userName ) ;
122
+
123
+ return new GitCredential ( testUserName , testPassword ) ;
124
+ }
125
+ }
126
+ } ;
127
+
128
+ var basicAuth = new BasicAuthentication ( context ) ;
129
+
130
+ ICredential credential = basicAuth . GetCredentials ( testResource , testUserName ) ;
131
+
132
+ Assert . NotNull ( credential ) ;
133
+ Assert . Equal ( testUserName , credential . UserName ) ;
134
+ Assert . Equal ( testPassword , credential . Password ) ;
135
+ }
136
+
137
+ [ PlatformFact ( Platform . Windows ) ]
138
+ public void BasicAuthentication_GetCredentials_DesktopSession_ResourceAndUser_PassPromptDiffUserReturnsCredentials ( )
139
+ {
140
+ const string testResource = "https://example.com" ;
141
+ const string testUserName = "john.doe" ;
142
+ const string newUserName = "jane.doe" ;
143
+ const string testPassword = "letmein123" ;
144
+
145
+ var context = new TestCommandContext
146
+ {
147
+ IsDesktopSession = true ,
148
+ SystemPrompts =
149
+ {
150
+ CredentialPrompt = ( resource , userName ) =>
151
+ {
152
+ Assert . Equal ( testResource , resource ) ;
153
+ Assert . Equal ( testUserName , userName ) ;
154
+
155
+ return new GitCredential ( newUserName , testPassword ) ;
156
+ }
157
+ }
158
+ } ;
159
+
160
+ var basicAuth = new BasicAuthentication ( context ) ;
161
+
162
+ ICredential credential = basicAuth . GetCredentials ( testResource , testUserName ) ;
163
+
164
+ Assert . NotNull ( credential ) ;
165
+ Assert . Equal ( newUserName , credential . UserName ) ;
166
+ Assert . Equal ( testPassword , credential . Password ) ;
86
167
}
87
168
}
88
169
}
0 commit comments