Skip to content

Commit 6199829

Browse files
committed
feat: Add RBAC authorization integration tests and enhance authentication exception handling
1 parent 676e5f7 commit 6199829

File tree

5 files changed

+111
-3
lines changed

5 files changed

+111
-3
lines changed

src/Weaviate.Client.Tests/Integration/TestAuth.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
namespace Weaviate.Client.Tests.Integration;
22

3-
using System;
43
using System.Net.Http;
54
using System.Threading.Tasks;
65
using Weaviate.Client;
7-
using Xunit;
86

97
public class TestAuth : IntegrationTests
108
{
@@ -208,4 +206,21 @@ await client
208206

209207
// TODO Needs a finalized way to inject a logger and check that no warnings were logged
210208
}
209+
210+
[Fact]
211+
public async Task TestAuthenticationFailure()
212+
{
213+
string clientSecret = "invalid-secret";
214+
Assert.True(await IsAuthEnabled($"localhost:{OKTA_PORT_CC}"));
215+
216+
await Assert.ThrowsAsync<WeaviateAuthenticationException>(async () =>
217+
{
218+
await Connect.Local(
219+
hostname: "localhost",
220+
restPort: OKTA_PORT_CC,
221+
credentials: Auth.ClientCredentials(clientSecret, "some_scope"),
222+
httpMessageHandler: _httpMessageHandler
223+
);
224+
});
225+
}
211226
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
namespace Weaviate.Client.Tests.Integration;
2+
3+
using System.Linq;
4+
using Weaviate.Client;
5+
using Weaviate.Client.Models;
6+
using Xunit;
7+
8+
/// <summary>
9+
/// RBAC Groups integration tests (Rest:8092 gRPC:50063). Authorization checks for various operations.
10+
/// </summary>
11+
public class TestRbacAuthorization : IntegrationTests
12+
{
13+
public override ushort RestPort => 8092;
14+
public override ushort GrpcPort => 50063;
15+
private const string ADMIN_API_KEY = "admin-key";
16+
17+
public override async ValueTask InitializeAsync()
18+
{
19+
await base.InitializeAsync();
20+
21+
RequireVersion("1.32.0");
22+
}
23+
24+
public override ICredentials? Credentials => Auth.ApiKey(ADMIN_API_KEY);
25+
26+
[Fact, Trait("Category", "RBAC")]
27+
public async Task TestAuthorizationFailure()
28+
{
29+
// Generate random names for collection and user
30+
var collectionName = $"AuthorizationTest";
31+
var userId = Helpers.GenerateUniqueIdentifier("user");
32+
33+
// Create collection
34+
var collectionConfig = new CollectionConfig
35+
{
36+
Name = collectionName,
37+
Properties = new[]
38+
{
39+
new Property { Name = "name", DataType = new[] { "string" } },
40+
},
41+
};
42+
43+
var client = await CollectionFactory<object>(collectionConfig);
44+
45+
// Create a role with only read permission for this collection
46+
var readOnlyRole = await _weaviate.Roles.Create(
47+
"read-only-role",
48+
[
49+
new Permissions.Collections(collectionName) { Read = true },
50+
new Permissions.Data(collectionName, null, null) { Read = true },
51+
],
52+
TestContext.Current.CancellationToken
53+
);
54+
55+
// Create a user and assign the read-only role
56+
var apiKey = await _weaviate.Users.Db.Create(
57+
userId,
58+
cancellationToken: TestContext.Current.CancellationToken
59+
);
60+
await _weaviate.Users.Db.AssignRoles(
61+
userId,
62+
new[] { readOnlyRole.Name },
63+
cancellationToken: TestContext.Current.CancellationToken
64+
);
65+
66+
// Create a new client with the user's API key
67+
var userClient = await new WeaviateClientBuilder()
68+
.WithRestEndpoint("localhost")
69+
.WithRestPort(RestPort)
70+
.WithGrpcEndpoint("localhost")
71+
.WithGrpcPort(GrpcPort)
72+
.WithCredentials(Auth.ApiKey(apiKey))
73+
.BuildAsync();
74+
75+
var userCollection = userClient.Collections.Use(collectionName);
76+
77+
// Try to insert data and assert that authorization exception is thrown
78+
await Assert.ThrowsAsync<WeaviateAuthorizationException>(async () =>
79+
{
80+
await userCollection.Data.Insert(
81+
new { name = "should fail" },
82+
cancellationToken: TestContext.Current.CancellationToken
83+
);
84+
});
85+
}
86+
}

src/Weaviate.Client.Tests/Unit/TestTypedDataClient.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public async Task Insert_WithValidData_CallsUnderlyingDataClient()
6161
// Actual insertion would require a mock/fake HTTP client
6262
// For now, we just verify the method signature is correct
6363
Assert.NotNull(typedDataClient);
64+
await Task.CompletedTask;
6465
}
6566

6667
[Fact]
@@ -91,6 +92,7 @@ public async Task InsertMany_WithEnumerableOfT_AcceptsCorrectType()
9192
// Verify the method accepts IEnumerable<T>
9293
Assert.NotNull(typedDataClient);
9394
Assert.Equal(2, articles.Count);
95+
await Task.CompletedTask;
9496
}
9597

9698
[Fact]
@@ -111,6 +113,7 @@ public async Task InsertMany_WithTuplesOfDataAndId_AcceptsCorrectType()
111113
// Verify the method accepts tuples of (T, Guid)
112114
Assert.NotNull(typedDataClient);
113115
Assert.Equal(2, requests.Count);
116+
await Task.CompletedTask;
114117
}
115118

116119
[Fact]
@@ -131,6 +134,7 @@ public async Task InsertMany_WithTuplesOfDataAndVectors_AcceptsCorrectType()
131134
// Verify the method accepts tuples of (T, Vectors)
132135
Assert.NotNull(typedDataClient);
133136
Assert.Single(requests);
137+
await Task.CompletedTask;
134138
}
135139

136140
[Fact]
@@ -154,6 +158,7 @@ public async Task InsertMany_WithTuplesOfDataAndReferences_AcceptsCorrectType()
154158
// Verify the method accepts tuples of (T, IEnumerable<ObjectReference>)
155159
Assert.NotNull(typedDataClient);
156160
Assert.Single(requests);
161+
await Task.CompletedTask;
157162
}
158163

159164
[Fact]

src/Weaviate.Client.Tests/Unit/TimeoutEdgeCaseTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ public async Task ZeroTimeout_ClearsContext()
247247
// Assert - Context should be cleared
248248
Assert.Null(TimeoutHelper.GetTimeout());
249249
Assert.Null(TimeoutHelper.GetOperation());
250+
await Task.CompletedTask;
250251
}
251252

252253
[Fact]
@@ -298,6 +299,7 @@ public async Task RapidSequentialTimeouts_EachMaintainsOwnContext()
298299
Assert.Equal(TimeSpan.FromMilliseconds(10 + i * 5), results[i].timeout);
299300
Assert.Equal($"Rapid {i}", results[i].operation);
300301
}
302+
await Task.CompletedTask;
301303
}
302304

303305
[Fact]

src/Weaviate.Client/OIDC.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ private async Task AuthenticateAsync()
175175
tokenResponse.Error,
176176
tokenResponse.ErrorDescription
177177
);
178-
throw new AuthenticationException(
178+
throw new WeaviateAuthenticationException(
179179
$"OAuth authentication failed: {tokenResponse.Error}"
180180
);
181181
}

0 commit comments

Comments
 (0)