Skip to content

Commit

Permalink
SNOW-931788: Increase code coverage with CodeCov (#1537)
Browse files Browse the repository at this point in the history
* add okta auth tests to increase SessionUtil coverage

* Add more tests to cover some partial hits

* fix style
  • Loading branch information
sfc-gh-ext-simba-jl authored Oct 19, 2023
1 parent f0c3dce commit b084129
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -273,4 +273,18 @@ public void testSizeOfHttpClientMapWithGzipAndUserAgentSuffix() {
// Assert there are 3 entries because userAgentSuffix has changed
assertEquals(3, HttpUtil.httpClient.size());
}

@Test
public void testNullAndEmptyProxySettingsForS3() {
HttpClientSettingsKey testKey =
new HttpClientSettingsKey(OCSPMode.FAIL_OPEN, null, 443, null, null, null, "", "", false);
ClientConfiguration clientConfig = new ClientConfiguration();
HttpUtil.setProxyForS3(testKey, clientConfig);
assertEquals(Protocol.HTTP, clientConfig.getProxyProtocol());
assertEquals("", clientConfig.getProxyHost());
assertEquals(443, clientConfig.getProxyPort());
assertEquals("", clientConfig.getNonProxyHosts());
assertNull(clientConfig.getProxyUsername());
assertNull(clientConfig.getProxyPassword());
}
}
151 changes: 149 additions & 2 deletions src/test/java/net/snowflake/client/core/SessionUtilLatestIT.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
/*
* Copyright (c) 2012-2022 Snowflake Computing Inc. All rights reserved.
* Copyright (c) 2012-2023 Snowflake Computing Inc. All rights reserved.
*/

package net.snowflake.client.core;

import static net.snowflake.client.TestUtil.systemGetEnv;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;

Expand All @@ -16,14 +17,17 @@
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;
import net.snowflake.client.category.TestCategoryCore;
import net.snowflake.client.jdbc.BaseJDBCTest;
import net.snowflake.client.jdbc.ErrorCode;
import net.snowflake.client.jdbc.SnowflakeSQLException;
import net.snowflake.common.core.ClientAuthnDTO;
import net.snowflake.common.core.SqlState;
import org.apache.commons.io.IOUtils;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.junit.Ignore;
Expand Down Expand Up @@ -237,7 +241,150 @@ private SFLoginInput createLoginInput() {
input.setHttpClientSettingsKey(new HttpClientSettingsKey(OCSPMode.FAIL_OPEN));
input.setLoginTimeout(1000);
input.setSessionParameters(new HashMap<>());

return input;
}

@Test
public void testOktaAuthPostFail() throws Throwable {
SFLoginInput loginInput = createLoginInput();
loginInput.setAuthenticator("https://testauth.okta.com");
Map<SFSessionProperty, Object> connectionPropertiesMap = initConnectionPropertiesMap();

try (MockedStatic<HttpUtil> mockedHttpUtil = mockStatic(HttpUtil.class)) {
mockedHttpUtil
.when(
() ->
HttpUtil.executeGeneralRequest(
Mockito.any(HttpPost.class),
Mockito.anyInt(),
Mockito.anyInt(),
Mockito.anyInt(),
Mockito.anyInt(),
Mockito.nullable(HttpClientSettingsKey.class)))
.thenReturn("{\"code\":null,\"message\":\"POST request failed\",\"success\":false}");

SessionUtil.openSession(loginInput, connectionPropertiesMap, "ALL");
fail("Exception should have been thrown");
} catch (SnowflakeSQLException e) {
assertEquals("POST request failed", e.getMessage());
assertEquals(SqlState.SQLCLIENT_UNABLE_TO_ESTABLISH_SQLCONNECTION, e.getSQLState());
}
}

@Test
public void testOktaAuthMalformedUrl() throws Throwable {
SFLoginInput loginInput = createLoginInput();
loginInput.setAuthenticator("invalid!@url$%^");
Map<SFSessionProperty, Object> connectionPropertiesMap = initConnectionPropertiesMap();

try (MockedStatic<HttpUtil> mockedHttpUtil = mockStatic(HttpUtil.class)) {
mockedHttpUtil
.when(
() ->
HttpUtil.executeGeneralRequest(
Mockito.any(HttpPost.class),
Mockito.anyInt(),
Mockito.anyInt(),
Mockito.anyInt(),
Mockito.anyInt(),
Mockito.nullable(HttpClientSettingsKey.class)))
.thenReturn(
"{\"data\":{\"tokenUrl\":\"invalid!@url$%^\","
+ "\"ssoUrl\":\"invalid!@url$%^\","
+ "\"proofKey\":null},\"code\":null,\"message\":null,\"success\":true}");

SessionUtil.openSession(loginInput, connectionPropertiesMap, "ALL");
fail("Exception should have been thrown");
} catch (SnowflakeSQLException e) {
assertEquals((int) ErrorCode.NETWORK_ERROR.getMessageCode(), e.getErrorCode());
assertEquals(SqlState.IO_ERROR, e.getSQLState());
}
}

