Skip to content

Commit

Permalink
Move interface and implementations to subpackage
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-ext-simba-lf committed Jun 6, 2024
1 parent 94bce01 commit 83119f3
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 30 deletions.
1 change: 1 addition & 0 deletions Snowflake.Data.Tests/IntegrationTests/SFConnectionIT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ namespace Snowflake.Data.Tests.IntegrationTests
using System.Runtime.InteropServices;
using System.Net.Http;
using Snowflake.Data.Core.CredentialManager;
using Snowflake.Data.Core.CredentialManager.Infrastructure;

[TestFixture]
class SFConnectionIT : SFBaseTest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ namespace Snowflake.Data.Tests.UnitTests.CredentialManager
using Mono.Unix.Native;
using Moq;
using NUnit.Framework;
using Snowflake.Data.Client;
using Snowflake.Data.Core.CredentialManager;
using Snowflake.Data.Core.CredentialManager.Infrastructure;
using Snowflake.Data.Core.Tools;
using System;
using System.IO;
using System.Runtime.InteropServices;

public abstract class SFBaseCredentialManagerTest
{
protected ISnowflakeCredentialManager _credentialManager;
protected ISFCredentialManager _credentialManager;

[Test]
public void TestSavingAndRemovingCredentials()
Expand Down Expand Up @@ -102,7 +103,7 @@ public class SFNativeCredentialManagerTest : SFBaseCredentialManagerTest
[SetUp]
public void SetUp()
{
_credentialManager = SnowflakeCredentialManagerWindowsNativeImpl.Instance;
_credentialManager = SFCredentialManagerWindowsNativeImpl.Instance;
}
}

Expand All @@ -112,7 +113,7 @@ public class SFInMemoryCredentialManagerTest : SFBaseCredentialManagerTest
[SetUp]
public void SetUp()
{
_credentialManager = SnowflakeCredentialManagerInMemoryImpl.Instance;
_credentialManager = SFCredentialManagerInMemoryImpl.Instance;
}
}

Expand All @@ -122,14 +123,14 @@ public class SFFileCredentialManagerTest : SFBaseCredentialManagerTest
[SetUp]
public void SetUp()
{
_credentialManager = SnowflakeCredentialManagerFileImpl.Instance;
_credentialManager = SFCredentialManagerFileImpl.Instance;
}
}

[TestFixture]
class SFCredentialManagerTest
{
ISnowflakeCredentialManager _credentialManager;
ISFCredentialManager _credentialManager;

[ThreadStatic]
private static Mock<FileOperations> t_fileOperations;
Expand All @@ -145,53 +146,53 @@ class SFCredentialManagerTest

private const string CustomJsonDir = "testdirectory";

private static readonly string s_customJsonPath = Path.Combine(CustomJsonDir, SnowflakeCredentialManagerFileImpl.CredentialCacheFileName);
private static readonly string s_customJsonPath = Path.Combine(CustomJsonDir, SFCredentialManagerFileImpl.CredentialCacheFileName);

[SetUp] public void SetUp()
{
t_fileOperations = new Mock<FileOperations>();
t_directoryOperations = new Mock<DirectoryOperations>();
t_unixOperations = new Mock<UnixOperations>();
t_environmentOperations = new Mock<EnvironmentOperations>();
SnowflakeCredentialManagerFactory.SetCredentialManager(SnowflakeCredentialManagerInMemoryImpl.Instance);
SFCredentialManagerFactory.SetCredentialManager(SFCredentialManagerInMemoryImpl.Instance);
}

[TearDown] public void TearDown()
{
SnowflakeCredentialManagerFactory.UseDefaultCredentialManager();
SFCredentialManagerFactory.UseDefaultCredentialManager();
}

[Test]
public void TestUsingDefaultCredentialManager()
{
// arrange
SnowflakeCredentialManagerFactory.UseDefaultCredentialManager();
SFCredentialManagerFactory.UseDefaultCredentialManager();

// act
_credentialManager = SnowflakeCredentialManagerFactory.GetCredentialManager();
_credentialManager = SFCredentialManagerFactory.GetCredentialManager();

// assert
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
Assert.IsInstanceOf<SnowflakeCredentialManagerWindowsNativeImpl>(_credentialManager);
Assert.IsInstanceOf<SFCredentialManagerWindowsNativeImpl>(_credentialManager);
}
else
{
Assert.IsInstanceOf<SnowflakeCredentialManagerInMemoryImpl>(_credentialManager);
Assert.IsInstanceOf<SFCredentialManagerInMemoryImpl>(_credentialManager);
}
}

[Test]
public void TestSettingCustomCredentialManager()
{
// arrange
SnowflakeCredentialManagerFactory.SetCredentialManager(SnowflakeCredentialManagerFileImpl.Instance);
SFCredentialManagerFactory.SetCredentialManager(SFCredentialManagerFileImpl.Instance);

// act
_credentialManager = SnowflakeCredentialManagerFactory.GetCredentialManager();
_credentialManager = SFCredentialManagerFactory.GetCredentialManager();

// assert
Assert.IsInstanceOf<SnowflakeCredentialManagerFileImpl>(_credentialManager);
Assert.IsInstanceOf<SFCredentialManagerFileImpl>(_credentialManager);
}

