Skip to content

Commit

Permalink
Try to fix the failed test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
msJinLei committed Nov 17, 2022
1 parent 4ded8ba commit 9fa59c9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 36 deletions.
20 changes: 14 additions & 6 deletions src/Accounts/Accounts.Test/AutosaveTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
using Microsoft.Azure.Commands.ScenarioTest;
using Microsoft.Azure.Commands.ResourceManager.Common;
using Microsoft.Azure.Commands.TestFx.Mocks;
using Moq;

namespace Microsoft.Azure.Commands.Profile.Test
{
Expand All @@ -42,6 +43,18 @@ public AutosaveTests(ITestOutputHelper output)
ResetState();
}

private void SetMockedAzKeyStore()
{
var storageMocker = new Mock<IStorage>();
storageMocker.Setup(f => f.Create()).Returns(storageMocker.Object);
storageMocker.Setup(f => f.ReadData()).Returns(new byte[0]);
var keyStore = new AzKeyStore(AzureSession.Instance.ARMProfileDirectory, "keystore.cache", false, false, storageMocker.Object);
AzKeyStore.RegisterJsonConverter(typeof(ServicePrincipalKey), typeof(ServicePrincipalKey).Name);
AzKeyStore.RegisterJsonConverter(typeof(SecureString), typeof(SecureString).Name, new SecureStringConverter());
AzureSession.Instance.RegisterComponent(AzKeyStore.Name, () => keyStore);
keyStore.LoadStorage();
}

void ResetState()
{

Expand All @@ -55,12 +68,7 @@ void ResetState()
Environment.SetEnvironmentVariable("Azure_PS_Data_Collection", "false");
PowerShellTokenCacheProvider tokenProvider = new InMemoryTokenCacheProvider();
AzureSession.Instance.RegisterComponent(PowerShellTokenCacheProvider.PowerShellTokenCacheProviderKey, () => tokenProvider, true);

var keyStore = new AzKeyStore(AzureSession.Instance.ARMProfileDirectory, "keystore.cache", false);
AzKeyStore.RegisterJsonConverter(typeof(ServicePrincipalKey), typeof(ServicePrincipalKey).Name);
AzKeyStore.RegisterJsonConverter(typeof(SecureString), typeof(SecureString).Name, new SecureStringConverter());
AzureSession.Instance.RegisterComponent(AzKeyStore.Name, () => keyStore);
keyStore.LoadStorage();
SetMockedAzKeyStore();
}

[Fact]
Expand Down
41 changes: 13 additions & 28 deletions src/Accounts/Authentication.Test/AzKeyStorageTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,17 @@
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using Azure.Core;
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Commands.Common.Authentication;
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
using Microsoft.Azure.PowerShell.Authenticators;
using Microsoft.Azure.PowerShell.Authenticators.Factories;
using Microsoft.WindowsAzure.Commands.ScenarioTest;
using Moq;
using Xunit;
using Xunit.Abstractions;
using Azure.Identity;
using Microsoft.Azure.Commands.ResourceManager.Common;
using Microsoft.Identity.Client.Extensions.Msal;
using System.Collections.Generic;
using Microsoft.WindowsAzure.Commands.Common;
using System.Text;
using System.Security;
using Microsoft.WindowsAzure.Commands.ScenarioTest;
using Moq;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security;
using System.Text;
using Xunit;

