Skip to content

Commit

Permalink
fixup! 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 24, 2023
1 parent 3f6242e commit 1e04723
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
11 changes: 6 additions & 5 deletions backend/Testing/Services/JwtHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> 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<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);
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;
}
}
10 changes: 4 additions & 6 deletions backend/Testing/SyncReverseProxy/SendReceiveServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 1e04723

Please sign in to comment.