14
14
15
15
using Microsoft . Azure . Commands . Common . Authentication ;
16
16
using Microsoft . Azure . Commands . Common . Authentication . Abstractions ;
17
- using Microsoft . Azure . Commands . Common . Authentication . Abstractions . Interfaces ;
18
17
using Microsoft . Azure . Commands . Common . Authentication . Models ;
19
18
using Microsoft . Azure . Commands . Profile ;
20
19
using Microsoft . Azure . Commands . Profile . Models ;
@@ -40,18 +39,14 @@ namespace Microsoft.Azure.Commands.ResourceManager.Common.Test
40
39
{
41
40
public class AccessTokenCmdletTests
42
41
{
43
- private GetAzureRmAccessTokenCommand cmdlet ;
44
42
private Mock < IAuthenticationFactory > factoryMock = new Mock < IAuthenticationFactory > ( ) ;
45
43
private MockCommandRuntime mockedCommandRuntime ;
46
44
private IAuthenticationFactory previousFactory = null ;
47
45
48
46
private string tenantId = Guid . NewGuid ( ) . ToString ( ) ;
49
47
50
- public AccessTokenCmdletTests ( ITestOutputHelper output )
48
+ private GetAzureRmAccessTokenCommand CreateCommand ( )
51
49
{
52
- TestExecutionHelpers . SetUpSessionAndProfile ( ) ;
53
- XunitTracingInterceptor . AddToContext ( new XunitTracingInterceptor ( output ) ) ;
54
- AzureSession . Instance . RegisterComponent < AuthenticationTelemetry > ( AuthenticationTelemetry . Name , ( ) => new AuthenticationTelemetry ( ) ) ;
55
50
var defaultContext = new AzureContext (
56
51
new AzureSubscription ( )
57
52
{
@@ -70,29 +65,40 @@ public AccessTokenCmdletTests(ITestOutputHelper output)
70
65
} ) ;
71
66
72
67
mockedCommandRuntime = new MockCommandRuntime ( ) ;
73
- cmdlet = new GetAzureRmAccessTokenCommand ( )
68
+ var cmdlet = new GetAzureRmAccessTokenCommand ( )
74
69
{
75
70
CommandRuntime = mockedCommandRuntime ,
76
71
DefaultProfile = new AzureRmProfile ( )
77
72
} ;
78
73
cmdlet . DefaultProfile . DefaultContext = defaultContext ;
74
+ return cmdlet ;
75
+ }
76
+
77
+ public AccessTokenCmdletTests ( ITestOutputHelper output )
78
+ {
79
+ TestExecutionHelpers . SetUpSessionAndProfile ( ) ;
80
+ XunitTracingInterceptor . AddToContext ( new XunitTracingInterceptor ( output ) ) ;
81
+ AzureSession . Instance . RegisterComponent < AuthenticationTelemetry > ( AuthenticationTelemetry . Name , ( ) => new AuthenticationTelemetry ( ) ) ;
82
+
79
83
}
80
84
81
85
[ Fact ]
82
86
[ Trait ( Category . AcceptanceType , Category . CheckIn ) ]
83
87
public void TestGetAccessTokenAsPlainText ( )
84
88
{
85
89
// Setup
90
+ var cmdlet = CreateCommand ( ) ;
86
91
cmdlet . TenantId = tenantId ;
87
92
var fakeToken = "eyfaketoken.eyfaketoken" ;
88
93
Environment . SetEnvironmentVariable ( Constants . AzPsOutputPlainTextAccessToken , bool . TrueString ) ;
89
94
90
- var expected = new PSAccessToken {
95
+ var expected = new PSAccessToken
96
+ {
91
97
92
98
TenantId = cmdlet . TenantId ,
93
99
Token = fakeToken
94
100
} ;
95
-
101
+
96
102
factoryMock . Setup ( t => t . Authenticate (
97
103
It . IsAny < IAzureAccount > ( ) ,
98
104
It . IsAny < IAzureEnvironment > ( ) ,
@@ -132,6 +138,110 @@ public void TestGetAccessTokenAsPlainText()
132
138
public void TestGetAccessTokenAsSecureString ( )
133
139
{
134
140
// Setup
141
+ var cmdlet = CreateCommand ( ) ;
142
+ cmdlet . TenantId = tenantId ;
143
+ var fakeToken = "eyfaketoken.eyfaketoken" ;
144
+
145
+ var expected = new PSSecureAccessToken ( ) ;
146
+ expected . UserId = "[email protected] " ;
147
+ expected . TenantId = cmdlet . TenantId ;
148
+ expected . Token = fakeToken . ConvertToSecureString ( ) ;
149
+
150
+
151
+ factoryMock . Setup ( t => t . Authenticate (
152
+ It . IsAny < IAzureAccount > ( ) ,
153
+ It . IsAny < IAzureEnvironment > ( ) ,
154
+ It . IsAny < string > ( ) ,
155
+ It . IsAny < SecureString > ( ) ,
156
+ It . IsAny < string > ( ) ,
157
+ It . IsAny < Action < string > > ( ) ,
158
+ It . IsAny < IDictionary < string , object > > ( ) ) ) . Returns ( new MockAccessToken
159
+ {
160
+ UserId = expected . UserId ,
161
+ LoginType = LoginType . OrgId ,
162
+ AccessToken = fakeToken ,
163
+ TenantId = expected . TenantId
164
+ } ) ;
165
+ previousFactory = AzureSession . Instance . AuthenticationFactory ;
166
+ AzureSession . Instance . AuthenticationFactory = factoryMock . Object ;
167
+
168
+ // Act
169
+ cmdlet . InvokeBeginProcessing ( ) ;
170
+ cmdlet . ExecuteCmdlet ( ) ;
171
+ cmdlet . InvokeEndProcessing ( ) ;
172
+
173
+ //Verify
174
+ Assert . Single ( mockedCommandRuntime . OutputPipeline ) ;
175
+ var outputPipeline = mockedCommandRuntime . OutputPipeline ;
176
+ Assert . Equal ( expected . TenantId , ( ( PSSecureAccessToken ) outputPipeline . First ( ) ) . TenantId ) ;
177
+ Assert . Equal ( expected . UserId , ( ( PSSecureAccessToken ) outputPipeline . First ( ) ) . UserId ) ;
178
+ Assert . Equal ( "Bearer" , ( ( PSSecureAccessToken ) outputPipeline . First ( ) ) . Type ) ;
179
+ var expectedToken = expected . Token . ConvertToString ( ) ;
180
+ var actualToken = ( ( PSSecureAccessToken ) outputPipeline . First ( ) ) . Token . ConvertToString ( ) ;
181
+ Assert . Equal ( expectedToken , actualToken ) ;
182
+
183
+ AzureSession . Instance . AuthenticationFactory = previousFactory ;
184
+ }
185
+
186
+ [ Fact ]
187
+ [ Trait ( Category . AcceptanceType , Category . CheckIn ) ]
188
+ public void TestGetAccessTokenAsSecureStringWhenHasEnvVarAndAsSecureString ( )
189
+ {
190
+ // Setup
191
+ var cmdlet = CreateCommand ( ) ;
192
+ cmdlet . TenantId = tenantId ;
193
+ cmdlet . AsSecureString = true ;
194
+ var fakeToken = "eyfaketoken.eyfaketoken" ;
195
+ Environment . SetEnvironmentVariable ( Constants . AzPsOutputPlainTextAccessToken , bool . TrueString ) ;
196
+
197
+ var expected = new PSSecureAccessToken ( ) ;
198
+ expected . UserId = "[email protected] " ;
199
+ expected . TenantId = cmdlet . TenantId ;
200
+ expected . Token = fakeToken . ConvertToSecureString ( ) ;
201
+
202
+
203
+ factoryMock . Setup ( t => t . Authenticate (
204
+ It . IsAny < IAzureAccount > ( ) ,
205
+ It . IsAny < IAzureEnvironment > ( ) ,
206
+ It . IsAny < string > ( ) ,
207
+ It . IsAny < SecureString > ( ) ,
208
+ It . IsAny < string > ( ) ,
209
+ It . IsAny < Action < string > > ( ) ,
210
+ It . IsAny < IDictionary < string , object > > ( ) ) ) . Returns ( new MockAccessToken
211
+ {
212
+ UserId = expected . UserId ,
213
+ LoginType = LoginType . OrgId ,
214
+ AccessToken = fakeToken ,
215
+ TenantId = expected . TenantId
216
+ } ) ;
217
+ previousFactory = AzureSession . Instance . AuthenticationFactory ;
218
+ AzureSession . Instance . AuthenticationFactory = factoryMock . Object ;
219
+
220
+ // Act
221
+ cmdlet . InvokeBeginProcessing ( ) ;
222
+ cmdlet . ExecuteCmdlet ( ) ;
223
+ cmdlet . InvokeEndProcessing ( ) ;
224
+
225
+ //Verify
226
+ Assert . Single ( mockedCommandRuntime . OutputPipeline ) ;
227
+ var outputPipeline = mockedCommandRuntime . OutputPipeline ;
228
+ Assert . Equal ( expected . TenantId , ( ( PSSecureAccessToken ) outputPipeline . First ( ) ) . TenantId ) ;
229
+ Assert . Equal ( expected . UserId , ( ( PSSecureAccessToken ) outputPipeline . First ( ) ) . UserId ) ;
230
+ Assert . Equal ( "Bearer" , ( ( PSSecureAccessToken ) outputPipeline . First ( ) ) . Type ) ;
231
+ var expectedToken = expected . Token . ConvertToString ( ) ;
232
+ var actualToken = ( ( PSSecureAccessToken ) outputPipeline . First ( ) ) . Token . ConvertToString ( ) ;
233
+ Assert . Equal ( expectedToken , actualToken ) ;
234
+
235
+ Environment . SetEnvironmentVariable ( Constants . AzPsOutputPlainTextAccessToken , null ) ;
236
+ AzureSession . Instance . AuthenticationFactory = previousFactory ;
237
+ }
238
+
239
+ [ Fact ]
240
+ [ Trait ( Category . AcceptanceType , Category . CheckIn ) ]
241
+ public void TestGetAccessTokenAsSecureStringWhenHasSecureString ( )
242
+ {
243
+ // Setup
244
+ var cmdlet = CreateCommand ( ) ;
135
245
cmdlet . TenantId = tenantId ;
136
246
cmdlet . AsSecureString = true ;
137
247
var fakeToken = "eyfaketoken.eyfaketoken" ;
0 commit comments