diff --git a/Snowflake.Data.Tests/UnitTests/SnowflakeTomlConnectionBuilderTest.cs b/Snowflake.Data.Tests/UnitTests/SnowflakeTomlConnectionBuilderTest.cs index 370ae8cec..24c2cb259 100644 --- a/Snowflake.Data.Tests/UnitTests/SnowflakeTomlConnectionBuilderTest.cs +++ b/Snowflake.Data.Tests/UnitTests/SnowflakeTomlConnectionBuilderTest.cs @@ -3,6 +3,7 @@ */ using Mono.Unix; +using Snowflake.Data.Client; namespace Snowflake.Data.Tests.UnitTests { @@ -309,7 +310,7 @@ public void TestConnectionWithOauthAuthenticatorTokenFromFile() } [Test] - public void TestConnectionWithOauthAuthenticatorFromDefaultIfTokenFilePathNotExists() + public void TestConnectionWithOauthAuthenticatorThrowsExceptionIfTokenFilePathNotExists() { // Arrange var tokenFilePath = "/Users/testuser/token"; @@ -337,11 +338,9 @@ public void TestConnectionWithOauthAuthenticatorFromDefaultIfTokenFilePathNotExi var reader = new TomlConnectionBuilder(mockFileOperations.Object, mockEnvironmentOperations.Object); - // Act - var connectionString = reader.GetConnectionStringFromToml(); - - // Assert - Assert.AreEqual($"account=testaccountname;authenticator=oauth;token={defaultToken};", connectionString); + // Act and assert + var exception = Assert.Throws(() => reader.GetConnectionStringFromToml()); + Assert.IsTrue(exception.Message.StartsWith("Error: Invalid parameter value /Users/testuser/token for token_file_path")); } [Test] diff --git a/Snowflake.Data/Core/TomlConnectionBuilder.cs b/Snowflake.Data/Core/TomlConnectionBuilder.cs index 038deb706..61acbd83a 100644 --- a/Snowflake.Data/Core/TomlConnectionBuilder.cs +++ b/Snowflake.Data/Core/TomlConnectionBuilder.cs @@ -99,7 +99,21 @@ private void AppendTokenFromFileIfNotGivenExplicitly(TomlTable connectionToml, b private string LoadTokenFromFile(string tokenFilePathValue) { - var tokenFile = !string.IsNullOrEmpty(tokenFilePathValue) && _fileOperations.Exists(tokenFilePathValue) ? tokenFilePathValue : DefaultTokenPath; + string tokenFile; + if(string.IsNullOrEmpty(tokenFilePathValue)) + { + tokenFile = DefaultTokenPath; + } + else + { + if (!_fileOperations.Exists(tokenFilePathValue)) + { + s_logger.Debug($"Specified file {tokenFilePathValue} does not exists."); + throw new SnowflakeDbException(SFError.INVALID_CONNECTION_PARAMETER_VALUE, tokenFilePathValue, "token_file_path"); + } + + tokenFile = tokenFilePathValue; + } s_logger.Debug($"Read token from file path: {tokenFile}"); return _fileOperations.Exists(tokenFile) ? _fileOperations.ReadAllText(tokenFile, ValidateFilePermissions) : null; }