namespace Common.Authenticators.Test
{
Expand All @@ -47,6 +36,7 @@ public AzKeyStorageTest()
{
storageChecker = new List<byte>();
storageMocker = new Mock<IStorage>();
storageMocker.Setup(f => f.Create()).Returns(storageMocker.Object);
storageMocker.Setup(f => f.WriteData(It.IsAny<byte[]>())).Callback((byte[] s) => { storageChecker.Clear(); storageChecker.AddRange(s); });
}

Expand All @@ -65,11 +55,10 @@ private static bool CompareJsonObjects(string expected, string acutal)
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void SaveKey()
{
using (var store = new AzKeyStore(dummpyPath, keyStoreFileName, false))
using (var store = new AzKeyStore(dummpyPath, keyStoreFileName, false, true, storageMocker.Object))
{
AzKeyStore.RegisterJsonConverter(typeof(ServicePrincipalKey), typeof(ServicePrincipalKey).Name);
AzKeyStore.RegisterJsonConverter(typeof(SecureString), typeof(SecureString).Name, new SecureStringConverter());
store.Storage = storageMocker.Object;

IKeyStoreKey servicePrincipalKey = new ServicePrincipalKey("ServicePrincipalSecret", "6c984d31-5b4f-4734-b548-e230a248e347", "54826b22-38d6-4fb2-bad9-b7b93a3e9c5a");
var secret = "secret".ConvertToSecureString();
Expand All @@ -93,11 +82,10 @@ public void FindKey()
{
const string EXPECTED = @"[{""keyType"":""ServicePrincipalKey"",""keyStoreKey"":""{\""appId\"":\""6c984d31-5b4f-4734-b548-e230a248e347\"",\""tenantId\"":\""54826b22-38d6-4fb2-bad9-b7b93a3e9c5a\"",\""name\"":\""ServicePrincipalSecret\""}"",""valueType"":""SecureString"",""keyStoreValue"":""\""secret\""""}]";
storageChecker.AddRange(Encoding.UTF8.GetBytes(EXPECTED));
using (var store = new AzKeyStore(dummpyPath, keyStoreFileName, false))
using (var store = new AzKeyStore(dummpyPath, keyStoreFileName, false, true, storageMocker.Object))
{
AzKeyStore.RegisterJsonConverter(typeof(ServicePrincipalKey), typeof(ServicePrincipalKey).Name);
AzKeyStore.RegisterJsonConverter(typeof(SecureString), typeof(SecureString).Name, new SecureStringConverter());
store.Storage = storageMocker.Object;
storageMocker.Setup(f => f.ReadData()).Returns(storageChecker.ToArray());
store.LoadStorage();

Expand All @@ -115,11 +103,10 @@ public void FindNoKey()
{
const string EXPECTED = @"[{""keyType"":""ServicePrincipalKey"",""keyStoreKey"":""{\""appId\"":\""6c984d31-5b4f-4734-b548-e230a248e347\"",\""tenantId\"":\""54826b22-38d6-4fb2-bad9-b7b93a3e9c5a\"",\""name\"":\""ServicePrincipalSecret\""}"",""valueType"":""SecureString"",""keyStoreValue"":""\""secret\""""}]";
storageChecker.AddRange(Encoding.UTF8.GetBytes(EXPECTED));
using (var store = new AzKeyStore(dummpyPath, keyStoreFileName))
using (var store = new AzKeyStore(dummpyPath, keyStoreFileName, false, true, storageMocker.Object))
{
AzKeyStore.RegisterJsonConverter(typeof(ServicePrincipalKey), typeof(ServicePrincipalKey).Name);
AzKeyStore.RegisterJsonConverter(typeof(SecureString), typeof(SecureString).Name, new SecureStringConverter());
store.Storage = storageMocker.Object;
storageMocker.Setup(f => f.ReadData()).Returns(storageChecker.ToArray());
store.LoadStorage();

Expand All @@ -135,11 +122,10 @@ public void RemoveKey()
{
const string EXPECTED = @"[{""keyType"":""ServicePrincipalKey"",""keyStoreKey"":""{\""appId\"":\""6c984d31-5b4f-4734-b548-e230a248e347\"",\""tenantId\"":\""54826b22-38d6-4fb2-bad9-b7b93a3e9c5a\"",\""name\"":\""ServicePrincipalSecret\""}"",""valueType"":""SecureString"",""keyStoreValue"":""\""secret\""""}]";
storageChecker.AddRange(Encoding.UTF8.GetBytes(EXPECTED));
using (var store = new AzKeyStore(dummpyPath, keyStoreFileName))
using (var store = new AzKeyStore(dummpyPath, keyStoreFileName, false, true, storageMocker.Object))
{
AzKeyStore.RegisterJsonConverter(typeof(ServicePrincipalKey), typeof(ServicePrincipalKey).Name);
AzKeyStore.RegisterJsonConverter(typeof(SecureString), typeof(SecureString).Name, new SecureStringConverter());
store.Storage = storageMocker.Object;
storageMocker.Setup(f => f.ReadData()).Returns(storageChecker.ToArray());
store.LoadStorage();

Expand All @@ -159,11 +145,10 @@ public void RemoveNoKey()
{
const string EXPECTED = @"[{""keyType"":""ServicePrincipalKey"",""keyStoreKey"":""{\""appId\"":\""6c984d31-5b4f-4734-b548-e230a248e347\"",\""tenantId\"":\""54826b22-38d6-4fb2-bad9-b7b93a3e9c5a\"",\""name\"":\""ServicePrincipalSecret\""}"",""valueType"":""SecureString"",""keyStoreValue"":""\""secret\""""}]";
storageChecker.AddRange(Encoding.UTF8.GetBytes(EXPECTED));
using (var store = new AzKeyStore(dummpyPath, keyStoreFileName))
using (var store = new AzKeyStore(dummpyPath, keyStoreFileName, false, true, storageMocker.Object))
{
AzKeyStore.RegisterJsonConverter(typeof(ServicePrincipalKey), typeof(ServicePrincipalKey).Name);
AzKeyStore.RegisterJsonConverter(typeof(SecureString), typeof(SecureString).Name, new SecureStringConverter());
store.Storage = storageMocker.Object;
storageMocker.Setup(f => f.ReadData()).Returns(storageChecker.ToArray());
store.LoadStorage();

Expand Down
4 changes: 2 additions & 2 deletions src/Accounts/Authentication/KeyStore/AzKeyStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ public AzKeyStore()

}

public AzKeyStore(string directory, string fileName, bool loadStorage = true, bool autoSaveEnabled = true)
public AzKeyStore(string directory, string fileName, bool loadStorage = true, bool autoSaveEnabled = true, IStorage inputStorage = null)
{
autoSave = autoSaveEnabled;
Storage = new StorageWrapper()
Storage = inputStorage ?? new StorageWrapper()
{
FileName = fileName,
Directory = directory
Expand Down

0 comments on commit 9fa59c9

Please sign in to comment.