Skip to content

Commit 473d53b

Browse files
author
Bart Koelman
committed
Added test
1 parent a622954 commit 473d53b

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using System.Net;
2+
using FluentAssertions;
3+
using JsonApiDotNetCore.Serialization.Objects;
4+
using TestBuildingBlocks;
5+
using Xunit;
6+
7+
namespace JsonApiDotNetCoreTests.IntegrationTests.Links;
8+
9+
public sealed class LinkInclusionIncludeTests : IClassFixture<IntegrationTestContext<TestableStartup<LinksDbContext>, LinksDbContext>>
10+
{
11+
private readonly IntegrationTestContext<TestableStartup<LinksDbContext>, LinksDbContext> _testContext;
12+
private readonly LinksFakers _fakers = new();
13+
14+
public LinkInclusionIncludeTests(IntegrationTestContext<TestableStartup<LinksDbContext>, LinksDbContext> testContext)
15+
{
16+
_testContext = testContext;
17+
18+
testContext.UseController<PhotoLocationsController>();
19+
}
20+
21+
[Fact]
22+
public async Task Hides_Self_link_in_included_resources_for_unregistered_controllers()
23+
{
24+
// Arrange
25+
PhotoLocation location = _fakers.PhotoLocation.Generate();
26+
location.Photo = _fakers.Photo.Generate();
27+
location.Album = _fakers.PhotoAlbum.Generate();
28+
29+
await _testContext.RunOnDatabaseAsync(async dbContext =>
30+
{
31+
dbContext.PhotoLocations.Add(location);
32+
await dbContext.SaveChangesAsync();
33+
});
34+
35+
string route = $"/photoLocations/{location.StringId}?include=photo,album";
36+
37+
// Act
38+
(HttpResponseMessage httpResponse, Document responseDocument) = await _testContext.ExecuteGetAsync<Document>(route);
39+
40+
// Assert
41+
httpResponse.Should().HaveStatusCode(HttpStatusCode.OK);
42+
43+
responseDocument.Included.ShouldHaveCount(2);
44+
45+
responseDocument.Included.Should().ContainSingle(resource => resource.Type == "photos").Subject.With(resource =>
46+
{
47+
resource.Links.Should().BeNull();
48+
49+
resource.Relationships.ShouldContainKey("location").With(value =>
50+
{
51+
value.ShouldNotBeNull();
52+
value.Links.ShouldNotBeNull();
53+
});
54+
});
55+
56+
responseDocument.Included.Should().ContainSingle(resource => resource.Type == "photoAlbums").Subject.With(resource =>
57+
{
58+
resource.Links.Should().BeNull();
59+
});
60+
}
61+
}

0 commit comments

Comments
 (0)