diff --git a/Snowflake.Data.Tests/UnitTests/CredentialManager/SFCredentialManagerTest.cs b/Snowflake.Data.Tests/UnitTests/CredentialManager/SFCredentialManagerTest.cs index 9499dd919..7d178d152 100644 --- a/Snowflake.Data.Tests/UnitTests/CredentialManager/SFCredentialManagerTest.cs +++ b/Snowflake.Data.Tests/UnitTests/CredentialManager/SFCredentialManagerTest.cs @@ -143,12 +143,8 @@ class SFCredentialManagerTest [ThreadStatic] private static Mock t_environmentOperations; - private static readonly string s_defaultJsonDir = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); - private const string CustomJsonDir = "testdirectory"; - private static readonly string s_defaultJsonPath = Path.Combine(s_defaultJsonDir, SnowflakeCredentialManagerFileImpl.CredentialCacheFileName); - private static readonly string s_customJsonPath = Path.Combine(CustomJsonDir, SnowflakeCredentialManagerFileImpl.CredentialCacheFileName); [SetUp] public void SetUp() @@ -208,7 +204,7 @@ public void TestThatThrowsErrorWhenCacheFileIsNotCreated() // arrange t_directoryOperations - .Setup(d => d.Exists(s_defaultJsonDir)) + .Setup(d => d.Exists(s_customJsonPath)) .Returns(false); t_unixOperations .Setup(u => u.CreateFileWithPermissions(s_customJsonPath, @@ -237,11 +233,11 @@ public void TestThatThrowsErrorWhenCacheFileCanBeAccessedByOthers() // arrange t_unixOperations - .Setup(u => u.CreateFileWithPermissions(s_defaultJsonPath, + .Setup(u => u.CreateFileWithPermissions(s_customJsonPath, FilePermissions.S_IRUSR | FilePermissions.S_IWUSR | FilePermissions.S_IXUSR)) .Returns(0); t_unixOperations - .Setup(u => u.GetFilePermissions(s_defaultJsonPath)) + .Setup(u => u.GetFilePermissions(s_customJsonPath)) .Returns(FileAccessPermissions.AllPermissions); t_environmentOperations .Setup(e => e.GetEnvironmentVariable(SnowflakeCredentialManagerFileImpl.CredentialCacheDirectoryEnvironmentName)) @@ -255,5 +251,39 @@ public void TestThatThrowsErrorWhenCacheFileCanBeAccessedByOthers() // assert Assert.That(thrown.Message, Does.Contain("Permission for the JSON token cache file should contain only the owner access")); } + + [Test] + public void TestThatJsonFileIsCheckedIfAlreadyExists() + { + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + Assert.Ignore("skip test on Windows"); + } + + // arrange + t_unixOperations + .Setup(u => u.CreateFileWithPermissions(s_customJsonPath, + FilePermissions.S_IRUSR | FilePermissions.S_IWUSR | FilePermissions.S_IXUSR)) + .Returns(0); + t_unixOperations + .Setup(u => u.GetFilePermissions(s_customJsonPath)) + .Returns(FileAccessPermissions.UserReadWriteExecute); + t_environmentOperations + .Setup(e => e.GetEnvironmentVariable(SnowflakeCredentialManagerFileImpl.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(); + + // act + _credentialManager.SaveCredentials("key", "token"); + + // assert + t_fileOperations.Verify(f => f.Exists(s_customJsonPath), Times.Exactly(2)); + } } } diff --git a/Snowflake.Data/Client/SnowflakeCredentialManager/SnowflakeCredentialManagerFileImpl.cs b/Snowflake.Data/Client/SnowflakeCredentialManager/SnowflakeCredentialManagerFileImpl.cs index b3aa40fb4..c6f666e80 100644 --- a/Snowflake.Data/Client/SnowflakeCredentialManager/SnowflakeCredentialManagerFileImpl.cs +++ b/Snowflake.Data/Client/SnowflakeCredentialManager/SnowflakeCredentialManagerFileImpl.cs @@ -77,6 +77,10 @@ internal void WriteToJsonFile(string content) _directoryOperations.CreateDirectory(_jsonCacheDirectory); } s_logger.Info($"Creating the json file for credential cache in {_jsonCacheFilePath}"); + if (_fileOperations.Exists(_jsonCacheFilePath)) + { + s_logger.Info($"The existing json file for credential cache in {_jsonCacheFilePath} will be overwritten"); + } var createFileResult = _unixOperations.CreateFileWithPermissions(_jsonCacheFilePath, FilePermissions.S_IRUSR | FilePermissions.S_IWUSR | FilePermissions.S_IXUSR); if (createFileResult == -1)