Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add S/R tests using JWT over basic auth #342

Merged
merged 3 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions backend/Testing/Services/JwtHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Net.Http.Json;
using LexBoxApi.Auth;
using Shouldly;

namespace Testing.Services;

public class JwtHelper
{
private static readonly HttpClientHandler Handler = new();
private static readonly HttpClient Client = new(Handler);

public static async Task<string> GetJwtForUser(SendReceiveAuth auth)
{
var response = await Client.PostAsJsonAsync(
$"{TestingEnvironmentVariables.StandardHgBaseUrl}/api/login",
new Dictionary<string, object>
{
{ "password", auth.Password }, { "emailOrUsername", auth.Username }, { "preHashedPassword", false }
});
response.EnsureSuccessStatusCode();
var authCookie = Handler.CookieContainer.GetAllCookies()
.FirstOrDefault(c => c.Name == AuthKernel.AuthCookieName);
authCookie.ShouldNotBeNull();
var jwt = authCookie.Value;
jwt.ShouldNotBeNullOrEmpty();
Handler.CookieContainer = new(); // reset the cookies as we're using a shared client
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
26 changes: 21 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,7 @@ public async Task VerifyHgWorking()
[Fact]
public void CloneBigProject()
{
RunCloneSendReceive(HgProtocol.Hgweb, "admin", "elawa-dev-flex");
RunCloneSendReceive(HgProtocol.Hgweb, AdminAuth, "elawa-dev-flex");
}

[Theory]
Expand All @@ -88,13 +89,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),
hahn-kev marked this conversation as resolved.
Show resolved Hide resolved
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