Skip to content

Fix for OnReturn hook in GetSecondary pipeline #926

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public IEnumerable<TResource> BeforeDelete<TResource>(IEnumerable<TResource> res
/// <inheritdoc />
public IEnumerable<TResource> OnReturn<TResource>(IEnumerable<TResource> resources, ResourcePipeline pipeline) where TResource : class, IIdentifiable
{
if (GetHook(ResourceHook.OnReturn, resources, out var container, out var node) && pipeline != ResourcePipeline.GetRelationship)
if (GetHook(ResourceHook.OnReturn, resources, out var container, out var node))
{
IEnumerable<TResource> updated = container.OnReturn((HashSet<TResource>)node.UniqueResources, pipeline);
ValidateHookResponse(updated);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ public IReadOnlyCollection<TResource> OnReturnMany<TResource>(IReadOnlyCollectio

public object OnReturnRelationship(object resourceOrResources)
{
if (resourceOrResources is IEnumerable enumerable)
if (resourceOrResources is IEnumerable)
{
var resources = enumerable.Cast<IIdentifiable>();
dynamic resources = resourceOrResources;
return _resourceHookExecutor.OnReturn(resources, ResourcePipeline.GetRelationship).ToArray();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,30 @@ public async Task Article_Is_Hidden()
Assert.DoesNotContain(toBeExcluded, body);
}

[Fact]
public async Task Article_Through_Secondary_Endpoint_Is_Hidden()
{
// Arrange
var articles = _articleFaker.Generate(3);
string toBeExcluded = "This should not be included";
articles[0].Caption = toBeExcluded;
var author = _authorFaker.Generate();
author.Articles = articles;

_dbContext.AuthorDifferentDbContextName.Add(author);
await _dbContext.SaveChangesAsync();

var route = $"/api/v1/authors/{author.Id}/articles";

// Act
var response = await _client.GetAsync(route);

// Assert
var body = await response.Content.ReadAsStringAsync();
Assert.True(HttpStatusCode.OK == response.StatusCode, $"{route} returned {response.StatusCode} status code with body: {body}");
Assert.DoesNotContain(toBeExcluded, body);
}

[Fact]
public async Task Tag_Is_Hidden()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public void OnReturn_GetRelationship()
hookExecutor.OnReturn(articles, ResourcePipeline.GetRelationship);

// Assert
articleResourceMock.Verify(rd => rd.OnReturn(It.Is<HashSet<Article>>((collection) => !collection.Except(articles).Any()), ResourcePipeline.GetRelationship), Times.Once());
joinResourceMock.Verify(rd => rd.OnReturn(It.Is<HashSet<IdentifiableArticleTag>>((collection) => !collection.Except(joins).Any()), ResourcePipeline.GetRelationship), Times.Once());
tagResourceMock.Verify(rd => rd.OnReturn(It.Is<HashSet<Tag>>((collection) => !collection.Except(tags).Any()), ResourcePipeline.GetRelationship), Times.Once());
VerifyNoOtherCalls(articleResourceMock, joinResourceMock, tagResourceMock);
Expand Down