77 lines
2.7 KiB
C#
77 lines
2.7 KiB
C#
using System.Net;
|
|
using System.Net.Http.Json;
|
|
using System.Text.Json;
|
|
|
|
namespace VolcanicCore.Tests;
|
|
|
|
public class ConnectionsEndpointsTests : IClassFixture<WebFactory>
|
|
{
|
|
private readonly WebFactory _factory;
|
|
public ConnectionsEndpointsTests(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 Create_list_delete_round_trip()
|
|
{
|
|
var c = await LoggedIn();
|
|
var create = await c.PostAsJsonAsync("/api/connections", new
|
|
{
|
|
providerId = "nats",
|
|
name = "local",
|
|
config = new { url = "nats://127.0.0.1:4222", user = "u", pass = "p" }
|
|
});
|
|
Assert.Equal(HttpStatusCode.Created, create.StatusCode);
|
|
var created = await create.Content.ReadFromJsonAsync<ConnectionDto>();
|
|
Assert.NotNull(created);
|
|
Assert.False(string.IsNullOrEmpty(created!.Id));
|
|
|
|
var list = await c.GetFromJsonAsync<List<ConnectionDto>>("/api/connections");
|
|
Assert.Contains(list!, x => x.Id == created.Id && x.Name == "local");
|
|
|
|
var get = await c.GetFromJsonAsync<ConnectionDto>($"/api/connections/{created.Id}");
|
|
Assert.NotNull(get);
|
|
|
|
var del = await c.DeleteAsync($"/api/connections/{created.Id}");
|
|
Assert.Equal(HttpStatusCode.NoContent, del.StatusCode);
|
|
|
|
var list2 = await c.GetFromJsonAsync<List<ConnectionDto>>("/api/connections");
|
|
Assert.DoesNotContain(list2!, x => x.Id == created.Id);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Post_with_non_object_config_returns_400()
|
|
{
|
|
var c = await LoggedIn();
|
|
var res = await c.PostAsJsonAsync("/api/connections", new
|
|
{
|
|
providerId = "nats", name = "x", config = "not-an-object"
|
|
});
|
|
Assert.Equal(HttpStatusCode.BadRequest, res.StatusCode);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Post_with_duplicate_name_per_provider_returns_409()
|
|
{
|
|
var c = await LoggedIn();
|
|
var first = await c.PostAsJsonAsync("/api/connections", new
|
|
{
|
|
providerId = "nats", name = "dup", config = new { url = "nats://x" }
|
|
});
|
|
Assert.Equal(HttpStatusCode.Created, first.StatusCode);
|
|
|
|
var second = await c.PostAsJsonAsync("/api/connections", new
|
|
{
|
|
providerId = "nats", name = "dup", config = new { url = "nats://y" }
|
|
});
|
|
Assert.Equal(HttpStatusCode.Conflict, second.StatusCode);
|
|
}
|
|
|
|
private sealed record ConnectionDto(string Id, string ProviderId, string Name, JsonElement Config);
|
|
}
|