Skip to content

Commit f2c75d6

Browse files
committed
Add test case file
1 parent 0d8f6b0 commit f2c75d6

File tree

1 file changed

+161
-0
lines changed

1 file changed

+161
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Security;
4+
using System.Threading.Tasks;
5+
using Microsoft.Azure.Commands.Common.Authentication;
6+
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
7+
using Microsoft.Azure.Commands.Common.Authentication.Models;
8+
using Microsoft.Azure.Commands.Profile.Models;
9+
using Microsoft.Identity.Client;
10+
using Microsoft.Rest;
11+
using Moq;
12+
using Xunit;
13+
14+
namespace Microsoft.Azure.Commands.Common.Authentication.Factories.Test
15+
{
16+
public class AuthenticationFactoryTest
17+
{
18+
private Mock<IAzureAccount> _mockAccount;
19+
private Mock<IAzureEnvironment> _mockEnvironment;
20+
private Mock<IAzureSubscription> _mockSubscription;
21+
private Mock<IAzureTenant> _mockTenant;
22+
private Mock<IAzureTokenCache> _mockTokenCache;
23+
private Mock<ICmdletContext> _mockCmdletContext;
24+
private Mock<IAccessToken> _mockAccessToken;
25+
private Mock<IAuthenticatorBuilder> _mockAuthenticatorBuilder;
26+
private Mock<IAuthenticator> _mockAuthenticator;
27+
28+
public AuthenticationFactoryTest()
29+
{
30+
_mockAccount = new Mock<IAzureAccount>();
31+
_mockEnvironment = new Mock<IAzureEnvironment>();
32+
_mockSubscription = new Mock<IAzureSubscription>();
33+
_mockTenant = new Mock<IAzureTenant>();
34+
_mockTokenCache = new Mock<IAzureTokenCache>();
35+
_mockCmdletContext = new Mock<ICmdletContext>();
36+
_mockAccessToken = new Mock<IAccessToken>();
37+
_mockAuthenticatorBuilder = new Mock<IAuthenticatorBuilder>();
38+
_mockAuthenticator = new Mock<IAuthenticator>();
39+
}
40+
41+
[Fact]
42+
public void ConstructorInitializesProperties()
43+
{
44+
// Arrange & Act
45+
var factory = new AuthenticationFactory();
46+
47+
// Assert
48+
Assert.NotNull(factory);
49+
Assert.Null(factory.KeyStore);
50+
Assert.Null(factory.TokenProvider);
51+
}
52+
53+
[Fact]
54+
public void KeyStoreGetterShouldCallAzureSessionTryGetComponent()
55+
{
56+
// Arrange
57+
var mockKeyStore = new Mock<AzKeyStore>();
58+
var factory = new AuthenticationFactory();
59+
60+
// Mock AzureSession's TryGetComponent to return our mock
61+
var originalInstance = AzureSession.Instance;
62+
try
63+
{
64+
var mockAzureSession = new Mock<IAzureSession>();
65+
mockAzureSession.Setup(s => s.TryGetComponent(AzKeyStore.Name, out It.Ref<AzKeyStore>.IsAny))
66+
.Callback(new MockTryGetComponentCallback<AzKeyStore>((string name, out AzKeyStore component) =>
67+
{
68+
component = mockKeyStore.Object;
69+
return true;
70+
}))
71+
.Returns(true);
72+
AzureSession.Instance = mockAzureSession.Object;
73+
74+
// Act
75+
var result = factory.KeyStore;
76+
77+
// Assert
78+
Assert.Equal(mockKeyStore.Object, result);
79+
}
80+
finally
81+
{
82+
// Restore original instance
83+
AzureSession.Instance = originalInstance;
84+
}
85+
}
86+
87+
[Fact]
88+
public void AuthenticateShouldCallAuthenticator()
89+
{
90+
// Arrange
91+
var factory = new AuthenticationFactory();
92+
var tenant = "tenant";
93+
var password = new SecureString();
94+
var promptBehavior = "auto";
95+
Action<string> promptAction = s => { };
96+
var resourceId = "resourceId";
97+
98+
// Setup mocks
99+
_mockAccessToken.Setup(t => t.UserId).Returns("userId");
100+
_mockAccessToken.Setup(t => t.HomeAccountId).Returns("homeAccountId");
101+
102+
_mockAuthenticator.Setup(a => a.TryAuthenticate(It.IsAny<IDictionary<string, object>>(), out It.Ref<Task<IAccessToken>>.IsAny))
103+
.Callback(new MockTryAuthenticateCallback((IDictionary<string, object> parameters, out Task<IAccessToken> accessToken) =>
104+
{
105+
accessToken = Task.FromResult(_mockAccessToken.Object);
106+
}))
107+
.Returns(true);
108+
_mockAuthenticator.Setup(a => a.Next).Returns((IAuthenticator)null);
109+
110+
_mockAuthenticatorBuilder.Setup(b => b.Authenticator).Returns(_mockAuthenticator.Object);
111+
112+
// Mock AzureSession setup
113+
var originalInstance = AzureSession.Instance;
114+
try
115+
{
116+
var mockAzureSession = new Mock<IAzureSession>();
117+
var mockTokenCacheProvider = new Mock<PowerShellTokenCacheProvider>();
118+
119+
mockAzureSession.Setup(s => s.TryGetComponent(PowerShellTokenCacheProvider.PowerShellTokenCacheProviderKey, out It.Ref<PowerShellTokenCacheProvider>.IsAny))
120+
.Callback(new MockTryGetComponentCallback<PowerShellTokenCacheProvider>((string name, out PowerShellTokenCacheProvider component) =>
121+
{
122+
component = mockTokenCacheProvider.Object;
123+
return true;
124+
}))
125+
.Returns(true);
126+
127+
mockAzureSession.Setup(s => s.TryGetComponent(AuthenticatorBuilder.AuthenticatorBuilderKey, out It.Ref<IAuthenticatorBuilder>.IsAny))
128+
.Callback(new MockTryGetComponentCallback<IAuthenticatorBuilder>((string name, out IAuthenticatorBuilder component) =>
129+
{
130+
component = _mockAuthenticatorBuilder.Object;
131+
return true;
132+
}))
133+
.Returns(true);
134+
135+
AzureSession.Instance = mockAzureSession.Object;
136+
137+
// Act
138+
var result = factory.Authenticate(_mockAccount.Object, _mockEnvironment.Object, tenant, password, promptBehavior, promptAction, _mockTokenCache.Object, resourceId);
139+
140+
// Assert
141+
Assert.NotNull(result);
142+
Assert.Equal("userId", result.UserId);
143+
_mockAuthenticator.Verify(a => a.TryAuthenticate(It.IsAny<IDictionary<string, object>>(), out It.Ref<Task<IAccessToken>>.IsAny), Times.Once);
144+
_mockAccount.Verify(a => a.SetProperty(AzureAccount.Property.HomeAccountId, "homeAccountId"), Times.Once);
145+
}
146+
finally
147+
{
148+
// Restore original instance
149+
AzureSession.Instance = originalInstance;
150+
}
151+
}
152+
153+
// More tests would be added for other methods and edge cases
154+
155+
// Helper delegate for mocking TryGetComponent
156+
public delegate void MockTryGetComponentCallback<T>(string name, out T component);
157+
158+
// Helper delegate for mocking TryAuthenticate
159+
public delegate void MockTryAuthenticateCallback(IDictionary<string, object> parameters, out Task<IAccessToken> accessToken);
160+
}
161+
}

0 commit comments

Comments
 (0)