Skip to content

Commit

Permalink
fix(client): read all tuples
Browse files Browse the repository at this point in the history
  • Loading branch information
rhamzeh committed Dec 1, 2023
1 parent b0cfbcd commit ea615e8
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 5 deletions.
44 changes: 44 additions & 0 deletions src/OpenFga.Sdk.Test/Api/OpenFgaApiTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1373,6 +1373,50 @@ public async Task ReadTest() {
Assert.Equal(response, expectedResponse);
}

/// <summary>
/// Test Read With Empty Parameters
/// </summary>
[Fact]
public async Task ReadEmptyTest() {
var mockHandler = new Mock<HttpMessageHandler>(MockBehavior.Strict);
var expectedResponse = new ReadResponse() {
Tuples = new List<Model.Tuple>() {
new(new TupleKey("document:roadmap", "viewer", "user:81684243-9356-4421-8fbf-a4f8d36aa31b"), DateTime.Now)
}
};
mockHandler.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.Is<HttpRequestMessage>(req =>
req.RequestUri == new Uri($"{_config.BasePath}/stores/{_config.StoreId}/read") &&
req.Method == HttpMethod.Post),
ItExpr.IsAny<CancellationToken>()
)
.ReturnsAsync(new HttpResponseMessage() {
StatusCode = HttpStatusCode.OK,
Content = Utils.CreateJsonStringContent(expectedResponse),
});

var httpClient = new HttpClient(mockHandler.Object);
var openFgaApi = new OpenFgaApi(_config, httpClient);

var body = new ReadRequest { };
var response = await openFgaApi.Read(body);

mockHandler.Protected().Verify(
"SendAsync",
Times.Exactly(1),
ItExpr.Is<HttpRequestMessage>(req =>
req.RequestUri == new Uri($"{_config.BasePath}/stores/{_config.StoreId}/read") &&
req.Method == HttpMethod.Post),
ItExpr.IsAny<CancellationToken>()
);

Assert.IsType<ReadResponse>(response);
Assert.Single(response.Tuples);
Assert.Equal(response, expectedResponse);
}

/// <summary>
/// Test ReadChanges
/// </summary>
Expand Down
54 changes: 52 additions & 2 deletions src/OpenFga.Sdk.Test/Client/OpenFgaClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,8 @@ public async Task ReadTest() {
"SendAsync",
ItExpr.Is<HttpRequestMessage>(req =>
req.RequestUri == new Uri($"{_config.BasePath}/stores/{_config.StoreId}/read") &&
req.Method == HttpMethod.Post),
req.Method == HttpMethod.Post &&
req.Content.ReadAsStringAsync().Result.Contains("tuple")),
ItExpr.IsAny<CancellationToken>()
)
.ReturnsAsync(new HttpResponseMessage() {
Expand All @@ -632,7 +633,56 @@ public async Task ReadTest() {
Times.Exactly(1),
ItExpr.Is<HttpRequestMessage>(req =>
req.RequestUri == new Uri($"{_config.BasePath}/stores/{_config.StoreId}/read") &&
req.Method == HttpMethod.Post),
req.Method == HttpMethod.Post &&
req.Content.ReadAsStringAsync().Result.Contains("tuple")),
ItExpr.IsAny<CancellationToken>()
);

Assert.IsType<ReadResponse>(response);
Assert.Single(response.Tuples);
Assert.Equal(response, expectedResponse);
}

/// <summary>
/// Test Read with Empty Body
/// </summary>
[Fact]
public async Task ReadEmptyTest() {
var mockHandler = new Mock<HttpMessageHandler>(MockBehavior.Strict);
var expectedResponse = new ReadResponse() {
Tuples = new List<Model.Tuple>() {
new(new TupleKey("document:roadmap", "viewer", "user:81684243-9356-4421-8fbf-a4f8d36aa31b"),
DateTime.Now)
}
};
mockHandler.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.Is<HttpRequestMessage>(req =>
req.RequestUri == new Uri($"{_config.BasePath}/stores/{_config.StoreId}/read") &&
req.Method == HttpMethod.Post &&
!req.Content.ReadAsStringAsync().Result.Contains("tuple")),
ItExpr.IsAny<CancellationToken>()
)
.ReturnsAsync(new HttpResponseMessage() {
StatusCode = HttpStatusCode.OK,
Content = Utils.CreateJsonStringContent(expectedResponse),
});

var httpClient = new HttpClient(mockHandler.Object);
var fgaClient = new OpenFgaClient(_config, httpClient);

var body = new ClientReadRequest() { };
var options = new ClientReadOptions { };
var response = await fgaClient.Read(body, options);

mockHandler.Protected().Verify(
"SendAsync",
Times.Exactly(1),
ItExpr.Is<HttpRequestMessage>(req =>
req.RequestUri == new Uri($"{_config.BasePath}/stores/{_config.StoreId}/read") &&
req.Method == HttpMethod.Post &&
!req.Content.ReadAsStringAsync().Result.Contains("tuple")),
ItExpr.IsAny<CancellationToken>()
);

Expand Down
11 changes: 8 additions & 3 deletions src/OpenFga.Sdk/Client/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,18 @@ public async Task<ReadChangesResponse> ReadChanges(ClientReadChangesRequest body
* Read - Read tuples previously written to the store (does not evaluate)
*/
public async Task<ReadResponse> Read(ClientReadRequest body, IClientReadOptions? options = default,
CancellationToken cancellationToken = default) =>
await api.Read(
CancellationToken cancellationToken = default) {
TupleKey tupleKey = null;
if (body != null && (body.User != null || body.Relation != null || body.Object != null)) {
tupleKey = body;
}
return await api.Read(
new ReadRequest {
TupleKey = body,
TupleKey = tupleKey,
PageSize = options?.PageSize,
ContinuationToken = options?.ContinuationToken
}, cancellationToken);
}

/**
* Write - Create or delete relationship tuples
Expand Down

0 comments on commit ea615e8

Please sign in to comment.