Refactor code structure for improved readability and maintainability; removed redundant code blocks and optimized function calls.
This commit is contained in:
@@ -0,0 +1,140 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Http.Json;
|
||||
using EphemeralMongo;
|
||||
using Microsoft.AspNetCore.Mvc.Testing;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Driver;
|
||||
using StellaOps.Excititor.Storage.Mongo;
|
||||
using StellaOps.Excititor.WebService.Contracts;
|
||||
using Xunit;
|
||||
|
||||
namespace StellaOps.Excititor.WebService.Tests;
|
||||
|
||||
public sealed class VexLinksetListEndpointTests : IDisposable
|
||||
{
|
||||
private readonly IMongoRunner _runner;
|
||||
private readonly TestWebApplicationFactory _factory;
|
||||
|
||||
public VexLinksetListEndpointTests()
|
||||
{
|
||||
_runner = MongoRunner.Run(new MongoRunnerOptions { UseSingleNodeReplicaSet = true });
|
||||
|
||||
_factory = new TestWebApplicationFactory(
|
||||
configureConfiguration: configuration =>
|
||||
{
|
||||
configuration.AddInMemoryCollection(new Dictionary<string, string?>
|
||||
{
|
||||
["Excititor:Storage:Mongo:ConnectionString"] = _runner.ConnectionString,
|
||||
["Excititor:Storage:Mongo:DatabaseName"] = "linksets_tests",
|
||||
["Excititor:Storage:Mongo:DefaultTenant"] = "tests",
|
||||
});
|
||||
},
|
||||
configureServices: services =>
|
||||
{
|
||||
TestServiceOverrides.Apply(services);
|
||||
services.AddTestAuthentication();
|
||||
});
|
||||
|
||||
SeedObservations();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void LinksetsEndpoint_GroupsByVulnAndProduct()
|
||||
{
|
||||
using var client = _factory.CreateClient(new WebApplicationFactoryClientOptions { AllowAutoRedirect = false });
|
||||
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", "vex.read");
|
||||
client.DefaultRequestHeaders.Add("X-Stella-Tenant", "tests");
|
||||
|
||||
var response = await client.GetAsync("/v1/vex/linksets?vulnerabilityId=CVE-2025-0001&productKey=pkg:demo/app");
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
var payload = await response.Content.ReadFromJsonAsync<VexLinksetListResponse>();
|
||||
Assert.NotNull(payload);
|
||||
Assert.Single(payload!.Items);
|
||||
|
||||
var item = payload.Items.Single();
|
||||
Assert.Equal("CVE-2025-0001:pkg:demo/app", item.LinksetId);
|
||||
Assert.Equal("CVE-2025-0001", item.VulnerabilityId);
|
||||
Assert.Equal("pkg:demo/app", item.ProductKey);
|
||||
|
||||
item.Providers.Should().BeEquivalentTo(new[] { "provider-a", "provider-b" });
|
||||
item.Statuses.Should().BeEquivalentTo(new[] { "affected", "fixed" });
|
||||
item.Observations.Should().HaveCount(2);
|
||||
item.Observations.Should().Contain(o => o.ProviderId == "provider-a" && o.Status == "affected");
|
||||
item.Observations.Should().Contain(o => o.ProviderId == "provider-b" && o.Status == "fixed");
|
||||
}
|
||||
|
||||
private void SeedObservations()
|
||||
{
|
||||
var client = new MongoClient(_runner.ConnectionString);
|
||||
var database = client.GetDatabase("linksets_tests");
|
||||
var collection = database.GetCollection<BsonDocument>(VexMongoCollectionNames.Observations);
|
||||
|
||||
var observations = new List<BsonDocument>
|
||||
{
|
||||
new()
|
||||
{
|
||||
{ "_id", "obs-1" },
|
||||
{ "Tenant", "tests" },
|
||||
{ "ObservationId", "obs-1" },
|
||||
{ "VulnerabilityId", "cve-2025-0001" },
|
||||
{ "ProductKey", "pkg:demo/app" },
|
||||
{ "ProviderId", "provider-a" },
|
||||
{ "Status", "affected" },
|
||||
{ "StreamId", "stream" },
|
||||
{ "CreatedAt", DateTime.UtcNow },
|
||||
{ "Document", new BsonDocument { { "Digest", "digest-1" }, { "Format", "csaf" }, { "SourceUri", "https://example.test/a.json" } } },
|
||||
{ "Statements", new BsonArray
|
||||
{
|
||||
new BsonDocument
|
||||
{
|
||||
{ "VulnerabilityId", "cve-2025-0001" },
|
||||
{ "ProductKey", "pkg:demo/app" },
|
||||
{ "Status", "affected" },
|
||||
{ "LastObserved", DateTime.UtcNow },
|
||||
{ "Purl", "pkg:demo/app" }
|
||||
}
|
||||
}
|
||||
},
|
||||
{ "Linkset", new BsonDocument { { "Purls", new BsonArray { "pkg:demo/app" } } } }
|
||||
},
|
||||
new()
|
||||
{
|
||||
{ "_id", "obs-2" },
|
||||
{ "Tenant", "tests" },
|
||||
{ "ObservationId", "obs-2" },
|
||||
{ "VulnerabilityId", "cve-2025-0001" },
|
||||
{ "ProductKey", "pkg:demo/app" },
|
||||
{ "ProviderId", "provider-b" },
|
||||
{ "Status", "fixed" },
|
||||
{ "StreamId", "stream" },
|
||||
{ "CreatedAt", DateTime.UtcNow.AddMinutes(1) },
|
||||
{ "Document", new BsonDocument { { "Digest", "digest-2" }, { "Format", "csaf" }, { "SourceUri", "https://example.test/b.json" } } },
|
||||
{ "Statements", new BsonArray
|
||||
{
|
||||
new BsonDocument
|
||||
{
|
||||
{ "VulnerabilityId", "cve-2025-0001" },
|
||||
{ "ProductKey", "pkg:demo/app" },
|
||||
{ "Status", "fixed" },
|
||||
{ "LastObserved", DateTime.UtcNow },
|
||||
{ "Purl", "pkg:demo/app" }
|
||||
}
|
||||
}
|
||||
},
|
||||
{ "Linkset", new BsonDocument { { "Purls", new BsonArray { "pkg:demo/app" } } } }
|
||||
}
|
||||
};
|
||||
|
||||
collection.InsertMany(observations);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_factory.Dispose();
|
||||
_runner.Dispose();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user