@Test
public void testOktaAuthURISyntaxError() throws Throwable {
SFLoginInput loginInput = createLoginInput();
loginInput.setAuthenticator("https://testauth.okta.com/^123");
Map<SFSessionProperty, Object> connectionPropertiesMap = initConnectionPropertiesMap();

try (MockedStatic<HttpUtil> mockedHttpUtil = mockStatic(HttpUtil.class)) {
mockedHttpUtil
.when(
() ->
HttpUtil.executeGeneralRequest(
Mockito.any(HttpPost.class),
Mockito.anyInt(),
Mockito.anyInt(),
Mockito.anyInt(),
Mockito.anyInt(),
Mockito.nullable(HttpClientSettingsKey.class)))
.thenReturn(
"{\"data\":{\"tokenUrl\":\"https://testauth.okta.com/^123\","
+ "\"ssoUrl\":\"https://testauth.okta.com/^123\","
+ "\"proofKey\":null},\"code\":null,\"message\":null,\"success\":true}");

SessionUtil.openSession(loginInput, connectionPropertiesMap, "ALL");
fail("Exception should have been thrown");
} catch (SnowflakeSQLException e) {
assertEquals((int) ErrorCode.CONNECTION_ERROR.getMessageCode(), e.getErrorCode());
assertEquals(SqlState.SQLCLIENT_UNABLE_TO_ESTABLISH_SQLCONNECTION, e.getSQLState());
}
}

@Test
public void testOktaAuthGetFail() throws Throwable {
SFLoginInput loginInput = createLoginInput();
loginInput.setAuthenticator("https://testauth.okta.com");
Map<SFSessionProperty, Object> connectionPropertiesMap = initConnectionPropertiesMap();

try (MockedStatic<HttpUtil> mockedHttpUtil = mockStatic(HttpUtil.class)) {
mockedHttpUtil
.when(
() ->
HttpUtil.executeGeneralRequest(
Mockito.any(HttpPost.class),
Mockito.anyInt(),
Mockito.anyInt(),
Mockito.anyInt(),
Mockito.anyInt(),
Mockito.nullable(HttpClientSettingsKey.class)))
.thenReturn(
"{\"data\":{\"tokenUrl\":\"https://testauth.okta.com/api/v1/authn\","
+ "\"ssoUrl\":\"https://testauth.okta.com/app/snowflake/abcdefghijklmnopqrstuvwxyz/sso/saml\","
+ "\"proofKey\":null},\"code\":null,\"message\":null,\"success\":true}");

mockedHttpUtil
.when(
() ->
HttpUtil.executeRequestWithoutCookies(
Mockito.any(HttpRequestBase.class),
Mockito.anyInt(),
Mockito.anyInt(),
Mockito.anyInt(),
Mockito.anyInt(),
Mockito.anyInt(),
Mockito.nullable(AtomicBoolean.class),
Mockito.nullable(HttpClientSettingsKey.class)))
.thenReturn(
"{\"expiresAt\":\"2023-10-13T19:18:09.000Z\",\"status\":\"SUCCESS\",\"sessionToken\":\"testsessiontoken\"}");

mockedHttpUtil
.when(
() ->
HttpUtil.executeGeneralRequest(
Mockito.any(HttpGet.class),
Mockito.anyInt(),
Mockito.anyInt(),
Mockito.anyInt(),
Mockito.anyInt(),
Mockito.nullable(HttpClientSettingsKey.class)))
.thenThrow(new IOException());

SessionUtil.openSession(loginInput, connectionPropertiesMap, "ALL");
fail("Exception should have been thrown");
} catch (SnowflakeSQLException e) {
assertEquals((int) ErrorCode.NETWORK_ERROR.getMessageCode(), e.getErrorCode());
assertEquals(SqlState.IO_ERROR, e.getSQLState());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,17 @@ public void testGetPropertyInfo() throws SQLException {
"server URL in form of <protocol>://<host or domain>:<port number>/<path of resource>",
info[0].description);

// Test with null URL and no properties. ServerURL is needed.
url = null;
props = new Properties();
driver = DriverManager.getDriver("jdbc:snowflake://snowflake.reg.local:8082");
info = driver.getPropertyInfo(url, props);
assertEquals(1, info.length);
assertEquals("serverURL", info[0].name);
assertEquals(
"server URL in form of <protocol>://<host or domain>:<port number>/<path of resource>",
info[0].description);

// Test with URL that requires username and password.
url = "jdbc:snowflake://snowflake.reg.local:8082";
info = driver.getPropertyInfo(url, props);
Expand Down

0 comments on commit b084129

Please sign in to comment.