Skip to content

Commit

Permalink
Refactor test
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-ext-simba-lf committed Apr 18, 2024
1 parent a317158 commit 625e04b
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 77 deletions.
43 changes: 11 additions & 32 deletions Snowflake.Data.Tests/IntegrationTests/SFConnectionIT.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* Copyright (c) 2012-2023 Snowflake Computing Inc. All rights reserved.
*/

Expand Down Expand Up @@ -1026,16 +1026,9 @@ public void TestSSOConnectionWithTokenCaching()
{
using (IDbConnection conn = new SnowflakeDbConnection())
{
conn.ConnectionString = String.Format("scheme={0};host={1};port={2};" +
"account={3};user={4};password={5};authenticator={6};allow_sso_token_caching={7}",
testConfig.protocol,
testConfig.host,
testConfig.port,
testConfig.account,
testConfig.user,
"",
"externalbrowser",
true);
conn.ConnectionString
= ConnectionStringWithoutAuth
+ ";authenticator=externalbrowser;[email protected];allow_sso_token_caching=true;";

// Authenticate to retrieve and store the token if doesn't exist or invalid
conn.Open();
Expand All @@ -1056,19 +1049,12 @@ public void TestSSOConnectionWithInvalidCachedToken()
{
using (IDbConnection conn = new SnowflakeDbConnection())
{
conn.ConnectionString = String.Format("scheme={0};host={1};port={2};" +
"account={3};user={4};password={5};authenticator={6};allow_sso_token_caching={7}",
testConfig.protocol,
testConfig.host,
testConfig.port,
testConfig.account,
testConfig.user,
"",
"externalbrowser",
true);
conn.ConnectionString
= ConnectionStringWithoutAuth
+ ";authenticator=externalbrowser;[email protected];allow_sso_token_caching=true;";

var key = SnowflakeCredentialManagerFactory.BuildCredentialKey(testConfig.host, testConfig.user, TokenType.IdToken.ToString());
var credentialManager = new SnowflakeCredentialManagerInMemoryImpl();
var credentialManager = SnowflakeCredentialManagerInMemoryImpl.Instance;
credentialManager.SaveCredentials(key, "wrongToken");

SnowflakeCredentialManagerFactory.SetCredentialManager(credentialManager);
Expand Down Expand Up @@ -2290,16 +2276,9 @@ public void TestSSOConnectionWithTokenCachingAsync()
{
using (SnowflakeDbConnection conn = new SnowflakeDbConnection())
{
conn.ConnectionString = String.Format("scheme={0};host={1};port={2};" +
"account={3};user={4};password={5};authenticator={6};allow_sso_token_caching={7}",
testConfig.protocol,
testConfig.host,
testConfig.port,
testConfig.account,
testConfig.user,
"",
"externalbrowser",
true);
conn.ConnectionString
= ConnectionStringWithoutAuth
+ ";authenticator=externalbrowser;[email protected];allow_sso_token_caching=true;";

// Authenticate to retrieve and store the token if doesn't exist or invalid
Task connectTask = conn.OpenAsync(CancellationToken.None);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,39 @@ [TearDown] public void TearDown()
SnowflakeCredentialManagerFactory.UseDefaultCredentialManager();
}

private void TestCredentialManagerImplementation()
{
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 Down Expand Up @@ -79,63 +112,35 @@ public void TestSettingCustomCredentialManager()
}

[Test]
public void TestDefaultCredentialManager()
public void TestAdysTechCredentialManager()
{
// arrange
var key = SnowflakeCredentialManagerFactory.BuildCredentialKey("host", "user", "tokentype");
var expectedToken = "token";
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
Assert.Ignore("skip test on non-Windows");
}

// arrange
SnowflakeCredentialManagerFactory.SetCredentialManager(SnowflakeCredentialManagerAdysTechImpl.Instance);
_credentialManager = SnowflakeCredentialManagerFactory.GetCredentialManager();
TestCredentialManagerImplementation();
}

// 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));
[Test]
public void TestInMemoryCredentialManager()
{
// arrange
SnowflakeCredentialManagerFactory.SetCredentialManager(SnowflakeCredentialManagerInMemoryImpl.Instance);
_credentialManager = SnowflakeCredentialManagerFactory.GetCredentialManager();
TestCredentialManagerImplementation();
}

[Test]
public void TestJsonCredentialManager()
{
// arrange
var key = SnowflakeCredentialManagerFactory.BuildCredentialKey("host", "user", "tokentype");
var expectedToken = "token";
SnowflakeCredentialManagerFactory.SetCredentialManager(SnowflakeCredentialManagerIFileImpl.Instance);
_credentialManager = SnowflakeCredentialManagerFactory.GetCredentialManager();

// 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));
TestCredentialManagerImplementation();
}

[Test]
Expand Down

0 comments on commit 625e04b

Please sign in to comment.