Skip to content

Commit

Permalink
Refactor test and rename file
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-ext-simba-lf committed Apr 19, 2024
1 parent 3788474 commit c597c64
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 88 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
{
Expand All @@ -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()
{
Expand All @@ -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()
{
Expand All @@ -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<SnowflakeCredentialManagerIFileImpl>(_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<SnowflakeCredentialManagerFileImpl>(_credentialManager);
}

[Test]
Expand All @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions Snowflake.Data.Tests/UnitTests/SFSessionTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* Copyright (c) 2012-2019 Snowflake Computing Inc. All rights reserved.
*/

Expand Down Expand Up @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<SnowflakeCredentialManagerIFileImpl>();
private static readonly SFLogger s_logger = SFLoggerFactory.GetLogger<SnowflakeCredentialManagerFileImpl>();

private readonly string _jsonCacheDirectory;

Expand All @@ -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;
Expand Down

0 comments on commit c597c64

Please sign in to comment.