From a572300f1d90406387118304761656d9ca39c075 Mon Sep 17 00:00:00 2001 From: sfc-gh-ext-simba-lf Date: Fri, 9 Feb 2024 14:18:51 -0800 Subject: [PATCH] SNOW-916949: Refactor mock class and remove skip tag --- .../IntegrationTests/SFConnectionIT.cs | 38 ++++++++----- Snowflake.Data.Tests/Mock/MockOkta.cs | 4 ++ .../Mock/MockOktaRetryMaxTimeout.cs | 55 ------------------- Snowflake.Data.Tests/UnitTests/SFOktaTest.cs | 18 +++--- .../Core/Authenticator/OktaAuthenticator.cs | 1 - 5 files changed, 38 insertions(+), 78 deletions(-) delete mode 100644 Snowflake.Data.Tests/Mock/MockOktaRetryMaxTimeout.cs diff --git a/Snowflake.Data.Tests/IntegrationTests/SFConnectionIT.cs b/Snowflake.Data.Tests/IntegrationTests/SFConnectionIT.cs index f312a02bc..534d90a58 100644 --- a/Snowflake.Data.Tests/IntegrationTests/SFConnectionIT.cs +++ b/Snowflake.Data.Tests/IntegrationTests/SFConnectionIT.cs @@ -17,6 +17,7 @@ namespace Snowflake.Data.Tests.IntegrationTests using System.Diagnostics; using Snowflake.Data.Tests.Mock; using System.Runtime.InteropServices; + using System.Net.Http; [TestFixture] class SFConnectionIT : SFBaseTest @@ -818,12 +819,19 @@ public void TestOktaConnection() } [Test] - [Ignore("This test requires manual setup and therefore cannot be run in CI")] public void TestOktaConnectionUntilMaxTimeout() { var expectedMaxRetryCount = 15; var expectedMaxConnectionTimeout = 450; - var mockRestRequester = new MockOktaRetryMaxTimeout(expectedMaxRetryCount, expectedMaxConnectionTimeout); + var oktaUrl = "https://test.okta.com"; + var mockRestRequester = new MockOktaRestRequester() + { + TokenUrl = $"{oktaUrl}/api/v1/sessions?additionalFields=cookieToken", + SSOUrl = $"{oktaUrl}/app/testaccount/sso/saml", + ResponseContent = new StringContent("(IRestRequest request) { @@ -31,6 +33,8 @@ public Task GetAsync(IRestRequest request, CancellationToke { var response = new HttpResponseMessage(System.Net.HttpStatusCode.OK); response.Content = ResponseContent; + response.Content.Headers.Add(OktaAuthenticator.RetryCountHeader, MaxRetryCount.ToString()); + response.Content.Headers.Add(OktaAuthenticator.TimeoutElapsedHeader, MaxRetryTimeout.ToString()); return Task.FromResult(response); } diff --git a/Snowflake.Data.Tests/Mock/MockOktaRetryMaxTimeout.cs b/Snowflake.Data.Tests/Mock/MockOktaRetryMaxTimeout.cs deleted file mode 100644 index 20f7c573d..000000000 --- a/Snowflake.Data.Tests/Mock/MockOktaRetryMaxTimeout.cs +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright (c) 2024 Snowflake Computing Inc. All rights reserved. - */ - -using Newtonsoft.Json; -using Snowflake.Data.Core; -using Snowflake.Data.Core.Authenticator; -using System; -using System.Net.Http; -using System.Text; -using System.Threading; -using System.Threading.Tasks; - -namespace Snowflake.Data.Tests.Mock -{ - - class MockOktaRetryMaxTimeout : RestRequester, IMockRestRequester - { - internal bool _forceTimeoutForNonLoginRequestsOnly = false; - internal int _maxRetryCount; - internal int _maxRetryTimeout; - - public MockOktaRetryMaxTimeout(int maxRetryCount, int maxRetryTimeout) : base(null) - { - _maxRetryCount = maxRetryCount; - _maxRetryTimeout = maxRetryTimeout; - } - - public void setHttpClient(HttpClient httpClient) - { - _HttpClient = httpClient; - } - - protected override async Task SendAsync(HttpRequestMessage message, - TimeSpan restTimeout, - CancellationToken externalCancellationToken, - string sid = "") - { - if (HttpUtil.IsOktaSSORequest(message.RequestUri.Host, message.RequestUri.AbsolutePath)) - { - var mockContent = new StringContent(JsonConvert.SerializeObject("< html lang =\"en\">\n\n\n\n\n\n\n\n\n\n\n\nSnowflake - Signing in...\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
\n\n\n\n \n\n
\n
\n
\n
\n \"Please\"Please\"Okta\"
\n
\n

Signing in to TESTACCOUNT (regression)

\n
\n
\n
\n\n \n
\n \n \n
\n\n
\n\n\n\n"); StringContent noPostbackContent = new StringContent(" < !DOCTYPE html >< html lang =\"en\">\n\n\n\n\n\n\n\n\n\n\n\nSnowflake - Signing in...\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
\n\n\n\n \n\n
\n
\n
\n
\n \"Please\"Please\"Okta\"
\n
\n

Signing in to TESTACCOUNT (regression)

\n
\n
\n
\n\n \n
\n \n \n
\n\n
\n\n\n\n"); + const int MaxRetryCount = 1; + const int MaxRetryTimeout = 1; + [Test] public void TestSsoTokenUrlMismatch() { @@ -21,6 +23,8 @@ public void TestSsoTokenUrlMismatch() { TokenUrl = "https://snowflakecomputing.okta1.com/api/v1/sessions?additionalFields=cookieToken", SSOUrl = "https://snowflakecomputing.okta.com/app/snowflake_testaccountdev_1/blah/sso/saml", + MaxRetryCount = MaxRetryCount, + MaxRetryTimeout = MaxRetryTimeout }; var sfSession = new SFSession("account=test;user=test;password=test;authenticator=https://snowflake.okta.com", null, restRequester); sfSession.Open(); @@ -34,10 +38,6 @@ public void TestSsoTokenUrlMismatch() [Test] public void TestMissingPostbackUrl() { - const int retryCount = 1; - const int retryTimeout = 1; - noPostbackContent.Headers.Add(OktaAuthenticator.RetryCountHeader, retryCount.ToString()); - noPostbackContent.Headers.Add(OktaAuthenticator.TimeoutElapsedHeader, retryTimeout.ToString()); try { var restRequester = new Mock.MockOktaRestRequester() @@ -45,17 +45,17 @@ public void TestMissingPostbackUrl() TokenUrl = "https://snowflakecomputing.okta.com/api/v1/sessions?additionalFields=cookieToken", SSOUrl = "https://snowflakecomputing.okta.com/app/snowflake_testaccountdev_1/blah/sso/saml", ResponseContent = noPostbackContent, + MaxRetryCount = MaxRetryCount, + MaxRetryTimeout = MaxRetryTimeout }; var sfSession = new SFSession("account=test;user=test;password=test;authenticator=https://snowflakecomputing.okta.com;" + - $"host=test;MAXHTTPRETRIES={retryCount};RETRY_TIMEOUT={retryTimeout};", null, restRequester); + $"host=test;MAXHTTPRETRIES={MaxRetryCount};RETRY_TIMEOUT={MaxRetryTimeout};", null, restRequester); sfSession.Open(); Assert.Fail("Should not pass"); } catch (SnowflakeDbException e) { Assert.AreEqual(SFError.IDP_SAML_POSTBACK_NOTFOUND.GetAttribute().errorCode, ((SnowflakeDbException)e.InnerException).ErrorCode); } - noPostbackContent.Headers.Remove(OktaAuthenticator.RetryCountHeader); - noPostbackContent.Headers.Remove(OktaAuthenticator.TimeoutElapsedHeader); } [Test] @@ -68,6 +68,8 @@ public void TestWrongPostbackUrl() TokenUrl = "https://snowflakecomputing.okta.com/api/v1/sessions?additionalFields=cookieToken", SSOUrl = "https://snowflakecomputing.okta.com/app/snowflake_testaccountdev_1/blah/sso/saml", ResponseContent = wrongPostbackContent, + MaxRetryCount = MaxRetryCount, + MaxRetryTimeout = MaxRetryTimeout }; var sfSession = new SFSession("account=test;user=test;password=test;authenticator=https://snowflakecomputing.okta.com;host=test", null, restRequester); sfSession.Open(); diff --git a/Snowflake.Data/Core/Authenticator/OktaAuthenticator.cs b/Snowflake.Data/Core/Authenticator/OktaAuthenticator.cs index 5df3975c4..be04ceab7 100644 --- a/Snowflake.Data/Core/Authenticator/OktaAuthenticator.cs +++ b/Snowflake.Data/Core/Authenticator/OktaAuthenticator.cs @@ -153,7 +153,6 @@ void IAuthenticator.Authenticate() logger.Debug("step 5: verify postback url in SAML reponse"); VerifyPostbackUrl(); - logger.Debug("step 6: send SAML reponse to snowflake to login"); base.Login(); break;