diff --git a/backend/Testing/Services/JwtHelper.cs b/backend/Testing/Services/JwtHelper.cs new file mode 100644 index 0000000000..273cd96777 --- /dev/null +++ b/backend/Testing/Services/JwtHelper.cs @@ -0,0 +1,28 @@ +using System.Net.Http.Json; +using LexBoxApi.Auth; +using Shouldly; + +namespace Testing.Services; + +public class JwtHelper +{ + + public static async Task GetJwtForUser(SendReceiveAuth auth) + { + var handler = new HttpClientHandler(); + var client = new HttpClient(handler); + var response = await client.PostAsJsonAsync( + $"{TestingEnvironmentVariables.StandardHgBaseUrl}/api/login", + new Dictionary + { + { "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; + } +} diff --git a/backend/Testing/SyncReverseProxy/ProxyHgRequestTests.cs b/backend/Testing/SyncReverseProxy/ProxyHgRequestTests.cs index dcc0c360e1..92342b5cac 100644 --- a/backend/Testing/SyncReverseProxy/ProxyHgRequestTests.cs +++ b/backend/Testing/SyncReverseProxy/ProxyHgRequestTests.cs @@ -57,19 +57,7 @@ public async Task TestGetPrefixHg() [Fact] public async Task TestGetWithJwtInBasicAuth() { - var response = await Client.PostAsJsonAsync( - $"{_baseUrl}/api/login", - new Dictionary - { - { "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, diff --git a/backend/Testing/SyncReverseProxy/SendReceiveServiceTests.cs b/backend/Testing/SyncReverseProxy/SendReceiveServiceTests.cs index d3d2ea850f..d5c3cc6594 100644 --- a/backend/Testing/SyncReverseProxy/SendReceiveServiceTests.cs +++ b/backend/Testing/SyncReverseProxy/SendReceiveServiceTests.cs @@ -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; @@ -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] @@ -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;