Skip to content

Commit

Permalink
SNOW-1406763 Test for password with special characters (#944)
Browse files Browse the repository at this point in the history
### Description
SNOW-1406763 Test for password with special characters

### Checklist
- [x] Code compiles correctly
- [x] Code is formatted according to [Coding
Conventions](../blob/master/CodingConventions.md)
- [x] Created tests which fail without the change (if possible)
- [x] All tests passing (`dotnet test`)
- [x] Extended the README / documentation, if necessary
- [x] Provide JIRA issue id (if possible) or GitHub issue id in PR name
  • Loading branch information
sfc-gh-knozderko authored May 13, 2024
1 parent ef2041a commit 9ac574e
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
68 changes: 68 additions & 0 deletions Snowflake.Data.Tests/Mock/MockLoginStoringRestRequester.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Snowflake.Data.Core;

namespace Snowflake.Data.Tests.Mock
{
class MockLoginStoringRestRequester: IMockRestRequester
{
internal List<LoginRequest> LoginRequests { get; } = new();

public T Get<T>(IRestRequest request)
{
return Task.Run(async () => await (GetAsync<T>(request, CancellationToken.None)).ConfigureAwait(false)).Result;
}

public Task<T> GetAsync<T>(IRestRequest request, CancellationToken cancellationToken)
{
return Task.FromResult<T>((T)(object)null);
}

public Task<HttpResponseMessage> GetAsync(IRestRequest request, CancellationToken cancellationToken)
{
return Task.FromResult<HttpResponseMessage>(null);
}

public HttpResponseMessage Get(IRestRequest request)
{
return null;
}

public T Post<T>(IRestRequest postRequest)
{
return Task.Run(async () => await (PostAsync<T>(postRequest, CancellationToken.None)).ConfigureAwait(false)).Result;
}

public Task<T> PostAsync<T>(IRestRequest postRequest, CancellationToken cancellationToken)
{
SFRestRequest sfRequest = (SFRestRequest)postRequest;
if (sfRequest.jsonBody is LoginRequest)
{
LoginRequests.Add((LoginRequest) sfRequest.jsonBody);
LoginResponse authnResponse = new LoginResponse
{
data = new LoginResponseData()
{
token = "session_token",
masterToken = "master_token",
authResponseSessionInfo = new SessionInfo(),
nameValueParameter = new List<NameValueParameter>()
},
success = true
};

// login request return success
return Task.FromResult<T>((T)(object)authnResponse);
}
throw new NotImplementedException();
}

public void setHttpClient(HttpClient httpClient)
{
// Nothing to do
}
}
}
25 changes: 25 additions & 0 deletions Snowflake.Data.Tests/UnitTests/SFSessionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
* Copyright (c) 2012-2024 Snowflake Computing Inc. All rights reserved.
*/

using Newtonsoft.Json;
using Snowflake.Data.Core;
using NUnit.Framework;
using Snowflake.Data.Tests.Mock;

namespace Snowflake.Data.Tests.UnitTests
{
Expand Down Expand Up @@ -123,5 +125,28 @@ public void TestSessionPropertyQuotationSafeUpdateOnServerResponse(string sessio
else
Assert.AreEqual(sessionInitialValue, changedSessionValue);
}

[Test]
public void TestHandlePasswordWithQuotations()
{
// arrange
MockLoginStoringRestRequester restRequester = new MockLoginStoringRestRequester();
SFSession sfSession = new SFSession("account=test;user=test;password=test\"with'quotations{}", null, restRequester);

// act
sfSession.Open();

// assert
Assert.AreEqual(1, restRequester.LoginRequests.Count);
var loginRequest = restRequester.LoginRequests[0];
Assert.AreEqual("test\"with'quotations{}", loginRequest.data.password);

// act
var json = JsonConvert.SerializeObject(loginRequest, JsonUtils.JsonSettings);
var deserializedLoginRequest = (LoginRequest) JsonConvert.DeserializeObject(json, typeof(LoginRequest));

// assert
Assert.AreEqual(loginRequest.data.password, deserializedLoginRequest.data.password);
}
}
}

0 comments on commit 9ac574e

Please sign in to comment.