From c6bc69cd4773e3cb6ae3be9a5f5cb09d3077a4b7 Mon Sep 17 00:00:00 2001 From: Tim Haasdyk Date: Tue, 24 Oct 2023 09:29:08 +0200 Subject: [PATCH] fixup! Add S/R tests using JWT over basic auth --- backend/Testing/Services/JwtHelper.cs | 11 ++++++----- .../SyncReverseProxy/SendReceiveServiceTests.cs | 10 ++++------ 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/backend/Testing/Services/JwtHelper.cs b/backend/Testing/Services/JwtHelper.cs index 273cd9677..7149f628a 100644 --- a/backend/Testing/Services/JwtHelper.cs +++ b/backend/Testing/Services/JwtHelper.cs @@ -6,23 +6,24 @@ namespace Testing.Services; public class JwtHelper { + private static readonly HttpClientHandler Handler = new(); + private static readonly HttpClient Client = new(Handler); public static async Task GetJwtForUser(SendReceiveAuth auth) { - var handler = new HttpClientHandler(); - var client = new HttpClient(handler); - var response = await client.PostAsJsonAsync( + 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); + 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; } } diff --git a/backend/Testing/SyncReverseProxy/SendReceiveServiceTests.cs b/backend/Testing/SyncReverseProxy/SendReceiveServiceTests.cs index d5c3cc659..739c4b030 100644 --- a/backend/Testing/SyncReverseProxy/SendReceiveServiceTests.cs +++ b/backend/Testing/SyncReverseProxy/SendReceiveServiceTests.cs @@ -97,13 +97,11 @@ public void CanCloneSendReceive(HgProtocol hgProtocol, string user) } [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) + [InlineData(HgProtocol.Hgweb)] + [InlineData(HgProtocol.Resumable)] + public async Task CanCloneSendReceiveWithJwtOverBasicAuth(HgProtocol hgProtocol) { - var jwt = await JwtHelper.GetJwtForUser(new SendReceiveAuth(user, TestingEnvironmentVariables.DefaultPassword)); + var jwt = await JwtHelper.GetJwtForUser(AdminAuth); RunCloneSendReceive(hgProtocol, new SendReceiveAuth(AuthKernel.JwtOverBasicAuthUsername, jwt), TestingEnvironmentVariables.ProjectCode);