Skip to content

Commit

Permalink
fix(client): read all tuples (#32)
Browse files Browse the repository at this point in the history
<!-- Thanks for opening a PR! Here are some quick tips:
If this is your first time contributing, [read our Contributing
Guidelines](https://github.com/openfga/.github/blob/main/CONTRIBUTING.md)
to learn how to create an acceptable PR for this repo.
By submitting a PR to this repository, you agree to the terms within the
[OpenFGA Code of
Conduct](https://github.com/openfga/.github/blob/main/CODE_OF_CONDUCT.md)

If your PR is under active development, please submit it as a "draft".
Once it's ready, open it up for review.
-->

<!-- Provide a brief summary of the changes -->

## Description
<!-- Provide a detailed description of the changes -->

## References
<!-- Provide a list of any applicable references here (Github Issue,
[OpenFGA RFC](https://github.com/openfga/rfcs), other PRs, etc..) -->

- Closes #31

## Review Checklist
- [ ] I have clicked on ["allow edits by
maintainers"](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/allowing-changes-to-a-pull-request-branch-created-from-a-fork).
- [ ] I have added documentation for new/changed functionality in this
PR or in a PR to [openfga.dev](https://github.com/openfga/openfga.dev)
[Provide a link to any relevant PRs in the references section above]
- [ ] The correct base branch is being used, if not `main`
- [ ] I have added tests to validate that the change in functionality is
working as expected
  • Loading branch information
rhamzeh authored Dec 1, 2023
2 parents b0cfbcd + ea615e8 commit 2b64d9e
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 2b64d9e

Please sign in to comment.