Skip to content

Commit

Permalink
Add S/R tests using JWT over basic auth
Browse files Browse the repository at this point in the history
  • Loading branch information
myieye committed Oct 23, 2023
1 parent 46af37f commit 3f6242e
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 18 deletions.
28 changes: 28 additions & 0 deletions backend/Testing/Services/JwtHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Net.Http.Json;
using LexBoxApi.Auth;
using Shouldly;

namespace Testing.Services;

public class JwtHelper
{

public static async Task<string> GetJwtForUser(SendReceiveAuth auth)
{
var handler = new HttpClientHandler();
var client = new HttpClient(handler);
var response = await client.PostAsJsonAsync(
$"{TestingEnvironmentVariables.StandardHgBaseUrl}/api/login",
new Dictionary<string, object>
{
{ "password", auth.Password }, { "emailOrUsername", auth.Username }, { "preHashedPassword", false }
});
response.EnsureSuccessStatusCode();
var cookieContainer = handler.CookieContainer;
var authCookie = cookieContainer.GetAllCookies().FirstOrDefault(c => c.Name == AuthKernel.AuthCookieName);
authCookie.ShouldNotBeNull();
var jwt = authCookie.Value;
jwt.ShouldNotBeNullOrEmpty();
return jwt;
}
}
14 changes: 1 addition & 13 deletions backend/Testing/SyncReverseProxy/ProxyHgRequestTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,7 @@ public async Task TestGetPrefixHg()
[Fact]
public async Task TestGetWithJwtInBasicAuth()
{
var response = await Client.PostAsJsonAsync(
$"{_baseUrl}/api/login",
new Dictionary<string, object>
{
{ "password", TestData.Password }, { "emailOrUsername", TestData.User }, { "preHashedPassword", false }
});
response.EnsureSuccessStatusCode();
var cookies = response.Headers.GetValues("Set-Cookie");
var cookieContainer = new CookieContainer();
cookieContainer.SetCookies(response.RequestMessage!.RequestUri!, cookies.Single());
var authCookie = cookieContainer.GetAllCookies().FirstOrDefault(c => c.Name == AuthKernel.AuthCookieName);
authCookie.ShouldNotBeNull();
var jwt = authCookie.Value;
var jwt = await JwtHelper.GetJwtForUser(new(TestData.User, TestData.Password));
jwt.ShouldNotBeNullOrEmpty();

var responseMessage = await Client.SendAsync(new HttpRequestMessage(HttpMethod.Get,
Expand Down
28 changes: 23 additions & 5 deletions backend/Testing/SyncReverseProxy/SendReceiveServiceTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.IO.Compression;
using System.Runtime.CompilerServices;
using Chorus.VcsDrivers.Mercurial;
using LexBoxApi.Auth;
using LexCore.Utils;
using Shouldly;
using SIL.Progress;
Expand Down Expand Up @@ -78,7 +79,9 @@ public async Task VerifyHgWorking()
[Fact]
public void CloneBigProject()
{
RunCloneSendReceive(HgProtocol.Hgweb, "admin", "elawa-dev-flex");
RunCloneSendReceive(HgProtocol.Hgweb,
new SendReceiveAuth("admin", TestingEnvironmentVariables.DefaultPassword),
"elawa-dev-flex");
}

[Theory]
Expand All @@ -88,13 +91,28 @@ public void CloneBigProject()
[InlineData(HgProtocol.Resumable, "manager")]
public void CanCloneSendReceive(HgProtocol hgProtocol, string user)
{
RunCloneSendReceive(hgProtocol, user, TestingEnvironmentVariables.ProjectCode);
RunCloneSendReceive(hgProtocol,
new SendReceiveAuth(user, TestingEnvironmentVariables.DefaultPassword),
TestingEnvironmentVariables.ProjectCode);
}
private void RunCloneSendReceive(HgProtocol hgProtocol, string user, string projectCode)

[Theory]
[InlineData(HgProtocol.Hgweb, "admin")]
[InlineData(HgProtocol.Hgweb, "manager")]
[InlineData(HgProtocol.Resumable, "admin")]
[InlineData(HgProtocol.Resumable, "manager")]
public async Task CanCloneSendReceiveWithJwtOverBasicAuth(HgProtocol hgProtocol, string user)
{
var jwt = await JwtHelper.GetJwtForUser(new SendReceiveAuth(user, TestingEnvironmentVariables.DefaultPassword));
RunCloneSendReceive(hgProtocol,
new SendReceiveAuth(AuthKernel.JwtOverBasicAuthUsername, jwt),
TestingEnvironmentVariables.ProjectCode);
}

private void RunCloneSendReceive(HgProtocol hgProtocol, SendReceiveAuth auth, string projectCode)
{
var auth = new SendReceiveAuth(user, TestingEnvironmentVariables.DefaultPassword);
var sendReceiveParams = new SendReceiveParams(projectCode, hgProtocol.GetTestHostName(),
GetProjectDir(projectCode, Path.Join(hgProtocol.ToString(), user)));
GetProjectDir(projectCode, Path.Join(hgProtocol.ToString(), auth.Username)));
var projectDir = sendReceiveParams.DestDir;
var fwDataFile = sendReceiveParams.FwDataFile;

Expand Down

0 comments on commit 3f6242e

Please sign in to comment.