| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669 |
- using System.Net;
- using System.Net.Http.Json;
- using RackPeek.Domain.Api;
- using Xunit.Abstractions;
- namespace Tests.Api;
- public class InventoryEndpointTests(ITestOutputHelper output) : ApiTestBase(output) {
- [Fact]
- public async Task DryRun_Add_New_Resource_Does_Not_Persist() {
- HttpClient client = CreateClient(true);
- var yaml = """
- resources:
- - name: example-server
- kind: Server
- """;
- HttpResponseMessage response = await client.PostAsJsonAsync("/api/inventory",
- new {
- yaml,
- dryRun = true
- });
- Assert.Equal(HttpStatusCode.OK, response.StatusCode);
- ImportYamlResponse? result = await response.Content.ReadFromJsonAsync<ImportYamlResponse>();
- Assert.Single(result!.Added);
- Assert.Contains("example-server", result.Added);
- // Call again — still should be "added" because dry run did not persist
- HttpResponseMessage response2 = await client.PostAsJsonAsync("/api/inventory",
- new {
- yaml,
- dryRun = true
- });
- ImportYamlResponse? result2 = await response2.Content.ReadFromJsonAsync<ImportYamlResponse>();
- Assert.Single(result2!.Added);
- }
- [Fact]
- public async Task Merge_Add_New_Resource_Persists() {
- HttpClient client = CreateClient(true);
- var yaml = """
- version: 2
- resources:
- - kind: Server
- name: server-merge
-
- """;
- HttpResponseMessage response = await client.PostAsJsonAsync("/api/inventory",
- new {
- Yaml = yaml,
- mode = "Merge"
- });
- Assert.Equal(HttpStatusCode.OK, response.StatusCode);
- ImportYamlResponse? result = await response.Content.ReadFromJsonAsync<ImportYamlResponse>();
- Assert.Single(result!.Added);
- // Now second call should detect no change
- HttpResponseMessage response2 = await client.PostAsJsonAsync("/api/inventory",
- new {
- yaml,
- dryRun = true
- });
- ImportYamlResponse? result2 = await response2.Content.ReadFromJsonAsync<ImportYamlResponse>();
- Assert.Empty(result2!.Added);
- Assert.Empty(result2.Updated);
- Assert.Empty(result2.Replaced);
- }
- [Fact]
- public async Task Merge_Updates_Existing_Resource() {
- HttpClient client = CreateClient(true);
- var initial = """
- version: 2
- resources:
- - kind: Server
- name: server-update
- ipmi: true
- """;
- await client.PostAsJsonAsync("/api/inventory",
- new { Yaml = initial });
- var update = """
- version: 3
- resources:
- - kind: Server
- name: server-update
- ipmi: false
- """;
- HttpResponseMessage response = await client.PostAsJsonAsync("/api/inventory",
- new {
- Yaml = update,
- mode = "Merge"
- });
- ImportYamlResponse? result = await response.Content.ReadFromJsonAsync<ImportYamlResponse>();
- Assert.Single(result!.Updated);
- Assert.Contains("server-update", result.Updated);
- }
- [Fact]
- public async Task Replace_Replaces_Existing_Resource() {
- HttpClient client = CreateClient(true);
- var initial = """
- resources:
- - kind: Server
- name: server-replace
- ipmi: true
- """;
- await client.PostAsJsonAsync("/api/inventory",
- new { yaml = initial });
- var replace = """
- resources:
- - kind: Server
- name: server-replace
- """;
- HttpResponseMessage response = await client.PostAsJsonAsync("/api/inventory",
- new {
- yaml = replace,
- mode = "Replace"
- });
- ImportYamlResponse? result = await response.Content.ReadFromJsonAsync<ImportYamlResponse>();
- Assert.Single(result!.Replaced);
- Assert.Contains("server-replace", result.Replaced);
- }
- [Fact]
- public async Task Invalid_Yaml_Returns_400() {
- HttpClient client = CreateClient(true);
- HttpResponseMessage response = await client.PostAsJsonAsync("/api/inventory",
- new {
- yaml = "not: valid: yaml:"
- });
- Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
- }
- [Fact]
- public async Task Missing_Resources_Section_Returns_400() {
- HttpClient client = CreateClient(true);
- var yaml = """
- somethingElse:
- - name: test
- """;
- HttpResponseMessage response = await client.PostAsJsonAsync("/api/inventory",
- new { yaml });
- Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
- }
- [Fact]
- public async Task Accepts_Json_Root_Input() {
- HttpClient client = CreateClient(true);
- HttpResponseMessage response = await client.PostAsJsonAsync("/api/inventory",
- new {
- json = new {
- version = 1,
- resources = new[]
- {
- new { kind = "Server", name = "json-server" }
- }
- }
- });
- Assert.Equal(HttpStatusCode.OK, response.StatusCode);
- ImportYamlResponse? result = await response.Content.ReadFromJsonAsync<ImportYamlResponse>();
- Assert.Single(result!.Added);
- Assert.Contains("json-server", result.Added);
- }
- [Fact]
- public async Task Requires_Api_Key() {
- HttpClient client = CreateClient();
- var yaml = """
- resources:
- - name: no-auth
- kind: Server
- """;
- HttpResponseMessage response = await client.PostAsJsonAsync("/api/inventory",
- new { yaml });
- Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
- }
- [Fact]
- public async Task Import_Full_Config_Works() {
- HttpClient client = CreateClient(true);
- var yaml = await File.ReadAllTextAsync("TestConfigs/v2/11-demo-config.yaml");
- // Put your big sample YAML in TestData folder
- HttpResponseMessage response = await client.PostAsJsonAsync("/api/inventory",
- new { yaml });
- Assert.Equal(HttpStatusCode.OK, response.StatusCode);
- ImportYamlResponse? result = await response.Content.ReadFromJsonAsync<ImportYamlResponse>();
- Assert.True(result!.Added.Count > 10);
- Assert.Empty(result.Updated);
- Assert.Empty(result.Replaced);
- }
- [Fact]
- public async Task Import_Full_Config_Twice_Is_Idempotent() {
- HttpClient client = CreateClient(true);
- var yaml = await File.ReadAllTextAsync("TestConfigs/v2/11-demo-config.yaml");
- await client.PostAsJsonAsync("/api/inventory", new { yaml });
- HttpResponseMessage response2 = await client.PostAsJsonAsync("/api/inventory",
- new { yaml, dryRun = true });
- ImportYamlResponse? result2 = await response2.Content.ReadFromJsonAsync<ImportYamlResponse>();
- Assert.Empty(result2!.Added);
- Assert.Empty(result2.Updated);
- Assert.Empty(result2.Replaced);
- }
- [Fact]
- public async Task Merge_Updates_Nested_Object() {
- HttpClient client = CreateClient(true);
- var initial = """
- version: 2
- resources:
- - kind: Server
- name: nested-test
- ram:
- size: 64
- mts: 2666
- """;
- await client.PostAsJsonAsync("/api/inventory", new { yaml = initial });
- var update = """
- version: 3
- resources:
- - kind: Server
- name: nested-test
- ram:
- size: 128
- """;
- HttpResponseMessage response = await client.PostAsJsonAsync("/api/inventory",
- new { yaml = update, mode = "Merge" });
- ImportYamlResponse? result = await response.Content.ReadFromJsonAsync<ImportYamlResponse>();
- Assert.Single(result!.Updated);
- }
- [Fact]
- public async Task Merge_Does_Not_Clear_List_When_Empty() {
- HttpClient client = CreateClient(true);
- var initial = """
- resources:
- - kind: Server
- name: drive-test
- drives:
- - type: ssd
- size: 1024
- """;
- await client.PostAsJsonAsync("/api/inventory", new { yaml = initial });
- var update = """
- resources:
- - kind: Server
- name: drive-test
- drives: []
- """;
- HttpResponseMessage response = await client.PostAsJsonAsync("/api/inventory",
- new { yaml = update, mode = "Merge" });
- ImportYamlResponse? result = await response.Content.ReadFromJsonAsync<ImportYamlResponse>();
- // Should NOT count as update because empty list ignored
- Assert.Empty(result!.Updated);
- }
- [Fact]
- public async Task Replace_Clears_List() {
- HttpClient client = CreateClient(true);
- var initial = """
- resources:
- - kind: Server
- name: replace-drive-test
- drives:
- - type: ssd
- size: 1024
- """;
- await client.PostAsJsonAsync("/api/inventory", new { yaml = initial });
- var replace = """
- resources:
- - kind: Server
- name: replace-drive-test
- drives: []
- """;
- HttpResponseMessage response = await client.PostAsJsonAsync("/api/inventory",
- new { yaml = replace, mode = "Replace" });
- ImportYamlResponse? result = await response.Content.ReadFromJsonAsync<ImportYamlResponse>();
- Assert.Single(result!.Replaced);
- }
- [Fact]
- public async Task Type_Change_Forces_Replace() {
- HttpClient client = CreateClient(true);
- var initial = """
- version: 2
- resources:
- - kind: Server
- name: polymorph-test
- """;
- await client.PostAsJsonAsync("/api/inventory", new { yaml = initial });
- var update = """
- version: 3
- resources:
- - kind: Firewall
- name: polymorph-test
- """;
- HttpResponseMessage response = await client.PostAsJsonAsync("/api/inventory",
- new { yaml = update, mode = "Merge" });
- ImportYamlResponse? result = await response.Content.ReadFromJsonAsync<ImportYamlResponse>();
- Assert.Single(result!.Replaced);
- }
- [Fact]
- public async Task Name_Matching_Is_Case_Insensitive() {
- HttpClient client = CreateClient(true);
- var initial = """
- resources:
- - kind: Server
- name: CaseTest
- """;
- await client.PostAsJsonAsync("/api/inventory", new { yaml = initial });
- var update = """
- resources:
- - kind: Server
- name: casetest
- ipmi: true
- """;
- HttpResponseMessage response = await client.PostAsJsonAsync("/api/inventory",
- new { yaml = update });
- ImportYamlResponse? result = await response.Content.ReadFromJsonAsync<ImportYamlResponse>();
- Assert.Single(result!.Updated);
- }
- [Fact]
- public async Task Multiple_Resources_Are_Processed() {
- HttpClient client = CreateClient(true);
- var yaml = """
- resources:
- - kind: Server
- name: multi-1
- - kind: Firewall
- name: multi-2
- """;
- HttpResponseMessage response = await client.PostAsJsonAsync("/api/inventory",
- new { yaml });
- ImportYamlResponse? result = await response.Content.ReadFromJsonAsync<ImportYamlResponse>();
- Assert.Equal(2, result!.Added.Count);
- }
- [Fact]
- public async Task DryRun_Replace_Does_Not_Persist() {
- HttpClient client = CreateClient(true);
- var initial = """
- resources:
- - kind: Server
- name: dry-replace
- ipmi: true
- """;
- await client.PostAsJsonAsync("/api/inventory", new { yaml = initial });
- var replace = """
- resources:
- - kind: Server
- name: dry-replace
- """;
- await client.PostAsJsonAsync("/api/inventory",
- new { yaml = replace, mode = "Replace", dryRun = true });
- HttpResponseMessage check = await client.PostAsJsonAsync("/api/inventory",
- new { yaml = replace, mode = "Replace", dryRun = true });
- ImportYamlResponse? result = await check.Content.ReadFromJsonAsync<ImportYamlResponse>();
- Assert.Single(result!.Replaced);
- }
- [Fact]
- public async Task Providing_Both_Yaml_And_Json_Returns_400() {
- HttpClient client = CreateClient(true);
- HttpResponseMessage response = await client.PostAsJsonAsync("/api/inventory",
- new {
- yaml = "resources: []",
- json = new { resources = Array.Empty<object>() }
- });
- Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
- }
- [Fact]
- public async Task Empty_Request_Returns_400() {
- HttpClient client = CreateClient(true);
- HttpResponseMessage response = await client.PostAsJsonAsync("/api/inventory", new { });
- Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
- }
- [Fact]
- public async Task Version_1_Config_Is_Accepted() {
- HttpClient client = CreateClient(true);
- var yaml = """
- version: 1
- resources:
- - kind: Server
- name: v1-server
- """;
- HttpResponseMessage response = await client.PostAsJsonAsync("/api/inventory", new { yaml });
- Assert.Equal(HttpStatusCode.OK, response.StatusCode);
- ImportYamlResponse? result = await response.Content.ReadFromJsonAsync<ImportYamlResponse>();
- Assert.Single(result!.Added);
- }
- [Fact]
- public async Task Replace_Removes_Existing_Fields() {
- HttpClient client = CreateClient(true);
- var initial = """
- resources:
- - kind: Server
- name: destructive-test
- ipmi: true
- """;
- await client.PostAsJsonAsync("/api/inventory", new { yaml = initial });
- var replace = """
- resources:
- - kind: Server
- name: destructive-test
- """;
- HttpResponseMessage response = await client.PostAsJsonAsync("/api/inventory",
- new { yaml = replace, mode = "Replace" });
- ImportYamlResponse? result = await response.Content.ReadFromJsonAsync<ImportYamlResponse>();
- Assert.Single(result!.Replaced);
- Assert.Contains("destructive-test", result.OldYaml.Keys);
- Assert.Contains("destructive-test", result.NewYaml.Keys);
- }
- [Fact]
- public async Task Merge_Does_Not_Affect_Unspecified_Resources() {
- HttpClient client = CreateClient(true);
- var full = await File.ReadAllTextAsync("TestConfigs/v2/11-demo-config.yaml");
- await client.PostAsJsonAsync("/api/inventory", new { yaml = full });
- var update = """
- resources:
- - kind: Server
- name: proxmox-node01
- ipmi: false
- """;
- HttpResponseMessage response = await client.PostAsJsonAsync("/api/inventory",
- new { yaml = update, mode = "Merge" });
- ImportYamlResponse? result = await response.Content.ReadFromJsonAsync<ImportYamlResponse>();
- Assert.Single(result!.Updated);
- Assert.DoesNotContain("proxmox-node02", result.Updated);
- }
- [Fact]
- public async Task Json_Input_Resolves_Polymorphic_Resource() {
- HttpClient client = CreateClient(true);
- HttpResponseMessage response = await client.PostAsJsonAsync("/api/inventory",
- new {
- json = new {
- version = 2,
- resources = new[]
- {
- new { kind = "Firewall", name = "json-fw", model = "Test" }
- }
- }
- });
- ImportYamlResponse? result = await response.Content.ReadFromJsonAsync<ImportYamlResponse>();
- Assert.Single(result!.Added);
- Assert.Contains("json-fw", result.Added);
- }
- [Fact]
- public async Task Large_Config_Is_Fully_Idempotent() {
- HttpClient client = CreateClient(true);
- var yaml = await File.ReadAllTextAsync("TestConfigs/v2/11-demo-config.yaml");
- await client.PostAsJsonAsync("/api/inventory", new { yaml });
- HttpResponseMessage response = await client.PostAsJsonAsync("/api/inventory",
- new { yaml });
- ImportYamlResponse? result = await response.Content.ReadFromJsonAsync<ImportYamlResponse>();
- Assert.Empty(result!.Added);
- Assert.Empty(result.Updated);
- Assert.Empty(result.Replaced);
- }
- [Fact]
- public async Task Unknown_Kind_Returns_400() {
- HttpClient client = CreateClient(true);
- var yaml = """
- resources:
- - kind: UnknownThing
- name: mystery
- """;
- HttpResponseMessage response = await client.PostAsJsonAsync("/api/inventory", new { yaml });
- Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
- }
- [Fact]
- public async Task DryRun_Does_Not_Persist_Snapshots() {
- HttpClient client = CreateClient(true);
- var yaml = """
- resources:
- - kind: Server
- name: dry-snapshot
- """;
- HttpResponseMessage response = await client.PostAsJsonAsync("/api/inventory",
- new { yaml, dryRun = true });
- ImportYamlResponse? result = await response.Content.ReadFromJsonAsync<ImportYamlResponse>();
- Assert.Empty(result!.OldYaml);
- }
- [Fact]
- public async Task Reordering_List_Does_Not_Count_As_Update() {
- HttpClient client = CreateClient(true);
- var initial = """
- resources:
- - kind: Server
- name: order-test
- drives:
- - type: ssd
- size: 1024
- - type: hdd
- size: 4096
- """;
- await client.PostAsJsonAsync("/api/inventory", new { yaml = initial });
- var reordered = """
- resources:
- - kind: Server
- name: order-test
- drives:
- - type: hdd
- size: 4096
- - type: ssd
- size: 1024
- """;
- HttpResponseMessage response = await client.PostAsJsonAsync("/api/inventory",
- new { yaml = reordered });
- ImportYamlResponse? result = await response.Content.ReadFromJsonAsync<ImportYamlResponse>();
- Assert.Single(result!.Updated);
- Assert.Contains("order-test", result.Updated);
- }
- [Fact]
- public async Task Duplicate_Names_In_Same_Request_Returns_400() {
- HttpClient client = CreateClient(true);
- var yaml = """
- resources:
- - kind: Server
- name: dup
- - kind: Server
- name: dup
- """;
- HttpResponseMessage response = await client.PostAsJsonAsync("/api/inventory", new { yaml });
- Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
- }
- }
|