123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
using System.Collections.Concurrent;
using System.Net;
using System.Text.Json;
using Forge.Data;
using Forge.Features.Webhooks;
using Forge.Tests.Support;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
namespace Forge.Tests;
public class WebhookTests(ForgeHost host) : IClassFixture<ForgeHost>
{
private sealed record Received(string Event, string Delivery, string Signature, byte[] Body);
/// <summary>A local HTTP endpoint standing in for a CI server. Answers with whatever status the test queues.</summary>
private sealed class Receiver : IAsyncDisposable
{
private readonly WebApplication _app;
public ConcurrentQueue<Received> Requests { get; } = new();
public ConcurrentQueue<int> Statuses { get; } = new();
public string Url { get; }
private Receiver(WebApplication app)
{
_app = app;
Url = app.Urls.First().TrimEnd('/') + "/hook";
}
public static async Task<Receiver> StartAsync()
{
var builder = WebApplication.CreateBuilder(["--urls=http://127.0.0.1:0", "--Logging:LogLevel:Default=Warning"]);
var app = builder.Build();
Receiver? self = null;
app.MapPost("/hook", async (HttpContext context) =>
{
using var body = new MemoryStream();
await context.Request.Body.CopyToAsync(body);
self!.Requests.Enqueue(new Received(
context.Request.Headers["X-Forge-Event"].ToString(),
context.Request.Headers["X-Forge-Delivery"].ToString(),
context.Request.Headers["X-Forge-Signature"].ToString(),
body.ToArray()));
return Results.StatusCode(self.Statuses.TryDequeue(out var status) ? status : 200);
});
await app.StartAsync();
self = new Receiver(app);
return self;
}
public async Task<Received> NextAsync(int count = 1)
{
var deadline = DateTime.UtcNow.AddSeconds(30);
while (Requests.Count < count)
{
Assert.True(DateTime.UtcNow < deadline, $"Expected {count} webhook request(s), got {Requests.Count}.");
await Task.Delay(50);
}
return Requests.ElementAt(count - 1);
}
public async ValueTask DisposeAsync()
{
await _app.StopAsync();
await _app.DisposeAsync();
}
}
private Task<List<WebhookDelivery>> DeliveriesAsync(long webhookId) =>
host.WithScopeAsync(sp => sp.GetRequiredService<ForgeDb>().WebhookDeliveries
.Where(d => d.WebhookId == webhookId).OrderBy(d => d.Id).ToListAsync());
private async Task<List<WebhookDelivery>> WaitForDeliveriesAsync(long webhookId, Func<List<WebhookDelivery>, bool> done)
{
var deadline = DateTime.UtcNow.AddSeconds(30);
while (true)
{
var rows = await DeliveriesAsync(webhookId);
if (done(rows))
{
return rows;
}
Assert.True(DateTime.UtcNow < deadline, $"Deliveries did not reach the expected state; have {rows.Count}.");
await Task.Delay(50);
}
}
[Fact]
public async Task Push_sends_a_signed_payload_and_failures_are_retried()
{
WebhookWorker.RetryDelays = [TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(100)];
await using var receiver = await Receiver.StartAsync();
var user = await host.CreateUserAsync();
var repo = await host.CreateRepoAsync(user, "hooked", Visibility.Public);
var token = await host.CreateTokenAsync(user, canWrite: true);
using var client = await host.CreateSignedInClientAsync();
// Add the webhook through the UI.
var added = await ForgeHost.PostFormAsync(client, "/jessie/hooked/settings/webhooks", "add-webhook", new()
{
["Url"] = receiver.Url,
["Secret"] = "s3cret",
});
Assert.Equal(HttpStatusCode.Redirect, added.StatusCode);
var hook = await host.WithScopeAsync(sp => sp.GetRequiredService<ForgeDb>().Webhooks.SingleAsync(w => w.RepoId == repo.Id));
var detailUrl = $"/jessie/hooked/settings/webhooks/{hook.Id}";
// Test ping.
var pinged = await ForgeHost.PostFormAsync(client, detailUrl, "hook-action", new() { ["Action"] = "ping" });
Assert.Contains("The ping was delivered", await ForgeHost.TextAsync(pinged));
var ping = await receiver.NextAsync(1);
Assert.Equal("ping", ping.Event);
Assert.Equal(WebhookSender.Sign("s3cret", ping.Body), ping.Signature);
// A push: the first attempt fails with 500, the retry succeeds.
receiver.Statuses.Enqueue(500);
var work = host.NewDir("work");
var sha = await host.Git.CreateWorkRepoAsync(work);
await host.Git.MustAsync(work, "push", host.GitUrl("jessie", "hooked", token), "main");
var retried = await receiver.NextAsync(3);
Assert.Equal("push", retried.Event);
Assert.Equal(WebhookSender.Sign("s3cret", retried.Body), retried.Signature);
Assert.StartsWith("sha256=", retried.Signature);
using var payload = JsonDocument.Parse(retried.Body);
var root = payload.RootElement;
Assert.Equal("refs/heads/main", root.GetProperty("ref").GetString());
Assert.Equal(Git.GitParsers.ZeroSha, root.GetProperty("before").GetString());
Assert.Equal(sha, root.GetProperty("after").GetString());
Assert.Equal("jessie/hooked", root.GetProperty("repository").GetProperty("full_name").GetString());
Assert.Equal("http://forge.test/jessie/hooked.git", root.GetProperty("repository").GetProperty("clone_url").GetString());
Assert.StartsWith("http://forge.test/jessie/hooked/", root.GetProperty("compare").GetString());
var commit = Assert.Single(root.GetProperty("commits").EnumerateArray());
Assert.Equal(sha, commit.GetProperty("id").GetString());
Assert.Equal("First commit", commit.GetProperty("message").GetString());
// Every attempt is a row: ping, failed push, successful retry.
var rows = await WaitForDeliveriesAsync(hook.Id, r => r.Count(d => d.PushEventId != null && d.StatusCode != null) == 2);
var pushRows = rows.Where(d => d.PushEventId != null).ToList();
Assert.Equal([(1, 500), (2, 200)], pushRows.Select(d => (d.Attempt, d.StatusCode!.Value)));
Assert.Equal(retried.Delivery, pushRows[1].Id.ToString());
Assert.Null(rows[0].PushEventId);
Assert.Equal(200, rows[0].StatusCode);
// The history page lists them, and Redeliver sends the event again.
var history = await client.GetStringAsync(detailUrl);
Assert.Contains("refs/heads/main", history);
var redelivered = await ForgeHost.PostFormAsync(client, detailUrl, "redeliver", new() { ["DeliveryId"] = pushRows[0].Id.ToString() });
Assert.Contains("Delivered again", await ForgeHost.TextAsync(redelivered));
var again = await receiver.NextAsync(4);
Assert.Equal(retried.Body, again.Body);
Assert.Equal(3, (await DeliveriesAsync(hook.Id)).Last().Attempt);
// A disabled webhook receives nothing.
await ForgeHost.PostFormAsync(client, detailUrl, "hook-action", new() { ["Action"] = "toggle" });
await File.WriteAllTextAsync(Path.Combine(work, "more.txt"), "more\n");
await host.Git.MustAsync(work, "add", "-A");
await host.Git.MustAsync(work, "commit", "-m", "More");
await host.Git.MustAsync(work, "push", host.GitUrl("jessie", "hooked", token), "main");
await Task.Delay(500);
Assert.Equal(4, receiver.Requests.Count);
// Deleting the repository removes its webhooks and deliveries with it.
await host.WithScopeAsync(async sp =>
{
var service = sp.GetRequiredService<Features.Repos.RepoService>();
await service.DeleteAsync((await service.FindAsync("jessie", "hooked"))!);
return 0;
});
Assert.Empty(await DeliveriesAsync(hook.Id));
}
[Fact]
public async Task Unreachable_receiver_is_recorded_as_a_failed_delivery()
{
var user = await host.CreateUserAsync();
var repo = await host.CreateRepoAsync(user, "unreachable", Visibility.Public);
var hook = await host.WithScopeAsync(async sp =>
{
var db = sp.GetRequiredService<ForgeDb>();
// Port 9 (discard) on localhost: nothing listens there.
var row = new Webhook { RepoId = repo.Id, Url = "http://127.0.0.1:9/hook", Secret = "x", CreatedAt = DateTime.UtcNow };
db.Webhooks.Add(row);
await db.SaveChangesAsync();
return row;
});
var ok = await host.Services.GetRequiredService<WebhookSender>().DeliverAsync(hook.Id, null, 1);
Assert.False(ok);
var delivery = Assert.Single(await DeliveriesAsync(hook.Id));
Assert.Null(delivery.StatusCode);
Assert.False(string.IsNullOrEmpty(delivery.Error));
}
}