62 lines
2.3 KiB
C#
62 lines
2.3 KiB
C#
using System.Net.Http.Json;
|
|
using System.Text.Json;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using VolcanicCore.Api.Engine;
|
|
using VolcanicCore.Persistence.Repositories;
|
|
|
|
namespace VolcanicCore.Tests;
|
|
|
|
public class TriggerHostTests : IClassFixture<WebFactory>
|
|
{
|
|
private readonly WebFactory _factory;
|
|
public TriggerHostTests(WebFactory f) => _factory = f;
|
|
|
|
private async Task<HttpClient> LoggedIn()
|
|
{
|
|
var c = _factory.CreateClient(new() { HandleCookies = true, AllowAutoRedirect = false });
|
|
await c.PostAsJsonAsync("/api/session", new { password = _factory.AdminPassword });
|
|
return c;
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Activating_a_workflow_registers_its_webhook_and_deactivating_unregisters_it()
|
|
{
|
|
var c = await LoggedIn();
|
|
var graph = new
|
|
{
|
|
nodes = new object[]
|
|
{
|
|
new { id = "t", type_id = "http.webhook", config = new { path = "hook" } },
|
|
},
|
|
edges = Array.Empty<object>(),
|
|
};
|
|
var create = await c.PostAsJsonAsync("/api/workflows", new { name = "webhook-wf", graph });
|
|
var body = await create.Content.ReadFromJsonAsync<JsonElement>();
|
|
var wfId = body.GetProperty("id").GetString()!;
|
|
|
|
var pre = await c.PostAsJsonAsync($"/wh/{wfId}/hook", new { });
|
|
Assert.Equal(System.Net.HttpStatusCode.NotFound, pre.StatusCode);
|
|
|
|
await c.PostAsync($"/api/workflows/{wfId}/activate", null);
|
|
|
|
var mid = await c.PostAsJsonAsync($"/wh/{wfId}/hook", new { hello = "world" });
|
|
Assert.Equal(System.Net.HttpStatusCode.NoContent, mid.StatusCode);
|
|
|
|
await c.PostAsync($"/api/workflows/{wfId}/deactivate", null);
|
|
var post = await c.PostAsJsonAsync($"/wh/{wfId}/hook", new { });
|
|
Assert.Equal(System.Net.HttpStatusCode.NotFound, post.StatusCode);
|
|
}
|
|
|
|
[Fact]
|
|
public void Startup_aborts_any_execution_left_in_running()
|
|
{
|
|
var er = _factory.Services.GetRequiredService<ExecutionRepository>();
|
|
var id = er.Create("wf-orphan", "manual", "{}");
|
|
Assert.Equal("running", er.Get(id)!.Status);
|
|
|
|
var host = _factory.Services.GetRequiredService<TriggerHost>();
|
|
host.RunStartupAbortSweep();
|
|
Assert.Equal("aborted", er.Get(id)!.Status);
|
|
}
|
|
}
|