[Test]
Expand All @@ -211,10 +212,10 @@ public void TestThatThrowsErrorWhenCacheFileIsNotCreated()
FilePermissions.S_IRUSR | FilePermissions.S_IWUSR | FilePermissions.S_IXUSR))
.Returns(-1);
t_environmentOperations
.Setup(e => e.GetEnvironmentVariable(SnowflakeCredentialManagerFileImpl.CredentialCacheDirectoryEnvironmentName))
.Setup(e => e.GetEnvironmentVariable(SFCredentialManagerFileImpl.CredentialCacheDirectoryEnvironmentName))
.Returns(CustomJsonDir);
SnowflakeCredentialManagerFactory.SetCredentialManager(new SnowflakeCredentialManagerFileImpl(t_fileOperations.Object, t_directoryOperations.Object, t_unixOperations.Object, t_environmentOperations.Object));
_credentialManager = SnowflakeCredentialManagerFactory.GetCredentialManager();
SFCredentialManagerFactory.SetCredentialManager(new SFCredentialManagerFileImpl(t_fileOperations.Object, t_directoryOperations.Object, t_unixOperations.Object, t_environmentOperations.Object));
_credentialManager = SFCredentialManagerFactory.GetCredentialManager();

// act
var thrown = Assert.Throws<Exception>(() => _credentialManager.SaveCredentials("key", "token"));
Expand All @@ -240,10 +241,10 @@ public void TestThatThrowsErrorWhenCacheFileCanBeAccessedByOthers()
.Setup(u => u.GetFilePermissions(s_customJsonPath))
.Returns(FileAccessPermissions.AllPermissions);
t_environmentOperations
.Setup(e => e.GetEnvironmentVariable(SnowflakeCredentialManagerFileImpl.CredentialCacheDirectoryEnvironmentName))
.Setup(e => e.GetEnvironmentVariable(SFCredentialManagerFileImpl.CredentialCacheDirectoryEnvironmentName))
.Returns(CustomJsonDir);
SnowflakeCredentialManagerFactory.SetCredentialManager(new SnowflakeCredentialManagerFileImpl(t_fileOperations.Object, t_directoryOperations.Object, t_unixOperations.Object, t_environmentOperations.Object));
_credentialManager = SnowflakeCredentialManagerFactory.GetCredentialManager();
SFCredentialManagerFactory.SetCredentialManager(new SFCredentialManagerFileImpl(t_fileOperations.Object, t_directoryOperations.Object, t_unixOperations.Object, t_environmentOperations.Object));
_credentialManager = SFCredentialManagerFactory.GetCredentialManager();

// act
var thrown = Assert.Throws<Exception>(() => _credentialManager.SaveCredentials("key", "token"));
Expand All @@ -269,15 +270,15 @@ public void TestThatJsonFileIsCheckedIfAlreadyExists()
.Setup(u => u.GetFilePermissions(s_customJsonPath))
.Returns(FileAccessPermissions.UserReadWriteExecute);
t_environmentOperations
.Setup(e => e.GetEnvironmentVariable(SnowflakeCredentialManagerFileImpl.CredentialCacheDirectoryEnvironmentName))
.Setup(e => e.GetEnvironmentVariable(SFCredentialManagerFileImpl.CredentialCacheDirectoryEnvironmentName))
.Returns(CustomJsonDir);
t_fileOperations
.SetupSequence(f => f.Exists(s_customJsonPath))
.Returns(false)
.Returns(true);

SnowflakeCredentialManagerFactory.SetCredentialManager(new SnowflakeCredentialManagerFileImpl(t_fileOperations.Object, t_directoryOperations.Object, t_unixOperations.Object, t_environmentOperations.Object));
_credentialManager = SnowflakeCredentialManagerFactory.GetCredentialManager();
SFCredentialManagerFactory.SetCredentialManager(new SFCredentialManagerFileImpl(t_fileOperations.Object, t_directoryOperations.Object, t_unixOperations.Object, t_environmentOperations.Object));
_credentialManager = SFCredentialManagerFactory.GetCredentialManager();

// act
_credentialManager.SaveCredentials("key", "token");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
using Snowflake.Data.Client;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using Snowflake.Data.Core.CredentialManager;
using Snowflake.Data.Core.CredentialManager.Infrastructure;

namespace Snowflake.Data.Core.Authenticator
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright (c) 2024 Snowflake Computing Inc. All rights reserved.
*/

namespace Snowflake.Data.Core.CredentialManager
namespace Snowflake.Data.Core.CredentialManager.Infrastructure
{
internal enum TokenType
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
using System.Security.Principal;
using KeyToken = System.Collections.Generic.Dictionary<string, string>;

namespace Snowflake.Data.Core.CredentialManager
namespace Snowflake.Data.Core.CredentialManager.Infrastructure
{
public class SFCredentialManagerFileImpl : ISFCredentialManager
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using Snowflake.Data.Log;
using System.Collections.Generic;

namespace Snowflake.Data.Core.CredentialManager
namespace Snowflake.Data.Core.CredentialManager.Infrastructure
{
public class SFCredentialManagerInMemoryImpl : ISFCredentialManager
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
using System.Runtime.InteropServices;
using System.Text;

namespace Snowflake.Data.Core.CredentialManager
namespace Snowflake.Data.Core.CredentialManager.Infrastructure
{
public class SFCredentialManagerWindowsNativeImpl : ISFCredentialManager
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* Copyright (c) 2024 Snowflake Computing Inc. All rights reserved.
*/

using Snowflake.Data.Core.CredentialManager.Infrastructure;
using Snowflake.Data.Log;
using System.Runtime.InteropServices;

Expand Down
1 change: 1 addition & 0 deletions Snowflake.Data/Core/Session/SFSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using System.Text.RegularExpressions;
using Snowflake.Data.Configuration;
using Snowflake.Data.Core.CredentialManager;
using Snowflake.Data.Core.CredentialManager.Infrastructure;

namespace Snowflake.Data.Core
{
Expand Down

0 comments on commit 83119f3

Please sign in to comment.