Skip to content

Commit 190b420

Browse files
Works?
1 parent 55e40a0 commit 190b420

File tree

4 files changed

+39
-49
lines changed

4 files changed

+39
-49
lines changed

Samples/NucliaDbClient.Tests/NucliaDbApiTests.cs

Lines changed: 15 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,79 +3,56 @@
33
using Outcome;
44
using Xunit;
55

6+
#pragma warning disable CS8509 // The switch expression does not handle all possible values of its input type (it is not exhaustive).
7+
68
namespace NucliaDbClient.Tests;
79

810
public class NucliaDbApiTests
911
{
1012
private readonly IHttpClientFactory _httpClientFactory;
11-
private readonly string? _kbid;
1213

1314
public NucliaDbApiTests()
1415
{
1516
var services = new ServiceCollection();
1617
services.AddHttpClient();
1718
var serviceProvider = services.BuildServiceProvider();
1819
_httpClientFactory = serviceProvider.GetRequiredService<IHttpClientFactory>();
19-
20-
// Use environment variable or default to the KB created by Docker container
21-
_kbid =
22-
Environment.GetEnvironmentVariable("NUCLIA_KBID")
23-
?? "2edd5a30-8e28-4185-a0be-629971a9784c";
24-
}
25-
26-
private static HttpClient ConfigureAuthentication(HttpClient httpClient)
27-
{
28-
// NucliaDB requires either service account token or roles header
29-
// For local testing, we use the X-NUCLIADB-ROLES header to specify access level
30-
var serviceAccount = Environment.GetEnvironmentVariable("NUCLIA_SERVICE_ACCOUNT");
31-
if (!string.IsNullOrEmpty(serviceAccount))
32-
{
33-
httpClient.DefaultRequestHeaders.Add("X-NUCLIA-SERVICEACCOUNT", $"Bearer {serviceAccount}");
34-
}
35-
else
36-
{
37-
// For local Docker instance without authentication, use roles header
38-
httpClient.DefaultRequestHeaders.Add("X-NUCLIADB-ROLES", "READER");
39-
}
40-
return httpClient;
4120
}
4221

4322
[Fact]
4423
public async Task GetKnowledgeBox_ReturnsValidData()
4524
{
46-
// Arrange
47-
var httpClient = ConfigureAuthentication(_httpClientFactory.CreateClient());
48-
4925
// Act
50-
var result = await httpClient.GetKbKbKbidGet(_kbid!).ConfigureAwait(false);
26+
var result = await _httpClientFactory
27+
.CreateClient()
28+
.GetKbKbKbidGet("2edd5a30-8e28-4185-a0be-629971a9784c", "READER")
29+
.ConfigureAwait(false);
5130

5231
// Assert
5332
var kb = result switch
5433
{
5534
OkKnowledgeBoxObj(var value) => value,
5635
ErrorKnowledgeBoxObj(HttpError<string>.ExceptionError(var ex)) =>
5736
throw new InvalidOperationException("API call failed with exception", ex),
58-
ErrorKnowledgeBoxObj(
59-
HttpError<string>.ErrorResponseError
60-
(var body, var statusCode, _)
61-
) => throw new InvalidOperationException($"API call failed: HTTP {statusCode}: {body}"),
62-
_ => throw new InvalidOperationException("Unexpected result type"),
6337
};
6438

6539
Assert.NotNull(kb);
6640
Assert.NotNull(kb.Slug);
6741
Assert.NotNull(kb.Uuid);
6842
}
6943

70-
[SkippableFact]
44+
[Fact]
7145
public async Task ListResources_ReturnsResourceList()
7246
{
73-
// Arrange
74-
var httpClient = ConfigureAuthentication(_httpClientFactory.CreateClient());
75-
7647
// Act
77-
var result = await httpClient
78-
.ListResourcesKbKbidResourcesGet(_kbid!, 0, 10)
48+
var result = await _httpClientFactory
49+
.CreateClient()
50+
.ListResourcesKbKbidResourcesGet(
51+
"2edd5a30-8e28-4185-a0be-629971a9784c",
52+
0,
53+
10,
54+
"READER"
55+
)
7956
.ConfigureAwait(false);
8057

8158
// Assert

Samples/NucliaDbClient.Tests/NucliaDbClient.Tests.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1414
<PrivateAssets>all</PrivateAssets>
1515
</PackageReference>
16-
<PackageReference Include="Xunit.SkippableFact" Version="1.4.13" />
1716
<PackageReference Include="Microsoft.Extensions.Http" Version="9.0.0" />
1817
</ItemGroup>
1918
<ItemGroup>

Samples/NucliaDbClient/Generated/NucliaDBApiExtensions.g.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ public static Task<Result<KnowledgeBoxObj, HttpError<string>>> GetKbBySlugKbSSlu
4343
/// <summary>Get Knowledge Box</summary>
4444
public static Task<Result<KnowledgeBoxObj, HttpError<string>>> GetKbKbKbidGet(
4545
this HttpClient httpClient,
46-
string kbid,
46+
string kbid, string xNUCLIADBROLES,
4747
CancellationToken cancellationToken = default
48-
) => _getKbKbKbidGet(httpClient, kbid, cancellationToken);
48+
) => _getKbKbKbidGet(httpClient, (kbid, xNUCLIADBROLES), cancellationToken);
4949

5050
/// <summary>Ask Knowledge Box</summary>
5151
public static Task<Result<SyncAskResponse, HttpError<string>>> AskKnowledgeboxEndpointKbKbidAskPost(
@@ -505,9 +505,9 @@ public static Task<Result<ResourceCreated, HttpError<string>>> CreateResourceKbK
505505
/// <summary>List Resources</summary>
506506
public static Task<Result<ResourceList, HttpError<string>>> ListResourcesKbKbidResourcesGet(
507507
this HttpClient httpClient,
508-
string kbid, int page, int size,
508+
string kbid, int page, int size, string xNUCLIADBROLES,
509509
CancellationToken cancellationToken = default
510-
) => _listResourcesKbKbidResourcesGet(httpClient, (kbid, page, size), cancellationToken);
510+
) => _listResourcesKbKbidResourcesGet(httpClient, (kbid, page, size, xNUCLIADBROLES), cancellationToken);
511511

512512
/// <summary>Learning configuration schema</summary>
513513
public static Task<Result<object, HttpError<string>>> GetSchemaForConfigurationUpdatesKbKbidSchemaGet(
@@ -810,10 +810,10 @@ public static Task<Result<object, HttpError<string>>> LearningConfigurationSchem
810810
deserializeError: DeserializeError
811811
);
812812

813-
private static GetAsync<KnowledgeBoxObj, string, string> _getKbKbKbidGet { get; } =
814-
RestClient.Net.HttpClientFactoryExtensions.CreateGet<KnowledgeBoxObj, string, string>(
813+
private static GetAsync<KnowledgeBoxObj, string, (string kbid, string xNUCLIADBROLES)> _getKbKbKbidGet { get; } =
814+
RestClient.Net.HttpClientFactoryExtensions.CreateGet<KnowledgeBoxObj, string, (string kbid, string xNUCLIADBROLES)>(
815815
url: BaseUrl,
816-
buildRequest: static kbid => new HttpRequestParts(new RelativeUrl($"/api/v1/kb/{kbid}"), null, null),
816+
buildRequest: static param => new HttpRequestParts(new RelativeUrl($"/api/v1/kb/{param.kbid}"), null, new Dictionary<string, string> { ["X-NUCLIADB-ROLES"] = param.xNUCLIADBROLES.ToString() ?? string.Empty }),
817817
deserializeSuccess: DeserializeJson<KnowledgeBoxObj>,
818818
deserializeError: DeserializeError
819819
);
@@ -1338,10 +1338,10 @@ public static Task<Result<object, HttpError<string>>> LearningConfigurationSchem
13381338
deserializeError: DeserializeError
13391339
);
13401340

1341-
private static GetAsync<ResourceList, string, (string kbid, int page, int size)> _listResourcesKbKbidResourcesGet { get; } =
1342-
RestClient.Net.HttpClientFactoryExtensions.CreateGet<ResourceList, string, (string kbid, int page, int size)>(
1341+
private static GetAsync<ResourceList, string, (string kbid, int page, int size, string xNUCLIADBROLES)> _listResourcesKbKbidResourcesGet { get; } =
1342+
RestClient.Net.HttpClientFactoryExtensions.CreateGet<ResourceList, string, (string kbid, int page, int size, string xNUCLIADBROLES)>(
13431343
url: BaseUrl,
1344-
buildRequest: static param => new HttpRequestParts(new RelativeUrl($"/api/v1/kb/{param.kbid}/resources?page={param.page}&size={param.size}"), null, null),
1344+
buildRequest: static param => new HttpRequestParts(new RelativeUrl($"/api/v1/kb/{param.kbid}/resources?page={param.page}&size={param.size}"), null, new Dictionary<string, string> { ["X-NUCLIADB-ROLES"] = param.xNUCLIADBROLES.ToString() ?? string.Empty }),
13451345
deserializeSuccess: DeserializeJson<ResourceList>,
13461346
deserializeError: DeserializeError
13471347
);

Samples/NucliaDbClient/api.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,13 @@ paths:
121121
schema:
122122
type: string
123123
title: Kbid
124+
- name: X-NUCLIADB-ROLES
125+
in: header
126+
required: false
127+
schema:
128+
type: string
129+
default: READER
130+
title: X-NUCLIADB-ROLES
124131
responses:
125132
'200':
126133
description: Successful Response
@@ -4476,6 +4483,13 @@ paths:
44764483
default: 20
44774484
title: Size
44784485
description: Page size
4486+
- name: X-NUCLIADB-ROLES
4487+
in: header
4488+
required: false
4489+
schema:
4490+
type: string
4491+
default: READER
4492+
title: X-NUCLIADB-ROLES
44794493
responses:
44804494
'200':
44814495
description: Successful Response

0 commit comments

Comments
 (0)