Skip to content

Commit

Permalink
WIP Add paging to comments field
Browse files Browse the repository at this point in the history
TODO: Tests

I've specified a maximum page size to provide some protection against
excessively large queries.
  • Loading branch information
smfeest committed Oct 6, 2024
1 parent 288a44c commit a3fac53
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
38 changes: 24 additions & 14 deletions src/Buttercup.Web.Tests/Api/CommentsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ public sealed class CommentsTests(AppFactory appFactory) : EndToEndTests(appFact
private const string CommentsQuery = """
query {
comments {
id
recipe { id title }
author { id name }
body
created
modified
revision
revisions { revision created body }
nodes {
id
recipe { id title }
author { id name }
body
created
modified
revision
revisions { revision created body }
}
}
}
""";
Expand Down Expand Up @@ -56,7 +58,7 @@ public async Task QueryingComments()
}),
});

JsonAssert.Equivalent(expected, dataElement.GetProperty("comments"));
JsonAssert.Equivalent(expected, dataElement.GetProperty("comments").GetProperty("nodes"));
}

[Fact]
Expand All @@ -66,7 +68,7 @@ public async Task QueryingCommentsWhenUnauthenticated()
using var response = await client.PostQuery(CommentsQuery);
using var document = await response.Content.ReadAsJsonDocument();

JsonAssert.ValueIsNull(document.RootElement.GetProperty("data"));
JsonAssert.ValueIsNull(document.RootElement.GetProperty("data").GetProperty("comments"));
ApiAssert.HasSingleError(ErrorCodes.Authentication.NotAuthorized, document);
}

Expand All @@ -90,13 +92,17 @@ public async Task SortingComments()
using var client = await this.AppFactory.CreateClientForApiUser(currentUser);
using var response = await client.PostQuery("""
query {
comments(order: { created: ASC }) { id }
comments(order: { created: ASC }) {
nodes {
id
}
}
}
""");
using var document = await response.Content.ReadAsJsonDocument();

var dataElement = ApiAssert.SuccessResponse(document);
var ids = dataElement.GetProperty("comments").EnumerateArray().Select(
var ids = dataElement.GetProperty("comments").GetProperty("nodes").EnumerateArray().Select(
c => c.GetProperty("id").GetInt64());

Assert.Equal([1, 3, 2], ids);
Expand All @@ -111,12 +117,16 @@ public async Task SortingCommentsByAdminOnlyUserFieldsWhenNotAnAdmin()
using var client = await this.AppFactory.CreateClientForApiUser(currentUser);
using var response = await client.PostQuery("""
query {
comments(order: { author: { email: ASC } }) { id }
comments(order: { author: { email: ASC } }) {
nodes {
id
}
}
}
""");
using var document = await response.Content.ReadAsJsonDocument();

JsonAssert.ValueIsNull(document.RootElement.GetProperty("data"));
JsonAssert.ValueIsNull(document.RootElement.GetProperty("data").GetProperty("comments"));
ApiAssert.HasSingleError(ErrorCodes.Authentication.NotAuthorized, document);
}
}
1 change: 1 addition & 0 deletions src/Buttercup.Web/Api/Query.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public sealed class Query
{
[Authorize]
[Authorize(AuthorizationPolicyNames.AdminOnlyFilterAndSortFields)]
[UsePaging(MaxPageSize = 500)]
[UseProjection]
[UseFiltering]
[UseSorting]
Expand Down

0 comments on commit a3fac53

Please sign in to comment.