1414
1515using Microsoft . Azure . Commands . Common . Authentication ;
1616using Microsoft . Azure . Commands . Common . Authentication . Abstractions ;
17- using Microsoft . Azure . Commands . Common . Authentication . Abstractions . Interfaces ;
1817using Microsoft . Azure . Commands . Common . Authentication . Models ;
1918using Microsoft . Azure . Commands . Profile ;
2019using Microsoft . Azure . Commands . Profile . Models ;
@@ -40,18 +39,14 @@ namespace Microsoft.Azure.Commands.ResourceManager.Common.Test
4039{
4140 public class AccessTokenCmdletTests
4241 {
43- private GetAzureRmAccessTokenCommand cmdlet ;
4442 private Mock < IAuthenticationFactory > factoryMock = new Mock < IAuthenticationFactory > ( ) ;
4543 private MockCommandRuntime mockedCommandRuntime ;
4644 private IAuthenticationFactory previousFactory = null ;
4745
4846 private string tenantId = Guid . NewGuid ( ) . ToString ( ) ;
4947
50- public AccessTokenCmdletTests ( ITestOutputHelper output )
48+ private GetAzureRmAccessTokenCommand CreateCommand ( )
5149 {
52- TestExecutionHelpers . SetUpSessionAndProfile ( ) ;
53- XunitTracingInterceptor . AddToContext ( new XunitTracingInterceptor ( output ) ) ;
54- AzureSession . Instance . RegisterComponent < AuthenticationTelemetry > ( AuthenticationTelemetry . Name , ( ) => new AuthenticationTelemetry ( ) ) ;
5550 var defaultContext = new AzureContext (
5651 new AzureSubscription ( )
5752 {
@@ -70,29 +65,40 @@ public AccessTokenCmdletTests(ITestOutputHelper output)
7065 } ) ;
7166
7267 mockedCommandRuntime = new MockCommandRuntime ( ) ;
73- cmdlet = new GetAzureRmAccessTokenCommand ( )
68+ var cmdlet = new GetAzureRmAccessTokenCommand ( )
7469 {
7570 CommandRuntime = mockedCommandRuntime ,
7671 DefaultProfile = new AzureRmProfile ( )
7772 } ;
7873 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+
7983 }
8084
8185 [ Fact ]
8286 [ Trait ( Category . AcceptanceType , Category . CheckIn ) ]
8387 public void TestGetAccessTokenAsPlainText ( )
8488 {
8589 // Setup
90+ var cmdlet = CreateCommand ( ) ;
8691 cmdlet . TenantId = tenantId ;
8792 var fakeToken = "eyfaketoken.eyfaketoken" ;
8893 Environment . SetEnvironmentVariable ( Constants . AzPsOutputPlainTextAccessToken , bool . TrueString ) ;
8994
90- var expected = new PSAccessToken {
95+ var expected = new PSAccessToken
96+ {
9197 UserId = "faker@contoso.com" ,
9298 TenantId = cmdlet . TenantId ,
9399 Token = fakeToken
94100 } ;
95-
101+
96102 factoryMock . Setup ( t => t . Authenticate (
97103 It . IsAny < IAzureAccount > ( ) ,
98104 It . IsAny < IAzureEnvironment > ( ) ,
@@ -132,6 +138,110 @@ public void TestGetAccessTokenAsPlainText()
132138 public void TestGetAccessTokenAsSecureString ( )
133139 {
134140 // Setup
141+ var cmdlet = CreateCommand ( ) ;
142+ cmdlet . TenantId = tenantId ;
143+ var fakeToken = "eyfaketoken.eyfaketoken" ;
144+
145+ var expected = new PSSecureAccessToken ( ) ;
146+ expected . UserId = "faker@contoso.com" ;
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 = "faker@contoso.com" ;
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 ( ) ;
135245 cmdlet . TenantId = tenantId ;
136246 cmdlet . AsSecureString = true ;
137247 var fakeToken = "eyfaketoken.eyfaketoken" ;
0 commit comments