Skip to content

Commit

Permalink
Check if json file already exists
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-ext-simba-lf committed Jun 5, 2024
1 parent cdc9f80 commit 9622f5c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,8 @@ class SFCredentialManagerTest
[ThreadStatic]
private static Mock<EnvironmentOperations> 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()
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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))
Expand All @@ -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));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 9622f5c

Please sign in to comment.