From c597c64337780dce8985ea4c70f3e51808ef3cbc Mon Sep 17 00:00:00 2001 From: sfc-gh-ext-simba-lf Date: Fri, 19 Apr 2024 15:45:33 -0700 Subject: [PATCH] Refactor test and rename file --- .../SFCredentialManagerTest.cs | 200 +++++++++++------- .../UnitTests/SFSessionTest.cs | 4 +- ... => SnowflakeCredentialManagerFileImpl.cs} | 8 +- 3 files changed, 124 insertions(+), 88 deletions(-) rename Snowflake.Data/Client/SnowflakeCredentialManager/{SnowflakeCredentialManagerIFileImpl.cs => SnowflakeCredentialManagerFileImpl.cs} (90%) diff --git a/Snowflake.Data.Tests/UnitTests/CredentialManager/SFCredentialManagerTest.cs b/Snowflake.Data.Tests/UnitTests/CredentialManager/SFCredentialManagerTest.cs index 85a049e11..40dbe6ada 100644 --- a/Snowflake.Data.Tests/UnitTests/CredentialManager/SFCredentialManagerTest.cs +++ b/Snowflake.Data.Tests/UnitTests/CredentialManager/SFCredentialManagerTest.cs @@ -1,8 +1,8 @@ -/* +/* * Copyright (c) 2024 Snowflake Computing Inc. All rights reserved. */ -namespace Snowflake.Data.Tests.UnitTests +namespace Snowflake.Data.Tests.UnitTests.CredentialManager { using Mono.Unix; using Mono.Unix.Native; @@ -14,6 +14,114 @@ namespace Snowflake.Data.Tests.UnitTests using System.IO; using System.Runtime.InteropServices; + public abstract class SFBaseCredentialManagerTest + { + protected ISnowflakeCredentialManager _credentialManager; + + [Test] + public void TestSavingAndRemovingCredentials() + { + // arrange + var key = "mockKey"; + var expectedToken = "token"; + + // act + _credentialManager.SaveCredentials(key, expectedToken); + var actualToken = _credentialManager.GetCredentials(key); + + // assert + Assert.AreEqual(expectedToken, actualToken); + + // act + _credentialManager.RemoveCredentials(key); + actualToken = _credentialManager.GetCredentials(key); + + // assert + Assert.IsTrue(string.IsNullOrEmpty(actualToken)); + } + + [Test] + public void TestSavingCredentialsForAnExistingKey() + { + // arrange + var key = "mockKey"; + var firstExpectedToken = "mockToken1"; + var secondExpectedToken = "mockToken2"; + + try + { + // act + _credentialManager.SaveCredentials(key, firstExpectedToken); + + // assert + Assert.AreEqual(firstExpectedToken, _credentialManager.GetCredentials(key)); + + // act + _credentialManager.SaveCredentials(key, secondExpectedToken); + + // assert + Assert.AreEqual(secondExpectedToken, _credentialManager.GetCredentials(key)); + } + catch (Exception ex) + { + // assert + Assert.Fail("Should not throw an exception: " + ex.Message); + } + } + + [Test] + public void TestRemovingCredentialsForKeyThatDoesNotExist() + { + // arrange + var key = "mockKey"; + + try + { + // act + _credentialManager.RemoveCredentials(key); + + // assert + Assert.IsTrue(string.IsNullOrEmpty(_credentialManager.GetCredentials(key))); + } + catch (Exception ex) + { + // assert + Assert.Fail("Should not throw an exception: " + ex.Message); + } + } + } + + [TestFixture] + [Platform("Win")] + public class SFAdysTechCredentialManagerTest : SFBaseCredentialManagerTest + { + [SetUp] + public void SetUp() + { + _credentialManager = SnowflakeCredentialManagerAdysTechImpl.Instance; + } + } + + [TestFixture] + public class SFInMemoryCredentialManagerTest : SFBaseCredentialManagerTest + { + [SetUp] + public void SetUp() + { + _credentialManager = SnowflakeCredentialManagerInMemoryImpl.Instance; + } + } + + [TestFixture] + public class SFFileCredentialManagerTest : SFBaseCredentialManagerTest + { + [SetUp] + public void SetUp() + { + _credentialManager = SnowflakeCredentialManagerFileImpl.Instance; + } + } + [TestFixture] class SFCredentialManagerTest { @@ -35,9 +143,9 @@ class SFCredentialManagerTest private const string CustomJsonDir = "testdirectory"; - private static readonly string s_defaultJsonPath = Path.Combine(s_defaultJsonDir, SnowflakeCredentialManagerIFileImpl.CredentialCacheFileName); + private static readonly string s_defaultJsonPath = Path.Combine(s_defaultJsonDir, SnowflakeCredentialManagerFileImpl.CredentialCacheFileName); - private static readonly string s_customJsonPath = Path.Combine(CustomJsonDir, SnowflakeCredentialManagerIFileImpl.CredentialCacheFileName); + private static readonly string s_customJsonPath = Path.Combine(CustomJsonDir, SnowflakeCredentialManagerFileImpl.CredentialCacheFileName); [SetUp] public void SetUp() { @@ -53,40 +161,6 @@ [TearDown] public void TearDown() SnowflakeCredentialManagerFactory.UseDefaultCredentialManager(); } - private void TestCredentialManagerImplementation() - { - // arrange - var key = SnowflakeCredentialManagerFactory.BuildCredentialKey("host", "user", "tokentype"); - var expectedToken = "token"; - - // act - var actualToken = _credentialManager.GetCredentials(key); - - // assert - Assert.IsTrue(string.IsNullOrEmpty(actualToken)); - - // act - _credentialManager.SaveCredentials(key, expectedToken); - actualToken = _credentialManager.GetCredentials(key); - - // assert - Assert.AreEqual(expectedToken, actualToken); - - // act - _credentialManager.RemoveCredentials(key); - actualToken = _credentialManager.GetCredentials(key); - - // assert - Assert.IsTrue(string.IsNullOrEmpty(actualToken)); - - // act - _credentialManager.RemoveCredentials(key); - actualToken = _credentialManager.GetCredentials(key); - - // assert - Assert.IsTrue(string.IsNullOrEmpty(actualToken)); - } - [Test] public void TestUsingDefaultCredentialManager() { @@ -111,51 +185,13 @@ public void TestUsingDefaultCredentialManager() public void TestSettingCustomCredentialManager() { // arrange - SnowflakeCredentialManagerFactory.SetCredentialManager(SnowflakeCredentialManagerIFileImpl.Instance); + SnowflakeCredentialManagerFactory.SetCredentialManager(SnowflakeCredentialManagerFileImpl.Instance); // act _credentialManager = SnowflakeCredentialManagerFactory.GetCredentialManager(); // assert - Assert.IsInstanceOf(_credentialManager); - } - - [Test] - public void TestAdysTechCredentialManager() - { - if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) - { - Assert.Ignore("skip test on non-Windows"); - } - - // arrange - SnowflakeCredentialManagerFactory.SetCredentialManager(SnowflakeCredentialManagerAdysTechImpl.Instance); - _credentialManager = SnowflakeCredentialManagerFactory.GetCredentialManager(); - - // act & assert - TestCredentialManagerImplementation(); - } - - [Test] - public void TestInMemoryCredentialManager() - { - // arrange - SnowflakeCredentialManagerFactory.SetCredentialManager(SnowflakeCredentialManagerInMemoryImpl.Instance); - _credentialManager = SnowflakeCredentialManagerFactory.GetCredentialManager(); - - // act & assert - TestCredentialManagerImplementation(); - } - - [Test] - public void TestJsonCredentialManager() - { - // arrange - SnowflakeCredentialManagerFactory.SetCredentialManager(SnowflakeCredentialManagerIFileImpl.Instance); - _credentialManager = SnowflakeCredentialManagerFactory.GetCredentialManager(); - - // act & assert - TestCredentialManagerImplementation(); + Assert.IsInstanceOf(_credentialManager); } [Test] @@ -175,9 +211,9 @@ public void TestThatThrowsErrorWhenCacheFileIsNotCreated() FilePermissions.S_IRUSR | FilePermissions.S_IWUSR | FilePermissions.S_IXUSR)) .Returns(-1); t_environmentOperations - .Setup(e => e.GetEnvironmentVariable(SnowflakeCredentialManagerIFileImpl.CredentialCacheDirectoryEnvironmentName)) + .Setup(e => e.GetEnvironmentVariable(SnowflakeCredentialManagerFileImpl.CredentialCacheDirectoryEnvironmentName)) .Returns(CustomJsonDir); - SnowflakeCredentialManagerFactory.SetCredentialManager(new SnowflakeCredentialManagerIFileImpl(t_fileOperations.Object, t_directoryOperations.Object, t_unixOperations.Object, t_environmentOperations.Object)); + SnowflakeCredentialManagerFactory.SetCredentialManager(new SnowflakeCredentialManagerFileImpl(t_fileOperations.Object, t_directoryOperations.Object, t_unixOperations.Object, t_environmentOperations.Object)); _credentialManager = SnowflakeCredentialManagerFactory.GetCredentialManager(); // act @@ -204,9 +240,9 @@ public void TestThatThrowsErrorWhenCacheFileCanBeAccessedByOthers() .Setup(u => u.GetFilePermissions(s_defaultJsonPath)) .Returns(FileAccessPermissions.AllPermissions); t_environmentOperations - .Setup(e => e.GetEnvironmentVariable(SnowflakeCredentialManagerIFileImpl.CredentialCacheDirectoryEnvironmentName)) + .Setup(e => e.GetEnvironmentVariable(SnowflakeCredentialManagerFileImpl.CredentialCacheDirectoryEnvironmentName)) .Returns(CustomJsonDir); - SnowflakeCredentialManagerFactory.SetCredentialManager(new SnowflakeCredentialManagerIFileImpl(t_fileOperations.Object, t_directoryOperations.Object, t_unixOperations.Object, t_environmentOperations.Object)); + SnowflakeCredentialManagerFactory.SetCredentialManager(new SnowflakeCredentialManagerFileImpl(t_fileOperations.Object, t_directoryOperations.Object, t_unixOperations.Object, t_environmentOperations.Object)); _credentialManager = SnowflakeCredentialManagerFactory.GetCredentialManager(); // act diff --git a/Snowflake.Data.Tests/UnitTests/SFSessionTest.cs b/Snowflake.Data.Tests/UnitTests/SFSessionTest.cs index daf412d46..a30c86389 100644 --- a/Snowflake.Data.Tests/UnitTests/SFSessionTest.cs +++ b/Snowflake.Data.Tests/UnitTests/SFSessionTest.cs @@ -1,4 +1,4 @@ -/* +/* * Copyright (c) 2012-2019 Snowflake Computing Inc. All rights reserved. */ @@ -68,7 +68,7 @@ public void TestThatIdTokenIsStoredWhenCachingIsEnabled() { // arrange var expectedIdToken = "mockIdToken"; - var connectionString = $"account=account;user=user;password=test;authenticator=externalbrowser;allow_sso_token_caching=true"; + var connectionString = $"account=account;user=user;password=test;allow_sso_token_caching=true"; var session = new SFSession(connectionString, null); LoginResponse authnResponse = new LoginResponse { diff --git a/Snowflake.Data/Client/SnowflakeCredentialManager/SnowflakeCredentialManagerIFileImpl.cs b/Snowflake.Data/Client/SnowflakeCredentialManager/SnowflakeCredentialManagerFileImpl.cs similarity index 90% rename from Snowflake.Data/Client/SnowflakeCredentialManager/SnowflakeCredentialManagerIFileImpl.cs rename to Snowflake.Data/Client/SnowflakeCredentialManager/SnowflakeCredentialManagerFileImpl.cs index 90a4aa6fb..1bf141238 100644 --- a/Snowflake.Data/Client/SnowflakeCredentialManager/SnowflakeCredentialManagerIFileImpl.cs +++ b/Snowflake.Data/Client/SnowflakeCredentialManager/SnowflakeCredentialManagerFileImpl.cs @@ -16,13 +16,13 @@ namespace Snowflake.Data.Client { - public class SnowflakeCredentialManagerIFileImpl : ISnowflakeCredentialManager + public class SnowflakeCredentialManagerFileImpl : ISnowflakeCredentialManager { internal const string CredentialCacheDirectoryEnvironmentName = "SF_TEMPORARY_CREDENTIAL_CACHE_DIR"; internal const string CredentialCacheFileName = "temporary_credential.json"; - private static readonly SFLogger s_logger = SFLoggerFactory.GetLogger(); + private static readonly SFLogger s_logger = SFLoggerFactory.GetLogger(); private readonly string _jsonCacheDirectory; @@ -36,9 +36,9 @@ public class SnowflakeCredentialManagerIFileImpl : ISnowflakeCredentialManager private readonly EnvironmentOperations _environmentOperations; - public static readonly SnowflakeCredentialManagerIFileImpl Instance = new SnowflakeCredentialManagerIFileImpl(FileOperations.Instance, DirectoryOperations.Instance, UnixOperations.Instance, EnvironmentOperations.Instance); + public static readonly SnowflakeCredentialManagerFileImpl Instance = new SnowflakeCredentialManagerFileImpl(FileOperations.Instance, DirectoryOperations.Instance, UnixOperations.Instance, EnvironmentOperations.Instance); - internal SnowflakeCredentialManagerIFileImpl(FileOperations fileOperations, DirectoryOperations directoryOperations, UnixOperations unixOperations, EnvironmentOperations environmentOperations) + internal SnowflakeCredentialManagerFileImpl(FileOperations fileOperations, DirectoryOperations directoryOperations, UnixOperations unixOperations, EnvironmentOperations environmentOperations) { _fileOperations = fileOperations; _directoryOperations = directoryOperations;